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 (max_count != -1 && !max_count--)
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) {
156 static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
158 struct object_list *entry = xmalloc(sizeof(*entry));
166 static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
168 struct object *obj = &blob->object;
172 if (obj->flags & (UNINTERESTING | SEEN))
175 return add_object(obj, p, name);
178 static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
180 struct object *obj = &tree->object;
181 struct tree_entry_list *entry;
185 if (obj->flags & (UNINTERESTING | SEEN))
187 if (parse_tree(tree) < 0)
188 die("bad tree object %s", sha1_to_hex(obj->sha1));
190 p = add_object(obj, p, name);
191 entry = tree->entries;
192 tree->entries = NULL;
194 struct tree_entry_list *next = entry->next;
195 if (entry->directory)
196 p = process_tree(entry->item.tree, p, entry->name);
198 p = process_blob(entry->item.blob, p, entry->name);
205 static struct object_list *pending_objects = NULL;
207 static void show_commit_list(struct commit_list *list)
209 struct object_list *objects = NULL, **p = &objects, *pending;
211 struct commit *commit = pop_most_recent_commit(&list, SEEN);
213 p = process_tree(commit->tree, p, "");
214 if (process_commit(commit) == STOP)
217 for (pending = pending_objects; pending; pending = pending->next) {
218 struct object *obj = pending->item;
219 const char *name = pending->name;
220 if (obj->flags & (UNINTERESTING | SEEN))
222 if (obj->type == tag_type) {
224 p = add_object(obj, p, name);
227 if (obj->type == tree_type) {
228 p = process_tree((struct tree *)obj, p, name);
231 if (obj->type == blob_type) {
232 p = process_blob((struct blob *)obj, p, name);
235 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
238 /* An object with name "foo\n0000000000000000000000000000000000000000"
239 * can be used confuse downstream git-pack-objects very badly.
241 const char *ep = strchr(objects->name, '\n');
243 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
244 (int) (ep - objects->name),
248 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
249 objects = objects->next;
253 static void mark_blob_uninteresting(struct blob *blob)
257 if (blob->object.flags & UNINTERESTING)
259 blob->object.flags |= UNINTERESTING;
262 static void mark_tree_uninteresting(struct tree *tree)
264 struct object *obj = &tree->object;
265 struct tree_entry_list *entry;
269 if (obj->flags & UNINTERESTING)
271 obj->flags |= UNINTERESTING;
272 if (!has_sha1_file(obj->sha1))
274 if (parse_tree(tree) < 0)
275 die("bad tree %s", sha1_to_hex(obj->sha1));
276 entry = tree->entries;
277 tree->entries = NULL;
279 struct tree_entry_list *next = entry->next;
280 if (entry->directory)
281 mark_tree_uninteresting(entry->item.tree);
283 mark_blob_uninteresting(entry->item.blob);
289 static void mark_parents_uninteresting(struct commit *commit)
291 struct commit_list *parents = commit->parents;
294 struct commit *commit = parents->item;
295 commit->object.flags |= UNINTERESTING;
298 * Normally we haven't parsed the parent
299 * yet, so we won't have a parent of a parent
300 * here. However, it may turn out that we've
301 * reached this commit some other way (where it
302 * wasn't uninteresting), in which case we need
303 * to mark its parents recursively too..
306 mark_parents_uninteresting(commit);
309 * A missing commit is ok iff its parent is marked
312 * We just mark such a thing parsed, so that when
313 * it is popped next time around, we won't be trying
314 * to parse it and get an error.
316 if (!has_sha1_file(commit->object.sha1))
317 commit->object.parsed = 1;
318 parents = parents->next;
322 static int everybody_uninteresting(struct commit_list *orig)
324 struct commit_list *list = orig;
326 struct commit *commit = list->item;
328 if (commit->object.flags & UNINTERESTING)
336 * This is a truly stupid algorithm, but it's only
337 * used for bisection, and we just don't care enough.
339 * We care just barely enough to avoid recursing for
342 static int count_distance(struct commit_list *entry)
347 struct commit *commit = entry->item;
348 struct commit_list *p;
350 if (commit->object.flags & (UNINTERESTING | COUNTED))
353 commit->object.flags |= COUNTED;
359 nr += count_distance(p);
367 static void clear_distance(struct commit_list *list)
370 struct commit *commit = list->item;
371 commit->object.flags &= ~COUNTED;
376 static struct commit_list *find_bisection(struct commit_list *list)
379 struct commit_list *p, *best;
392 int distance = count_distance(p);
393 clear_distance(list);
394 if (nr - distance < distance)
395 distance = nr - distance;
396 if (distance > closest) {
407 static void mark_edges_uninteresting(struct commit_list *list)
409 for ( ; list; list = list->next) {
410 struct commit_list *parents = list->item->parents;
412 for ( ; parents; parents = parents->next) {
413 struct commit *commit = parents->item;
414 if (commit->object.flags & UNINTERESTING)
415 mark_tree_uninteresting(commit->tree);
420 static int is_different = 0;
422 static void file_add_remove(struct diff_options *options,
423 int addremove, unsigned mode,
424 const unsigned char *sha1,
425 const char *base, const char *path)
430 static void file_change(struct diff_options *options,
431 unsigned old_mode, unsigned new_mode,
432 const unsigned char *old_sha1,
433 const unsigned char *new_sha1,
434 const char *base, const char *path)
439 static struct diff_options diff_opt = {
441 .add_remove = file_add_remove,
442 .change = file_change,
445 static int same_tree(struct tree *t1, struct tree *t2)
448 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "", &diff_opt) < 0)
450 return !is_different;
453 static int same_tree_as_empty(struct tree *t1)
457 struct tree_desc empty, real;
462 tree = read_object_with_reference(t1->object.sha1, "tree", &real.size, NULL);
471 retval = diff_tree(&empty, &real, "", &diff_opt);
474 return retval >= 0 && !is_different;
477 static struct commit *try_to_simplify_merge(struct commit *commit, struct commit_list *parent)
483 struct commit *p = parent->item;
484 parent = parent->next;
488 if (same_tree(commit->tree, p->tree))
494 static void add_parents_to_list(struct commit *commit, struct commit_list **list)
496 struct commit_list *parent = commit->parents;
499 * If the commit is uninteresting, don't try to
500 * prune parents - we want the maximal uninteresting
503 * Normally we haven't parsed the parent
504 * yet, so we won't have a parent of a parent
505 * here. However, it may turn out that we've
506 * reached this commit some other way (where it
507 * wasn't uninteresting), in which case we need
508 * to mark its parents recursively too..
510 if (commit->object.flags & UNINTERESTING) {
512 struct commit *p = parent->item;
513 parent = parent->next;
515 p->object.flags |= UNINTERESTING;
517 mark_parents_uninteresting(p);
518 if (p->object.flags & SEEN)
520 p->object.flags |= SEEN;
521 insert_by_date(p, list);
527 * Ok, the commit wasn't uninteresting. If it
528 * is a merge, try to find the parent that has
529 * no differences in the path set if one exists.
531 if (paths && parent && parent->next) {
532 struct commit *preferred;
534 preferred = try_to_simplify_merge(commit, parent);
536 parent->item = preferred;
542 struct commit *p = parent->item;
544 parent = parent->next;
547 if (p->object.flags & SEEN)
549 p->object.flags |= SEEN;
550 insert_by_date(p, list);
554 static void compress_list(struct commit_list *list)
557 struct commit *commit = list->item;
558 struct commit_list *parent = commit->parents;
562 if (!same_tree_as_empty(commit->tree))
563 commit->object.flags |= TREECHANGE;
568 * Exactly one parent? Check if it leaves the tree
572 struct tree *t1 = commit->tree;
573 struct tree *t2 = parent->item->tree;
574 if (!t1 || !t2 || same_tree(t1, t2))
577 commit->object.flags |= TREECHANGE;
581 static struct commit_list *limit_list(struct commit_list *list)
583 struct commit_list *newlist = NULL;
584 struct commit_list **p = &newlist;
586 struct commit_list *entry = list;
587 struct commit *commit = list->item;
588 struct object *obj = &commit->object;
593 if (max_age != -1 && (commit->date < max_age))
594 obj->flags |= UNINTERESTING;
595 if (unpacked && has_sha1_pack(obj->sha1))
596 obj->flags |= UNINTERESTING;
597 add_parents_to_list(commit, &list);
598 if (obj->flags & UNINTERESTING) {
599 mark_parents_uninteresting(commit);
600 if (everybody_uninteresting(list))
604 if (min_age != -1 && (commit->date > min_age))
606 p = &commit_list_insert(commit, p)->next;
609 mark_edges_uninteresting(newlist);
611 compress_list(newlist);
613 newlist = find_bisection(newlist);
617 static void add_pending_object(struct object *obj, const char *name)
619 add_object(obj, &pending_objects, name);
622 static struct commit *get_commit_reference(const char *name, const unsigned char *sha1, unsigned int flags)
624 struct object *object;
626 object = parse_object(sha1);
628 die("bad object %s", name);
631 * Tag object? Look what it points to..
633 while (object->type == tag_type) {
634 struct tag *tag = (struct tag *) object;
635 object->flags |= flags;
636 if (tag_objects && !(object->flags & UNINTERESTING))
637 add_pending_object(object, tag->tag);
638 object = parse_object(tag->tagged->sha1);
640 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
644 * Commit object? Just return it, we'll do all the complex
647 if (object->type == commit_type) {
648 struct commit *commit = (struct commit *)object;
649 object->flags |= flags;
650 if (parse_commit(commit) < 0)
651 die("unable to parse commit %s", name);
652 if (flags & UNINTERESTING)
653 mark_parents_uninteresting(commit);
658 * Tree object? Either mark it uniniteresting, or add it
659 * to the list of objects to look at later..
661 if (object->type == tree_type) {
662 struct tree *tree = (struct tree *)object;
665 if (flags & UNINTERESTING) {
666 mark_tree_uninteresting(tree);
669 add_pending_object(object, "");
674 * Blob object? You know the drill by now..
676 if (object->type == blob_type) {
677 struct blob *blob = (struct blob *)object;
680 if (flags & UNINTERESTING) {
681 mark_blob_uninteresting(blob);
684 add_pending_object(object, "");
687 die("%s is unknown object", name);
690 static void handle_one_commit(struct commit *com, struct commit_list **lst)
692 if (!com || com->object.flags & SEEN)
694 com->object.flags |= SEEN;
695 commit_list_insert(com, lst);
698 /* for_each_ref() callback does not allow user data -- Yuck. */
699 static struct commit_list **global_lst;
701 static int include_one_commit(const char *path, const unsigned char *sha1)
703 struct commit *com = get_commit_reference(path, sha1, 0);
704 handle_one_commit(com, global_lst);
708 static void handle_all(struct commit_list **lst)
711 for_each_ref(include_one_commit);
715 int main(int argc, const char **argv)
717 const char *prefix = setup_git_directory();
718 struct commit_list *list = NULL;
721 for (i = 1 ; i < argc; i++) {
723 const char *arg = argv[i];
725 struct commit *commit;
726 unsigned char sha1[20];
728 if (!strncmp(arg, "--max-count=", 12)) {
729 max_count = atoi(arg + 12);
732 if (!strncmp(arg, "--max-age=", 10)) {
733 max_age = atoi(arg + 10);
737 if (!strncmp(arg, "--min-age=", 10)) {
738 min_age = atoi(arg + 10);
742 if (!strcmp(arg, "--header")) {
746 if (!strncmp(arg, "--pretty", 8)) {
747 commit_format = get_commit_format(arg+8);
749 hdr_termination = '\n';
750 if (commit_format == CMIT_FMT_ONELINE)
753 commit_prefix = "commit ";
756 if (!strncmp(arg, "--no-merges", 11)) {
760 if (!strcmp(arg, "--parents")) {
764 if (!strcmp(arg, "--bisect")) {
768 if (!strcmp(arg, "--all")) {
772 if (!strcmp(arg, "--objects")) {
778 if (!strcmp(arg, "--unpacked")) {
783 if (!strcmp(arg, "--merge-order")) {
787 if (!strcmp(arg, "--show-breaks")) {
791 if (!strcmp(arg, "--topo-order")) {
796 if (!strcmp(arg, "--dense")) {
800 if (!strcmp(arg, "--sparse")) {
804 if (!strcmp(arg, "--")) {
809 if (show_breaks && !merge_order)
810 usage(rev_list_usage);
813 dotdot = strstr(arg, "..");
815 unsigned char from_sha1[20];
816 char *next = dotdot + 2;
820 if (!get_sha1(arg, from_sha1) && !get_sha1(next, sha1)) {
821 struct commit *exclude;
822 struct commit *include;
824 exclude = get_commit_reference(arg, from_sha1, UNINTERESTING);
825 include = get_commit_reference(next, sha1, 0);
826 if (!exclude || !include)
827 die("Invalid revision range %s..%s", arg, next);
829 handle_one_commit(exclude, &list);
830 handle_one_commit(include, &list);
836 flags = UNINTERESTING;
840 if (get_sha1(arg, sha1) < 0)
842 commit = get_commit_reference(arg, sha1, flags);
843 handle_one_commit(commit, &list);
847 usage(rev_list_usage);
849 paths = get_pathspec(prefix, argv + i);
852 diff_tree_setup_paths(paths);
855 save_commit_buffer = verbose_header;
856 track_object_refs = 0;
860 if (list && !limited && max_count == 1 &&
861 !tag_objects && !tree_objects && !blob_objects) {
862 show_commit(list->item);
866 list = limit_list(list);
868 sort_in_topological_order(&list);
869 show_commit_list(list);
872 if (sort_list_in_merge_order(list, &process_commit)) {
873 die("merge order sort failed\n");
876 die("merge order sort unsupported, OpenSSL not linked");