git-fetch: fail if specified refspec does not match remote.
[debian-git.git] / rev-list.c
blob6e6ffde39600f048f5c365dd478256feba321db9
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;
58 static void show_commit(struct commit *commit)
60 commit->object.flags |= SHOWN;
61 if (show_breaks) {
62 commit_prefix = "| ";
63 if (commit->object.flags & DISCONTINUITY) {
64 commit_prefix = "^ ";
65 } else if (commit->object.flags & BOUNDARY) {
66 commit_prefix = "= ";
69 printf("%s%s", commit_prefix, sha1_to_hex(commit->object.sha1));
70 if (show_parents) {
71 struct commit_list *parents = commit->parents;
72 while (parents) {
73 printf(" %s", sha1_to_hex(parents->item->object.sha1));
74 parents = parents->next;
77 if (commit_format == CMIT_FMT_ONELINE)
78 putchar(' ');
79 else
80 putchar('\n');
82 if (verbose_header) {
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);
87 fflush(stdout);
90 static int rewrite_one(struct commit **pp)
92 for (;;) {
93 struct commit *p = *pp;
94 if (p->object.flags & (TREECHANGE | UNINTERESTING))
95 return 0;
96 if (!p->parents)
97 return -1;
98 *pp = p->parents->item;
102 static void rewrite_parents(struct commit *commit)
104 struct commit_list **pp = &commit->parents;
105 while (*pp) {
106 struct commit_list *parent = *pp;
107 if (rewrite_one(&parent->item) < 0) {
108 *pp = parent->next;
109 continue;
111 pp = &parent->next;
115 static int filter_commit(struct commit * commit)
117 if (stop_traversal && (commit->object.flags & BOUNDARY))
118 return STOP;
119 if (commit->object.flags & (UNINTERESTING|SHOWN))
120 return CONTINUE;
121 if (min_age != -1 && (commit->date > min_age))
122 return CONTINUE;
123 if (max_age != -1 && (commit->date < max_age)) {
124 stop_traversal=1;
125 return CONTINUE;
127 if (max_count != -1 && !max_count--)
128 return STOP;
129 if (no_merges && (commit->parents && commit->parents->next))
130 return CONTINUE;
131 if (paths && dense) {
132 if (!(commit->object.flags & TREECHANGE))
133 return CONTINUE;
134 rewrite_parents(commit);
136 return DO;
139 static int process_commit(struct commit * commit)
141 int action=filter_commit(commit);
143 if (action == STOP) {
144 return STOP;
147 if (action == CONTINUE) {
148 return CONTINUE;
151 show_commit(commit);
153 return 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));
159 entry->item = obj;
160 entry->next = *p;
161 entry->name = name;
162 *p = entry;
163 return &entry->next;
166 static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
168 struct object *obj = &blob->object;
170 if (!blob_objects)
171 return p;
172 if (obj->flags & (UNINTERESTING | SEEN))
173 return p;
174 obj->flags |= 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;
183 if (!tree_objects)
184 return p;
185 if (obj->flags & (UNINTERESTING | SEEN))
186 return p;
187 if (parse_tree(tree) < 0)
188 die("bad tree object %s", sha1_to_hex(obj->sha1));
189 obj->flags |= SEEN;
190 p = add_object(obj, p, name);
191 entry = tree->entries;
192 tree->entries = NULL;
193 while (entry) {
194 struct tree_entry_list *next = entry->next;
195 if (entry->directory)
196 p = process_tree(entry->item.tree, p, entry->name);
197 else
198 p = process_blob(entry->item.blob, p, entry->name);
199 free(entry);
200 entry = next;
202 return p;
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;
210 while (list) {
211 struct commit *commit = pop_most_recent_commit(&list, SEEN);
213 p = process_tree(commit->tree, p, "");
214 if (process_commit(commit) == STOP)
215 break;
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))
221 continue;
222 if (obj->type == tag_type) {
223 obj->flags |= SEEN;
224 p = add_object(obj, p, name);
225 continue;
227 if (obj->type == tree_type) {
228 p = process_tree((struct tree *)obj, p, name);
229 continue;
231 if (obj->type == blob_type) {
232 p = process_blob((struct blob *)obj, p, name);
233 continue;
235 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
237 while (objects) {
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');
242 if (ep) {
243 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
244 (int) (ep - objects->name),
245 objects->name);
247 else
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)
255 if (!blob_objects)
256 return;
257 if (blob->object.flags & UNINTERESTING)
258 return;
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;
267 if (!tree_objects)
268 return;
269 if (obj->flags & UNINTERESTING)
270 return;
271 obj->flags |= UNINTERESTING;
272 if (!has_sha1_file(obj->sha1))
273 return;
274 if (parse_tree(tree) < 0)
275 die("bad tree %s", sha1_to_hex(obj->sha1));
276 entry = tree->entries;
277 tree->entries = NULL;
278 while (entry) {
279 struct tree_entry_list *next = entry->next;
280 if (entry->directory)
281 mark_tree_uninteresting(entry->item.tree);
282 else
283 mark_blob_uninteresting(entry->item.blob);
284 free(entry);
285 entry = next;
289 static void mark_parents_uninteresting(struct commit *commit)
291 struct commit_list *parents = commit->parents;
293 while (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..
305 if (commit->parents)
306 mark_parents_uninteresting(commit);
309 * A missing commit is ok iff its parent is marked
310 * uninteresting.
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;
325 while (list) {
326 struct commit *commit = list->item;
327 list = list->next;
328 if (commit->object.flags & UNINTERESTING)
329 continue;
330 return 0;
332 return 1;
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
340 * non-merge entries.
342 static int count_distance(struct commit_list *entry)
344 int nr = 0;
346 while (entry) {
347 struct commit *commit = entry->item;
348 struct commit_list *p;
350 if (commit->object.flags & (UNINTERESTING | COUNTED))
351 break;
352 nr++;
353 commit->object.flags |= COUNTED;
354 p = commit->parents;
355 entry = p;
356 if (p) {
357 p = p->next;
358 while (p) {
359 nr += count_distance(p);
360 p = p->next;
364 return nr;
367 static void clear_distance(struct commit_list *list)
369 while (list) {
370 struct commit *commit = list->item;
371 commit->object.flags &= ~COUNTED;
372 list = list->next;
376 static struct commit_list *find_bisection(struct commit_list *list)
378 int nr, closest;
379 struct commit_list *p, *best;
381 nr = 0;
382 p = list;
383 while (p) {
384 nr++;
385 p = p->next;
387 closest = 0;
388 best = list;
390 p = list;
391 while (p) {
392 int distance = count_distance(p);
393 clear_distance(list);
394 if (nr - distance < distance)
395 distance = nr - distance;
396 if (distance > closest) {
397 best = p;
398 closest = distance;
400 p = p->next;
402 if (best)
403 best->next = NULL;
404 return best;
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)
427 is_different = 1;
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)
436 is_different = 1;
439 static struct diff_options diff_opt = {
440 .recursive = 1,
441 .add_remove = file_add_remove,
442 .change = file_change,
445 static int same_tree(struct tree *t1, struct tree *t2)
447 is_different = 0;
448 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "", &diff_opt) < 0)
449 return 0;
450 return !is_different;
453 static int same_tree_as_empty(struct tree *t1)
455 int retval;
456 void *tree;
457 struct tree_desc empty, real;
459 if (!t1)
460 return 0;
462 tree = read_object_with_reference(t1->object.sha1, "tree", &real.size, NULL);
463 if (!tree)
464 return 0;
465 real.buf = tree;
467 empty.buf = "";
468 empty.size = 0;
470 is_different = 0;
471 retval = diff_tree(&empty, &real, "", &diff_opt);
472 free(tree);
474 return retval >= 0 && !is_different;
477 static struct commit *try_to_simplify_merge(struct commit *commit, struct commit_list *parent)
479 if (!commit->tree)
480 return NULL;
482 while (parent) {
483 struct commit *p = parent->item;
484 parent = parent->next;
485 parse_commit(p);
486 if (!p->tree)
487 continue;
488 if (same_tree(commit->tree, p->tree))
489 return p;
491 return NULL;
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
501 * set.
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) {
511 while (parent) {
512 struct commit *p = parent->item;
513 parent = parent->next;
514 parse_commit(p);
515 p->object.flags |= UNINTERESTING;
516 if (p->parents)
517 mark_parents_uninteresting(p);
518 if (p->object.flags & SEEN)
519 continue;
520 p->object.flags |= SEEN;
521 insert_by_date(p, list);
523 return;
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);
535 if (preferred) {
536 parent->item = preferred;
537 parent->next = NULL;
541 while (parent) {
542 struct commit *p = parent->item;
544 parent = parent->next;
546 parse_commit(p);
547 if (p->object.flags & SEEN)
548 continue;
549 p->object.flags |= SEEN;
550 insert_by_date(p, list);
554 static void compress_list(struct commit_list *list)
556 while (list) {
557 struct commit *commit = list->item;
558 struct commit_list *parent = commit->parents;
559 list = list->next;
561 if (!parent) {
562 if (!same_tree_as_empty(commit->tree))
563 commit->object.flags |= TREECHANGE;
564 continue;
568 * Exactly one parent? Check if it leaves the tree
569 * unchanged
571 if (!parent->next) {
572 struct tree *t1 = commit->tree;
573 struct tree *t2 = parent->item->tree;
574 if (!t1 || !t2 || same_tree(t1, t2))
575 continue;
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;
585 while (list) {
586 struct commit_list *entry = list;
587 struct commit *commit = list->item;
588 struct object *obj = &commit->object;
590 list = list->next;
591 free(entry);
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))
601 break;
602 continue;
604 if (min_age != -1 && (commit->date > min_age))
605 continue;
606 p = &commit_list_insert(commit, p)->next;
608 if (tree_objects)
609 mark_edges_uninteresting(newlist);
610 if (paths && dense)
611 compress_list(newlist);
612 if (bisect_list)
613 newlist = find_bisection(newlist);
614 return 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);
627 if (!object)
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);
639 if (!object)
640 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
644 * Commit object? Just return it, we'll do all the complex
645 * reachability crud.
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);
654 return 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;
663 if (!tree_objects)
664 return NULL;
665 if (flags & UNINTERESTING) {
666 mark_tree_uninteresting(tree);
667 return NULL;
669 add_pending_object(object, "");
670 return NULL;
674 * Blob object? You know the drill by now..
676 if (object->type == blob_type) {
677 struct blob *blob = (struct blob *)object;
678 if (!blob_objects)
679 return NULL;
680 if (flags & UNINTERESTING) {
681 mark_blob_uninteresting(blob);
682 return NULL;
684 add_pending_object(object, "");
685 return NULL;
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)
693 return;
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);
705 return 0;
708 static void handle_all(struct commit_list **lst)
710 global_lst = lst;
711 for_each_ref(include_one_commit);
712 global_lst = NULL;
715 int main(int argc, const char **argv)
717 const char *prefix = setup_git_directory();
718 struct commit_list *list = NULL;
719 int i, limited = 0;
721 for (i = 1 ; i < argc; i++) {
722 int flags;
723 const char *arg = argv[i];
724 char *dotdot;
725 struct commit *commit;
726 unsigned char sha1[20];
728 if (!strncmp(arg, "--max-count=", 12)) {
729 max_count = atoi(arg + 12);
730 continue;
732 if (!strncmp(arg, "--max-age=", 10)) {
733 max_age = atoi(arg + 10);
734 limited = 1;
735 continue;
737 if (!strncmp(arg, "--min-age=", 10)) {
738 min_age = atoi(arg + 10);
739 limited = 1;
740 continue;
742 if (!strcmp(arg, "--header")) {
743 verbose_header = 1;
744 continue;
746 if (!strncmp(arg, "--pretty", 8)) {
747 commit_format = get_commit_format(arg+8);
748 verbose_header = 1;
749 hdr_termination = '\n';
750 if (commit_format == CMIT_FMT_ONELINE)
751 commit_prefix = "";
752 else
753 commit_prefix = "commit ";
754 continue;
756 if (!strncmp(arg, "--no-merges", 11)) {
757 no_merges = 1;
758 continue;
760 if (!strcmp(arg, "--parents")) {
761 show_parents = 1;
762 continue;
764 if (!strcmp(arg, "--bisect")) {
765 bisect_list = 1;
766 continue;
768 if (!strcmp(arg, "--all")) {
769 handle_all(&list);
770 continue;
772 if (!strcmp(arg, "--objects")) {
773 tag_objects = 1;
774 tree_objects = 1;
775 blob_objects = 1;
776 continue;
778 if (!strcmp(arg, "--unpacked")) {
779 unpacked = 1;
780 limited = 1;
781 continue;
783 if (!strcmp(arg, "--merge-order")) {
784 merge_order = 1;
785 continue;
787 if (!strcmp(arg, "--show-breaks")) {
788 show_breaks = 1;
789 continue;
791 if (!strcmp(arg, "--topo-order")) {
792 topo_order = 1;
793 limited = 1;
794 continue;
796 if (!strcmp(arg, "--dense")) {
797 dense = 1;
798 continue;
800 if (!strcmp(arg, "--sparse")) {
801 dense = 0;
802 continue;
804 if (!strcmp(arg, "--")) {
805 i++;
806 break;
809 if (show_breaks && !merge_order)
810 usage(rev_list_usage);
812 flags = 0;
813 dotdot = strstr(arg, "..");
814 if (dotdot) {
815 unsigned char from_sha1[20];
816 char *next = dotdot + 2;
817 *dotdot = 0;
818 if (!*next)
819 next = "HEAD";
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);
828 limited = 1;
829 handle_one_commit(exclude, &list);
830 handle_one_commit(include, &list);
831 continue;
833 *dotdot = '.';
835 if (*arg == '^') {
836 flags = UNINTERESTING;
837 arg++;
838 limited = 1;
840 if (get_sha1(arg, sha1) < 0)
841 break;
842 commit = get_commit_reference(arg, sha1, flags);
843 handle_one_commit(commit, &list);
846 if (!list)
847 usage(rev_list_usage);
849 paths = get_pathspec(prefix, argv + i);
850 if (paths) {
851 limited = 1;
852 diff_tree_setup_paths(paths);
855 save_commit_buffer = verbose_header;
856 track_object_refs = 0;
858 if (!merge_order) {
859 sort_by_date(&list);
860 if (list && !limited && max_count == 1 &&
861 !tag_objects && !tree_objects && !blob_objects) {
862 show_commit(list->item);
863 return 0;
865 if (limited)
866 list = limit_list(list);
867 if (topo_order)
868 sort_in_topological_order(&list);
869 show_commit_list(list);
870 } else {
871 #ifndef NO_OPENSSL
872 if (sort_list_in_merge_order(list, &process_commit)) {
873 die("merge order sort failed\n");
875 #else
876 die("merge order sort unsupported, OpenSSL not linked");
877 #endif
880 return 0;