Add to documentation of git-update-index arguments and usage.
[git/dscho.git] / rev-list.c
blob2b82b8a96c71d9485028e69f4a3fdd703255c1f7
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 <commit-id>\n"
18 " --max-count=nr\n"
19 " --max-age=epoch\n"
20 " --min-age=epoch\n"
21 " --parents\n"
22 " --bisect\n"
23 " --objects\n"
24 " --unpacked\n"
25 " --header\n"
26 " --pretty\n"
27 " --no-merges\n"
28 " --merge-order [ --show-breaks ]\n"
29 " --topo-order";
31 static int dense = 1;
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;
55 if (show_breaks) {
56 commit_prefix = "| ";
57 if (commit->object.flags & DISCONTINUITY) {
58 commit_prefix = "^ ";
59 } else if (commit->object.flags & BOUNDARY) {
60 commit_prefix = "= ";
63 printf("%s%s", commit_prefix, sha1_to_hex(commit->object.sha1));
64 if (show_parents) {
65 struct commit_list *parents = commit->parents;
66 while (parents) {
67 printf(" %s", sha1_to_hex(parents->item->object.sha1));
68 parents = parents->next;
71 if (commit_format == CMIT_FMT_ONELINE)
72 putchar(' ');
73 else
74 putchar('\n');
76 if (verbose_header) {
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);
81 fflush(stdout);
84 static int rewrite_one(struct commit **pp)
86 for (;;) {
87 struct commit *p = *pp;
88 if (p->object.flags & (TREECHANGE | UNINTERESTING))
89 return 0;
90 if (!p->parents)
91 return -1;
92 *pp = p->parents->item;
96 static void rewrite_parents(struct commit *commit)
98 struct commit_list **pp = &commit->parents;
99 while (*pp) {
100 struct commit_list *parent = *pp;
101 if (rewrite_one(&parent->item) < 0) {
102 *pp = parent->next;
103 continue;
105 pp = &parent->next;
109 static int filter_commit(struct commit * commit)
111 if (stop_traversal && (commit->object.flags & BOUNDARY))
112 return STOP;
113 if (commit->object.flags & (UNINTERESTING|SHOWN))
114 return CONTINUE;
115 if (min_age != -1 && (commit->date > min_age))
116 return CONTINUE;
117 if (max_age != -1 && (commit->date < max_age)) {
118 stop_traversal=1;
119 return CONTINUE;
121 if (max_count != -1 && !max_count--)
122 return STOP;
123 if (no_merges && (commit->parents && commit->parents->next))
124 return CONTINUE;
125 if (paths && dense) {
126 if (!(commit->object.flags & TREECHANGE))
127 return CONTINUE;
128 rewrite_parents(commit);
130 return DO;
133 static int process_commit(struct commit * commit)
135 int action=filter_commit(commit);
137 if (action == STOP) {
138 return STOP;
141 if (action == CONTINUE) {
142 return CONTINUE;
145 show_commit(commit);
147 return CONTINUE;
150 static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
152 struct object_list *entry = xmalloc(sizeof(*entry));
153 entry->item = obj;
154 entry->next = *p;
155 entry->name = name;
156 *p = entry;
157 return &entry->next;
160 static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
162 struct object *obj = &blob->object;
164 if (!blob_objects)
165 return p;
166 if (obj->flags & (UNINTERESTING | SEEN))
167 return p;
168 obj->flags |= SEEN;
169 return add_object(obj, p, name);
172 static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
174 struct object *obj = &tree->object;
175 struct tree_entry_list *entry;
177 if (!tree_objects)
178 return p;
179 if (obj->flags & (UNINTERESTING | SEEN))
180 return p;
181 if (parse_tree(tree) < 0)
182 die("bad tree object %s", sha1_to_hex(obj->sha1));
183 obj->flags |= SEEN;
184 p = add_object(obj, p, name);
185 entry = tree->entries;
186 tree->entries = NULL;
187 while (entry) {
188 struct tree_entry_list *next = entry->next;
189 if (entry->directory)
190 p = process_tree(entry->item.tree, p, entry->name);
191 else
192 p = process_blob(entry->item.blob, p, entry->name);
193 free(entry);
194 entry = next;
196 return p;
199 static struct object_list *pending_objects = NULL;
201 static void show_commit_list(struct commit_list *list)
203 struct object_list *objects = NULL, **p = &objects, *pending;
204 while (list) {
205 struct commit *commit = pop_most_recent_commit(&list, SEEN);
207 p = process_tree(commit->tree, p, "");
208 if (process_commit(commit) == STOP)
209 break;
211 for (pending = pending_objects; pending; pending = pending->next) {
212 struct object *obj = pending->item;
213 const char *name = pending->name;
214 if (obj->flags & (UNINTERESTING | SEEN))
215 continue;
216 if (obj->type == tag_type) {
217 obj->flags |= SEEN;
218 p = add_object(obj, p, name);
219 continue;
221 if (obj->type == tree_type) {
222 p = process_tree((struct tree *)obj, p, name);
223 continue;
225 if (obj->type == blob_type) {
226 p = process_blob((struct blob *)obj, p, name);
227 continue;
229 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
231 while (objects) {
232 /* An object with name "foo\n0000000000000000000000000000000000000000"
233 * can be used confuse downstream git-pack-objects very badly.
235 const char *ep = strchr(objects->name, '\n');
236 if (ep) {
237 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
238 (int) (ep - objects->name),
239 objects->name);
241 else
242 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
243 objects = objects->next;
247 static void mark_blob_uninteresting(struct blob *blob)
249 if (!blob_objects)
250 return;
251 if (blob->object.flags & UNINTERESTING)
252 return;
253 blob->object.flags |= UNINTERESTING;
256 static void mark_tree_uninteresting(struct tree *tree)
258 struct object *obj = &tree->object;
259 struct tree_entry_list *entry;
261 if (!tree_objects)
262 return;
263 if (obj->flags & UNINTERESTING)
264 return;
265 obj->flags |= UNINTERESTING;
266 if (!has_sha1_file(obj->sha1))
267 return;
268 if (parse_tree(tree) < 0)
269 die("bad tree %s", sha1_to_hex(obj->sha1));
270 entry = tree->entries;
271 tree->entries = NULL;
272 while (entry) {
273 struct tree_entry_list *next = entry->next;
274 if (entry->directory)
275 mark_tree_uninteresting(entry->item.tree);
276 else
277 mark_blob_uninteresting(entry->item.blob);
278 free(entry);
279 entry = next;
283 static void mark_parents_uninteresting(struct commit *commit)
285 struct commit_list *parents = commit->parents;
287 while (parents) {
288 struct commit *commit = parents->item;
289 commit->object.flags |= UNINTERESTING;
292 * Normally we haven't parsed the parent
293 * yet, so we won't have a parent of a parent
294 * here. However, it may turn out that we've
295 * reached this commit some other way (where it
296 * wasn't uninteresting), in which case we need
297 * to mark its parents recursively too..
299 if (commit->parents)
300 mark_parents_uninteresting(commit);
303 * A missing commit is ok iff its parent is marked
304 * uninteresting.
306 * We just mark such a thing parsed, so that when
307 * it is popped next time around, we won't be trying
308 * to parse it and get an error.
310 if (!has_sha1_file(commit->object.sha1))
311 commit->object.parsed = 1;
312 parents = parents->next;
316 static int everybody_uninteresting(struct commit_list *orig)
318 struct commit_list *list = orig;
319 while (list) {
320 struct commit *commit = list->item;
321 list = list->next;
322 if (commit->object.flags & UNINTERESTING)
323 continue;
324 return 0;
326 return 1;
330 * This is a truly stupid algorithm, but it's only
331 * used for bisection, and we just don't care enough.
333 * We care just barely enough to avoid recursing for
334 * non-merge entries.
336 static int count_distance(struct commit_list *entry)
338 int nr = 0;
340 while (entry) {
341 struct commit *commit = entry->item;
342 struct commit_list *p;
344 if (commit->object.flags & (UNINTERESTING | COUNTED))
345 break;
346 nr++;
347 commit->object.flags |= COUNTED;
348 p = commit->parents;
349 entry = p;
350 if (p) {
351 p = p->next;
352 while (p) {
353 nr += count_distance(p);
354 p = p->next;
358 return nr;
361 static void clear_distance(struct commit_list *list)
363 while (list) {
364 struct commit *commit = list->item;
365 commit->object.flags &= ~COUNTED;
366 list = list->next;
370 static struct commit_list *find_bisection(struct commit_list *list)
372 int nr, closest;
373 struct commit_list *p, *best;
375 nr = 0;
376 p = list;
377 while (p) {
378 nr++;
379 p = p->next;
381 closest = 0;
382 best = list;
384 p = list;
385 while (p) {
386 int distance = count_distance(p);
387 clear_distance(list);
388 if (nr - distance < distance)
389 distance = nr - distance;
390 if (distance > closest) {
391 best = p;
392 closest = distance;
394 p = p->next;
396 if (best)
397 best->next = NULL;
398 return best;
401 static void mark_edges_uninteresting(struct commit_list *list)
403 for ( ; list; list = list->next) {
404 struct commit_list *parents = list->item->parents;
406 for ( ; parents; parents = parents->next) {
407 struct commit *commit = parents->item;
408 if (commit->object.flags & UNINTERESTING)
409 mark_tree_uninteresting(commit->tree);
414 static int is_different = 0;
416 static void file_add_remove(struct diff_options *options,
417 int addremove, unsigned mode,
418 const unsigned char *sha1,
419 const char *base, const char *path)
421 is_different = 1;
424 static void file_change(struct diff_options *options,
425 unsigned old_mode, unsigned new_mode,
426 const unsigned char *old_sha1,
427 const unsigned char *new_sha1,
428 const char *base, const char *path)
430 is_different = 1;
433 static struct diff_options diff_opt = {
434 .recursive = 1,
435 .add_remove = file_add_remove,
436 .change = file_change,
439 static int same_tree(struct tree *t1, struct tree *t2)
441 is_different = 0;
442 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "", &diff_opt) < 0)
443 return 0;
444 return !is_different;
447 static int same_tree_as_empty(struct tree *t1)
449 int retval;
450 void *tree;
451 struct tree_desc empty, real;
453 if (!t1)
454 return 0;
456 tree = read_object_with_reference(t1->object.sha1, "tree", &real.size, NULL);
457 if (!tree)
458 return 0;
459 real.buf = tree;
461 empty.buf = "";
462 empty.size = 0;
464 is_different = 0;
465 retval = diff_tree(&empty, &real, "", &diff_opt);
466 free(tree);
468 return retval >= 0 && !is_different;
471 static struct commit *try_to_simplify_merge(struct commit *commit, struct commit_list *parent)
473 if (!commit->tree)
474 return NULL;
476 while (parent) {
477 struct commit *p = parent->item;
478 parent = parent->next;
479 parse_commit(p);
480 if (!p->tree)
481 continue;
482 if (same_tree(commit->tree, p->tree))
483 return p;
485 return NULL;
488 static void add_parents_to_list(struct commit *commit, struct commit_list **list)
490 struct commit_list *parent = commit->parents;
493 * If the commit is uninteresting, don't try to
494 * prune parents - we want the maximal uninteresting
495 * set.
497 * Normally we haven't parsed the parent
498 * yet, so we won't have a parent of a parent
499 * here. However, it may turn out that we've
500 * reached this commit some other way (where it
501 * wasn't uninteresting), in which case we need
502 * to mark its parents recursively too..
504 if (commit->object.flags & UNINTERESTING) {
505 while (parent) {
506 struct commit *p = parent->item;
507 parent = parent->next;
508 parse_commit(p);
509 p->object.flags |= UNINTERESTING;
510 if (p->parents)
511 mark_parents_uninteresting(p);
512 if (p->object.flags & SEEN)
513 continue;
514 p->object.flags |= SEEN;
515 insert_by_date(p, list);
517 return;
521 * Ok, the commit wasn't uninteresting. If it
522 * is a merge, try to find the parent that has
523 * no differences in the path set if one exists.
525 if (paths && parent && parent->next) {
526 struct commit *preferred;
528 preferred = try_to_simplify_merge(commit, parent);
529 if (preferred) {
530 parent->item = preferred;
531 parent->next = NULL;
535 while (parent) {
536 struct commit *p = parent->item;
538 parent = parent->next;
540 parse_commit(p);
541 if (p->object.flags & SEEN)
542 continue;
543 p->object.flags |= SEEN;
544 insert_by_date(p, list);
548 static void compress_list(struct commit_list *list)
550 while (list) {
551 struct commit *commit = list->item;
552 struct commit_list *parent = commit->parents;
553 list = list->next;
555 if (!parent) {
556 if (!same_tree_as_empty(commit->tree))
557 commit->object.flags |= TREECHANGE;
558 continue;
562 * Exactly one parent? Check if it leaves the tree
563 * unchanged
565 if (!parent->next) {
566 struct tree *t1 = commit->tree;
567 struct tree *t2 = parent->item->tree;
568 if (!t1 || !t2 || same_tree(t1, t2))
569 continue;
571 commit->object.flags |= TREECHANGE;
575 static struct commit_list *limit_list(struct commit_list *list)
577 struct commit_list *newlist = NULL;
578 struct commit_list **p = &newlist;
579 while (list) {
580 struct commit_list *entry = list;
581 struct commit *commit = list->item;
582 struct object *obj = &commit->object;
584 list = list->next;
585 free(entry);
587 if (max_age != -1 && (commit->date < max_age))
588 obj->flags |= UNINTERESTING;
589 if (unpacked && has_sha1_pack(obj->sha1))
590 obj->flags |= UNINTERESTING;
591 add_parents_to_list(commit, &list);
592 if (obj->flags & UNINTERESTING) {
593 mark_parents_uninteresting(commit);
594 if (everybody_uninteresting(list))
595 break;
596 continue;
598 if (min_age != -1 && (commit->date > min_age))
599 continue;
600 p = &commit_list_insert(commit, p)->next;
602 if (tree_objects)
603 mark_edges_uninteresting(newlist);
604 if (paths && dense)
605 compress_list(newlist);
606 if (bisect_list)
607 newlist = find_bisection(newlist);
608 return newlist;
611 static void add_pending_object(struct object *obj, const char *name)
613 add_object(obj, &pending_objects, name);
616 static struct commit *get_commit_reference(const char *name, const unsigned char *sha1, unsigned int flags)
618 struct object *object;
620 object = parse_object(sha1);
621 if (!object)
622 die("bad object %s", name);
625 * Tag object? Look what it points to..
627 while (object->type == tag_type) {
628 struct tag *tag = (struct tag *) object;
629 object->flags |= flags;
630 if (tag_objects && !(object->flags & UNINTERESTING))
631 add_pending_object(object, tag->tag);
632 object = parse_object(tag->tagged->sha1);
633 if (!object)
634 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
638 * Commit object? Just return it, we'll do all the complex
639 * reachability crud.
641 if (object->type == commit_type) {
642 struct commit *commit = (struct commit *)object;
643 object->flags |= flags;
644 if (parse_commit(commit) < 0)
645 die("unable to parse commit %s", name);
646 if (flags & UNINTERESTING)
647 mark_parents_uninteresting(commit);
648 return commit;
652 * Tree object? Either mark it uniniteresting, or add it
653 * to the list of objects to look at later..
655 if (object->type == tree_type) {
656 struct tree *tree = (struct tree *)object;
657 if (!tree_objects)
658 return NULL;
659 if (flags & UNINTERESTING) {
660 mark_tree_uninteresting(tree);
661 return NULL;
663 add_pending_object(object, "");
664 return NULL;
668 * Blob object? You know the drill by now..
670 if (object->type == blob_type) {
671 struct blob *blob = (struct blob *)object;
672 if (!blob_objects)
673 return NULL;
674 if (flags & UNINTERESTING) {
675 mark_blob_uninteresting(blob);
676 return NULL;
678 add_pending_object(object, "");
679 return NULL;
681 die("%s is unknown object", name);
684 static void handle_one_commit(struct commit *com, struct commit_list **lst)
686 if (!com || com->object.flags & SEEN)
687 return;
688 com->object.flags |= SEEN;
689 commit_list_insert(com, lst);
692 /* for_each_ref() callback does not allow user data -- Yuck. */
693 static struct commit_list **global_lst;
695 static int include_one_commit(const char *path, const unsigned char *sha1)
697 struct commit *com = get_commit_reference(path, sha1, 0);
698 handle_one_commit(com, global_lst);
699 return 0;
702 static void handle_all(struct commit_list **lst)
704 global_lst = lst;
705 for_each_ref(include_one_commit);
706 global_lst = NULL;
709 int main(int argc, const char **argv)
711 const char *prefix = setup_git_directory();
712 struct commit_list *list = NULL;
713 int i, limited = 0;
715 for (i = 1 ; i < argc; i++) {
716 int flags;
717 const char *arg = argv[i];
718 char *dotdot;
719 struct commit *commit;
720 unsigned char sha1[20];
722 if (!strncmp(arg, "--max-count=", 12)) {
723 max_count = atoi(arg + 12);
724 continue;
726 if (!strncmp(arg, "--max-age=", 10)) {
727 max_age = atoi(arg + 10);
728 limited = 1;
729 continue;
731 if (!strncmp(arg, "--min-age=", 10)) {
732 min_age = atoi(arg + 10);
733 limited = 1;
734 continue;
736 if (!strcmp(arg, "--header")) {
737 verbose_header = 1;
738 continue;
740 if (!strncmp(arg, "--pretty", 8)) {
741 commit_format = get_commit_format(arg+8);
742 verbose_header = 1;
743 hdr_termination = '\n';
744 if (commit_format == CMIT_FMT_ONELINE)
745 commit_prefix = "";
746 else
747 commit_prefix = "commit ";
748 continue;
750 if (!strncmp(arg, "--no-merges", 11)) {
751 no_merges = 1;
752 continue;
754 if (!strcmp(arg, "--parents")) {
755 show_parents = 1;
756 continue;
758 if (!strcmp(arg, "--bisect")) {
759 bisect_list = 1;
760 continue;
762 if (!strcmp(arg, "--all")) {
763 handle_all(&list);
764 continue;
766 if (!strcmp(arg, "--objects")) {
767 tag_objects = 1;
768 tree_objects = 1;
769 blob_objects = 1;
770 continue;
772 if (!strcmp(arg, "--unpacked")) {
773 unpacked = 1;
774 limited = 1;
775 continue;
777 if (!strcmp(arg, "--merge-order")) {
778 merge_order = 1;
779 continue;
781 if (!strcmp(arg, "--show-breaks")) {
782 show_breaks = 1;
783 continue;
785 if (!strcmp(arg, "--topo-order")) {
786 topo_order = 1;
787 limited = 1;
788 continue;
790 if (!strcmp(arg, "--dense")) {
791 dense = 1;
792 continue;
794 if (!strcmp(arg, "--sparse")) {
795 dense = 0;
796 continue;
798 if (!strcmp(arg, "--")) {
799 i++;
800 break;
803 if (show_breaks && !merge_order)
804 usage(rev_list_usage);
806 flags = 0;
807 dotdot = strstr(arg, "..");
808 if (dotdot) {
809 unsigned char from_sha1[20];
810 char *next = dotdot + 2;
811 *dotdot = 0;
812 if (!*next)
813 next = "HEAD";
814 if (!get_sha1(arg, from_sha1) && !get_sha1(next, sha1)) {
815 struct commit *exclude;
816 struct commit *include;
818 exclude = get_commit_reference(arg, from_sha1, UNINTERESTING);
819 include = get_commit_reference(next, sha1, 0);
820 if (!exclude || !include)
821 die("Invalid revision range %s..%s", arg, next);
822 limited = 1;
823 handle_one_commit(exclude, &list);
824 handle_one_commit(include, &list);
825 continue;
827 *dotdot = '.';
829 if (*arg == '^') {
830 flags = UNINTERESTING;
831 arg++;
832 limited = 1;
834 if (get_sha1(arg, sha1) < 0)
835 break;
836 commit = get_commit_reference(arg, sha1, flags);
837 handle_one_commit(commit, &list);
840 if (!list)
841 usage(rev_list_usage);
843 paths = get_pathspec(prefix, argv + i);
844 if (paths) {
845 limited = 1;
846 diff_tree_setup_paths(paths);
849 save_commit_buffer = verbose_header;
850 track_object_refs = 0;
852 if (!merge_order) {
853 sort_by_date(&list);
854 if (list && !limited && max_count == 1 &&
855 !tag_objects && !tree_objects && !blob_objects) {
856 show_commit(list->item);
857 return 0;
859 if (limited)
860 list = limit_list(list);
861 if (topo_order)
862 sort_in_topological_order(&list);
863 show_commit_list(list);
864 } else {
865 #ifndef NO_OPENSSL
866 if (sort_list_in_merge_order(list, &process_commit)) {
867 die("merge order sort failed\n");
869 #else
870 die("merge order sort unsupported, OpenSSL not linked");
871 #endif
874 return 0;