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 <commit-id>\n"
28 " --merge-order [ --show-breaks ]\n"
32 static int unpacked = 0;
33 static int bisect_list = 0;
34 static int tag_objects = 0;
35 static int tree_objects = 0;
36 static int blob_objects = 0;
37 static int verbose_header = 0;
38 static int show_parents = 0;
39 static int hdr_termination = 0;
40 static const char *commit_prefix = "";
41 static unsigned long max_age = -1;
42 static unsigned long min_age = -1;
43 static int max_count = -1;
44 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
45 static int merge_order = 0;
46 static int show_breaks = 0;
47 static int stop_traversal = 0;
48 static int topo_order = 0;
49 static int no_merges = 0;
50 static const char **paths = NULL;
52 static void show_commit(struct commit *commit)
54 commit->object.flags |= SHOWN;
57 if (commit->object.flags & DISCONTINUITY) {
59 } else if (commit->object.flags & BOUNDARY) {
63 printf("%s%s", commit_prefix, sha1_to_hex(commit->object.sha1));
65 struct commit_list *parents = commit->parents;
67 printf(" %s", sha1_to_hex(parents->item->object.sha1));
68 parents = parents->next;
71 if (commit_format == CMIT_FMT_ONELINE)
77 static char pretty_header[16384];
78 pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header));
79 printf("%s%c", pretty_header, hdr_termination);
84 static void rewrite_one(struct commit **pp)
87 struct commit *p = *pp;
88 if (p->object.flags & (TREECHANGE | UNINTERESTING))
90 /* Only single-parent commits don't have TREECHANGE */
91 *pp = p->parents->item;
95 static void rewrite_parents(struct commit *commit)
97 struct commit_list *parent = commit->parents;
99 rewrite_one(&parent->item);
100 parent = parent->next;
104 static int filter_commit(struct commit * commit)
106 if (stop_traversal && (commit->object.flags & BOUNDARY))
108 if (commit->object.flags & (UNINTERESTING|SHOWN))
110 if (min_age != -1 && (commit->date > min_age))
112 if (max_age != -1 && (commit->date < max_age)) {
116 if (max_count != -1 && !max_count--)
118 if (no_merges && (commit->parents && commit->parents->next))
120 if (paths && dense) {
121 if (!(commit->object.flags & TREECHANGE))
123 rewrite_parents(commit);
128 static int process_commit(struct commit * commit)
130 int action=filter_commit(commit);
132 if (action == STOP) {
136 if (action == CONTINUE) {
145 static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
147 struct object_list *entry = xmalloc(sizeof(*entry));
155 static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
157 struct object *obj = &blob->object;
161 if (obj->flags & (UNINTERESTING | SEEN))
164 return add_object(obj, p, name);
167 static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
169 struct object *obj = &tree->object;
170 struct tree_entry_list *entry;
174 if (obj->flags & (UNINTERESTING | SEEN))
176 if (parse_tree(tree) < 0)
177 die("bad tree object %s", sha1_to_hex(obj->sha1));
179 p = add_object(obj, p, name);
180 entry = tree->entries;
181 tree->entries = NULL;
183 struct tree_entry_list *next = entry->next;
184 if (entry->directory)
185 p = process_tree(entry->item.tree, p, entry->name);
187 p = process_blob(entry->item.blob, p, entry->name);
194 static struct object_list *pending_objects = NULL;
196 static void show_commit_list(struct commit_list *list)
198 struct object_list *objects = NULL, **p = &objects, *pending;
200 struct commit *commit = pop_most_recent_commit(&list, SEEN);
202 p = process_tree(commit->tree, p, "");
203 if (process_commit(commit) == STOP)
206 for (pending = pending_objects; pending; pending = pending->next) {
207 struct object *obj = pending->item;
208 const char *name = pending->name;
209 if (obj->flags & (UNINTERESTING | SEEN))
211 if (obj->type == tag_type) {
213 p = add_object(obj, p, name);
216 if (obj->type == tree_type) {
217 p = process_tree((struct tree *)obj, p, name);
220 if (obj->type == blob_type) {
221 p = process_blob((struct blob *)obj, p, name);
224 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
227 /* An object with name "foo\n0000000000000000000000000000000000000000"
228 * can be used confuse downstream git-pack-objects very badly.
230 const char *ep = strchr(objects->name, '\n');
232 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
233 (int) (ep - objects->name),
237 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
238 objects = objects->next;
242 static void mark_blob_uninteresting(struct blob *blob)
246 if (blob->object.flags & UNINTERESTING)
248 blob->object.flags |= UNINTERESTING;
251 static void mark_tree_uninteresting(struct tree *tree)
253 struct object *obj = &tree->object;
254 struct tree_entry_list *entry;
258 if (obj->flags & UNINTERESTING)
260 obj->flags |= UNINTERESTING;
261 if (!has_sha1_file(obj->sha1))
263 if (parse_tree(tree) < 0)
264 die("bad tree %s", sha1_to_hex(obj->sha1));
265 entry = tree->entries;
266 tree->entries = NULL;
268 struct tree_entry_list *next = entry->next;
269 if (entry->directory)
270 mark_tree_uninteresting(entry->item.tree);
272 mark_blob_uninteresting(entry->item.blob);
278 static void mark_parents_uninteresting(struct commit *commit)
280 struct commit_list *parents = commit->parents;
283 struct commit *commit = parents->item;
284 commit->object.flags |= UNINTERESTING;
287 * Normally we haven't parsed the parent
288 * yet, so we won't have a parent of a parent
289 * here. However, it may turn out that we've
290 * reached this commit some other way (where it
291 * wasn't uninteresting), in which case we need
292 * to mark its parents recursively too..
295 mark_parents_uninteresting(commit);
298 * A missing commit is ok iff its parent is marked
301 * We just mark such a thing parsed, so that when
302 * it is popped next time around, we won't be trying
303 * to parse it and get an error.
305 if (!has_sha1_file(commit->object.sha1))
306 commit->object.parsed = 1;
307 parents = parents->next;
311 static int everybody_uninteresting(struct commit_list *orig)
313 struct commit_list *list = orig;
315 struct commit *commit = list->item;
317 if (commit->object.flags & UNINTERESTING)
325 * This is a truly stupid algorithm, but it's only
326 * used for bisection, and we just don't care enough.
328 * We care just barely enough to avoid recursing for
331 static int count_distance(struct commit_list *entry)
336 struct commit *commit = entry->item;
337 struct commit_list *p;
339 if (commit->object.flags & (UNINTERESTING | COUNTED))
342 commit->object.flags |= COUNTED;
348 nr += count_distance(p);
356 static void clear_distance(struct commit_list *list)
359 struct commit *commit = list->item;
360 commit->object.flags &= ~COUNTED;
365 static struct commit_list *find_bisection(struct commit_list *list)
368 struct commit_list *p, *best;
381 int distance = count_distance(p);
382 clear_distance(list);
383 if (nr - distance < distance)
384 distance = nr - distance;
385 if (distance > closest) {
396 static void mark_edges_uninteresting(struct commit_list *list)
398 for ( ; list; list = list->next) {
399 struct commit_list *parents = list->item->parents;
401 for ( ; parents; parents = parents->next) {
402 struct commit *commit = parents->item;
403 if (commit->object.flags & UNINTERESTING)
404 mark_tree_uninteresting(commit->tree);
409 static int is_different = 0;
411 static void file_add_remove(struct diff_options *options,
412 int addremove, unsigned mode,
413 const unsigned char *sha1,
414 const char *base, const char *path)
419 static void file_change(struct diff_options *options,
420 unsigned old_mode, unsigned new_mode,
421 const unsigned char *old_sha1,
422 const unsigned char *new_sha1,
423 const char *base, const char *path)
428 static struct diff_options diff_opt = {
430 .add_remove = file_add_remove,
431 .change = file_change,
434 static int same_tree(struct tree *t1, struct tree *t2)
437 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "", &diff_opt) < 0)
439 return !is_different;
442 static struct commit *try_to_simplify_merge(struct commit *commit, struct commit_list *parent)
448 struct commit *p = parent->item;
449 parent = parent->next;
453 if (same_tree(commit->tree, p->tree))
459 static void add_parents_to_list(struct commit *commit, struct commit_list **list)
461 struct commit_list *parent = commit->parents;
464 * If the commit is uninteresting, don't try to
465 * prune parents - we want the maximal uninteresting
468 * Normally we haven't parsed the parent
469 * yet, so we won't have a parent of a parent
470 * here. However, it may turn out that we've
471 * reached this commit some other way (where it
472 * wasn't uninteresting), in which case we need
473 * to mark its parents recursively too..
475 if (commit->object.flags & UNINTERESTING) {
477 struct commit *p = parent->item;
478 parent = parent->next;
480 p->object.flags |= UNINTERESTING;
482 mark_parents_uninteresting(p);
483 if (p->object.flags & SEEN)
485 p->object.flags |= SEEN;
486 insert_by_date(p, list);
492 * Ok, the commit wasn't uninteresting. If it
493 * is a merge, try to find the parent that has
494 * no differences in the path set if one exists.
496 if (paths && parent && parent->next) {
497 struct commit *preferred;
499 preferred = try_to_simplify_merge(commit, parent);
501 parent->item = preferred;
507 struct commit *p = parent->item;
509 parent = parent->next;
512 if (p->object.flags & SEEN)
514 p->object.flags |= SEEN;
515 insert_by_date(p, list);
519 static void compress_list(struct commit_list *list)
522 struct commit *commit = list->item;
523 struct commit_list *parent = commit->parents;
527 * Exactly one parent? Check if it leaves the tree
530 if (parent && !parent->next) {
531 struct tree *t1 = commit->tree;
532 struct tree *t2 = parent->item->tree;
533 if (!t1 || !t2 || same_tree(t1, t2))
536 commit->object.flags |= TREECHANGE;
540 static struct commit_list *limit_list(struct commit_list *list)
542 struct commit_list *newlist = NULL;
543 struct commit_list **p = &newlist;
545 struct commit_list *entry = list;
546 struct commit *commit = list->item;
547 struct object *obj = &commit->object;
552 if (max_age != -1 && (commit->date < max_age))
553 obj->flags |= UNINTERESTING;
554 if (unpacked && has_sha1_pack(obj->sha1))
555 obj->flags |= UNINTERESTING;
556 add_parents_to_list(commit, &list);
557 if (obj->flags & UNINTERESTING) {
558 mark_parents_uninteresting(commit);
559 if (everybody_uninteresting(list))
563 if (min_age != -1 && (commit->date > min_age))
565 p = &commit_list_insert(commit, p)->next;
568 mark_edges_uninteresting(newlist);
570 compress_list(newlist);
572 newlist = find_bisection(newlist);
576 static void add_pending_object(struct object *obj, const char *name)
578 add_object(obj, &pending_objects, name);
581 static struct commit *get_commit_reference(const char *name, unsigned int flags)
583 unsigned char sha1[20];
584 struct object *object;
586 if (get_sha1(name, sha1))
587 usage(rev_list_usage);
588 object = parse_object(sha1);
590 die("bad object %s", name);
593 * Tag object? Look what it points to..
595 while (object->type == tag_type) {
596 struct tag *tag = (struct tag *) object;
597 object->flags |= flags;
598 if (tag_objects && !(object->flags & UNINTERESTING))
599 add_pending_object(object, tag->tag);
600 object = parse_object(tag->tagged->sha1);
602 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
606 * Commit object? Just return it, we'll do all the complex
609 if (object->type == commit_type) {
610 struct commit *commit = (struct commit *)object;
611 object->flags |= flags;
612 if (parse_commit(commit) < 0)
613 die("unable to parse commit %s", name);
614 if (flags & UNINTERESTING)
615 mark_parents_uninteresting(commit);
620 * Tree object? Either mark it uniniteresting, or add it
621 * to the list of objects to look at later..
623 if (object->type == tree_type) {
624 struct tree *tree = (struct tree *)object;
627 if (flags & UNINTERESTING) {
628 mark_tree_uninteresting(tree);
631 add_pending_object(object, "");
636 * Blob object? You know the drill by now..
638 if (object->type == blob_type) {
639 struct blob *blob = (struct blob *)object;
642 if (flags & UNINTERESTING) {
643 mark_blob_uninteresting(blob);
646 add_pending_object(object, "");
649 die("%s is unknown object", name);
652 static void handle_one_commit(struct commit *com, struct commit_list **lst)
654 if (!com || com->object.flags & SEEN)
656 com->object.flags |= SEEN;
657 commit_list_insert(com, lst);
660 /* for_each_ref() callback does not allow user data -- Yuck. */
661 static struct commit_list **global_lst;
663 static int include_one_commit(const char *path, const unsigned char *sha1)
665 struct commit *com = get_commit_reference(path, 0);
666 handle_one_commit(com, global_lst);
670 static void handle_all(struct commit_list **lst)
673 for_each_ref(include_one_commit);
677 int main(int argc, const char **argv)
679 const char *prefix = setup_git_directory();
680 struct commit_list *list = NULL;
683 for (i = 1 ; i < argc; i++) {
685 const char *arg = argv[i];
687 struct commit *commit;
689 if (!strncmp(arg, "--max-count=", 12)) {
690 max_count = atoi(arg + 12);
693 if (!strncmp(arg, "--max-age=", 10)) {
694 max_age = atoi(arg + 10);
698 if (!strncmp(arg, "--min-age=", 10)) {
699 min_age = atoi(arg + 10);
703 if (!strcmp(arg, "--header")) {
707 if (!strncmp(arg, "--pretty", 8)) {
708 commit_format = get_commit_format(arg+8);
710 hdr_termination = '\n';
711 if (commit_format == CMIT_FMT_ONELINE)
714 commit_prefix = "commit ";
717 if (!strncmp(arg, "--no-merges", 11)) {
721 if (!strcmp(arg, "--parents")) {
725 if (!strcmp(arg, "--bisect")) {
729 if (!strcmp(arg, "--all")) {
733 if (!strcmp(arg, "--objects")) {
739 if (!strcmp(arg, "--unpacked")) {
744 if (!strcmp(arg, "--merge-order")) {
748 if (!strcmp(arg, "--show-breaks")) {
752 if (!strcmp(arg, "--topo-order")) {
757 if (!strcmp(arg, "--dense")) {
761 if (!strcmp(arg, "--")) {
762 paths = get_pathspec(prefix, argv + i + 1);
765 diff_tree_setup_paths(paths);
770 if (show_breaks && !merge_order)
771 usage(rev_list_usage);
774 dotdot = strstr(arg, "..");
776 char *next = dotdot + 2;
777 struct commit *exclude = NULL;
778 struct commit *include = NULL;
782 exclude = get_commit_reference(arg, UNINTERESTING);
783 include = get_commit_reference(next, 0);
784 if (exclude && include) {
786 handle_one_commit(exclude, &list);
787 handle_one_commit(include, &list);
793 flags = UNINTERESTING;
797 commit = get_commit_reference(arg, flags);
798 handle_one_commit(commit, &list);
801 save_commit_buffer = verbose_header;
802 track_object_refs = 0;
806 if (list && !limited && max_count == 1 &&
807 !tag_objects && !tree_objects && !blob_objects) {
808 show_commit(list->item);
812 list = limit_list(list);
814 sort_in_topological_order(&list);
815 show_commit_list(list);
818 if (sort_list_in_merge_order(list, &process_commit)) {
819 die("merge order sort failed\n");
822 die("merge order sort unsupported, OpenSSL not linked");