10 #define SEEN (1u << 0)
11 #define INTERESTING (1u << 1)
12 #define COUNTED (1u << 2)
13 #define SHOWN (1u << 3)
14 #define TREECHANGE (1u << 4)
16 static const char rev_list_usage[] =
17 "git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
27 " --merge-order [ --show-breaks ]\n"
29 " formatting output:\n"
33 " --header | --pretty\n"
39 static int unpacked = 0;
40 static int bisect_list = 0;
41 static int tag_objects = 0;
42 static int tree_objects = 0;
43 static int blob_objects = 0;
44 static int verbose_header = 0;
45 static int show_parents = 0;
46 static int hdr_termination = 0;
47 static const char *commit_prefix = "";
48 static unsigned long max_age = -1;
49 static unsigned long min_age = -1;
50 static int max_count = -1;
51 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
52 static int merge_order = 0;
53 static int show_breaks = 0;
54 static int stop_traversal = 0;
55 static int topo_order = 0;
56 static int no_merges = 0;
57 static const char **paths = NULL;
58 static int remove_empty_trees = 0;
60 static void show_commit(struct commit *commit)
62 commit->object.flags |= SHOWN;
65 if (commit->object.flags & DISCONTINUITY) {
67 } else if (commit->object.flags & BOUNDARY) {
71 printf("%s%s", commit_prefix, sha1_to_hex(commit->object.sha1));
73 struct commit_list *parents = commit->parents;
75 printf(" %s", sha1_to_hex(parents->item->object.sha1));
76 parents = parents->next;
79 if (commit_format == CMIT_FMT_ONELINE)
85 static char pretty_header[16384];
86 pretty_print_commit(commit_format, commit, ~0, pretty_header, sizeof(pretty_header), 0);
87 printf("%s%c", pretty_header, hdr_termination);
92 static int rewrite_one(struct commit **pp)
95 struct commit *p = *pp;
96 if (p->object.flags & (TREECHANGE | UNINTERESTING))
100 *pp = p->parents->item;
104 static void rewrite_parents(struct commit *commit)
106 struct commit_list **pp = &commit->parents;
108 struct commit_list *parent = *pp;
109 if (rewrite_one(&parent->item) < 0) {
117 static int filter_commit(struct commit * commit)
119 if (stop_traversal && (commit->object.flags & BOUNDARY))
121 if (commit->object.flags & (UNINTERESTING|SHOWN))
123 if (min_age != -1 && (commit->date > min_age))
125 if (max_age != -1 && (commit->date < max_age)) {
129 if (no_merges && (commit->parents && commit->parents->next))
131 if (paths && dense) {
132 if (!(commit->object.flags & TREECHANGE))
134 rewrite_parents(commit);
139 static int process_commit(struct commit * commit)
141 int action=filter_commit(commit);
143 if (action == STOP) {
147 if (action == CONTINUE) {
151 if (max_count != -1 && !max_count--)
159 static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
161 struct object_list *entry = xmalloc(sizeof(*entry));
169 static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
171 struct object *obj = &blob->object;
175 if (obj->flags & (UNINTERESTING | SEEN))
178 return add_object(obj, p, name);
181 static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
183 struct object *obj = &tree->object;
184 struct tree_entry_list *entry;
188 if (obj->flags & (UNINTERESTING | SEEN))
190 if (parse_tree(tree) < 0)
191 die("bad tree object %s", sha1_to_hex(obj->sha1));
193 p = add_object(obj, p, name);
194 entry = tree->entries;
195 tree->entries = NULL;
197 struct tree_entry_list *next = entry->next;
198 if (entry->directory)
199 p = process_tree(entry->item.tree, p, entry->name);
201 p = process_blob(entry->item.blob, p, entry->name);
208 static struct object_list *pending_objects = NULL;
210 static void show_commit_list(struct commit_list *list)
212 struct object_list *objects = NULL, **p = &objects, *pending;
214 struct commit *commit = pop_most_recent_commit(&list, SEEN);
216 p = process_tree(commit->tree, p, "");
217 if (process_commit(commit) == STOP)
220 for (pending = pending_objects; pending; pending = pending->next) {
221 struct object *obj = pending->item;
222 const char *name = pending->name;
223 if (obj->flags & (UNINTERESTING | SEEN))
225 if (obj->type == tag_type) {
227 p = add_object(obj, p, name);
230 if (obj->type == tree_type) {
231 p = process_tree((struct tree *)obj, p, name);
234 if (obj->type == blob_type) {
235 p = process_blob((struct blob *)obj, p, name);
238 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
241 /* An object with name "foo\n0000000000000000000000000000000000000000"
242 * can be used confuse downstream git-pack-objects very badly.
244 const char *ep = strchr(objects->name, '\n');
246 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
247 (int) (ep - objects->name),
251 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
252 objects = objects->next;
256 static void mark_blob_uninteresting(struct blob *blob)
260 if (blob->object.flags & UNINTERESTING)
262 blob->object.flags |= UNINTERESTING;
265 static void mark_tree_uninteresting(struct tree *tree)
267 struct object *obj = &tree->object;
268 struct tree_entry_list *entry;
272 if (obj->flags & UNINTERESTING)
274 obj->flags |= UNINTERESTING;
275 if (!has_sha1_file(obj->sha1))
277 if (parse_tree(tree) < 0)
278 die("bad tree %s", sha1_to_hex(obj->sha1));
279 entry = tree->entries;
280 tree->entries = NULL;
282 struct tree_entry_list *next = entry->next;
283 if (entry->directory)
284 mark_tree_uninteresting(entry->item.tree);
286 mark_blob_uninteresting(entry->item.blob);
292 static void mark_parents_uninteresting(struct commit *commit)
294 struct commit_list *parents = commit->parents;
297 struct commit *commit = parents->item;
298 commit->object.flags |= UNINTERESTING;
301 * Normally we haven't parsed the parent
302 * yet, so we won't have a parent of a parent
303 * here. However, it may turn out that we've
304 * reached this commit some other way (where it
305 * wasn't uninteresting), in which case we need
306 * to mark its parents recursively too..
309 mark_parents_uninteresting(commit);
312 * A missing commit is ok iff its parent is marked
315 * We just mark such a thing parsed, so that when
316 * it is popped next time around, we won't be trying
317 * to parse it and get an error.
319 if (!has_sha1_file(commit->object.sha1))
320 commit->object.parsed = 1;
321 parents = parents->next;
325 static int everybody_uninteresting(struct commit_list *orig)
327 struct commit_list *list = orig;
329 struct commit *commit = list->item;
331 if (commit->object.flags & UNINTERESTING)
339 * This is a truly stupid algorithm, but it's only
340 * used for bisection, and we just don't care enough.
342 * We care just barely enough to avoid recursing for
345 static int count_distance(struct commit_list *entry)
350 struct commit *commit = entry->item;
351 struct commit_list *p;
353 if (commit->object.flags & (UNINTERESTING | COUNTED))
355 if (!paths || (commit->object.flags & TREECHANGE))
357 commit->object.flags |= COUNTED;
363 nr += count_distance(p);
372 static void clear_distance(struct commit_list *list)
375 struct commit *commit = list->item;
376 commit->object.flags &= ~COUNTED;
381 static struct commit_list *find_bisection(struct commit_list *list)
384 struct commit_list *p, *best;
389 if (!paths || (p->item->object.flags & TREECHANGE))
396 for (p = list; p; p = p->next) {
399 if (paths && !(p->item->object.flags & TREECHANGE))
402 distance = count_distance(p);
403 clear_distance(list);
404 if (nr - distance < distance)
405 distance = nr - distance;
406 if (distance > closest) {
416 static void mark_edges_uninteresting(struct commit_list *list)
418 for ( ; list; list = list->next) {
419 struct commit_list *parents = list->item->parents;
421 for ( ; parents; parents = parents->next) {
422 struct commit *commit = parents->item;
423 if (commit->object.flags & UNINTERESTING)
424 mark_tree_uninteresting(commit->tree);
431 #define TREE_DIFFERENT 2
432 static int tree_difference = TREE_SAME;
434 static void file_add_remove(struct diff_options *options,
435 int addremove, unsigned mode,
436 const unsigned char *sha1,
437 const char *base, const char *path)
439 int diff = TREE_DIFFERENT;
442 * Is it an add of a new file? It means that
443 * the old tree didn't have it at all, so we
444 * will turn "TREE_SAME" -> "TREE_NEW", but
445 * leave any "TREE_DIFFERENT" alone (and if
446 * it already was "TREE_NEW", we'll keep it
447 * "TREE_NEW" of course).
449 if (addremove == '+') {
450 diff = tree_difference;
451 if (diff != TREE_SAME)
455 tree_difference = diff;
458 static void file_change(struct diff_options *options,
459 unsigned old_mode, unsigned new_mode,
460 const unsigned char *old_sha1,
461 const unsigned char *new_sha1,
462 const char *base, const char *path)
464 tree_difference = TREE_DIFFERENT;
467 static struct diff_options diff_opt = {
469 .add_remove = file_add_remove,
470 .change = file_change,
473 static int compare_tree(struct tree *t1, struct tree *t2)
478 return TREE_DIFFERENT;
479 tree_difference = TREE_SAME;
480 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "", &diff_opt) < 0)
481 return TREE_DIFFERENT;
482 return tree_difference;
485 static int same_tree_as_empty(struct tree *t1)
489 struct tree_desc empty, real;
494 tree = read_object_with_reference(t1->object.sha1, "tree", &real.size, NULL);
503 retval = diff_tree(&empty, &real, "", &diff_opt);
506 return retval >= 0 && !tree_difference;
509 static void try_to_simplify_commit(struct commit *commit)
511 struct commit_list **pp, *parent;
516 if (!commit->parents) {
517 if (!same_tree_as_empty(commit->tree))
518 commit->object.flags |= TREECHANGE;
522 pp = &commit->parents;
523 while ((parent = *pp) != NULL) {
524 struct commit *p = parent->item;
526 if (p->object.flags & UNINTERESTING) {
532 switch (compare_tree(p->tree, commit->tree)) {
535 commit->parents = parent;
539 if (remove_empty_trees && same_tree_as_empty(p->tree)) {
548 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
550 commit->object.flags |= TREECHANGE;
553 static void add_parents_to_list(struct commit *commit, struct commit_list **list)
555 struct commit_list *parent = commit->parents;
558 * If the commit is uninteresting, don't try to
559 * prune parents - we want the maximal uninteresting
562 * Normally we haven't parsed the parent
563 * yet, so we won't have a parent of a parent
564 * here. However, it may turn out that we've
565 * reached this commit some other way (where it
566 * wasn't uninteresting), in which case we need
567 * to mark its parents recursively too..
569 if (commit->object.flags & UNINTERESTING) {
571 struct commit *p = parent->item;
572 parent = parent->next;
574 p->object.flags |= UNINTERESTING;
576 mark_parents_uninteresting(p);
577 if (p->object.flags & SEEN)
579 p->object.flags |= SEEN;
580 insert_by_date(p, list);
586 * Ok, the commit wasn't uninteresting. Try to
587 * simplify the commit history and find the parent
588 * that has no differences in the path set if one exists.
591 try_to_simplify_commit(commit);
593 parent = commit->parents;
595 struct commit *p = parent->item;
597 parent = parent->next;
600 if (p->object.flags & SEEN)
602 p->object.flags |= SEEN;
603 insert_by_date(p, list);
607 static struct commit_list *limit_list(struct commit_list *list)
609 struct commit_list *newlist = NULL;
610 struct commit_list **p = &newlist;
612 struct commit_list *entry = list;
613 struct commit *commit = list->item;
614 struct object *obj = &commit->object;
619 if (max_age != -1 && (commit->date < max_age))
620 obj->flags |= UNINTERESTING;
621 if (unpacked && has_sha1_pack(obj->sha1))
622 obj->flags |= UNINTERESTING;
623 add_parents_to_list(commit, &list);
624 if (obj->flags & UNINTERESTING) {
625 mark_parents_uninteresting(commit);
626 if (everybody_uninteresting(list))
630 if (min_age != -1 && (commit->date > min_age))
632 p = &commit_list_insert(commit, p)->next;
635 mark_edges_uninteresting(newlist);
637 newlist = find_bisection(newlist);
641 static void add_pending_object(struct object *obj, const char *name)
643 add_object(obj, &pending_objects, name);
646 static struct commit *get_commit_reference(const char *name, const unsigned char *sha1, unsigned int flags)
648 struct object *object;
650 object = parse_object(sha1);
652 die("bad object %s", name);
655 * Tag object? Look what it points to..
657 while (object->type == tag_type) {
658 struct tag *tag = (struct tag *) object;
659 object->flags |= flags;
660 if (tag_objects && !(object->flags & UNINTERESTING))
661 add_pending_object(object, tag->tag);
662 object = parse_object(tag->tagged->sha1);
664 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
668 * Commit object? Just return it, we'll do all the complex
671 if (object->type == commit_type) {
672 struct commit *commit = (struct commit *)object;
673 object->flags |= flags;
674 if (parse_commit(commit) < 0)
675 die("unable to parse commit %s", name);
676 if (flags & UNINTERESTING)
677 mark_parents_uninteresting(commit);
682 * Tree object? Either mark it uniniteresting, or add it
683 * to the list of objects to look at later..
685 if (object->type == tree_type) {
686 struct tree *tree = (struct tree *)object;
689 if (flags & UNINTERESTING) {
690 mark_tree_uninteresting(tree);
693 add_pending_object(object, "");
698 * Blob object? You know the drill by now..
700 if (object->type == blob_type) {
701 struct blob *blob = (struct blob *)object;
704 if (flags & UNINTERESTING) {
705 mark_blob_uninteresting(blob);
708 add_pending_object(object, "");
711 die("%s is unknown object", name);
714 static void handle_one_commit(struct commit *com, struct commit_list **lst)
716 if (!com || com->object.flags & SEEN)
718 com->object.flags |= SEEN;
719 commit_list_insert(com, lst);
722 /* for_each_ref() callback does not allow user data -- Yuck. */
723 static struct commit_list **global_lst;
725 static int include_one_commit(const char *path, const unsigned char *sha1)
727 struct commit *com = get_commit_reference(path, sha1, 0);
728 handle_one_commit(com, global_lst);
732 static void handle_all(struct commit_list **lst)
735 for_each_ref(include_one_commit);
739 int main(int argc, const char **argv)
741 const char *prefix = setup_git_directory();
742 struct commit_list *list = NULL;
745 for (i = 1 ; i < argc; i++) {
747 const char *arg = argv[i];
749 struct commit *commit;
750 unsigned char sha1[20];
752 /* accept -<digit>, like traditilnal "head" */
753 if ((*arg == '-') && isdigit(arg[1])) {
754 max_count = atoi(arg + 1);
757 if (!strcmp(arg, "-n")) {
759 die("-n requires an argument");
760 max_count = atoi(argv[i]);
763 if (!strncmp(arg,"-n",2)) {
764 max_count = atoi(arg + 2);
767 if (!strncmp(arg, "--max-count=", 12)) {
768 max_count = atoi(arg + 12);
771 if (!strncmp(arg, "--max-age=", 10)) {
772 max_age = atoi(arg + 10);
776 if (!strncmp(arg, "--min-age=", 10)) {
777 min_age = atoi(arg + 10);
781 if (!strcmp(arg, "--header")) {
785 if (!strncmp(arg, "--pretty", 8)) {
786 commit_format = get_commit_format(arg+8);
788 hdr_termination = '\n';
789 if (commit_format == CMIT_FMT_ONELINE)
792 commit_prefix = "commit ";
795 if (!strncmp(arg, "--no-merges", 11)) {
799 if (!strcmp(arg, "--parents")) {
803 if (!strcmp(arg, "--bisect")) {
807 if (!strcmp(arg, "--all")) {
811 if (!strcmp(arg, "--objects")) {
817 if (!strcmp(arg, "--unpacked")) {
822 if (!strcmp(arg, "--merge-order")) {
826 if (!strcmp(arg, "--show-breaks")) {
830 if (!strcmp(arg, "--topo-order")) {
835 if (!strcmp(arg, "--dense")) {
839 if (!strcmp(arg, "--sparse")) {
843 if (!strcmp(arg, "--remove-empty")) {
844 remove_empty_trees = 1;
847 if (!strcmp(arg, "--")) {
852 if (show_breaks && !merge_order)
853 usage(rev_list_usage);
856 dotdot = strstr(arg, "..");
858 unsigned char from_sha1[20];
859 char *next = dotdot + 2;
863 if (!get_sha1(arg, from_sha1) && !get_sha1(next, sha1)) {
864 struct commit *exclude;
865 struct commit *include;
867 exclude = get_commit_reference(arg, from_sha1, UNINTERESTING);
868 include = get_commit_reference(next, sha1, 0);
869 if (!exclude || !include)
870 die("Invalid revision range %s..%s", arg, next);
872 handle_one_commit(exclude, &list);
873 handle_one_commit(include, &list);
879 flags = UNINTERESTING;
883 if (get_sha1(arg, sha1) < 0) {
885 if (lstat(arg, &st) < 0)
886 die("'%s': %s", arg, strerror(errno));
889 commit = get_commit_reference(arg, sha1, flags);
890 handle_one_commit(commit, &list);
894 (!(tag_objects||tree_objects||blob_objects) && !pending_objects))
895 usage(rev_list_usage);
897 paths = get_pathspec(prefix, argv + i);
900 diff_tree_setup_paths(paths);
903 save_commit_buffer = verbose_header;
904 track_object_refs = 0;
908 if (list && !limited && max_count == 1 &&
909 !tag_objects && !tree_objects && !blob_objects) {
910 show_commit(list->item);
914 list = limit_list(list);
916 sort_in_topological_order(&list);
917 show_commit_list(list);
920 if (sort_list_in_merge_order(list, &process_commit)) {
921 die("merge order sort failed\n");
924 die("merge order sort unsupported, OpenSSL not linked");