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)
15 #define TMP_MARK (1u << 5) /* for isolated cases; clean after use */
17 static const char rev_list_usage[] =
18 "git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
28 " --merge-order [ --show-breaks ]\n"
31 " formatting output:\n"
33 " --objects | --objects-edge\n"
35 " --header | --pretty\n"
36 " --abbrev=nr | --no-abbrev\n"
42 static int unpacked = 0;
43 static int bisect_list = 0;
44 static int tag_objects = 0;
45 static int tree_objects = 0;
46 static int blob_objects = 0;
47 static int edge_hint = 0;
48 static int verbose_header = 0;
49 static int abbrev = DEFAULT_ABBREV;
50 static int show_parents = 0;
51 static int hdr_termination = 0;
52 static const char *commit_prefix = "";
53 static unsigned long max_age = -1;
54 static unsigned long min_age = -1;
55 static int max_count = -1;
56 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
57 static int merge_order = 0;
58 static int show_breaks = 0;
59 static int stop_traversal = 0;
60 static int topo_order = 0;
62 static int no_merges = 0;
63 static const char **paths = NULL;
64 static int remove_empty_trees = 0;
66 static void show_commit(struct commit *commit)
68 commit->object.flags |= SHOWN;
71 if (commit->object.flags & DISCONTINUITY) {
73 } else if (commit->object.flags & BOUNDARY) {
77 printf("%s%s", commit_prefix, sha1_to_hex(commit->object.sha1));
79 struct commit_list *parents = commit->parents;
81 struct object *o = &(parents->item->object);
82 parents = parents->next;
83 if (o->flags & TMP_MARK)
85 printf(" %s", sha1_to_hex(o->sha1));
88 /* TMP_MARK is a general purpose flag that can
89 * be used locally, but the user should clean
90 * things up after it is done with them.
92 for (parents = commit->parents;
94 parents = parents->next)
95 parents->item->object.flags &= ~TMP_MARK;
97 if (commit_format == CMIT_FMT_ONELINE)
102 if (verbose_header) {
103 static char pretty_header[16384];
104 pretty_print_commit(commit_format, commit, ~0, pretty_header, sizeof(pretty_header), abbrev);
105 printf("%s%c", pretty_header, hdr_termination);
110 static int rewrite_one(struct commit **pp)
113 struct commit *p = *pp;
114 if (p->object.flags & (TREECHANGE | UNINTERESTING))
118 *pp = p->parents->item;
122 static void rewrite_parents(struct commit *commit)
124 struct commit_list **pp = &commit->parents;
126 struct commit_list *parent = *pp;
127 if (rewrite_one(&parent->item) < 0) {
135 static int filter_commit(struct commit * commit)
137 if (stop_traversal && (commit->object.flags & BOUNDARY))
139 if (commit->object.flags & (UNINTERESTING|SHOWN))
141 if (min_age != -1 && (commit->date > min_age))
143 if (max_age != -1 && (commit->date < max_age)) {
147 if (no_merges && (commit->parents && commit->parents->next))
149 if (paths && dense) {
150 if (!(commit->object.flags & TREECHANGE))
152 rewrite_parents(commit);
157 static int process_commit(struct commit * commit)
159 int action=filter_commit(commit);
161 if (action == STOP) {
165 if (action == CONTINUE) {
169 if (max_count != -1 && !max_count--)
177 static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
179 struct object_list *entry = xmalloc(sizeof(*entry));
187 static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
189 struct object *obj = &blob->object;
193 if (obj->flags & (UNINTERESTING | SEEN))
196 return add_object(obj, p, name);
199 static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
201 struct object *obj = &tree->object;
202 struct tree_entry_list *entry;
206 if (obj->flags & (UNINTERESTING | SEEN))
208 if (parse_tree(tree) < 0)
209 die("bad tree object %s", sha1_to_hex(obj->sha1));
211 p = add_object(obj, p, name);
212 entry = tree->entries;
213 tree->entries = NULL;
215 struct tree_entry_list *next = entry->next;
216 if (entry->directory)
217 p = process_tree(entry->item.tree, p, entry->name);
219 p = process_blob(entry->item.blob, p, entry->name);
226 static struct object_list *pending_objects = NULL;
228 static void show_commit_list(struct commit_list *list)
230 struct object_list *objects = NULL, **p = &objects, *pending;
232 struct commit *commit = pop_most_recent_commit(&list, SEEN);
234 p = process_tree(commit->tree, p, "");
235 if (process_commit(commit) == STOP)
238 for (pending = pending_objects; pending; pending = pending->next) {
239 struct object *obj = pending->item;
240 const char *name = pending->name;
241 if (obj->flags & (UNINTERESTING | SEEN))
243 if (obj->type == tag_type) {
245 p = add_object(obj, p, name);
248 if (obj->type == tree_type) {
249 p = process_tree((struct tree *)obj, p, name);
252 if (obj->type == blob_type) {
253 p = process_blob((struct blob *)obj, p, name);
256 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
259 /* An object with name "foo\n0000000000000000000000000000000000000000"
260 * can be used confuse downstream git-pack-objects very badly.
262 const char *ep = strchr(objects->name, '\n');
264 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
265 (int) (ep - objects->name),
269 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
270 objects = objects->next;
274 static void mark_blob_uninteresting(struct blob *blob)
278 if (blob->object.flags & UNINTERESTING)
280 blob->object.flags |= UNINTERESTING;
283 static void mark_tree_uninteresting(struct tree *tree)
285 struct object *obj = &tree->object;
286 struct tree_entry_list *entry;
290 if (obj->flags & UNINTERESTING)
292 obj->flags |= UNINTERESTING;
293 if (!has_sha1_file(obj->sha1))
295 if (parse_tree(tree) < 0)
296 die("bad tree %s", sha1_to_hex(obj->sha1));
297 entry = tree->entries;
298 tree->entries = NULL;
300 struct tree_entry_list *next = entry->next;
301 if (entry->directory)
302 mark_tree_uninteresting(entry->item.tree);
304 mark_blob_uninteresting(entry->item.blob);
310 static void mark_parents_uninteresting(struct commit *commit)
312 struct commit_list *parents = commit->parents;
315 struct commit *commit = parents->item;
316 commit->object.flags |= UNINTERESTING;
319 * Normally we haven't parsed the parent
320 * yet, so we won't have a parent of a parent
321 * here. However, it may turn out that we've
322 * reached this commit some other way (where it
323 * wasn't uninteresting), in which case we need
324 * to mark its parents recursively too..
327 mark_parents_uninteresting(commit);
330 * A missing commit is ok iff its parent is marked
333 * We just mark such a thing parsed, so that when
334 * it is popped next time around, we won't be trying
335 * to parse it and get an error.
337 if (!has_sha1_file(commit->object.sha1))
338 commit->object.parsed = 1;
339 parents = parents->next;
343 static int everybody_uninteresting(struct commit_list *orig)
345 struct commit_list *list = orig;
347 struct commit *commit = list->item;
349 if (commit->object.flags & UNINTERESTING)
357 * This is a truly stupid algorithm, but it's only
358 * used for bisection, and we just don't care enough.
360 * We care just barely enough to avoid recursing for
363 static int count_distance(struct commit_list *entry)
368 struct commit *commit = entry->item;
369 struct commit_list *p;
371 if (commit->object.flags & (UNINTERESTING | COUNTED))
373 if (!paths || (commit->object.flags & TREECHANGE))
375 commit->object.flags |= COUNTED;
381 nr += count_distance(p);
390 static void clear_distance(struct commit_list *list)
393 struct commit *commit = list->item;
394 commit->object.flags &= ~COUNTED;
399 static struct commit_list *find_bisection(struct commit_list *list)
402 struct commit_list *p, *best;
407 if (!paths || (p->item->object.flags & TREECHANGE))
414 for (p = list; p; p = p->next) {
417 if (paths && !(p->item->object.flags & TREECHANGE))
420 distance = count_distance(p);
421 clear_distance(list);
422 if (nr - distance < distance)
423 distance = nr - distance;
424 if (distance > closest) {
434 static void mark_edge_parents_uninteresting(struct commit *commit)
436 struct commit_list *parents;
438 for (parents = commit->parents; parents; parents = parents->next) {
439 struct commit *parent = parents->item;
440 if (!(parent->object.flags & UNINTERESTING))
442 mark_tree_uninteresting(parent->tree);
443 if (edge_hint && !(parent->object.flags & SHOWN)) {
444 parent->object.flags |= SHOWN;
445 printf("-%s\n", sha1_to_hex(parent->object.sha1));
450 static void mark_edges_uninteresting(struct commit_list *list)
452 for ( ; list; list = list->next) {
453 struct commit *commit = list->item;
455 if (commit->object.flags & UNINTERESTING) {
456 mark_tree_uninteresting(commit->tree);
459 mark_edge_parents_uninteresting(commit);
465 #define TREE_DIFFERENT 2
466 static int tree_difference = TREE_SAME;
468 static void file_add_remove(struct diff_options *options,
469 int addremove, unsigned mode,
470 const unsigned char *sha1,
471 const char *base, const char *path)
473 int diff = TREE_DIFFERENT;
476 * Is it an add of a new file? It means that
477 * the old tree didn't have it at all, so we
478 * will turn "TREE_SAME" -> "TREE_NEW", but
479 * leave any "TREE_DIFFERENT" alone (and if
480 * it already was "TREE_NEW", we'll keep it
481 * "TREE_NEW" of course).
483 if (addremove == '+') {
484 diff = tree_difference;
485 if (diff != TREE_SAME)
489 tree_difference = diff;
492 static void file_change(struct diff_options *options,
493 unsigned old_mode, unsigned new_mode,
494 const unsigned char *old_sha1,
495 const unsigned char *new_sha1,
496 const char *base, const char *path)
498 tree_difference = TREE_DIFFERENT;
501 static struct diff_options diff_opt = {
503 .add_remove = file_add_remove,
504 .change = file_change,
507 static int compare_tree(struct tree *t1, struct tree *t2)
512 return TREE_DIFFERENT;
513 tree_difference = TREE_SAME;
514 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "", &diff_opt) < 0)
515 return TREE_DIFFERENT;
516 return tree_difference;
519 static int same_tree_as_empty(struct tree *t1)
523 struct tree_desc empty, real;
528 tree = read_object_with_reference(t1->object.sha1, "tree", &real.size, NULL);
537 retval = diff_tree(&empty, &real, "", &diff_opt);
540 return retval >= 0 && !tree_difference;
543 static void try_to_simplify_commit(struct commit *commit)
545 struct commit_list **pp, *parent;
550 if (!commit->parents) {
551 if (!same_tree_as_empty(commit->tree))
552 commit->object.flags |= TREECHANGE;
556 pp = &commit->parents;
557 while ((parent = *pp) != NULL) {
558 struct commit *p = parent->item;
560 if (p->object.flags & UNINTERESTING) {
566 switch (compare_tree(p->tree, commit->tree)) {
569 commit->parents = parent;
573 if (remove_empty_trees && same_tree_as_empty(p->tree)) {
582 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
584 commit->object.flags |= TREECHANGE;
587 static void add_parents_to_list(struct commit *commit, struct commit_list **list)
589 struct commit_list *parent = commit->parents;
592 * If the commit is uninteresting, don't try to
593 * prune parents - we want the maximal uninteresting
596 * Normally we haven't parsed the parent
597 * yet, so we won't have a parent of a parent
598 * here. However, it may turn out that we've
599 * reached this commit some other way (where it
600 * wasn't uninteresting), in which case we need
601 * to mark its parents recursively too..
603 if (commit->object.flags & UNINTERESTING) {
605 struct commit *p = parent->item;
606 parent = parent->next;
608 p->object.flags |= UNINTERESTING;
610 mark_parents_uninteresting(p);
611 if (p->object.flags & SEEN)
613 p->object.flags |= SEEN;
614 insert_by_date(p, list);
620 * Ok, the commit wasn't uninteresting. Try to
621 * simplify the commit history and find the parent
622 * that has no differences in the path set if one exists.
625 try_to_simplify_commit(commit);
627 parent = commit->parents;
629 struct commit *p = parent->item;
631 parent = parent->next;
634 if (p->object.flags & SEEN)
636 p->object.flags |= SEEN;
637 insert_by_date(p, list);
641 static struct commit_list *limit_list(struct commit_list *list)
643 struct commit_list *newlist = NULL;
644 struct commit_list **p = &newlist;
646 struct commit_list *entry = list;
647 struct commit *commit = list->item;
648 struct object *obj = &commit->object;
653 if (max_age != -1 && (commit->date < max_age))
654 obj->flags |= UNINTERESTING;
655 if (unpacked && has_sha1_pack(obj->sha1))
656 obj->flags |= UNINTERESTING;
657 add_parents_to_list(commit, &list);
658 if (obj->flags & UNINTERESTING) {
659 mark_parents_uninteresting(commit);
660 if (everybody_uninteresting(list))
664 if (min_age != -1 && (commit->date > min_age))
666 p = &commit_list_insert(commit, p)->next;
669 mark_edges_uninteresting(newlist);
671 newlist = find_bisection(newlist);
675 static void add_pending_object(struct object *obj, const char *name)
677 add_object(obj, &pending_objects, name);
680 static struct commit *get_commit_reference(const char *name, const unsigned char *sha1, unsigned int flags)
682 struct object *object;
684 object = parse_object(sha1);
686 die("bad object %s", name);
689 * Tag object? Look what it points to..
691 while (object->type == tag_type) {
692 struct tag *tag = (struct tag *) object;
693 object->flags |= flags;
694 if (tag_objects && !(object->flags & UNINTERESTING))
695 add_pending_object(object, tag->tag);
696 object = parse_object(tag->tagged->sha1);
698 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
702 * Commit object? Just return it, we'll do all the complex
705 if (object->type == commit_type) {
706 struct commit *commit = (struct commit *)object;
707 object->flags |= flags;
708 if (parse_commit(commit) < 0)
709 die("unable to parse commit %s", name);
710 if (flags & UNINTERESTING)
711 mark_parents_uninteresting(commit);
716 * Tree object? Either mark it uniniteresting, or add it
717 * to the list of objects to look at later..
719 if (object->type == tree_type) {
720 struct tree *tree = (struct tree *)object;
723 if (flags & UNINTERESTING) {
724 mark_tree_uninteresting(tree);
727 add_pending_object(object, "");
732 * Blob object? You know the drill by now..
734 if (object->type == blob_type) {
735 struct blob *blob = (struct blob *)object;
738 if (flags & UNINTERESTING) {
739 mark_blob_uninteresting(blob);
742 add_pending_object(object, "");
745 die("%s is unknown object", name);
748 static void handle_one_commit(struct commit *com, struct commit_list **lst)
750 if (!com || com->object.flags & SEEN)
752 com->object.flags |= SEEN;
753 commit_list_insert(com, lst);
756 /* for_each_ref() callback does not allow user data -- Yuck. */
757 static struct commit_list **global_lst;
759 static int include_one_commit(const char *path, const unsigned char *sha1)
761 struct commit *com = get_commit_reference(path, sha1, 0);
762 handle_one_commit(com, global_lst);
766 static void handle_all(struct commit_list **lst)
769 for_each_ref(include_one_commit);
773 int main(int argc, const char **argv)
775 const char *prefix = setup_git_directory();
776 struct commit_list *list = NULL;
779 for (i = 1 ; i < argc; i++) {
781 const char *arg = argv[i];
783 struct commit *commit;
784 unsigned char sha1[20];
786 /* accept -<digit>, like traditilnal "head" */
787 if ((*arg == '-') && isdigit(arg[1])) {
788 max_count = atoi(arg + 1);
791 if (!strcmp(arg, "-n")) {
793 die("-n requires an argument");
794 max_count = atoi(argv[i]);
797 if (!strncmp(arg,"-n",2)) {
798 max_count = atoi(arg + 2);
801 if (!strncmp(arg, "--max-count=", 12)) {
802 max_count = atoi(arg + 12);
805 if (!strncmp(arg, "--max-age=", 10)) {
806 max_age = atoi(arg + 10);
810 if (!strncmp(arg, "--min-age=", 10)) {
811 min_age = atoi(arg + 10);
815 if (!strcmp(arg, "--header")) {
819 if (!strcmp(arg, "--no-abbrev")) {
823 if (!strncmp(arg, "--abbrev=", 9)) {
824 abbrev = strtoul(arg + 9, NULL, 10);
825 if (abbrev && abbrev < MINIMUM_ABBREV)
826 abbrev = MINIMUM_ABBREV;
827 else if (40 < abbrev)
831 if (!strncmp(arg, "--pretty", 8)) {
832 commit_format = get_commit_format(arg+8);
834 hdr_termination = '\n';
835 if (commit_format == CMIT_FMT_ONELINE)
838 commit_prefix = "commit ";
841 if (!strncmp(arg, "--no-merges", 11)) {
845 if (!strcmp(arg, "--parents")) {
849 if (!strcmp(arg, "--bisect")) {
853 if (!strcmp(arg, "--all")) {
857 if (!strcmp(arg, "--objects")) {
863 if (!strcmp(arg, "--objects-edge")) {
870 if (!strcmp(arg, "--unpacked")) {
875 if (!strcmp(arg, "--merge-order")) {
879 if (!strcmp(arg, "--show-breaks")) {
883 if (!strcmp(arg, "--topo-order")) {
889 if (!strcmp(arg, "--date-order")) {
895 if (!strcmp(arg, "--dense")) {
899 if (!strcmp(arg, "--sparse")) {
903 if (!strcmp(arg, "--remove-empty")) {
904 remove_empty_trees = 1;
907 if (!strcmp(arg, "--")) {
912 if (show_breaks && !merge_order)
913 usage(rev_list_usage);
916 dotdot = strstr(arg, "..");
918 unsigned char from_sha1[20];
919 char *next = dotdot + 2;
923 if (!get_sha1(arg, from_sha1) && !get_sha1(next, sha1)) {
924 struct commit *exclude;
925 struct commit *include;
927 exclude = get_commit_reference(arg, from_sha1, UNINTERESTING);
928 include = get_commit_reference(next, sha1, 0);
929 if (!exclude || !include)
930 die("Invalid revision range %s..%s", arg, next);
932 handle_one_commit(exclude, &list);
933 handle_one_commit(include, &list);
939 flags = UNINTERESTING;
943 if (get_sha1(arg, sha1) < 0) {
945 if (lstat(arg, &st) < 0)
946 die("'%s': %s", arg, strerror(errno));
949 commit = get_commit_reference(arg, sha1, flags);
950 handle_one_commit(commit, &list);
954 (!(tag_objects||tree_objects||blob_objects) && !pending_objects))
955 usage(rev_list_usage);
957 paths = get_pathspec(prefix, argv + i);
960 diff_tree_setup_paths(paths);
963 save_commit_buffer = verbose_header;
964 track_object_refs = 0;
968 if (list && !limited && max_count == 1 &&
969 !tag_objects && !tree_objects && !blob_objects) {
970 show_commit(list->item);
974 list = limit_list(list);
976 sort_in_topological_order(&list, lifo);
977 show_commit_list(list);
980 if (sort_list_in_merge_order(list, &process_commit)) {
981 die("merge order sort failed\n");
984 die("merge order sort unsupported, OpenSSL not linked");