rev-list: stop when the file disappears
[git/repo.git] / rev-list.c
blob7d3ddc6ad5b2d6e420993b113f29b991f1f8e138
1 #include "cache.h"
2 #include "refs.h"
3 #include "tag.h"
4 #include "commit.h"
5 #include "tree.h"
6 #include "blob.h"
7 #include "epoch.h"
8 #include "diff.h"
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"
18 " limiting output:\n"
19 " --max-count=nr\n"
20 " --max-age=epoch\n"
21 " --min-age=epoch\n"
22 " --sparse\n"
23 " --no-merges\n"
24 " --all\n"
25 " ordering output:\n"
26 " --merge-order [ --show-breaks ]\n"
27 " --topo-order\n"
28 " formatting output:\n"
29 " --parents\n"
30 " --objects\n"
31 " --unpacked\n"
32 " --header | --pretty\n"
33 " special purpose:\n"
34 " --bisect"
37 static int dense = 1;
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;
57 static int remove_empty_trees = 0;
59 static void show_commit(struct commit *commit)
61 commit->object.flags |= SHOWN;
62 if (show_breaks) {
63 commit_prefix = "| ";
64 if (commit->object.flags & DISCONTINUITY) {
65 commit_prefix = "^ ";
66 } else if (commit->object.flags & BOUNDARY) {
67 commit_prefix = "= ";
70 printf("%s%s", commit_prefix, sha1_to_hex(commit->object.sha1));
71 if (show_parents) {
72 struct commit_list *parents = commit->parents;
73 while (parents) {
74 printf(" %s", sha1_to_hex(parents->item->object.sha1));
75 parents = parents->next;
78 if (commit_format == CMIT_FMT_ONELINE)
79 putchar(' ');
80 else
81 putchar('\n');
83 if (verbose_header) {
84 static char pretty_header[16384];
85 pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header));
86 printf("%s%c", pretty_header, hdr_termination);
88 fflush(stdout);
91 static int rewrite_one(struct commit **pp)
93 for (;;) {
94 struct commit *p = *pp;
95 if (p->object.flags & (TREECHANGE | UNINTERESTING))
96 return 0;
97 if (!p->parents)
98 return -1;
99 *pp = p->parents->item;
103 static void rewrite_parents(struct commit *commit)
105 struct commit_list **pp = &commit->parents;
106 while (*pp) {
107 struct commit_list *parent = *pp;
108 if (rewrite_one(&parent->item) < 0) {
109 *pp = parent->next;
110 continue;
112 pp = &parent->next;
116 static int filter_commit(struct commit * commit)
118 if (stop_traversal && (commit->object.flags & BOUNDARY))
119 return STOP;
120 if (commit->object.flags & (UNINTERESTING|SHOWN))
121 return CONTINUE;
122 if (min_age != -1 && (commit->date > min_age))
123 return CONTINUE;
124 if (max_age != -1 && (commit->date < max_age)) {
125 stop_traversal=1;
126 return CONTINUE;
128 if (no_merges && (commit->parents && commit->parents->next))
129 return CONTINUE;
130 if (paths && dense) {
131 if (!(commit->object.flags & TREECHANGE))
132 return CONTINUE;
133 rewrite_parents(commit);
135 return DO;
138 static int process_commit(struct commit * commit)
140 int action=filter_commit(commit);
142 if (action == STOP) {
143 return STOP;
146 if (action == CONTINUE) {
147 return CONTINUE;
150 if (max_count != -1 && !max_count--)
151 return STOP;
153 show_commit(commit);
155 return CONTINUE;
158 static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
160 struct object_list *entry = xmalloc(sizeof(*entry));
161 entry->item = obj;
162 entry->next = *p;
163 entry->name = name;
164 *p = entry;
165 return &entry->next;
168 static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
170 struct object *obj = &blob->object;
172 if (!blob_objects)
173 return p;
174 if (obj->flags & (UNINTERESTING | SEEN))
175 return p;
176 obj->flags |= SEEN;
177 return add_object(obj, p, name);
180 static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
182 struct object *obj = &tree->object;
183 struct tree_entry_list *entry;
185 if (!tree_objects)
186 return p;
187 if (obj->flags & (UNINTERESTING | SEEN))
188 return p;
189 if (parse_tree(tree) < 0)
190 die("bad tree object %s", sha1_to_hex(obj->sha1));
191 obj->flags |= SEEN;
192 p = add_object(obj, p, name);
193 entry = tree->entries;
194 tree->entries = NULL;
195 while (entry) {
196 struct tree_entry_list *next = entry->next;
197 if (entry->directory)
198 p = process_tree(entry->item.tree, p, entry->name);
199 else
200 p = process_blob(entry->item.blob, p, entry->name);
201 free(entry);
202 entry = next;
204 return p;
207 static struct object_list *pending_objects = NULL;
209 static void show_commit_list(struct commit_list *list)
211 struct object_list *objects = NULL, **p = &objects, *pending;
212 while (list) {
213 struct commit *commit = pop_most_recent_commit(&list, SEEN);
215 p = process_tree(commit->tree, p, "");
216 if (process_commit(commit) == STOP)
217 break;
219 for (pending = pending_objects; pending; pending = pending->next) {
220 struct object *obj = pending->item;
221 const char *name = pending->name;
222 if (obj->flags & (UNINTERESTING | SEEN))
223 continue;
224 if (obj->type == tag_type) {
225 obj->flags |= SEEN;
226 p = add_object(obj, p, name);
227 continue;
229 if (obj->type == tree_type) {
230 p = process_tree((struct tree *)obj, p, name);
231 continue;
233 if (obj->type == blob_type) {
234 p = process_blob((struct blob *)obj, p, name);
235 continue;
237 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
239 while (objects) {
240 /* An object with name "foo\n0000000000000000000000000000000000000000"
241 * can be used confuse downstream git-pack-objects very badly.
243 const char *ep = strchr(objects->name, '\n');
244 if (ep) {
245 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
246 (int) (ep - objects->name),
247 objects->name);
249 else
250 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
251 objects = objects->next;
255 static void mark_blob_uninteresting(struct blob *blob)
257 if (!blob_objects)
258 return;
259 if (blob->object.flags & UNINTERESTING)
260 return;
261 blob->object.flags |= UNINTERESTING;
264 static void mark_tree_uninteresting(struct tree *tree)
266 struct object *obj = &tree->object;
267 struct tree_entry_list *entry;
269 if (!tree_objects)
270 return;
271 if (obj->flags & UNINTERESTING)
272 return;
273 obj->flags |= UNINTERESTING;
274 if (!has_sha1_file(obj->sha1))
275 return;
276 if (parse_tree(tree) < 0)
277 die("bad tree %s", sha1_to_hex(obj->sha1));
278 entry = tree->entries;
279 tree->entries = NULL;
280 while (entry) {
281 struct tree_entry_list *next = entry->next;
282 if (entry->directory)
283 mark_tree_uninteresting(entry->item.tree);
284 else
285 mark_blob_uninteresting(entry->item.blob);
286 free(entry);
287 entry = next;
291 static void mark_parents_uninteresting(struct commit *commit)
293 struct commit_list *parents = commit->parents;
295 while (parents) {
296 struct commit *commit = parents->item;
297 commit->object.flags |= UNINTERESTING;
300 * Normally we haven't parsed the parent
301 * yet, so we won't have a parent of a parent
302 * here. However, it may turn out that we've
303 * reached this commit some other way (where it
304 * wasn't uninteresting), in which case we need
305 * to mark its parents recursively too..
307 if (commit->parents)
308 mark_parents_uninteresting(commit);
311 * A missing commit is ok iff its parent is marked
312 * uninteresting.
314 * We just mark such a thing parsed, so that when
315 * it is popped next time around, we won't be trying
316 * to parse it and get an error.
318 if (!has_sha1_file(commit->object.sha1))
319 commit->object.parsed = 1;
320 parents = parents->next;
324 static int everybody_uninteresting(struct commit_list *orig)
326 struct commit_list *list = orig;
327 while (list) {
328 struct commit *commit = list->item;
329 list = list->next;
330 if (commit->object.flags & UNINTERESTING)
331 continue;
332 return 0;
334 return 1;
338 * This is a truly stupid algorithm, but it's only
339 * used for bisection, and we just don't care enough.
341 * We care just barely enough to avoid recursing for
342 * non-merge entries.
344 static int count_distance(struct commit_list *entry)
346 int nr = 0;
348 while (entry) {
349 struct commit *commit = entry->item;
350 struct commit_list *p;
352 if (commit->object.flags & (UNINTERESTING | COUNTED))
353 break;
354 if (!paths || (commit->object.flags & TREECHANGE))
355 nr++;
356 commit->object.flags |= COUNTED;
357 p = commit->parents;
358 entry = p;
359 if (p) {
360 p = p->next;
361 while (p) {
362 nr += count_distance(p);
363 p = p->next;
368 return nr;
371 static void clear_distance(struct commit_list *list)
373 while (list) {
374 struct commit *commit = list->item;
375 commit->object.flags &= ~COUNTED;
376 list = list->next;
380 static struct commit_list *find_bisection(struct commit_list *list)
382 int nr, closest;
383 struct commit_list *p, *best;
385 nr = 0;
386 p = list;
387 while (p) {
388 if (!paths || (p->item->object.flags & TREECHANGE))
389 nr++;
390 p = p->next;
392 closest = 0;
393 best = list;
395 for (p = list; p; p = p->next) {
396 int distance;
398 if (paths && !(p->item->object.flags & TREECHANGE))
399 continue;
401 distance = count_distance(p);
402 clear_distance(list);
403 if (nr - distance < distance)
404 distance = nr - distance;
405 if (distance > closest) {
406 best = p;
407 closest = distance;
410 if (best)
411 best->next = NULL;
412 return best;
415 static void mark_edges_uninteresting(struct commit_list *list)
417 for ( ; list; list = list->next) {
418 struct commit_list *parents = list->item->parents;
420 for ( ; parents; parents = parents->next) {
421 struct commit *commit = parents->item;
422 if (commit->object.flags & UNINTERESTING)
423 mark_tree_uninteresting(commit->tree);
428 #define TREE_SAME 0
429 #define TREE_NEW 1
430 #define TREE_DIFFERENT 2
431 static int tree_difference = TREE_SAME;
433 static void file_add_remove(struct diff_options *options,
434 int addremove, unsigned mode,
435 const unsigned char *sha1,
436 const char *base, const char *path)
438 int diff = TREE_DIFFERENT;
441 * Is it an add of a new file? It means that
442 * the old tree didn't have it at all, so we
443 * will turn "TREE_SAME" -> "TREE_NEW", but
444 * leave any "TREE_DIFFERENT" alone (and if
445 * it already was "TREE_NEW", we'll keep it
446 * "TREE_NEW" of course).
448 if (addremove == '+') {
449 diff = tree_difference;
450 if (diff != TREE_SAME)
451 return;
452 diff = TREE_NEW;
454 tree_difference = diff;
457 static void file_change(struct diff_options *options,
458 unsigned old_mode, unsigned new_mode,
459 const unsigned char *old_sha1,
460 const unsigned char *new_sha1,
461 const char *base, const char *path)
463 tree_difference = TREE_DIFFERENT;
466 static struct diff_options diff_opt = {
467 .recursive = 1,
468 .add_remove = file_add_remove,
469 .change = file_change,
472 static int compare_tree(struct tree *t1, struct tree *t2)
474 if (!t1)
475 return TREE_NEW;
476 if (!t2)
477 return TREE_DIFFERENT;
478 tree_difference = TREE_SAME;
479 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "", &diff_opt) < 0)
480 return TREE_DIFFERENT;
481 return tree_difference;
484 static int same_tree_as_empty(struct tree *t1)
486 int retval;
487 void *tree;
488 struct tree_desc empty, real;
490 if (!t1)
491 return 0;
493 tree = read_object_with_reference(t1->object.sha1, "tree", &real.size, NULL);
494 if (!tree)
495 return 0;
496 real.buf = tree;
498 empty.buf = "";
499 empty.size = 0;
501 tree_difference = 0;
502 retval = diff_tree(&empty, &real, "", &diff_opt);
503 free(tree);
505 return retval >= 0 && !tree_difference;
508 static void try_to_simplify_commit(struct commit *commit)
510 struct commit_list **pp, *parent;
512 if (!commit->tree)
513 return;
515 if (!commit->parents) {
516 if (!same_tree_as_empty(commit->tree))
517 commit->object.flags |= TREECHANGE;
518 return;
521 pp = &commit->parents;
522 while ((parent = *pp) != NULL) {
523 struct commit *p = parent->item;
525 if (p->object.flags & UNINTERESTING) {
526 pp = &parent->next;
527 continue;
530 parse_commit(p);
531 switch (compare_tree(p->tree, commit->tree)) {
532 case TREE_SAME:
533 parent->next = NULL;
534 commit->parents = parent;
535 return;
537 case TREE_NEW:
538 if (remove_empty_trees && same_tree_as_empty(p->tree)) {
539 *pp = parent->next;
540 continue;
542 /* fallthrough */
543 case TREE_DIFFERENT:
544 pp = &parent->next;
545 continue;
547 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
549 commit->object.flags |= TREECHANGE;
552 static void add_parents_to_list(struct commit *commit, struct commit_list **list)
554 struct commit_list *parent = commit->parents;
557 * If the commit is uninteresting, don't try to
558 * prune parents - we want the maximal uninteresting
559 * set.
561 * Normally we haven't parsed the parent
562 * yet, so we won't have a parent of a parent
563 * here. However, it may turn out that we've
564 * reached this commit some other way (where it
565 * wasn't uninteresting), in which case we need
566 * to mark its parents recursively too..
568 if (commit->object.flags & UNINTERESTING) {
569 while (parent) {
570 struct commit *p = parent->item;
571 parent = parent->next;
572 parse_commit(p);
573 p->object.flags |= UNINTERESTING;
574 if (p->parents)
575 mark_parents_uninteresting(p);
576 if (p->object.flags & SEEN)
577 continue;
578 p->object.flags |= SEEN;
579 insert_by_date(p, list);
581 return;
585 * Ok, the commit wasn't uninteresting. Try to
586 * simplify the commit history and find the parent
587 * that has no differences in the path set if one exists.
589 if (paths)
590 try_to_simplify_commit(commit);
592 parent = commit->parents;
593 while (parent) {
594 struct commit *p = parent->item;
596 parent = parent->next;
598 parse_commit(p);
599 if (p->object.flags & SEEN)
600 continue;
601 p->object.flags |= SEEN;
602 insert_by_date(p, list);
606 static struct commit_list *limit_list(struct commit_list *list)
608 struct commit_list *newlist = NULL;
609 struct commit_list **p = &newlist;
610 while (list) {
611 struct commit_list *entry = list;
612 struct commit *commit = list->item;
613 struct object *obj = &commit->object;
615 list = list->next;
616 free(entry);
618 if (max_age != -1 && (commit->date < max_age))
619 obj->flags |= UNINTERESTING;
620 if (unpacked && has_sha1_pack(obj->sha1))
621 obj->flags |= UNINTERESTING;
622 add_parents_to_list(commit, &list);
623 if (obj->flags & UNINTERESTING) {
624 mark_parents_uninteresting(commit);
625 if (everybody_uninteresting(list))
626 break;
627 continue;
629 if (min_age != -1 && (commit->date > min_age))
630 continue;
631 p = &commit_list_insert(commit, p)->next;
633 if (tree_objects)
634 mark_edges_uninteresting(newlist);
635 if (bisect_list)
636 newlist = find_bisection(newlist);
637 return newlist;
640 static void add_pending_object(struct object *obj, const char *name)
642 add_object(obj, &pending_objects, name);
645 static struct commit *get_commit_reference(const char *name, const unsigned char *sha1, unsigned int flags)
647 struct object *object;
649 object = parse_object(sha1);
650 if (!object)
651 die("bad object %s", name);
654 * Tag object? Look what it points to..
656 while (object->type == tag_type) {
657 struct tag *tag = (struct tag *) object;
658 object->flags |= flags;
659 if (tag_objects && !(object->flags & UNINTERESTING))
660 add_pending_object(object, tag->tag);
661 object = parse_object(tag->tagged->sha1);
662 if (!object)
663 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
667 * Commit object? Just return it, we'll do all the complex
668 * reachability crud.
670 if (object->type == commit_type) {
671 struct commit *commit = (struct commit *)object;
672 object->flags |= flags;
673 if (parse_commit(commit) < 0)
674 die("unable to parse commit %s", name);
675 if (flags & UNINTERESTING)
676 mark_parents_uninteresting(commit);
677 return commit;
681 * Tree object? Either mark it uniniteresting, or add it
682 * to the list of objects to look at later..
684 if (object->type == tree_type) {
685 struct tree *tree = (struct tree *)object;
686 if (!tree_objects)
687 return NULL;
688 if (flags & UNINTERESTING) {
689 mark_tree_uninteresting(tree);
690 return NULL;
692 add_pending_object(object, "");
693 return NULL;
697 * Blob object? You know the drill by now..
699 if (object->type == blob_type) {
700 struct blob *blob = (struct blob *)object;
701 if (!blob_objects)
702 return NULL;
703 if (flags & UNINTERESTING) {
704 mark_blob_uninteresting(blob);
705 return NULL;
707 add_pending_object(object, "");
708 return NULL;
710 die("%s is unknown object", name);
713 static void handle_one_commit(struct commit *com, struct commit_list **lst)
715 if (!com || com->object.flags & SEEN)
716 return;
717 com->object.flags |= SEEN;
718 commit_list_insert(com, lst);
721 /* for_each_ref() callback does not allow user data -- Yuck. */
722 static struct commit_list **global_lst;
724 static int include_one_commit(const char *path, const unsigned char *sha1)
726 struct commit *com = get_commit_reference(path, sha1, 0);
727 handle_one_commit(com, global_lst);
728 return 0;
731 static void handle_all(struct commit_list **lst)
733 global_lst = lst;
734 for_each_ref(include_one_commit);
735 global_lst = NULL;
738 int main(int argc, const char **argv)
740 const char *prefix = setup_git_directory();
741 struct commit_list *list = NULL;
742 int i, limited = 0;
744 for (i = 1 ; i < argc; i++) {
745 int flags;
746 const char *arg = argv[i];
747 char *dotdot;
748 struct commit *commit;
749 unsigned char sha1[20];
751 if (!strncmp(arg, "--max-count=", 12)) {
752 max_count = atoi(arg + 12);
753 continue;
755 if (!strncmp(arg, "--max-age=", 10)) {
756 max_age = atoi(arg + 10);
757 limited = 1;
758 continue;
760 if (!strncmp(arg, "--min-age=", 10)) {
761 min_age = atoi(arg + 10);
762 limited = 1;
763 continue;
765 if (!strcmp(arg, "--header")) {
766 verbose_header = 1;
767 continue;
769 if (!strncmp(arg, "--pretty", 8)) {
770 commit_format = get_commit_format(arg+8);
771 verbose_header = 1;
772 hdr_termination = '\n';
773 if (commit_format == CMIT_FMT_ONELINE)
774 commit_prefix = "";
775 else
776 commit_prefix = "commit ";
777 continue;
779 if (!strncmp(arg, "--no-merges", 11)) {
780 no_merges = 1;
781 continue;
783 if (!strcmp(arg, "--parents")) {
784 show_parents = 1;
785 continue;
787 if (!strcmp(arg, "--bisect")) {
788 bisect_list = 1;
789 continue;
791 if (!strcmp(arg, "--all")) {
792 handle_all(&list);
793 continue;
795 if (!strcmp(arg, "--objects")) {
796 tag_objects = 1;
797 tree_objects = 1;
798 blob_objects = 1;
799 continue;
801 if (!strcmp(arg, "--unpacked")) {
802 unpacked = 1;
803 limited = 1;
804 continue;
806 if (!strcmp(arg, "--merge-order")) {
807 merge_order = 1;
808 continue;
810 if (!strcmp(arg, "--show-breaks")) {
811 show_breaks = 1;
812 continue;
814 if (!strcmp(arg, "--topo-order")) {
815 topo_order = 1;
816 limited = 1;
817 continue;
819 if (!strcmp(arg, "--dense")) {
820 dense = 1;
821 continue;
823 if (!strcmp(arg, "--sparse")) {
824 dense = 0;
825 continue;
827 if (!strcmp(arg, "--remove-empty")) {
828 remove_empty_trees = 1;
829 continue;
831 if (!strcmp(arg, "--")) {
832 i++;
833 break;
836 if (show_breaks && !merge_order)
837 usage(rev_list_usage);
839 flags = 0;
840 dotdot = strstr(arg, "..");
841 if (dotdot) {
842 unsigned char from_sha1[20];
843 char *next = dotdot + 2;
844 *dotdot = 0;
845 if (!*next)
846 next = "HEAD";
847 if (!get_sha1(arg, from_sha1) && !get_sha1(next, sha1)) {
848 struct commit *exclude;
849 struct commit *include;
851 exclude = get_commit_reference(arg, from_sha1, UNINTERESTING);
852 include = get_commit_reference(next, sha1, 0);
853 if (!exclude || !include)
854 die("Invalid revision range %s..%s", arg, next);
855 limited = 1;
856 handle_one_commit(exclude, &list);
857 handle_one_commit(include, &list);
858 continue;
860 *dotdot = '.';
862 if (*arg == '^') {
863 flags = UNINTERESTING;
864 arg++;
865 limited = 1;
867 if (get_sha1(arg, sha1) < 0) {
868 struct stat st;
869 if (lstat(arg, &st) < 0)
870 die("'%s': %s", arg, strerror(errno));
871 break;
873 commit = get_commit_reference(arg, sha1, flags);
874 handle_one_commit(commit, &list);
877 if (!list &&
878 (!(tag_objects||tree_objects||blob_objects) && !pending_objects))
879 usage(rev_list_usage);
881 paths = get_pathspec(prefix, argv + i);
882 if (paths) {
883 limited = 1;
884 diff_tree_setup_paths(paths);
887 save_commit_buffer = verbose_header;
888 track_object_refs = 0;
890 if (!merge_order) {
891 sort_by_date(&list);
892 if (list && !limited && max_count == 1 &&
893 !tag_objects && !tree_objects && !blob_objects) {
894 show_commit(list->item);
895 return 0;
897 if (limited)
898 list = limit_list(list);
899 if (topo_order)
900 sort_in_topological_order(&list);
901 show_commit_list(list);
902 } else {
903 #ifndef NO_OPENSSL
904 if (sort_list_in_merge_order(list, &process_commit)) {
905 die("merge order sort failed\n");
907 #else
908 die("merge order sort unsupported, OpenSSL not linked");
909 #endif
912 return 0;