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 #define TMP_MARK (1u << 5) /* for isolated cases; clean after use */
17 static const char rev_list_usage
[] =
18 "git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
28 " --merge-order [ --show-breaks ]\n"
30 " formatting output:\n"
34 " --header | --pretty\n"
40 static int unpacked
= 0;
41 static int bisect_list
= 0;
42 static int tag_objects
= 0;
43 static int tree_objects
= 0;
44 static int blob_objects
= 0;
45 static int verbose_header
= 0;
46 static int show_parents
= 0;
47 static int hdr_termination
= 0;
48 static const char *commit_prefix
= "";
49 static unsigned long max_age
= -1;
50 static unsigned long min_age
= -1;
51 static int max_count
= -1;
52 static enum cmit_fmt commit_format
= CMIT_FMT_RAW
;
53 static int merge_order
= 0;
54 static int show_breaks
= 0;
55 static int stop_traversal
= 0;
56 static int topo_order
= 0;
57 static int no_merges
= 0;
58 static const char **paths
= NULL
;
59 static int remove_empty_trees
= 0;
61 static void show_commit(struct commit
*commit
)
63 commit
->object
.flags
|= SHOWN
;
66 if (commit
->object
.flags
& DISCONTINUITY
) {
68 } else if (commit
->object
.flags
& BOUNDARY
) {
72 printf("%s%s", commit_prefix
, sha1_to_hex(commit
->object
.sha1
));
74 struct commit_list
*parents
= commit
->parents
;
76 struct object
*o
= &(parents
->item
->object
);
77 parents
= parents
->next
;
78 if (o
->flags
& TMP_MARK
)
80 printf(" %s", sha1_to_hex(o
->sha1
));
83 /* TMP_MARK is a general purpose flag that can
84 * be used locally, but the user should clean
85 * things up after it is done with them.
87 for (parents
= commit
->parents
;
89 parents
= parents
->next
)
90 parents
->item
->object
.flags
&= ~TMP_MARK
;
92 if (commit_format
== CMIT_FMT_ONELINE
)
98 static char pretty_header
[16384];
99 pretty_print_commit(commit_format
, commit
, ~0, pretty_header
, sizeof(pretty_header
), 0);
100 printf("%s%c", pretty_header
, hdr_termination
);
105 static int rewrite_one(struct commit
**pp
)
108 struct commit
*p
= *pp
;
109 if (p
->object
.flags
& (TREECHANGE
| UNINTERESTING
))
113 *pp
= p
->parents
->item
;
117 static void rewrite_parents(struct commit
*commit
)
119 struct commit_list
**pp
= &commit
->parents
;
121 struct commit_list
*parent
= *pp
;
122 if (rewrite_one(&parent
->item
) < 0) {
130 static int filter_commit(struct commit
* commit
)
132 if (stop_traversal
&& (commit
->object
.flags
& BOUNDARY
))
134 if (commit
->object
.flags
& (UNINTERESTING
|SHOWN
))
136 if (min_age
!= -1 && (commit
->date
> min_age
))
138 if (max_age
!= -1 && (commit
->date
< max_age
)) {
142 if (no_merges
&& (commit
->parents
&& commit
->parents
->next
))
144 if (paths
&& dense
) {
145 if (!(commit
->object
.flags
& TREECHANGE
))
147 rewrite_parents(commit
);
152 static int process_commit(struct commit
* commit
)
154 int action
=filter_commit(commit
);
156 if (action
== STOP
) {
160 if (action
== CONTINUE
) {
164 if (max_count
!= -1 && !max_count
--)
172 static struct object_list
**add_object(struct object
*obj
, struct object_list
**p
, const char *name
)
174 struct object_list
*entry
= xmalloc(sizeof(*entry
));
182 static struct object_list
**process_blob(struct blob
*blob
, struct object_list
**p
, const char *name
)
184 struct object
*obj
= &blob
->object
;
188 if (obj
->flags
& (UNINTERESTING
| SEEN
))
191 return add_object(obj
, p
, name
);
194 static struct object_list
**process_tree(struct tree
*tree
, struct object_list
**p
, const char *name
)
196 struct object
*obj
= &tree
->object
;
197 struct tree_entry_list
*entry
;
201 if (obj
->flags
& (UNINTERESTING
| SEEN
))
203 if (parse_tree(tree
) < 0)
204 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
206 p
= add_object(obj
, p
, name
);
207 entry
= tree
->entries
;
208 tree
->entries
= NULL
;
210 struct tree_entry_list
*next
= entry
->next
;
211 if (entry
->directory
)
212 p
= process_tree(entry
->item
.tree
, p
, entry
->name
);
214 p
= process_blob(entry
->item
.blob
, p
, entry
->name
);
221 static struct object_list
*pending_objects
= NULL
;
223 static void show_commit_list(struct commit_list
*list
)
225 struct object_list
*objects
= NULL
, **p
= &objects
, *pending
;
227 struct commit
*commit
= pop_most_recent_commit(&list
, SEEN
);
229 p
= process_tree(commit
->tree
, p
, "");
230 if (process_commit(commit
) == STOP
)
233 for (pending
= pending_objects
; pending
; pending
= pending
->next
) {
234 struct object
*obj
= pending
->item
;
235 const char *name
= pending
->name
;
236 if (obj
->flags
& (UNINTERESTING
| SEEN
))
238 if (obj
->type
== tag_type
) {
240 p
= add_object(obj
, p
, name
);
243 if (obj
->type
== tree_type
) {
244 p
= process_tree((struct tree
*)obj
, p
, name
);
247 if (obj
->type
== blob_type
) {
248 p
= process_blob((struct blob
*)obj
, p
, name
);
251 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
254 /* An object with name "foo\n0000000000000000000000000000000000000000"
255 * can be used confuse downstream git-pack-objects very badly.
257 const char *ep
= strchr(objects
->name
, '\n');
259 printf("%s %.*s\n", sha1_to_hex(objects
->item
->sha1
),
260 (int) (ep
- objects
->name
),
264 printf("%s %s\n", sha1_to_hex(objects
->item
->sha1
), objects
->name
);
265 objects
= objects
->next
;
269 static void mark_blob_uninteresting(struct blob
*blob
)
273 if (blob
->object
.flags
& UNINTERESTING
)
275 blob
->object
.flags
|= UNINTERESTING
;
278 static void mark_tree_uninteresting(struct tree
*tree
)
280 struct object
*obj
= &tree
->object
;
281 struct tree_entry_list
*entry
;
285 if (obj
->flags
& UNINTERESTING
)
287 obj
->flags
|= UNINTERESTING
;
288 if (!has_sha1_file(obj
->sha1
))
290 if (parse_tree(tree
) < 0)
291 die("bad tree %s", sha1_to_hex(obj
->sha1
));
292 entry
= tree
->entries
;
293 tree
->entries
= NULL
;
295 struct tree_entry_list
*next
= entry
->next
;
296 if (entry
->directory
)
297 mark_tree_uninteresting(entry
->item
.tree
);
299 mark_blob_uninteresting(entry
->item
.blob
);
305 static void mark_parents_uninteresting(struct commit
*commit
)
307 struct commit_list
*parents
= commit
->parents
;
310 struct commit
*commit
= parents
->item
;
311 commit
->object
.flags
|= UNINTERESTING
;
314 * Normally we haven't parsed the parent
315 * yet, so we won't have a parent of a parent
316 * here. However, it may turn out that we've
317 * reached this commit some other way (where it
318 * wasn't uninteresting), in which case we need
319 * to mark its parents recursively too..
322 mark_parents_uninteresting(commit
);
325 * A missing commit is ok iff its parent is marked
328 * We just mark such a thing parsed, so that when
329 * it is popped next time around, we won't be trying
330 * to parse it and get an error.
332 if (!has_sha1_file(commit
->object
.sha1
))
333 commit
->object
.parsed
= 1;
334 parents
= parents
->next
;
338 static int everybody_uninteresting(struct commit_list
*orig
)
340 struct commit_list
*list
= orig
;
342 struct commit
*commit
= list
->item
;
344 if (commit
->object
.flags
& UNINTERESTING
)
352 * This is a truly stupid algorithm, but it's only
353 * used for bisection, and we just don't care enough.
355 * We care just barely enough to avoid recursing for
358 static int count_distance(struct commit_list
*entry
)
363 struct commit
*commit
= entry
->item
;
364 struct commit_list
*p
;
366 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
368 if (!paths
|| (commit
->object
.flags
& TREECHANGE
))
370 commit
->object
.flags
|= COUNTED
;
376 nr
+= count_distance(p
);
385 static void clear_distance(struct commit_list
*list
)
388 struct commit
*commit
= list
->item
;
389 commit
->object
.flags
&= ~COUNTED
;
394 static struct commit_list
*find_bisection(struct commit_list
*list
)
397 struct commit_list
*p
, *best
;
402 if (!paths
|| (p
->item
->object
.flags
& TREECHANGE
))
409 for (p
= list
; p
; p
= p
->next
) {
412 if (paths
&& !(p
->item
->object
.flags
& TREECHANGE
))
415 distance
= count_distance(p
);
416 clear_distance(list
);
417 if (nr
- distance
< distance
)
418 distance
= nr
- distance
;
419 if (distance
> closest
) {
429 static void mark_edges_uninteresting(struct commit_list
*list
)
431 for ( ; list
; list
= list
->next
) {
432 struct commit_list
*parents
= list
->item
->parents
;
434 for ( ; parents
; parents
= parents
->next
) {
435 struct commit
*commit
= parents
->item
;
436 if (commit
->object
.flags
& UNINTERESTING
)
437 mark_tree_uninteresting(commit
->tree
);
444 #define TREE_DIFFERENT 2
445 static int tree_difference
= TREE_SAME
;
447 static void file_add_remove(struct diff_options
*options
,
448 int addremove
, unsigned mode
,
449 const unsigned char *sha1
,
450 const char *base
, const char *path
)
452 int diff
= TREE_DIFFERENT
;
455 * Is it an add of a new file? It means that
456 * the old tree didn't have it at all, so we
457 * will turn "TREE_SAME" -> "TREE_NEW", but
458 * leave any "TREE_DIFFERENT" alone (and if
459 * it already was "TREE_NEW", we'll keep it
460 * "TREE_NEW" of course).
462 if (addremove
== '+') {
463 diff
= tree_difference
;
464 if (diff
!= TREE_SAME
)
468 tree_difference
= diff
;
471 static void file_change(struct diff_options
*options
,
472 unsigned old_mode
, unsigned new_mode
,
473 const unsigned char *old_sha1
,
474 const unsigned char *new_sha1
,
475 const char *base
, const char *path
)
477 tree_difference
= TREE_DIFFERENT
;
480 static struct diff_options diff_opt
= {
482 .add_remove
= file_add_remove
,
483 .change
= file_change
,
486 static int compare_tree(struct tree
*t1
, struct tree
*t2
)
491 return TREE_DIFFERENT
;
492 tree_difference
= TREE_SAME
;
493 if (diff_tree_sha1(t1
->object
.sha1
, t2
->object
.sha1
, "", &diff_opt
) < 0)
494 return TREE_DIFFERENT
;
495 return tree_difference
;
498 static int same_tree_as_empty(struct tree
*t1
)
502 struct tree_desc empty
, real
;
507 tree
= read_object_with_reference(t1
->object
.sha1
, "tree", &real
.size
, NULL
);
516 retval
= diff_tree(&empty
, &real
, "", &diff_opt
);
519 return retval
>= 0 && !tree_difference
;
522 static void try_to_simplify_commit(struct commit
*commit
)
524 struct commit_list
**pp
, *parent
;
529 if (!commit
->parents
) {
530 if (!same_tree_as_empty(commit
->tree
))
531 commit
->object
.flags
|= TREECHANGE
;
535 pp
= &commit
->parents
;
536 while ((parent
= *pp
) != NULL
) {
537 struct commit
*p
= parent
->item
;
539 if (p
->object
.flags
& UNINTERESTING
) {
545 switch (compare_tree(p
->tree
, commit
->tree
)) {
548 commit
->parents
= parent
;
552 if (remove_empty_trees
&& same_tree_as_empty(p
->tree
)) {
561 die("bad tree compare for commit %s", sha1_to_hex(commit
->object
.sha1
));
563 commit
->object
.flags
|= TREECHANGE
;
566 static void add_parents_to_list(struct commit
*commit
, struct commit_list
**list
)
568 struct commit_list
*parent
= commit
->parents
;
571 * If the commit is uninteresting, don't try to
572 * prune parents - we want the maximal uninteresting
575 * Normally we haven't parsed the parent
576 * yet, so we won't have a parent of a parent
577 * here. However, it may turn out that we've
578 * reached this commit some other way (where it
579 * wasn't uninteresting), in which case we need
580 * to mark its parents recursively too..
582 if (commit
->object
.flags
& UNINTERESTING
) {
584 struct commit
*p
= parent
->item
;
585 parent
= parent
->next
;
587 p
->object
.flags
|= UNINTERESTING
;
589 mark_parents_uninteresting(p
);
590 if (p
->object
.flags
& SEEN
)
592 p
->object
.flags
|= SEEN
;
593 insert_by_date(p
, list
);
599 * Ok, the commit wasn't uninteresting. Try to
600 * simplify the commit history and find the parent
601 * that has no differences in the path set if one exists.
604 try_to_simplify_commit(commit
);
606 parent
= commit
->parents
;
608 struct commit
*p
= parent
->item
;
610 parent
= parent
->next
;
613 if (p
->object
.flags
& SEEN
)
615 p
->object
.flags
|= SEEN
;
616 insert_by_date(p
, list
);
620 static struct commit_list
*limit_list(struct commit_list
*list
)
622 struct commit_list
*newlist
= NULL
;
623 struct commit_list
**p
= &newlist
;
625 struct commit_list
*entry
= list
;
626 struct commit
*commit
= list
->item
;
627 struct object
*obj
= &commit
->object
;
632 if (max_age
!= -1 && (commit
->date
< max_age
))
633 obj
->flags
|= UNINTERESTING
;
634 if (unpacked
&& has_sha1_pack(obj
->sha1
))
635 obj
->flags
|= UNINTERESTING
;
636 add_parents_to_list(commit
, &list
);
637 if (obj
->flags
& UNINTERESTING
) {
638 mark_parents_uninteresting(commit
);
639 if (everybody_uninteresting(list
))
643 if (min_age
!= -1 && (commit
->date
> min_age
))
645 p
= &commit_list_insert(commit
, p
)->next
;
648 mark_edges_uninteresting(newlist
);
650 newlist
= find_bisection(newlist
);
654 static void add_pending_object(struct object
*obj
, const char *name
)
656 add_object(obj
, &pending_objects
, name
);
659 static struct commit
*get_commit_reference(const char *name
, const unsigned char *sha1
, unsigned int flags
)
661 struct object
*object
;
663 object
= parse_object(sha1
);
665 die("bad object %s", name
);
668 * Tag object? Look what it points to..
670 while (object
->type
== tag_type
) {
671 struct tag
*tag
= (struct tag
*) object
;
672 object
->flags
|= flags
;
673 if (tag_objects
&& !(object
->flags
& UNINTERESTING
))
674 add_pending_object(object
, tag
->tag
);
675 object
= parse_object(tag
->tagged
->sha1
);
677 die("bad object %s", sha1_to_hex(tag
->tagged
->sha1
));
681 * Commit object? Just return it, we'll do all the complex
684 if (object
->type
== commit_type
) {
685 struct commit
*commit
= (struct commit
*)object
;
686 object
->flags
|= flags
;
687 if (parse_commit(commit
) < 0)
688 die("unable to parse commit %s", name
);
689 if (flags
& UNINTERESTING
)
690 mark_parents_uninteresting(commit
);
695 * Tree object? Either mark it uniniteresting, or add it
696 * to the list of objects to look at later..
698 if (object
->type
== tree_type
) {
699 struct tree
*tree
= (struct tree
*)object
;
702 if (flags
& UNINTERESTING
) {
703 mark_tree_uninteresting(tree
);
706 add_pending_object(object
, "");
711 * Blob object? You know the drill by now..
713 if (object
->type
== blob_type
) {
714 struct blob
*blob
= (struct blob
*)object
;
717 if (flags
& UNINTERESTING
) {
718 mark_blob_uninteresting(blob
);
721 add_pending_object(object
, "");
724 die("%s is unknown object", name
);
727 static void handle_one_commit(struct commit
*com
, struct commit_list
**lst
)
729 if (!com
|| com
->object
.flags
& SEEN
)
731 com
->object
.flags
|= SEEN
;
732 commit_list_insert(com
, lst
);
735 /* for_each_ref() callback does not allow user data -- Yuck. */
736 static struct commit_list
**global_lst
;
738 static int include_one_commit(const char *path
, const unsigned char *sha1
)
740 struct commit
*com
= get_commit_reference(path
, sha1
, 0);
741 handle_one_commit(com
, global_lst
);
745 static void handle_all(struct commit_list
**lst
)
748 for_each_ref(include_one_commit
);
752 int main(int argc
, const char **argv
)
754 const char *prefix
= setup_git_directory();
755 struct commit_list
*list
= NULL
;
758 for (i
= 1 ; i
< argc
; i
++) {
760 const char *arg
= argv
[i
];
762 struct commit
*commit
;
763 unsigned char sha1
[20];
765 /* accept -<digit>, like traditilnal "head" */
766 if ((*arg
== '-') && isdigit(arg
[1])) {
767 max_count
= atoi(arg
+ 1);
770 if (!strcmp(arg
, "-n")) {
772 die("-n requires an argument");
773 max_count
= atoi(argv
[i
]);
776 if (!strncmp(arg
,"-n",2)) {
777 max_count
= atoi(arg
+ 2);
780 if (!strncmp(arg
, "--max-count=", 12)) {
781 max_count
= atoi(arg
+ 12);
784 if (!strncmp(arg
, "--max-age=", 10)) {
785 max_age
= atoi(arg
+ 10);
789 if (!strncmp(arg
, "--min-age=", 10)) {
790 min_age
= atoi(arg
+ 10);
794 if (!strcmp(arg
, "--header")) {
798 if (!strncmp(arg
, "--pretty", 8)) {
799 commit_format
= get_commit_format(arg
+8);
801 hdr_termination
= '\n';
802 if (commit_format
== CMIT_FMT_ONELINE
)
805 commit_prefix
= "commit ";
808 if (!strncmp(arg
, "--no-merges", 11)) {
812 if (!strcmp(arg
, "--parents")) {
816 if (!strcmp(arg
, "--bisect")) {
820 if (!strcmp(arg
, "--all")) {
824 if (!strcmp(arg
, "--objects")) {
830 if (!strcmp(arg
, "--unpacked")) {
835 if (!strcmp(arg
, "--merge-order")) {
839 if (!strcmp(arg
, "--show-breaks")) {
843 if (!strcmp(arg
, "--topo-order")) {
848 if (!strcmp(arg
, "--dense")) {
852 if (!strcmp(arg
, "--sparse")) {
856 if (!strcmp(arg
, "--remove-empty")) {
857 remove_empty_trees
= 1;
860 if (!strcmp(arg
, "--")) {
865 if (show_breaks
&& !merge_order
)
866 usage(rev_list_usage
);
869 dotdot
= strstr(arg
, "..");
871 unsigned char from_sha1
[20];
872 char *next
= dotdot
+ 2;
876 if (!get_sha1(arg
, from_sha1
) && !get_sha1(next
, sha1
)) {
877 struct commit
*exclude
;
878 struct commit
*include
;
880 exclude
= get_commit_reference(arg
, from_sha1
, UNINTERESTING
);
881 include
= get_commit_reference(next
, sha1
, 0);
882 if (!exclude
|| !include
)
883 die("Invalid revision range %s..%s", arg
, next
);
885 handle_one_commit(exclude
, &list
);
886 handle_one_commit(include
, &list
);
892 flags
= UNINTERESTING
;
896 if (get_sha1(arg
, sha1
) < 0) {
898 if (lstat(arg
, &st
) < 0)
899 die("'%s': %s", arg
, strerror(errno
));
902 commit
= get_commit_reference(arg
, sha1
, flags
);
903 handle_one_commit(commit
, &list
);
907 (!(tag_objects
||tree_objects
||blob_objects
) && !pending_objects
))
908 usage(rev_list_usage
);
910 paths
= get_pathspec(prefix
, argv
+ i
);
913 diff_tree_setup_paths(paths
);
916 save_commit_buffer
= verbose_header
;
917 track_object_refs
= 0;
921 if (list
&& !limited
&& max_count
== 1 &&
922 !tag_objects
&& !tree_objects
&& !blob_objects
) {
923 show_commit(list
->item
);
927 list
= limit_list(list
);
929 sort_in_topological_order(&list
);
930 show_commit_list(list
);
933 if (sort_list_in_merge_order(list
, &process_commit
)) {
934 die("merge order sort failed\n");
937 die("merge order sort unsupported, OpenSSL not linked");