Use struct commit in tar-tree
[git/jrn.git] / rev-list.c
blob0b142c1a6f79bf12901da71ca4e336cc32488171
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 " --remove-empty\n"
25 " --all\n"
26 " ordering output:\n"
27 " --merge-order [ --show-breaks ]\n"
28 " --topo-order\n"
29 " formatting output:\n"
30 " --parents\n"
31 " --objects\n"
32 " --unpacked\n"
33 " --header | --pretty\n"
34 " special purpose:\n"
35 " --bisect"
38 static int dense = 1;
39 static int unpacked = 0;
40 static int bisect_list = 0;
41 static int tag_objects = 0;
42 static int tree_objects = 0;
43 static int blob_objects = 0;
44 static int verbose_header = 0;
45 static int show_parents = 0;
46 static int hdr_termination = 0;
47 static const char *commit_prefix = "";
48 static unsigned long max_age = -1;
49 static unsigned long min_age = -1;
50 static int max_count = -1;
51 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
52 static int merge_order = 0;
53 static int show_breaks = 0;
54 static int stop_traversal = 0;
55 static int topo_order = 0;
56 static int no_merges = 0;
57 static const char **paths = NULL;
58 static int remove_empty_trees = 0;
60 static void show_commit(struct commit *commit)
62 commit->object.flags |= SHOWN;
63 if (show_breaks) {
64 commit_prefix = "| ";
65 if (commit->object.flags & DISCONTINUITY) {
66 commit_prefix = "^ ";
67 } else if (commit->object.flags & BOUNDARY) {
68 commit_prefix = "= ";
71 printf("%s%s", commit_prefix, sha1_to_hex(commit->object.sha1));
72 if (show_parents) {
73 struct commit_list *parents = commit->parents;
74 while (parents) {
75 printf(" %s", sha1_to_hex(parents->item->object.sha1));
76 parents = parents->next;
79 if (commit_format == CMIT_FMT_ONELINE)
80 putchar(' ');
81 else
82 putchar('\n');
84 if (verbose_header) {
85 static char pretty_header[16384];
86 pretty_print_commit(commit_format, commit, ~0, pretty_header, sizeof(pretty_header), 0);
87 printf("%s%c", pretty_header, hdr_termination);
89 fflush(stdout);
92 static int rewrite_one(struct commit **pp)
94 for (;;) {
95 struct commit *p = *pp;
96 if (p->object.flags & (TREECHANGE | UNINTERESTING))
97 return 0;
98 if (!p->parents)
99 return -1;
100 *pp = p->parents->item;
104 static void rewrite_parents(struct commit *commit)
106 struct commit_list **pp = &commit->parents;
107 while (*pp) {
108 struct commit_list *parent = *pp;
109 if (rewrite_one(&parent->item) < 0) {
110 *pp = parent->next;
111 continue;
113 pp = &parent->next;
117 static int filter_commit(struct commit * commit)
119 if (stop_traversal && (commit->object.flags & BOUNDARY))
120 return STOP;
121 if (commit->object.flags & (UNINTERESTING|SHOWN))
122 return CONTINUE;
123 if (min_age != -1 && (commit->date > min_age))
124 return CONTINUE;
125 if (max_age != -1 && (commit->date < max_age)) {
126 stop_traversal=1;
127 return CONTINUE;
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 if (max_count != -1 && !max_count--)
152 return STOP;
154 show_commit(commit);
156 return CONTINUE;
159 static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
161 struct object_list *entry = xmalloc(sizeof(*entry));
162 entry->item = obj;
163 entry->next = *p;
164 entry->name = name;
165 *p = entry;
166 return &entry->next;
169 static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
171 struct object *obj = &blob->object;
173 if (!blob_objects)
174 return p;
175 if (obj->flags & (UNINTERESTING | SEEN))
176 return p;
177 obj->flags |= SEEN;
178 return add_object(obj, p, name);
181 static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
183 struct object *obj = &tree->object;
184 struct tree_entry_list *entry;
186 if (!tree_objects)
187 return p;
188 if (obj->flags & (UNINTERESTING | SEEN))
189 return p;
190 if (parse_tree(tree) < 0)
191 die("bad tree object %s", sha1_to_hex(obj->sha1));
192 obj->flags |= SEEN;
193 p = add_object(obj, p, name);
194 entry = tree->entries;
195 tree->entries = NULL;
196 while (entry) {
197 struct tree_entry_list *next = entry->next;
198 if (entry->directory)
199 p = process_tree(entry->item.tree, p, entry->name);
200 else
201 p = process_blob(entry->item.blob, p, entry->name);
202 free(entry);
203 entry = next;
205 return p;
208 static struct object_list *pending_objects = NULL;
210 static void show_commit_list(struct commit_list *list)
212 struct object_list *objects = NULL, **p = &objects, *pending;
213 while (list) {
214 struct commit *commit = pop_most_recent_commit(&list, SEEN);
216 p = process_tree(commit->tree, p, "");
217 if (process_commit(commit) == STOP)
218 break;
220 for (pending = pending_objects; pending; pending = pending->next) {
221 struct object *obj = pending->item;
222 const char *name = pending->name;
223 if (obj->flags & (UNINTERESTING | SEEN))
224 continue;
225 if (obj->type == tag_type) {
226 obj->flags |= SEEN;
227 p = add_object(obj, p, name);
228 continue;
230 if (obj->type == tree_type) {
231 p = process_tree((struct tree *)obj, p, name);
232 continue;
234 if (obj->type == blob_type) {
235 p = process_blob((struct blob *)obj, p, name);
236 continue;
238 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
240 while (objects) {
241 /* An object with name "foo\n0000000000000000000000000000000000000000"
242 * can be used confuse downstream git-pack-objects very badly.
244 const char *ep = strchr(objects->name, '\n');
245 if (ep) {
246 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
247 (int) (ep - objects->name),
248 objects->name);
250 else
251 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
252 objects = objects->next;
256 static void mark_blob_uninteresting(struct blob *blob)
258 if (!blob_objects)
259 return;
260 if (blob->object.flags & UNINTERESTING)
261 return;
262 blob->object.flags |= UNINTERESTING;
265 static void mark_tree_uninteresting(struct tree *tree)
267 struct object *obj = &tree->object;
268 struct tree_entry_list *entry;
270 if (!tree_objects)
271 return;
272 if (obj->flags & UNINTERESTING)
273 return;
274 obj->flags |= UNINTERESTING;
275 if (!has_sha1_file(obj->sha1))
276 return;
277 if (parse_tree(tree) < 0)
278 die("bad tree %s", sha1_to_hex(obj->sha1));
279 entry = tree->entries;
280 tree->entries = NULL;
281 while (entry) {
282 struct tree_entry_list *next = entry->next;
283 if (entry->directory)
284 mark_tree_uninteresting(entry->item.tree);
285 else
286 mark_blob_uninteresting(entry->item.blob);
287 free(entry);
288 entry = next;
292 static void mark_parents_uninteresting(struct commit *commit)
294 struct commit_list *parents = commit->parents;
296 while (parents) {
297 struct commit *commit = parents->item;
298 commit->object.flags |= UNINTERESTING;
301 * Normally we haven't parsed the parent
302 * yet, so we won't have a parent of a parent
303 * here. However, it may turn out that we've
304 * reached this commit some other way (where it
305 * wasn't uninteresting), in which case we need
306 * to mark its parents recursively too..
308 if (commit->parents)
309 mark_parents_uninteresting(commit);
312 * A missing commit is ok iff its parent is marked
313 * uninteresting.
315 * We just mark such a thing parsed, so that when
316 * it is popped next time around, we won't be trying
317 * to parse it and get an error.
319 if (!has_sha1_file(commit->object.sha1))
320 commit->object.parsed = 1;
321 parents = parents->next;
325 static int everybody_uninteresting(struct commit_list *orig)
327 struct commit_list *list = orig;
328 while (list) {
329 struct commit *commit = list->item;
330 list = list->next;
331 if (commit->object.flags & UNINTERESTING)
332 continue;
333 return 0;
335 return 1;
339 * This is a truly stupid algorithm, but it's only
340 * used for bisection, and we just don't care enough.
342 * We care just barely enough to avoid recursing for
343 * non-merge entries.
345 static int count_distance(struct commit_list *entry)
347 int nr = 0;
349 while (entry) {
350 struct commit *commit = entry->item;
351 struct commit_list *p;
353 if (commit->object.flags & (UNINTERESTING | COUNTED))
354 break;
355 if (!paths || (commit->object.flags & TREECHANGE))
356 nr++;
357 commit->object.flags |= COUNTED;
358 p = commit->parents;
359 entry = p;
360 if (p) {
361 p = p->next;
362 while (p) {
363 nr += count_distance(p);
364 p = p->next;
369 return nr;
372 static void clear_distance(struct commit_list *list)
374 while (list) {
375 struct commit *commit = list->item;
376 commit->object.flags &= ~COUNTED;
377 list = list->next;
381 static struct commit_list *find_bisection(struct commit_list *list)
383 int nr, closest;
384 struct commit_list *p, *best;
386 nr = 0;
387 p = list;
388 while (p) {
389 if (!paths || (p->item->object.flags & TREECHANGE))
390 nr++;
391 p = p->next;
393 closest = 0;
394 best = list;
396 for (p = list; p; p = p->next) {
397 int distance;
399 if (paths && !(p->item->object.flags & TREECHANGE))
400 continue;
402 distance = count_distance(p);
403 clear_distance(list);
404 if (nr - distance < distance)
405 distance = nr - distance;
406 if (distance > closest) {
407 best = p;
408 closest = distance;
411 if (best)
412 best->next = NULL;
413 return best;
416 static void mark_edges_uninteresting(struct commit_list *list)
418 for ( ; list; list = list->next) {
419 struct commit_list *parents = list->item->parents;
421 for ( ; parents; parents = parents->next) {
422 struct commit *commit = parents->item;
423 if (commit->object.flags & UNINTERESTING)
424 mark_tree_uninteresting(commit->tree);
429 #define TREE_SAME 0
430 #define TREE_NEW 1
431 #define TREE_DIFFERENT 2
432 static int tree_difference = TREE_SAME;
434 static void file_add_remove(struct diff_options *options,
435 int addremove, unsigned mode,
436 const unsigned char *sha1,
437 const char *base, const char *path)
439 int diff = TREE_DIFFERENT;
442 * Is it an add of a new file? It means that
443 * the old tree didn't have it at all, so we
444 * will turn "TREE_SAME" -> "TREE_NEW", but
445 * leave any "TREE_DIFFERENT" alone (and if
446 * it already was "TREE_NEW", we'll keep it
447 * "TREE_NEW" of course).
449 if (addremove == '+') {
450 diff = tree_difference;
451 if (diff != TREE_SAME)
452 return;
453 diff = TREE_NEW;
455 tree_difference = diff;
458 static void file_change(struct diff_options *options,
459 unsigned old_mode, unsigned new_mode,
460 const unsigned char *old_sha1,
461 const unsigned char *new_sha1,
462 const char *base, const char *path)
464 tree_difference = TREE_DIFFERENT;
467 static struct diff_options diff_opt = {
468 .recursive = 1,
469 .add_remove = file_add_remove,
470 .change = file_change,
473 static int compare_tree(struct tree *t1, struct tree *t2)
475 if (!t1)
476 return TREE_NEW;
477 if (!t2)
478 return TREE_DIFFERENT;
479 tree_difference = TREE_SAME;
480 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "", &diff_opt) < 0)
481 return TREE_DIFFERENT;
482 return tree_difference;
485 static int same_tree_as_empty(struct tree *t1)
487 int retval;
488 void *tree;
489 struct tree_desc empty, real;
491 if (!t1)
492 return 0;
494 tree = read_object_with_reference(t1->object.sha1, "tree", &real.size, NULL);
495 if (!tree)
496 return 0;
497 real.buf = tree;
499 empty.buf = "";
500 empty.size = 0;
502 tree_difference = 0;
503 retval = diff_tree(&empty, &real, "", &diff_opt);
504 free(tree);
506 return retval >= 0 && !tree_difference;
509 static void try_to_simplify_commit(struct commit *commit)
511 struct commit_list **pp, *parent;
513 if (!commit->tree)
514 return;
516 if (!commit->parents) {
517 if (!same_tree_as_empty(commit->tree))
518 commit->object.flags |= TREECHANGE;
519 return;
522 pp = &commit->parents;
523 while ((parent = *pp) != NULL) {
524 struct commit *p = parent->item;
526 if (p->object.flags & UNINTERESTING) {
527 pp = &parent->next;
528 continue;
531 parse_commit(p);
532 switch (compare_tree(p->tree, commit->tree)) {
533 case TREE_SAME:
534 parent->next = NULL;
535 commit->parents = parent;
536 return;
538 case TREE_NEW:
539 if (remove_empty_trees && same_tree_as_empty(p->tree)) {
540 *pp = parent->next;
541 continue;
543 /* fallthrough */
544 case TREE_DIFFERENT:
545 pp = &parent->next;
546 continue;
548 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
550 commit->object.flags |= TREECHANGE;
553 static void add_parents_to_list(struct commit *commit, struct commit_list **list)
555 struct commit_list *parent = commit->parents;
558 * If the commit is uninteresting, don't try to
559 * prune parents - we want the maximal uninteresting
560 * set.
562 * Normally we haven't parsed the parent
563 * yet, so we won't have a parent of a parent
564 * here. However, it may turn out that we've
565 * reached this commit some other way (where it
566 * wasn't uninteresting), in which case we need
567 * to mark its parents recursively too..
569 if (commit->object.flags & UNINTERESTING) {
570 while (parent) {
571 struct commit *p = parent->item;
572 parent = parent->next;
573 parse_commit(p);
574 p->object.flags |= UNINTERESTING;
575 if (p->parents)
576 mark_parents_uninteresting(p);
577 if (p->object.flags & SEEN)
578 continue;
579 p->object.flags |= SEEN;
580 insert_by_date(p, list);
582 return;
586 * Ok, the commit wasn't uninteresting. Try to
587 * simplify the commit history and find the parent
588 * that has no differences in the path set if one exists.
590 if (paths)
591 try_to_simplify_commit(commit);
593 parent = commit->parents;
594 while (parent) {
595 struct commit *p = parent->item;
597 parent = parent->next;
599 parse_commit(p);
600 if (p->object.flags & SEEN)
601 continue;
602 p->object.flags |= SEEN;
603 insert_by_date(p, list);
607 static struct commit_list *limit_list(struct commit_list *list)
609 struct commit_list *newlist = NULL;
610 struct commit_list **p = &newlist;
611 while (list) {
612 struct commit_list *entry = list;
613 struct commit *commit = list->item;
614 struct object *obj = &commit->object;
616 list = list->next;
617 free(entry);
619 if (max_age != -1 && (commit->date < max_age))
620 obj->flags |= UNINTERESTING;
621 if (unpacked && has_sha1_pack(obj->sha1))
622 obj->flags |= UNINTERESTING;
623 add_parents_to_list(commit, &list);
624 if (obj->flags & UNINTERESTING) {
625 mark_parents_uninteresting(commit);
626 if (everybody_uninteresting(list))
627 break;
628 continue;
630 if (min_age != -1 && (commit->date > min_age))
631 continue;
632 p = &commit_list_insert(commit, p)->next;
634 if (tree_objects)
635 mark_edges_uninteresting(newlist);
636 if (bisect_list)
637 newlist = find_bisection(newlist);
638 return newlist;
641 static void add_pending_object(struct object *obj, const char *name)
643 add_object(obj, &pending_objects, name);
646 static struct commit *get_commit_reference(const char *name, const unsigned char *sha1, unsigned int flags)
648 struct object *object;
650 object = parse_object(sha1);
651 if (!object)
652 die("bad object %s", name);
655 * Tag object? Look what it points to..
657 while (object->type == tag_type) {
658 struct tag *tag = (struct tag *) object;
659 object->flags |= flags;
660 if (tag_objects && !(object->flags & UNINTERESTING))
661 add_pending_object(object, tag->tag);
662 object = parse_object(tag->tagged->sha1);
663 if (!object)
664 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
668 * Commit object? Just return it, we'll do all the complex
669 * reachability crud.
671 if (object->type == commit_type) {
672 struct commit *commit = (struct commit *)object;
673 object->flags |= flags;
674 if (parse_commit(commit) < 0)
675 die("unable to parse commit %s", name);
676 if (flags & UNINTERESTING)
677 mark_parents_uninteresting(commit);
678 return commit;
682 * Tree object? Either mark it uniniteresting, or add it
683 * to the list of objects to look at later..
685 if (object->type == tree_type) {
686 struct tree *tree = (struct tree *)object;
687 if (!tree_objects)
688 return NULL;
689 if (flags & UNINTERESTING) {
690 mark_tree_uninteresting(tree);
691 return NULL;
693 add_pending_object(object, "");
694 return NULL;
698 * Blob object? You know the drill by now..
700 if (object->type == blob_type) {
701 struct blob *blob = (struct blob *)object;
702 if (!blob_objects)
703 return NULL;
704 if (flags & UNINTERESTING) {
705 mark_blob_uninteresting(blob);
706 return NULL;
708 add_pending_object(object, "");
709 return NULL;
711 die("%s is unknown object", name);
714 static void handle_one_commit(struct commit *com, struct commit_list **lst)
716 if (!com || com->object.flags & SEEN)
717 return;
718 com->object.flags |= SEEN;
719 commit_list_insert(com, lst);
722 /* for_each_ref() callback does not allow user data -- Yuck. */
723 static struct commit_list **global_lst;
725 static int include_one_commit(const char *path, const unsigned char *sha1)
727 struct commit *com = get_commit_reference(path, sha1, 0);
728 handle_one_commit(com, global_lst);
729 return 0;
732 static void handle_all(struct commit_list **lst)
734 global_lst = lst;
735 for_each_ref(include_one_commit);
736 global_lst = NULL;
739 int main(int argc, const char **argv)
741 const char *prefix = setup_git_directory();
742 struct commit_list *list = NULL;
743 int i, limited = 0;
745 for (i = 1 ; i < argc; i++) {
746 int flags;
747 const char *arg = argv[i];
748 char *dotdot;
749 struct commit *commit;
750 unsigned char sha1[20];
752 if (!strncmp(arg, "--max-count=", 12)) {
753 max_count = atoi(arg + 12);
754 continue;
756 if (!strncmp(arg, "--max-age=", 10)) {
757 max_age = atoi(arg + 10);
758 limited = 1;
759 continue;
761 if (!strncmp(arg, "--min-age=", 10)) {
762 min_age = atoi(arg + 10);
763 limited = 1;
764 continue;
766 if (!strcmp(arg, "--header")) {
767 verbose_header = 1;
768 continue;
770 if (!strncmp(arg, "--pretty", 8)) {
771 commit_format = get_commit_format(arg+8);
772 verbose_header = 1;
773 hdr_termination = '\n';
774 if (commit_format == CMIT_FMT_ONELINE)
775 commit_prefix = "";
776 else
777 commit_prefix = "commit ";
778 continue;
780 if (!strncmp(arg, "--no-merges", 11)) {
781 no_merges = 1;
782 continue;
784 if (!strcmp(arg, "--parents")) {
785 show_parents = 1;
786 continue;
788 if (!strcmp(arg, "--bisect")) {
789 bisect_list = 1;
790 continue;
792 if (!strcmp(arg, "--all")) {
793 handle_all(&list);
794 continue;
796 if (!strcmp(arg, "--objects")) {
797 tag_objects = 1;
798 tree_objects = 1;
799 blob_objects = 1;
800 continue;
802 if (!strcmp(arg, "--unpacked")) {
803 unpacked = 1;
804 limited = 1;
805 continue;
807 if (!strcmp(arg, "--merge-order")) {
808 merge_order = 1;
809 continue;
811 if (!strcmp(arg, "--show-breaks")) {
812 show_breaks = 1;
813 continue;
815 if (!strcmp(arg, "--topo-order")) {
816 topo_order = 1;
817 limited = 1;
818 continue;
820 if (!strcmp(arg, "--dense")) {
821 dense = 1;
822 continue;
824 if (!strcmp(arg, "--sparse")) {
825 dense = 0;
826 continue;
828 if (!strcmp(arg, "--remove-empty")) {
829 remove_empty_trees = 1;
830 continue;
832 if (!strcmp(arg, "--")) {
833 i++;
834 break;
837 if (show_breaks && !merge_order)
838 usage(rev_list_usage);
840 flags = 0;
841 dotdot = strstr(arg, "..");
842 if (dotdot) {
843 unsigned char from_sha1[20];
844 char *next = dotdot + 2;
845 *dotdot = 0;
846 if (!*next)
847 next = "HEAD";
848 if (!get_sha1(arg, from_sha1) && !get_sha1(next, sha1)) {
849 struct commit *exclude;
850 struct commit *include;
852 exclude = get_commit_reference(arg, from_sha1, UNINTERESTING);
853 include = get_commit_reference(next, sha1, 0);
854 if (!exclude || !include)
855 die("Invalid revision range %s..%s", arg, next);
856 limited = 1;
857 handle_one_commit(exclude, &list);
858 handle_one_commit(include, &list);
859 continue;
861 *dotdot = '.';
863 if (*arg == '^') {
864 flags = UNINTERESTING;
865 arg++;
866 limited = 1;
868 if (get_sha1(arg, sha1) < 0) {
869 struct stat st;
870 if (lstat(arg, &st) < 0)
871 die("'%s': %s", arg, strerror(errno));
872 break;
874 commit = get_commit_reference(arg, sha1, flags);
875 handle_one_commit(commit, &list);
878 if (!list &&
879 (!(tag_objects||tree_objects||blob_objects) && !pending_objects))
880 usage(rev_list_usage);
882 paths = get_pathspec(prefix, argv + i);
883 if (paths) {
884 limited = 1;
885 diff_tree_setup_paths(paths);
888 save_commit_buffer = verbose_header;
889 track_object_refs = 0;
891 if (!merge_order) {
892 sort_by_date(&list);
893 if (list && !limited && max_count == 1 &&
894 !tag_objects && !tree_objects && !blob_objects) {
895 show_commit(list->item);
896 return 0;
898 if (limited)
899 list = limit_list(list);
900 if (topo_order)
901 sort_in_topological_order(&list);
902 show_commit_list(list);
903 } else {
904 #ifndef NO_OPENSSL
905 if (sort_list_in_merge_order(list, &process_commit)) {
906 die("merge order sort failed\n");
908 #else
909 die("merge order sort unsupported, OpenSSL not linked");
910 #endif
913 return 0;