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"
9
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)
15
16static 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"
35;
36
37static int dense = 1;
38static int unpacked = 0;
39static int bisect_list = 0;
40static int tag_objects = 0;
41static int tree_objects = 0;
42static int blob_objects = 0;
43static int verbose_header = 0;
44static int show_parents = 0;
45static int hdr_termination = 0;
46static const char *commit_prefix = "";
47static unsigned long max_age = -1;
48static unsigned long min_age = -1;
49static int max_count = -1;
50static enum cmit_fmt commit_format = CMIT_FMT_RAW;
51static int merge_order = 0;
52static int show_breaks = 0;
53static int stop_traversal = 0;
54static int topo_order = 0;
55static int no_merges = 0;
56static const char **paths = NULL;
57
58static void show_commit(struct commit *commit)
59{
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 = "= ";
67 }
68 }
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;
75 }
76 }
77 if (commit_format == CMIT_FMT_ONELINE)
78 putchar(' ');
79 else
80 putchar('\n');
81
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);
86 }
87 fflush(stdout);
88}
89
90static int rewrite_one(struct commit **pp)
91{
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;
99 }
100}
101
102static void rewrite_parents(struct commit *commit)
103{
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;
110 }
111 pp = &parent->next;
112 }
113}
114
115static int filter_commit(struct commit * commit)
116{
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;
126 }
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);
135 }
136 return DO;
137}
138
139static int process_commit(struct commit * commit)
140{
141 int action=filter_commit(commit);
142
143 if (action == STOP) {
144 return STOP;
145 }
146
147 if (action == CONTINUE) {
148 return CONTINUE;
149 }
150
151 show_commit(commit);
152
153 return CONTINUE;
154}
155
156static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
157{
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;
164}
165
166static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
167{
168 struct object *obj = &blob->object;
169
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);
176}
177
178static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
179{
180 struct object *obj = &tree->object;
181 struct tree_entry_list *entry;
182
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;
201 }
202 return p;
203}
204
205static struct object_list *pending_objects = NULL;
206
207static void show_commit_list(struct commit_list *list)
208{
209 struct object_list *objects = NULL, **p = &objects, *pending;
210 while (list) {
211 struct commit *commit = pop_most_recent_commit(&list, SEEN);
212
213 p = process_tree(commit->tree, p, "");
214 if (process_commit(commit) == STOP)
215 break;
216 }
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;
226 }
227 if (obj->type == tree_type) {
228 p = process_tree((struct tree *)obj, p, name);
229 continue;
230 }
231 if (obj->type == blob_type) {
232 p = process_blob((struct blob *)obj, p, name);
233 continue;
234 }
235 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
236 }
237 while (objects) {
238 /* An object with name "foo\n0000000000000000000000000000000000000000"
239 * can be used confuse downstream git-pack-objects very badly.
240 */
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);
246 }
247 else
248 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
249 objects = objects->next;
250 }
251}
252
253static void mark_blob_uninteresting(struct blob *blob)
254{
255 if (!blob_objects)
256 return;
257 if (blob->object.flags & UNINTERESTING)
258 return;
259 blob->object.flags |= UNINTERESTING;
260}
261
262static void mark_tree_uninteresting(struct tree *tree)
263{
264 struct object *obj = &tree->object;
265 struct tree_entry_list *entry;
266
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;
286 }
287}
288
289static void mark_parents_uninteresting(struct commit *commit)
290{
291 struct commit_list *parents = commit->parents;
292
293 while (parents) {
294 struct commit *commit = parents->item;
295 commit->object.flags |= UNINTERESTING;
296
297 /*
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..
304 */
305 if (commit->parents)
306 mark_parents_uninteresting(commit);
307
308 /*
309 * A missing commit is ok iff its parent is marked
310 * uninteresting.
311 *
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.
315 */
316 if (!has_sha1_file(commit->object.sha1))
317 commit->object.parsed = 1;
318 parents = parents->next;
319 }
320}
321
322static int everybody_uninteresting(struct commit_list *orig)
323{
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;
331 }
332 return 1;
333}
334
335/*
336 * This is a truly stupid algorithm, but it's only
337 * used for bisection, and we just don't care enough.
338 *
339 * We care just barely enough to avoid recursing for
340 * non-merge entries.
341 */
342static int count_distance(struct commit_list *entry)
343{
344 int nr = 0;
345
346 while (entry) {
347 struct commit *commit = entry->item;
348 struct commit_list *p;
349
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;
361 }
362 }
363 }
364 return nr;
365}
366
367static void clear_distance(struct commit_list *list)
368{
369 while (list) {
370 struct commit *commit = list->item;
371 commit->object.flags &= ~COUNTED;
372 list = list->next;
373 }
374}
375
376static struct commit_list *find_bisection(struct commit_list *list)
377{
378 int nr, closest;
379 struct commit_list *p, *best;
380
381 nr = 0;
382 p = list;
383 while (p) {
384 nr++;
385 p = p->next;
386 }
387 closest = 0;
388 best = list;
389
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;
399 }
400 p = p->next;
401 }
402 if (best)
403 best->next = NULL;
404 return best;
405}
406
407static void mark_edges_uninteresting(struct commit_list *list)
408{
409 for ( ; list; list = list->next) {
410 struct commit_list *parents = list->item->parents;
411
412 for ( ; parents; parents = parents->next) {
413 struct commit *commit = parents->item;
414 if (commit->object.flags & UNINTERESTING)
415 mark_tree_uninteresting(commit->tree);
416 }
417 }
418}
419
420static int is_different = 0;
421
422static 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)
426{
427 is_different = 1;
428}
429
430static 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)
435{
436 is_different = 1;
437}
438
439static struct diff_options diff_opt = {
440 .recursive = 1,
441 .add_remove = file_add_remove,
442 .change = file_change,
443};
444
445static int same_tree(struct tree *t1, struct tree *t2)
446{
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;
451}
452
453static int same_tree_as_empty(struct tree *t1)
454{
455 int retval;
456 void *tree;
457 struct tree_desc empty, real;
458
459 if (!t1)
460 return 0;
461
462 tree = read_object_with_reference(t1->object.sha1, "tree", &real.size, NULL);
463 if (!tree)
464 return 0;
465 real.buf = tree;
466
467 empty.buf = "";
468 empty.size = 0;
469
470 is_different = 0;
471 retval = diff_tree(&empty, &real, "", &diff_opt);
472 free(tree);
473
474 return retval >= 0 && !is_different;
475}
476
477static struct commit *try_to_simplify_merge(struct commit *commit, struct commit_list *parent)
478{
479 if (!commit->tree)
480 return NULL;
481
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;
490 }
491 return NULL;
492}
493
494static void add_parents_to_list(struct commit *commit, struct commit_list **list)
495{
496 struct commit_list *parent = commit->parents;
497
498 /*
499 * If the commit is uninteresting, don't try to
500 * prune parents - we want the maximal uninteresting
501 * set.
502 *
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..
509 */
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);
522 }
523 return;
524 }
525
526 /*
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.
530 */
531 if (paths && parent && parent->next) {
532 struct commit *preferred;
533
534 preferred = try_to_simplify_merge(commit, parent);
535 if (preferred) {
536 parent->item = preferred;
537 parent->next = NULL;
538 }
539 }
540
541 while (parent) {
542 struct commit *p = parent->item;
543
544 parent = parent->next;
545
546 parse_commit(p);
547 if (p->object.flags & SEEN)
548 continue;
549 p->object.flags |= SEEN;
550 insert_by_date(p, list);
551 }
552}
553
554static void compress_list(struct commit_list *list)
555{
556 while (list) {
557 struct commit *commit = list->item;
558 struct commit_list *parent = commit->parents;
559 list = list->next;
560
561 if (!parent) {
562 if (!same_tree_as_empty(commit->tree))
563 commit->object.flags |= TREECHANGE;
564 continue;
565 }
566
567 /*
568 * Exactly one parent? Check if it leaves the tree
569 * unchanged
570 */
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;
576 }
577 commit->object.flags |= TREECHANGE;
578 }
579}
580
581static struct commit_list *limit_list(struct commit_list *list)
582{
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;
589
590 list = list->next;
591 free(entry);
592
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;
603 }
604 if (min_age != -1 && (commit->date > min_age))
605 continue;
606 p = &commit_list_insert(commit, p)->next;
607 }
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;
615}
616
617static void add_pending_object(struct object *obj, const char *name)
618{
619 add_object(obj, &pending_objects, name);
620}
621
622static struct commit *get_commit_reference(const char *name, const unsigned char *sha1, unsigned int flags)
623{
624 struct object *object;
625
626 object = parse_object(sha1);
627 if (!object)
628 die("bad object %s", name);
629
630 /*
631 * Tag object? Look what it points to..
632 */
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));
641 }
642
643 /*
644 * Commit object? Just return it, we'll do all the complex
645 * reachability crud.
646 */
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;
655 }
656
657 /*
658 * Tree object? Either mark it uniniteresting, or add it
659 * to the list of objects to look at later..
660 */
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;
668 }
669 add_pending_object(object, "");
670 return NULL;
671 }
672
673 /*
674 * Blob object? You know the drill by now..
675 */
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;
683 }
684 add_pending_object(object, "");
685 return NULL;
686 }
687 die("%s is unknown object", name);
688}
689
690static void handle_one_commit(struct commit *com, struct commit_list **lst)
691{
692 if (!com || com->object.flags & SEEN)
693 return;
694 com->object.flags |= SEEN;
695 commit_list_insert(com, lst);
696}
697
698/* for_each_ref() callback does not allow user data -- Yuck. */
699static struct commit_list **global_lst;
700
701static int include_one_commit(const char *path, const unsigned char *sha1)
702{
703 struct commit *com = get_commit_reference(path, sha1, 0);
704 handle_one_commit(com, global_lst);
705 return 0;
706}
707
708static void handle_all(struct commit_list **lst)
709{
710 global_lst = lst;
711 for_each_ref(include_one_commit);
712 global_lst = NULL;
713}
714
715int main(int argc, const char **argv)
716{
717 const char *prefix = setup_git_directory();
718 struct commit_list *list = NULL;
719 int i, limited = 0;
720
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];
727
728 if (!strncmp(arg, "--max-count=", 12)) {
729 max_count = atoi(arg + 12);
730 continue;
731 }
732 if (!strncmp(arg, "--max-age=", 10)) {
733 max_age = atoi(arg + 10);
734 limited = 1;
735 continue;
736 }
737 if (!strncmp(arg, "--min-age=", 10)) {
738 min_age = atoi(arg + 10);
739 limited = 1;
740 continue;
741 }
742 if (!strcmp(arg, "--header")) {
743 verbose_header = 1;
744 continue;
745 }
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;
755 }
756 if (!strncmp(arg, "--no-merges", 11)) {
757 no_merges = 1;
758 continue;
759 }
760 if (!strcmp(arg, "--parents")) {
761 show_parents = 1;
762 continue;
763 }
764 if (!strcmp(arg, "--bisect")) {
765 bisect_list = 1;
766 continue;
767 }
768 if (!strcmp(arg, "--all")) {
769 handle_all(&list);
770 continue;
771 }
772 if (!strcmp(arg, "--objects")) {
773 tag_objects = 1;
774 tree_objects = 1;
775 blob_objects = 1;
776 continue;
777 }
778 if (!strcmp(arg, "--unpacked")) {
779 unpacked = 1;
780 limited = 1;
781 continue;
782 }
783 if (!strcmp(arg, "--merge-order")) {
784 merge_order = 1;
785 continue;
786 }
787 if (!strcmp(arg, "--show-breaks")) {
788 show_breaks = 1;
789 continue;
790 }
791 if (!strcmp(arg, "--topo-order")) {
792 topo_order = 1;
793 limited = 1;
794 continue;
795 }
796 if (!strcmp(arg, "--dense")) {
797 dense = 1;
798 continue;
799 }
800 if (!strcmp(arg, "--sparse")) {
801 dense = 0;
802 continue;
803 }
804 if (!strcmp(arg, "--")) {
805 i++;
806 break;
807 }
808
809 if (show_breaks && !merge_order)
810 usage(rev_list_usage);
811
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;
823
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;
832 }
833 *dotdot = '.';
834 }
835 if (*arg == '^') {
836 flags = UNINTERESTING;
837 arg++;
838 limited = 1;
839 }
840 if (get_sha1(arg, sha1) < 0)
841 break;
842 commit = get_commit_reference(arg, sha1, flags);
843 handle_one_commit(commit, &list);
844 }
845
846 if (!list)
847 usage(rev_list_usage);
848
849 paths = get_pathspec(prefix, argv + i);
850 if (paths) {
851 limited = 1;
852 diff_tree_setup_paths(paths);
853 }
854
855 save_commit_buffer = verbose_header;
856 track_object_refs = 0;
857
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;
864 }
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");
874 }
875#else
876 die("merge order sort unsupported, OpenSSL not linked");
877#endif
878 }
879
880 return 0;
881}