9 #define INTERESTING (1u << 1)
10 #define COUNTED (1u << 2)
11 #define SHOWN (1u << 3)
13 static const char rev_list_usage[] =
14 "git-rev-list [OPTION] commit-id <commit-id>\n"
25 " --merge-order [ --show-breaks ]\n"
28 static int unpacked = 0;
29 static int bisect_list = 0;
30 static int tag_objects = 0;
31 static int tree_objects = 0;
32 static int blob_objects = 0;
33 static int verbose_header = 0;
34 static int show_parents = 0;
35 static int hdr_termination = 0;
36 static const char *prefix = "";
37 static unsigned long max_age = -1;
38 static unsigned long min_age = -1;
39 static int max_count = -1;
40 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
41 static int merge_order = 0;
42 static int show_breaks = 0;
43 static int stop_traversal = 0;
44 static int topo_order = 0;
45 static int no_merges = 0;
47 static void show_commit(struct commit *commit)
49 commit->object.flags |= SHOWN;
52 if (commit->object.flags & DISCONTINUITY) {
54 } else if (commit->object.flags & BOUNDARY) {
58 printf("%s%s", prefix, sha1_to_hex(commit->object.sha1));
60 struct commit_list *parents = commit->parents;
62 printf(" %s", sha1_to_hex(parents->item->object.sha1));
63 parents = parents->next;
68 static char pretty_header[16384];
69 pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header));
70 printf("%s%c", pretty_header, hdr_termination);
75 static int filter_commit(struct commit * commit)
77 if (stop_traversal && (commit->object.flags & BOUNDARY))
79 if (commit->object.flags & (UNINTERESTING|SHOWN))
81 if (min_age != -1 && (commit->date > min_age))
83 if (max_age != -1 && (commit->date < max_age)) {
85 return merge_order?CONTINUE:STOP;
87 if (max_count != -1 && !max_count--)
89 if (no_merges && (commit->parents && commit->parents->next))
94 static int process_commit(struct commit * commit)
96 int action=filter_commit(commit);
102 if (action == CONTINUE) {
111 static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
113 struct object_list *entry = xmalloc(sizeof(*entry));
121 static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
123 struct object *obj = &blob->object;
127 if (obj->flags & (UNINTERESTING | SEEN))
130 return add_object(obj, p, name);
133 static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
135 struct object *obj = &tree->object;
136 struct tree_entry_list *entry;
140 if (obj->flags & (UNINTERESTING | SEEN))
142 if (parse_tree(tree) < 0)
143 die("bad tree object %s", sha1_to_hex(obj->sha1));
145 p = add_object(obj, p, name);
146 for (entry = tree->entries ; entry ; entry = entry->next) {
147 if (entry->directory)
148 p = process_tree(entry->item.tree, p, entry->name);
150 p = process_blob(entry->item.blob, p, entry->name);
155 static struct object_list *pending_objects = NULL;
157 static void show_commit_list(struct commit_list *list)
159 struct object_list *objects = NULL, **p = &objects, *pending;
161 struct commit *commit = pop_most_recent_commit(&list, SEEN);
163 p = process_tree(commit->tree, p, "");
164 if (process_commit(commit) == STOP)
167 for (pending = pending_objects; pending; pending = pending->next) {
168 struct object *obj = pending->item;
169 const char *name = pending->name;
170 if (obj->flags & (UNINTERESTING | SEEN))
172 if (obj->type == tag_type) {
174 p = add_object(obj, p, name);
177 if (obj->type == tree_type) {
178 p = process_tree((struct tree *)obj, p, name);
181 if (obj->type == blob_type) {
182 p = process_blob((struct blob *)obj, p, name);
185 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
188 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
189 objects = objects->next;
193 static void mark_blob_uninteresting(struct blob *blob)
197 if (blob->object.flags & UNINTERESTING)
199 blob->object.flags |= UNINTERESTING;
202 static void mark_tree_uninteresting(struct tree *tree)
204 struct object *obj = &tree->object;
205 struct tree_entry_list *entry;
209 if (obj->flags & UNINTERESTING)
211 obj->flags |= UNINTERESTING;
212 if (!has_sha1_file(obj->sha1))
214 if (parse_tree(tree) < 0)
215 die("bad tree %s", sha1_to_hex(obj->sha1));
216 entry = tree->entries;
218 if (entry->directory)
219 mark_tree_uninteresting(entry->item.tree);
221 mark_blob_uninteresting(entry->item.blob);
226 static void mark_parents_uninteresting(struct commit *commit)
228 struct commit_list *parents = commit->parents;
231 mark_tree_uninteresting(commit->tree);
233 struct commit *commit = parents->item;
234 commit->object.flags |= UNINTERESTING;
237 * Normally we haven't parsed the parent
238 * yet, so we won't have a parent of a parent
239 * here. However, it may turn out that we've
240 * reached this commit some other way (where it
241 * wasn't uninteresting), in which case we need
242 * to mark its parents recursively too..
245 mark_parents_uninteresting(commit);
248 * A missing commit is ok iff its parent is marked
251 * We just mark such a thing parsed, so that when
252 * it is popped next time around, we won't be trying
253 * to parse it and get an error.
255 if (!has_sha1_file(commit->object.sha1))
256 commit->object.parsed = 1;
257 parents = parents->next;
261 static int everybody_uninteresting(struct commit_list *orig)
263 struct commit_list *list = orig;
265 struct commit *commit = list->item;
267 if (commit->object.flags & UNINTERESTING)
273 * Ok, go back and mark all the edge trees uninteresting,
274 * since otherwise we can have situations where a parent
275 * that was marked uninteresting (and we never even had
276 * to look at) had lots of objects that we don't want to
279 * NOTE! This still doesn't mean that the object list is
280 * "correct", since we may end up listing objects that
281 * even older commits (that we don't list) do actually
282 * reference, but it gets us to a minimal list (or very
283 * close) in practice.
289 struct commit *commit = orig->item;
290 if (!parse_commit(commit) && commit->tree)
291 mark_tree_uninteresting(commit->tree);
298 * This is a truly stupid algorithm, but it's only
299 * used for bisection, and we just don't care enough.
301 * We care just barely enough to avoid recursing for
304 static int count_distance(struct commit_list *entry)
309 struct commit *commit = entry->item;
310 struct commit_list *p;
312 if (commit->object.flags & (UNINTERESTING | COUNTED))
315 commit->object.flags |= COUNTED;
321 nr += count_distance(p);
329 static void clear_distance(struct commit_list *list)
332 struct commit *commit = list->item;
333 commit->object.flags &= ~COUNTED;
338 static struct commit_list *find_bisection(struct commit_list *list)
341 struct commit_list *p, *best;
354 int distance = count_distance(p);
355 clear_distance(list);
356 if (nr - distance < distance)
357 distance = nr - distance;
358 if (distance > closest) {
369 static struct commit_list *limit_list(struct commit_list *list)
371 struct commit_list *newlist = NULL;
372 struct commit_list **p = &newlist;
374 struct commit *commit = pop_most_recent_commit(&list, SEEN);
375 struct object *obj = &commit->object;
377 if (unpacked && has_sha1_pack(obj->sha1))
378 obj->flags |= UNINTERESTING;
379 if (obj->flags & UNINTERESTING) {
380 mark_parents_uninteresting(commit);
381 if (everybody_uninteresting(list))
385 p = &commit_list_insert(commit, p)->next;
388 newlist = find_bisection(newlist);
392 static void add_pending_object(struct object *obj, const char *name)
394 add_object(obj, &pending_objects, name);
397 static struct commit *get_commit_reference(const char *name, unsigned int flags)
399 unsigned char sha1[20];
400 struct object *object;
402 if (get_sha1(name, sha1))
403 usage(rev_list_usage);
404 object = parse_object(sha1);
406 die("bad object %s", name);
409 * Tag object? Look what it points to..
411 while (object->type == tag_type) {
412 struct tag *tag = (struct tag *) object;
413 object->flags |= flags;
414 if (tag_objects && !(object->flags & UNINTERESTING))
415 add_pending_object(object, tag->tag);
416 object = parse_object(tag->tagged->sha1);
420 * Commit object? Just return it, we'll do all the complex
423 if (object->type == commit_type) {
424 struct commit *commit = (struct commit *)object;
425 object->flags |= flags;
426 if (parse_commit(commit) < 0)
427 die("unable to parse commit %s", name);
428 if (flags & UNINTERESTING)
429 mark_parents_uninteresting(commit);
434 * Tree object? Either mark it uniniteresting, or add it
435 * to the list of objects to look at later..
437 if (object->type == tree_type) {
438 struct tree *tree = (struct tree *)object;
441 if (flags & UNINTERESTING) {
442 mark_tree_uninteresting(tree);
445 add_pending_object(object, "");
450 * Blob object? You know the drill by now..
452 if (object->type == blob_type) {
453 struct blob *blob = (struct blob *)object;
456 if (flags & UNINTERESTING) {
457 mark_blob_uninteresting(blob);
460 add_pending_object(object, "");
463 die("%s is unknown object", name);
466 static void handle_one_commit(struct commit *com, struct commit_list **lst)
468 if (!com || com->object.flags & SEEN)
470 com->object.flags |= SEEN;
471 commit_list_insert(com, lst);
475 int main(int argc, char **argv)
477 struct commit_list *list = NULL;
480 for (i = 1 ; i < argc; i++) {
484 struct commit *commit;
486 if (!strncmp(arg, "--max-count=", 12)) {
487 max_count = atoi(arg + 12);
490 if (!strncmp(arg, "--max-age=", 10)) {
491 max_age = atoi(arg + 10);
494 if (!strncmp(arg, "--min-age=", 10)) {
495 min_age = atoi(arg + 10);
498 if (!strcmp(arg, "--header")) {
502 if (!strncmp(arg, "--pretty", 8)) {
503 commit_format = get_commit_format(arg+8);
505 hdr_termination = '\n';
509 if (!strncmp(arg, "--no-merges", 11)) {
513 if (!strcmp(arg, "--parents")) {
517 if (!strcmp(arg, "--bisect")) {
521 if (!strcmp(arg, "--objects")) {
527 if (!strcmp(arg, "--unpacked")) {
532 if (!strcmp(arg, "--merge-order")) {
536 if (!strcmp(arg, "--show-breaks")) {
540 if (!strcmp(arg, "--topo-order")) {
546 if (show_breaks && !merge_order)
547 usage(rev_list_usage);
550 dotdot = strstr(arg, "..");
552 char *next = dotdot + 2;
553 struct commit *exclude = NULL;
554 struct commit *include = NULL;
556 exclude = get_commit_reference(arg, UNINTERESTING);
557 include = get_commit_reference(next, 0);
558 if (exclude && include) {
560 handle_one_commit(exclude, &list);
561 handle_one_commit(include, &list);
567 flags = UNINTERESTING;
571 commit = get_commit_reference(arg, flags);
572 handle_one_commit(commit, &list);
578 list = limit_list(list);
580 sort_in_topological_order(&list);
581 show_commit_list(list);
584 if (sort_list_in_merge_order(list, &process_commit)) {
585 die("merge order sort failed\n");
588 die("merge order sort unsupported, OpenSSL not linked");