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"
26 " --merge-order [ --show-breaks ]\n"
28 " formatting output:\n"
32 " --header | --pretty\n"
38 static int unpacked = 0;
39 static int bisect_list = 0;
40 static int tag_objects = 0;
41 static int tree_objects = 0;
42 static int blob_objects = 0;
43 static int verbose_header = 0;
44 static int show_parents = 0;
45 static int hdr_termination = 0;
46 static const char *commit_prefix = "";
47 static unsigned long max_age = -1;
48 static unsigned long min_age = -1;
49 static int max_count = -1;
50 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
51 static int merge_order = 0;
52 static int show_breaks = 0;
53 static int stop_traversal = 0;
54 static int topo_order = 0;
55 static int no_merges = 0;
56 static const char **paths = NULL;
58 static void show_commit(struct commit *commit)
60 commit->object.flags |= SHOWN;
63 if (commit->object.flags & DISCONTINUITY) {
65 } else if (commit->object.flags & BOUNDARY) {
69 printf("%s%s", commit_prefix, sha1_to_hex(commit->object.sha1));
71 struct commit_list *parents = commit->parents;
73 printf(" %s", sha1_to_hex(parents->item->object.sha1));
74 parents = parents->next;
77 if (commit_format == CMIT_FMT_ONELINE)
83 static char pretty_header[16384];
84 pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header));
85 printf("%s%c", pretty_header, hdr_termination);
90 static int rewrite_one(struct commit **pp)
93 struct commit *p = *pp;
94 if (p->object.flags & (TREECHANGE | UNINTERESTING))
98 *pp = p->parents->item;
102 static void rewrite_parents(struct commit *commit)
104 struct commit_list **pp = &commit->parents;
106 struct commit_list *parent = *pp;
107 if (rewrite_one(&parent->item) < 0) {
115 static int filter_commit(struct commit * commit)
117 if (stop_traversal && (commit->object.flags & BOUNDARY))
119 if (commit->object.flags & (UNINTERESTING|SHOWN))
121 if (min_age != -1 && (commit->date > min_age))
123 if (max_age != -1 && (commit->date < max_age)) {
127 if (no_merges && (commit->parents && commit->parents->next))
129 if (paths && dense) {
130 if (!(commit->object.flags & TREECHANGE))
132 rewrite_parents(commit);
137 static int process_commit(struct commit * commit)
139 int action=filter_commit(commit);
141 if (action == STOP) {
145 if (action == CONTINUE) {
149 if (max_count != -1 && !max_count--)
157 static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
159 struct object_list *entry = xmalloc(sizeof(*entry));
167 static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
169 struct object *obj = &blob->object;
173 if (obj->flags & (UNINTERESTING | SEEN))
176 return add_object(obj, p, name);
179 static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
181 struct object *obj = &tree->object;
182 struct tree_entry_list *entry;
186 if (obj->flags & (UNINTERESTING | SEEN))
188 if (parse_tree(tree) < 0)
189 die("bad tree object %s", sha1_to_hex(obj->sha1));
191 p = add_object(obj, p, name);
192 entry = tree->entries;
193 tree->entries = NULL;
195 struct tree_entry_list *next = entry->next;
196 if (entry->directory)
197 p = process_tree(entry->item.tree, p, entry->name);
199 p = process_blob(entry->item.blob, p, entry->name);
206 static struct object_list *pending_objects = NULL;
208 static void show_commit_list(struct commit_list *list)
210 struct object_list *objects = NULL, **p = &objects, *pending;
212 struct commit *commit = pop_most_recent_commit(&list, SEEN);
214 p = process_tree(commit->tree, p, "");
215 if (process_commit(commit) == STOP)
218 for (pending = pending_objects; pending; pending = pending->next) {
219 struct object *obj = pending->item;
220 const char *name = pending->name;
221 if (obj->flags & (UNINTERESTING | SEEN))
223 if (obj->type == tag_type) {
225 p = add_object(obj, p, name);
228 if (obj->type == tree_type) {
229 p = process_tree((struct tree *)obj, p, name);
232 if (obj->type == blob_type) {
233 p = process_blob((struct blob *)obj, p, name);
236 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
239 /* An object with name "foo\n0000000000000000000000000000000000000000"
240 * can be used confuse downstream git-pack-objects very badly.
242 const char *ep = strchr(objects->name, '\n');
244 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
245 (int) (ep - objects->name),
249 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
250 objects = objects->next;
254 static void mark_blob_uninteresting(struct blob *blob)
258 if (blob->object.flags & UNINTERESTING)
260 blob->object.flags |= UNINTERESTING;
263 static void mark_tree_uninteresting(struct tree *tree)
265 struct object *obj = &tree->object;
266 struct tree_entry_list *entry;
270 if (obj->flags & UNINTERESTING)
272 obj->flags |= UNINTERESTING;
273 if (!has_sha1_file(obj->sha1))
275 if (parse_tree(tree) < 0)
276 die("bad tree %s", sha1_to_hex(obj->sha1));
277 entry = tree->entries;
278 tree->entries = NULL;
280 struct tree_entry_list *next = entry->next;
281 if (entry->directory)
282 mark_tree_uninteresting(entry->item.tree);
284 mark_blob_uninteresting(entry->item.blob);
290 static void mark_parents_uninteresting(struct commit *commit)
292 struct commit_list *parents = commit->parents;
295 struct commit *commit = parents->item;
296 commit->object.flags |= UNINTERESTING;
299 * Normally we haven't parsed the parent
300 * yet, so we won't have a parent of a parent
301 * here. However, it may turn out that we've
302 * reached this commit some other way (where it
303 * wasn't uninteresting), in which case we need
304 * to mark its parents recursively too..
307 mark_parents_uninteresting(commit);
310 * A missing commit is ok iff its parent is marked
313 * We just mark such a thing parsed, so that when
314 * it is popped next time around, we won't be trying
315 * to parse it and get an error.
317 if (!has_sha1_file(commit->object.sha1))
318 commit->object.parsed = 1;
319 parents = parents->next;
323 static int everybody_uninteresting(struct commit_list *orig)
325 struct commit_list *list = orig;
327 struct commit *commit = list->item;
329 if (commit->object.flags & UNINTERESTING)
337 * This is a truly stupid algorithm, but it's only
338 * used for bisection, and we just don't care enough.
340 * We care just barely enough to avoid recursing for
343 static int count_distance(struct commit_list *entry)
348 struct commit *commit = entry->item;
349 struct commit_list *p;
351 if (commit->object.flags & (UNINTERESTING | COUNTED))
353 if (!paths || (commit->object.flags & TREECHANGE))
355 commit->object.flags |= COUNTED;
361 nr += count_distance(p);
370 static void clear_distance(struct commit_list *list)
373 struct commit *commit = list->item;
374 commit->object.flags &= ~COUNTED;
379 static struct commit_list *find_bisection(struct commit_list *list)
382 struct commit_list *p, *best;
387 if (!paths || (p->item->object.flags & TREECHANGE))
394 for (p = list; p; p = p->next) {
397 if (paths && !(p->item->object.flags & TREECHANGE))
400 distance = count_distance(p);
401 clear_distance(list);
402 if (nr - distance < distance)
403 distance = nr - distance;
404 if (distance > closest) {
414 static void mark_edges_uninteresting(struct commit_list *list)
416 for ( ; list; list = list->next) {
417 struct commit_list *parents = list->item->parents;
419 for ( ; parents; parents = parents->next) {
420 struct commit *commit = parents->item;
421 if (commit->object.flags & UNINTERESTING)
422 mark_tree_uninteresting(commit->tree);
427 static int is_different = 0;
429 static void file_add_remove(struct diff_options *options,
430 int addremove, unsigned mode,
431 const unsigned char *sha1,
432 const char *base, const char *path)
437 static void file_change(struct diff_options *options,
438 unsigned old_mode, unsigned new_mode,
439 const unsigned char *old_sha1,
440 const unsigned char *new_sha1,
441 const char *base, const char *path)
446 static struct diff_options diff_opt = {
448 .add_remove = file_add_remove,
449 .change = file_change,
452 static int same_tree(struct tree *t1, struct tree *t2)
455 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "", &diff_opt) < 0)
457 return !is_different;
460 static int same_tree_as_empty(struct tree *t1)
464 struct tree_desc empty, real;
469 tree = read_object_with_reference(t1->object.sha1, "tree", &real.size, NULL);
478 retval = diff_tree(&empty, &real, "", &diff_opt);
481 return retval >= 0 && !is_different;
484 static struct commit *try_to_simplify_merge(struct commit *commit, struct commit_list *parent)
490 struct commit *p = parent->item;
491 parent = parent->next;
495 if (same_tree(commit->tree, p->tree))
501 static void add_parents_to_list(struct commit *commit, struct commit_list **list)
503 struct commit_list *parent = commit->parents;
506 * If the commit is uninteresting, don't try to
507 * prune parents - we want the maximal uninteresting
510 * Normally we haven't parsed the parent
511 * yet, so we won't have a parent of a parent
512 * here. However, it may turn out that we've
513 * reached this commit some other way (where it
514 * wasn't uninteresting), in which case we need
515 * to mark its parents recursively too..
517 if (commit->object.flags & UNINTERESTING) {
519 struct commit *p = parent->item;
520 parent = parent->next;
522 p->object.flags |= UNINTERESTING;
524 mark_parents_uninteresting(p);
525 if (p->object.flags & SEEN)
527 p->object.flags |= SEEN;
528 insert_by_date(p, list);
534 * Ok, the commit wasn't uninteresting. If it
535 * is a merge, try to find the parent that has
536 * no differences in the path set if one exists.
538 if (paths && parent && parent->next) {
539 struct commit *preferred;
541 preferred = try_to_simplify_merge(commit, parent);
543 parent->item = preferred;
549 struct commit *p = parent->item;
551 parent = parent->next;
554 if (p->object.flags & SEEN)
556 p->object.flags |= SEEN;
557 insert_by_date(p, list);
561 static void compress_list(struct commit_list *list)
564 struct commit *commit = list->item;
565 struct commit_list *parent = commit->parents;
569 if (!same_tree_as_empty(commit->tree))
570 commit->object.flags |= TREECHANGE;
575 * Exactly one parent? Check if it leaves the tree
579 struct tree *t1 = commit->tree;
580 struct tree *t2 = parent->item->tree;
581 if (!t1 || !t2 || same_tree(t1, t2))
584 commit->object.flags |= TREECHANGE;
588 static struct commit_list *limit_list(struct commit_list *list)
590 struct commit_list *newlist = NULL;
591 struct commit_list **p = &newlist;
593 struct commit_list *entry = list;
594 struct commit *commit = list->item;
595 struct object *obj = &commit->object;
600 if (max_age != -1 && (commit->date < max_age))
601 obj->flags |= UNINTERESTING;
602 if (unpacked && has_sha1_pack(obj->sha1))
603 obj->flags |= UNINTERESTING;
604 add_parents_to_list(commit, &list);
605 if (obj->flags & UNINTERESTING) {
606 mark_parents_uninteresting(commit);
607 if (everybody_uninteresting(list))
611 if (min_age != -1 && (commit->date > min_age))
613 p = &commit_list_insert(commit, p)->next;
616 mark_edges_uninteresting(newlist);
618 compress_list(newlist);
620 newlist = find_bisection(newlist);
624 static void add_pending_object(struct object *obj, const char *name)
626 add_object(obj, &pending_objects, name);
629 static struct commit *get_commit_reference(const char *name, const unsigned char *sha1, unsigned int flags)
631 struct object *object;
633 object = parse_object(sha1);
635 die("bad object %s", name);
638 * Tag object? Look what it points to..
640 while (object->type == tag_type) {
641 struct tag *tag = (struct tag *) object;
642 object->flags |= flags;
643 if (tag_objects && !(object->flags & UNINTERESTING))
644 add_pending_object(object, tag->tag);
645 object = parse_object(tag->tagged->sha1);
647 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
651 * Commit object? Just return it, we'll do all the complex
654 if (object->type == commit_type) {
655 struct commit *commit = (struct commit *)object;
656 object->flags |= flags;
657 if (parse_commit(commit) < 0)
658 die("unable to parse commit %s", name);
659 if (flags & UNINTERESTING)
660 mark_parents_uninteresting(commit);
665 * Tree object? Either mark it uniniteresting, or add it
666 * to the list of objects to look at later..
668 if (object->type == tree_type) {
669 struct tree *tree = (struct tree *)object;
672 if (flags & UNINTERESTING) {
673 mark_tree_uninteresting(tree);
676 add_pending_object(object, "");
681 * Blob object? You know the drill by now..
683 if (object->type == blob_type) {
684 struct blob *blob = (struct blob *)object;
687 if (flags & UNINTERESTING) {
688 mark_blob_uninteresting(blob);
691 add_pending_object(object, "");
694 die("%s is unknown object", name);
697 static void handle_one_commit(struct commit *com, struct commit_list **lst)
699 if (!com || com->object.flags & SEEN)
701 com->object.flags |= SEEN;
702 commit_list_insert(com, lst);
705 /* for_each_ref() callback does not allow user data -- Yuck. */
706 static struct commit_list **global_lst;
708 static int include_one_commit(const char *path, const unsigned char *sha1)
710 struct commit *com = get_commit_reference(path, sha1, 0);
711 handle_one_commit(com, global_lst);
715 static void handle_all(struct commit_list **lst)
718 for_each_ref(include_one_commit);
722 int main(int argc, const char **argv)
724 const char *prefix = setup_git_directory();
725 struct commit_list *list = NULL;
728 for (i = 1 ; i < argc; i++) {
730 const char *arg = argv[i];
732 struct commit *commit;
733 unsigned char sha1[20];
735 if (!strncmp(arg, "--max-count=", 12)) {
736 max_count = atoi(arg + 12);
739 if (!strncmp(arg, "--max-age=", 10)) {
740 max_age = atoi(arg + 10);
744 if (!strncmp(arg, "--min-age=", 10)) {
745 min_age = atoi(arg + 10);
749 if (!strcmp(arg, "--header")) {
753 if (!strncmp(arg, "--pretty", 8)) {
754 commit_format = get_commit_format(arg+8);
756 hdr_termination = '\n';
757 if (commit_format == CMIT_FMT_ONELINE)
760 commit_prefix = "commit ";
763 if (!strncmp(arg, "--no-merges", 11)) {
767 if (!strcmp(arg, "--parents")) {
771 if (!strcmp(arg, "--bisect")) {
775 if (!strcmp(arg, "--all")) {
779 if (!strcmp(arg, "--objects")) {
785 if (!strcmp(arg, "--unpacked")) {
790 if (!strcmp(arg, "--merge-order")) {
794 if (!strcmp(arg, "--show-breaks")) {
798 if (!strcmp(arg, "--topo-order")) {
803 if (!strcmp(arg, "--dense")) {
807 if (!strcmp(arg, "--sparse")) {
811 if (!strcmp(arg, "--")) {
816 if (show_breaks && !merge_order)
817 usage(rev_list_usage);
820 dotdot = strstr(arg, "..");
822 unsigned char from_sha1[20];
823 char *next = dotdot + 2;
827 if (!get_sha1(arg, from_sha1) && !get_sha1(next, sha1)) {
828 struct commit *exclude;
829 struct commit *include;
831 exclude = get_commit_reference(arg, from_sha1, UNINTERESTING);
832 include = get_commit_reference(next, sha1, 0);
833 if (!exclude || !include)
834 die("Invalid revision range %s..%s", arg, next);
836 handle_one_commit(exclude, &list);
837 handle_one_commit(include, &list);
843 flags = UNINTERESTING;
847 if (get_sha1(arg, sha1) < 0)
849 commit = get_commit_reference(arg, sha1, flags);
850 handle_one_commit(commit, &list);
854 (!(tag_objects||tree_objects||blob_objects) && !pending_objects))
855 usage(rev_list_usage);
857 paths = get_pathspec(prefix, argv + i);
860 diff_tree_setup_paths(paths);
863 save_commit_buffer = verbose_header;
864 track_object_refs = 0;
868 if (list && !limited && max_count == 1 &&
869 !tag_objects && !tree_objects && !blob_objects) {
870 show_commit(list->item);
874 list = limit_list(list);
876 sort_in_topological_order(&list);
877 show_commit_list(list);
880 if (sort_list_in_merge_order(list, &process_commit)) {
881 die("merge order sort failed\n");
884 die("merge order sort unsupported, OpenSSL not linked");