Convert git-annotate to use Git.pm
[git/repo.git] / builtin-rev-list.c
blob63bad0e96ade657bef21d9ca0e0de2e0ba8af61c
1 #include "cache.h"
2 #include "refs.h"
3 #include "tag.h"
4 #include "commit.h"
5 #include "tree.h"
6 #include "blob.h"
7 #include "tree-walk.h"
8 #include "diff.h"
9 #include "revision.h"
10 #include "builtin.h"
12 /* bits #0-15 in revision.h */
14 #define COUNTED (1u<<16)
16 static const char rev_list_usage[] =
17 "git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
18 " limiting output:\n"
19 " --max-count=nr\n"
20 " --max-age=epoch\n"
21 " --min-age=epoch\n"
22 " --sparse\n"
23 " --no-merges\n"
24 " --remove-empty\n"
25 " --all\n"
26 " ordering output:\n"
27 " --topo-order\n"
28 " --date-order\n"
29 " formatting output:\n"
30 " --parents\n"
31 " --objects | --objects-edge\n"
32 " --unpacked\n"
33 " --header | --pretty\n"
34 " --abbrev=nr | --no-abbrev\n"
35 " --abbrev-commit\n"
36 " special purpose:\n"
37 " --bisect"
40 static struct rev_info revs;
42 static int bisect_list = 0;
43 static int show_timestamp = 0;
44 static int hdr_termination = 0;
45 static const char *header_prefix;
47 static void show_commit(struct commit *commit)
49 if (show_timestamp)
50 printf("%lu ", commit->date);
51 if (header_prefix)
52 fputs(header_prefix, stdout);
53 if (commit->object.flags & BOUNDARY)
54 putchar('-');
55 if (revs.abbrev_commit && revs.abbrev)
56 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
57 stdout);
58 else
59 fputs(sha1_to_hex(commit->object.sha1), stdout);
60 if (revs.parents) {
61 struct commit_list *parents = commit->parents;
62 while (parents) {
63 struct object *o = &(parents->item->object);
64 parents = parents->next;
65 if (o->flags & TMP_MARK)
66 continue;
67 printf(" %s", sha1_to_hex(o->sha1));
68 o->flags |= TMP_MARK;
70 /* TMP_MARK is a general purpose flag that can
71 * be used locally, but the user should clean
72 * things up after it is done with them.
74 for (parents = commit->parents;
75 parents;
76 parents = parents->next)
77 parents->item->object.flags &= ~TMP_MARK;
79 if (revs.commit_format == CMIT_FMT_ONELINE)
80 putchar(' ');
81 else
82 putchar('\n');
84 if (revs.verbose_header) {
85 static char pretty_header[16384];
86 pretty_print_commit(revs.commit_format, commit, ~0,
87 pretty_header, sizeof(pretty_header),
88 revs.abbrev, NULL, NULL);
89 printf("%s%c", pretty_header, hdr_termination);
91 fflush(stdout);
92 if (commit->parents) {
93 free_commit_list(commit->parents);
94 commit->parents = NULL;
96 if (commit->buffer) {
97 free(commit->buffer);
98 commit->buffer = NULL;
102 static void process_blob(struct blob *blob,
103 struct object_array *p,
104 struct name_path *path,
105 const char *name)
107 struct object *obj = &blob->object;
109 if (!revs.blob_objects)
110 return;
111 if (obj->flags & (UNINTERESTING | SEEN))
112 return;
113 obj->flags |= SEEN;
114 name = strdup(name);
115 add_object(obj, p, path, name);
118 static void process_tree(struct tree *tree,
119 struct object_array *p,
120 struct name_path *path,
121 const char *name)
123 struct object *obj = &tree->object;
124 struct tree_desc desc;
125 struct name_entry entry;
126 struct name_path me;
128 if (!revs.tree_objects)
129 return;
130 if (obj->flags & (UNINTERESTING | SEEN))
131 return;
132 if (parse_tree(tree) < 0)
133 die("bad tree object %s", sha1_to_hex(obj->sha1));
134 obj->flags |= SEEN;
135 name = strdup(name);
136 add_object(obj, p, path, name);
137 me.up = path;
138 me.elem = name;
139 me.elem_len = strlen(name);
141 desc.buf = tree->buffer;
142 desc.size = tree->size;
144 while (tree_entry(&desc, &entry)) {
145 if (S_ISDIR(entry.mode))
146 process_tree(lookup_tree(entry.sha1), p, &me, entry.path);
147 else
148 process_blob(lookup_blob(entry.sha1), p, &me, entry.path);
150 free(tree->buffer);
151 tree->buffer = NULL;
154 static void show_commit_list(struct rev_info *revs)
156 int i;
157 struct commit *commit;
158 struct object_array objects = { 0, 0, NULL };
160 while ((commit = get_revision(revs)) != NULL) {
161 process_tree(commit->tree, &objects, NULL, "");
162 show_commit(commit);
164 for (i = 0; i < revs->pending.nr; i++) {
165 struct object_array_entry *pending = revs->pending.objects + i;
166 struct object *obj = pending->item;
167 const char *name = pending->name;
168 if (obj->flags & (UNINTERESTING | SEEN))
169 continue;
170 if (obj->type == TYPE_TAG) {
171 obj->flags |= SEEN;
172 add_object_array(obj, name, &objects);
173 continue;
175 if (obj->type == TYPE_TREE) {
176 process_tree((struct tree *)obj, &objects, NULL, name);
177 continue;
179 if (obj->type == TYPE_BLOB) {
180 process_blob((struct blob *)obj, &objects, NULL, name);
181 continue;
183 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
185 for (i = 0; i < objects.nr; i++) {
186 struct object_array_entry *p = objects.objects + i;
188 /* An object with name "foo\n0000000..." can be used to
189 * confuse downstream git-pack-objects very badly.
191 const char *ep = strchr(p->name, '\n');
192 if (ep) {
193 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
194 (int) (ep - p->name),
195 p->name);
197 else
198 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
203 * This is a truly stupid algorithm, but it's only
204 * used for bisection, and we just don't care enough.
206 * We care just barely enough to avoid recursing for
207 * non-merge entries.
209 static int count_distance(struct commit_list *entry)
211 int nr = 0;
213 while (entry) {
214 struct commit *commit = entry->item;
215 struct commit_list *p;
217 if (commit->object.flags & (UNINTERESTING | COUNTED))
218 break;
219 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
220 nr++;
221 commit->object.flags |= COUNTED;
222 p = commit->parents;
223 entry = p;
224 if (p) {
225 p = p->next;
226 while (p) {
227 nr += count_distance(p);
228 p = p->next;
233 return nr;
236 static void clear_distance(struct commit_list *list)
238 while (list) {
239 struct commit *commit = list->item;
240 commit->object.flags &= ~COUNTED;
241 list = list->next;
245 static struct commit_list *find_bisection(struct commit_list *list)
247 int nr, closest;
248 struct commit_list *p, *best;
250 nr = 0;
251 p = list;
252 while (p) {
253 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
254 nr++;
255 p = p->next;
257 closest = 0;
258 best = list;
260 for (p = list; p; p = p->next) {
261 int distance;
263 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
264 continue;
266 distance = count_distance(p);
267 clear_distance(list);
268 if (nr - distance < distance)
269 distance = nr - distance;
270 if (distance > closest) {
271 best = p;
272 closest = distance;
275 if (best)
276 best->next = NULL;
277 return best;
280 static void mark_edge_parents_uninteresting(struct commit *commit)
282 struct commit_list *parents;
284 for (parents = commit->parents; parents; parents = parents->next) {
285 struct commit *parent = parents->item;
286 if (!(parent->object.flags & UNINTERESTING))
287 continue;
288 mark_tree_uninteresting(parent->tree);
289 if (revs.edge_hint && !(parent->object.flags & SHOWN)) {
290 parent->object.flags |= SHOWN;
291 printf("-%s\n", sha1_to_hex(parent->object.sha1));
296 static void mark_edges_uninteresting(struct commit_list *list)
298 for ( ; list; list = list->next) {
299 struct commit *commit = list->item;
301 if (commit->object.flags & UNINTERESTING) {
302 mark_tree_uninteresting(commit->tree);
303 continue;
305 mark_edge_parents_uninteresting(commit);
309 int cmd_rev_list(int argc, const char **argv, char **envp)
311 struct commit_list *list;
312 int i;
314 init_revisions(&revs);
315 revs.abbrev = 0;
316 revs.commit_format = CMIT_FMT_UNSPECIFIED;
317 argc = setup_revisions(argc, argv, &revs, NULL);
319 for (i = 1 ; i < argc; i++) {
320 const char *arg = argv[i];
322 if (!strcmp(arg, "--header")) {
323 revs.verbose_header = 1;
324 continue;
326 if (!strcmp(arg, "--timestamp")) {
327 show_timestamp = 1;
328 continue;
330 if (!strcmp(arg, "--bisect")) {
331 bisect_list = 1;
332 continue;
334 usage(rev_list_usage);
337 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
338 /* The command line has a --pretty */
339 hdr_termination = '\n';
340 if (revs.commit_format == CMIT_FMT_ONELINE)
341 header_prefix = "";
342 else
343 header_prefix = "commit ";
345 else if (revs.verbose_header)
346 /* Only --header was specified */
347 revs.commit_format = CMIT_FMT_RAW;
349 list = revs.commits;
351 if ((!list &&
352 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
353 !revs.pending.nr)) ||
354 revs.diff)
355 usage(rev_list_usage);
357 save_commit_buffer = revs.verbose_header;
358 track_object_refs = 0;
359 if (bisect_list)
360 revs.limited = 1;
362 prepare_revision_walk(&revs);
363 if (revs.tree_objects)
364 mark_edges_uninteresting(revs.commits);
366 if (bisect_list)
367 revs.commits = find_bisection(revs.commits);
369 show_commit_list(&revs);
371 return 0;