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"
35 " --abbrev=nr | --no-abbrev\n"
41 static int unpacked
= 0;
42 static int bisect_list
= 0;
43 static int tag_objects
= 0;
44 static int tree_objects
= 0;
45 static int blob_objects
= 0;
46 static int verbose_header
= 0;
47 static int abbrev
= DEFAULT_ABBREV
;
48 static int show_parents
= 0;
49 static int hdr_termination
= 0;
50 static const char *commit_prefix
= "";
51 static unsigned long max_age
= -1;
52 static unsigned long min_age
= -1;
53 static int max_count
= -1;
54 static enum cmit_fmt commit_format
= CMIT_FMT_RAW
;
55 static int merge_order
= 0;
56 static int show_breaks
= 0;
57 static int stop_traversal
= 0;
58 static int topo_order
= 0;
59 static int no_merges
= 0;
60 static const char **paths
= NULL
;
61 static int remove_empty_trees
= 0;
63 static void show_commit(struct commit
*commit
)
65 commit
->object
.flags
|= SHOWN
;
68 if (commit
->object
.flags
& DISCONTINUITY
) {
70 } else if (commit
->object
.flags
& BOUNDARY
) {
74 printf("%s%s", commit_prefix
, sha1_to_hex(commit
->object
.sha1
));
76 struct commit_list
*parents
= commit
->parents
;
78 struct object
*o
= &(parents
->item
->object
);
79 parents
= parents
->next
;
80 if (o
->flags
& TMP_MARK
)
82 printf(" %s", sha1_to_hex(o
->sha1
));
85 /* TMP_MARK is a general purpose flag that can
86 * be used locally, but the user should clean
87 * things up after it is done with them.
89 for (parents
= commit
->parents
;
91 parents
= parents
->next
)
92 parents
->item
->object
.flags
&= ~TMP_MARK
;
94 if (commit_format
== CMIT_FMT_ONELINE
)
100 static char pretty_header
[16384];
101 pretty_print_commit(commit_format
, commit
, ~0, pretty_header
, sizeof(pretty_header
), abbrev
);
102 printf("%s%c", pretty_header
, hdr_termination
);
107 static int rewrite_one(struct commit
**pp
)
110 struct commit
*p
= *pp
;
111 if (p
->object
.flags
& (TREECHANGE
| UNINTERESTING
))
115 *pp
= p
->parents
->item
;
119 static void rewrite_parents(struct commit
*commit
)
121 struct commit_list
**pp
= &commit
->parents
;
123 struct commit_list
*parent
= *pp
;
124 if (rewrite_one(&parent
->item
) < 0) {
132 static int filter_commit(struct commit
* commit
)
134 if (stop_traversal
&& (commit
->object
.flags
& BOUNDARY
))
136 if (commit
->object
.flags
& (UNINTERESTING
|SHOWN
))
138 if (min_age
!= -1 && (commit
->date
> min_age
))
140 if (max_age
!= -1 && (commit
->date
< max_age
)) {
144 if (no_merges
&& (commit
->parents
&& commit
->parents
->next
))
146 if (paths
&& dense
) {
147 if (!(commit
->object
.flags
& TREECHANGE
))
149 rewrite_parents(commit
);
154 static int process_commit(struct commit
* commit
)
156 int action
=filter_commit(commit
);
158 if (action
== STOP
) {
162 if (action
== CONTINUE
) {
166 if (max_count
!= -1 && !max_count
--)
174 static struct object_list
**add_object(struct object
*obj
, struct object_list
**p
, const char *name
)
176 struct object_list
*entry
= xmalloc(sizeof(*entry
));
184 static struct object_list
**process_blob(struct blob
*blob
, struct object_list
**p
, const char *name
)
186 struct object
*obj
= &blob
->object
;
190 if (obj
->flags
& (UNINTERESTING
| SEEN
))
193 return add_object(obj
, p
, name
);
196 static struct object_list
**process_tree(struct tree
*tree
, struct object_list
**p
, const char *name
)
198 struct object
*obj
= &tree
->object
;
199 struct tree_entry_list
*entry
;
203 if (obj
->flags
& (UNINTERESTING
| SEEN
))
205 if (parse_tree(tree
) < 0)
206 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
208 p
= add_object(obj
, p
, name
);
209 entry
= tree
->entries
;
210 tree
->entries
= NULL
;
212 struct tree_entry_list
*next
= entry
->next
;
213 if (entry
->directory
)
214 p
= process_tree(entry
->item
.tree
, p
, entry
->name
);
216 p
= process_blob(entry
->item
.blob
, p
, entry
->name
);
223 static struct object_list
*pending_objects
= NULL
;
225 static void show_commit_list(struct commit_list
*list
)
227 struct object_list
*objects
= NULL
, **p
= &objects
, *pending
;
229 struct commit
*commit
= pop_most_recent_commit(&list
, SEEN
);
231 p
= process_tree(commit
->tree
, p
, "");
232 if (process_commit(commit
) == STOP
)
235 for (pending
= pending_objects
; pending
; pending
= pending
->next
) {
236 struct object
*obj
= pending
->item
;
237 const char *name
= pending
->name
;
238 if (obj
->flags
& (UNINTERESTING
| SEEN
))
240 if (obj
->type
== tag_type
) {
242 p
= add_object(obj
, p
, name
);
245 if (obj
->type
== tree_type
) {
246 p
= process_tree((struct tree
*)obj
, p
, name
);
249 if (obj
->type
== blob_type
) {
250 p
= process_blob((struct blob
*)obj
, p
, name
);
253 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
256 /* An object with name "foo\n0000000000000000000000000000000000000000"
257 * can be used confuse downstream git-pack-objects very badly.
259 const char *ep
= strchr(objects
->name
, '\n');
261 printf("%s %.*s\n", sha1_to_hex(objects
->item
->sha1
),
262 (int) (ep
- objects
->name
),
266 printf("%s %s\n", sha1_to_hex(objects
->item
->sha1
), objects
->name
);
267 objects
= objects
->next
;
271 static void mark_blob_uninteresting(struct blob
*blob
)
275 if (blob
->object
.flags
& UNINTERESTING
)
277 blob
->object
.flags
|= UNINTERESTING
;
280 static void mark_tree_uninteresting(struct tree
*tree
)
282 struct object
*obj
= &tree
->object
;
283 struct tree_entry_list
*entry
;
287 if (obj
->flags
& UNINTERESTING
)
289 obj
->flags
|= UNINTERESTING
;
290 if (!has_sha1_file(obj
->sha1
))
292 if (parse_tree(tree
) < 0)
293 die("bad tree %s", sha1_to_hex(obj
->sha1
));
294 entry
= tree
->entries
;
295 tree
->entries
= NULL
;
297 struct tree_entry_list
*next
= entry
->next
;
298 if (entry
->directory
)
299 mark_tree_uninteresting(entry
->item
.tree
);
301 mark_blob_uninteresting(entry
->item
.blob
);
307 static void mark_parents_uninteresting(struct commit
*commit
)
309 struct commit_list
*parents
= commit
->parents
;
312 struct commit
*commit
= parents
->item
;
313 commit
->object
.flags
|= UNINTERESTING
;
316 * Normally we haven't parsed the parent
317 * yet, so we won't have a parent of a parent
318 * here. However, it may turn out that we've
319 * reached this commit some other way (where it
320 * wasn't uninteresting), in which case we need
321 * to mark its parents recursively too..
324 mark_parents_uninteresting(commit
);
327 * A missing commit is ok iff its parent is marked
330 * We just mark such a thing parsed, so that when
331 * it is popped next time around, we won't be trying
332 * to parse it and get an error.
334 if (!has_sha1_file(commit
->object
.sha1
))
335 commit
->object
.parsed
= 1;
336 parents
= parents
->next
;
340 static int everybody_uninteresting(struct commit_list
*orig
)
342 struct commit_list
*list
= orig
;
344 struct commit
*commit
= list
->item
;
346 if (commit
->object
.flags
& UNINTERESTING
)
354 * This is a truly stupid algorithm, but it's only
355 * used for bisection, and we just don't care enough.
357 * We care just barely enough to avoid recursing for
360 static int count_distance(struct commit_list
*entry
)
365 struct commit
*commit
= entry
->item
;
366 struct commit_list
*p
;
368 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
370 if (!paths
|| (commit
->object
.flags
& TREECHANGE
))
372 commit
->object
.flags
|= COUNTED
;
378 nr
+= count_distance(p
);
387 static void clear_distance(struct commit_list
*list
)
390 struct commit
*commit
= list
->item
;
391 commit
->object
.flags
&= ~COUNTED
;
396 static struct commit_list
*find_bisection(struct commit_list
*list
)
399 struct commit_list
*p
, *best
;
404 if (!paths
|| (p
->item
->object
.flags
& TREECHANGE
))
411 for (p
= list
; p
; p
= p
->next
) {
414 if (paths
&& !(p
->item
->object
.flags
& TREECHANGE
))
417 distance
= count_distance(p
);
418 clear_distance(list
);
419 if (nr
- distance
< distance
)
420 distance
= nr
- distance
;
421 if (distance
> closest
) {
431 static void mark_edges_uninteresting(struct commit_list
*list
)
433 for ( ; list
; list
= list
->next
) {
434 struct commit_list
*parents
= list
->item
->parents
;
436 for ( ; parents
; parents
= parents
->next
) {
437 struct commit
*commit
= parents
->item
;
438 if (commit
->object
.flags
& UNINTERESTING
)
439 mark_tree_uninteresting(commit
->tree
);
446 #define TREE_DIFFERENT 2
447 static int tree_difference
= TREE_SAME
;
449 static void file_add_remove(struct diff_options
*options
,
450 int addremove
, unsigned mode
,
451 const unsigned char *sha1
,
452 const char *base
, const char *path
)
454 int diff
= TREE_DIFFERENT
;
457 * Is it an add of a new file? It means that
458 * the old tree didn't have it at all, so we
459 * will turn "TREE_SAME" -> "TREE_NEW", but
460 * leave any "TREE_DIFFERENT" alone (and if
461 * it already was "TREE_NEW", we'll keep it
462 * "TREE_NEW" of course).
464 if (addremove
== '+') {
465 diff
= tree_difference
;
466 if (diff
!= TREE_SAME
)
470 tree_difference
= diff
;
473 static void file_change(struct diff_options
*options
,
474 unsigned old_mode
, unsigned new_mode
,
475 const unsigned char *old_sha1
,
476 const unsigned char *new_sha1
,
477 const char *base
, const char *path
)
479 tree_difference
= TREE_DIFFERENT
;
482 static struct diff_options diff_opt
= {
484 .add_remove
= file_add_remove
,
485 .change
= file_change
,
488 static int compare_tree(struct tree
*t1
, struct tree
*t2
)
493 return TREE_DIFFERENT
;
494 tree_difference
= TREE_SAME
;
495 if (diff_tree_sha1(t1
->object
.sha1
, t2
->object
.sha1
, "", &diff_opt
) < 0)
496 return TREE_DIFFERENT
;
497 return tree_difference
;
500 static int same_tree_as_empty(struct tree
*t1
)
504 struct tree_desc empty
, real
;
509 tree
= read_object_with_reference(t1
->object
.sha1
, "tree", &real
.size
, NULL
);
518 retval
= diff_tree(&empty
, &real
, "", &diff_opt
);
521 return retval
>= 0 && !tree_difference
;
524 static void try_to_simplify_commit(struct commit
*commit
)
526 struct commit_list
**pp
, *parent
;
531 if (!commit
->parents
) {
532 if (!same_tree_as_empty(commit
->tree
))
533 commit
->object
.flags
|= TREECHANGE
;
537 pp
= &commit
->parents
;
538 while ((parent
= *pp
) != NULL
) {
539 struct commit
*p
= parent
->item
;
541 if (p
->object
.flags
& UNINTERESTING
) {
547 switch (compare_tree(p
->tree
, commit
->tree
)) {
550 commit
->parents
= parent
;
554 if (remove_empty_trees
&& same_tree_as_empty(p
->tree
)) {
563 die("bad tree compare for commit %s", sha1_to_hex(commit
->object
.sha1
));
565 commit
->object
.flags
|= TREECHANGE
;
568 static void add_parents_to_list(struct commit
*commit
, struct commit_list
**list
)
570 struct commit_list
*parent
= commit
->parents
;
573 * If the commit is uninteresting, don't try to
574 * prune parents - we want the maximal uninteresting
577 * Normally we haven't parsed the parent
578 * yet, so we won't have a parent of a parent
579 * here. However, it may turn out that we've
580 * reached this commit some other way (where it
581 * wasn't uninteresting), in which case we need
582 * to mark its parents recursively too..
584 if (commit
->object
.flags
& UNINTERESTING
) {
586 struct commit
*p
= parent
->item
;
587 parent
= parent
->next
;
589 p
->object
.flags
|= UNINTERESTING
;
591 mark_parents_uninteresting(p
);
592 if (p
->object
.flags
& SEEN
)
594 p
->object
.flags
|= SEEN
;
595 insert_by_date(p
, list
);
601 * Ok, the commit wasn't uninteresting. Try to
602 * simplify the commit history and find the parent
603 * that has no differences in the path set if one exists.
606 try_to_simplify_commit(commit
);
608 parent
= commit
->parents
;
610 struct commit
*p
= parent
->item
;
612 parent
= parent
->next
;
615 if (p
->object
.flags
& SEEN
)
617 p
->object
.flags
|= SEEN
;
618 insert_by_date(p
, list
);
622 static struct commit_list
*limit_list(struct commit_list
*list
)
624 struct commit_list
*newlist
= NULL
;
625 struct commit_list
**p
= &newlist
;
627 struct commit_list
*entry
= list
;
628 struct commit
*commit
= list
->item
;
629 struct object
*obj
= &commit
->object
;
634 if (max_age
!= -1 && (commit
->date
< max_age
))
635 obj
->flags
|= UNINTERESTING
;
636 if (unpacked
&& has_sha1_pack(obj
->sha1
))
637 obj
->flags
|= UNINTERESTING
;
638 add_parents_to_list(commit
, &list
);
639 if (obj
->flags
& UNINTERESTING
) {
640 mark_parents_uninteresting(commit
);
641 if (everybody_uninteresting(list
))
645 if (min_age
!= -1 && (commit
->date
> min_age
))
647 p
= &commit_list_insert(commit
, p
)->next
;
650 mark_edges_uninteresting(newlist
);
652 newlist
= find_bisection(newlist
);
656 static void add_pending_object(struct object
*obj
, const char *name
)
658 add_object(obj
, &pending_objects
, name
);
661 static struct commit
*get_commit_reference(const char *name
, const unsigned char *sha1
, unsigned int flags
)
663 struct object
*object
;
665 object
= parse_object(sha1
);
667 die("bad object %s", name
);
670 * Tag object? Look what it points to..
672 while (object
->type
== tag_type
) {
673 struct tag
*tag
= (struct tag
*) object
;
674 object
->flags
|= flags
;
675 if (tag_objects
&& !(object
->flags
& UNINTERESTING
))
676 add_pending_object(object
, tag
->tag
);
677 object
= parse_object(tag
->tagged
->sha1
);
679 die("bad object %s", sha1_to_hex(tag
->tagged
->sha1
));
683 * Commit object? Just return it, we'll do all the complex
686 if (object
->type
== commit_type
) {
687 struct commit
*commit
= (struct commit
*)object
;
688 object
->flags
|= flags
;
689 if (parse_commit(commit
) < 0)
690 die("unable to parse commit %s", name
);
691 if (flags
& UNINTERESTING
)
692 mark_parents_uninteresting(commit
);
697 * Tree object? Either mark it uniniteresting, or add it
698 * to the list of objects to look at later..
700 if (object
->type
== tree_type
) {
701 struct tree
*tree
= (struct tree
*)object
;
704 if (flags
& UNINTERESTING
) {
705 mark_tree_uninteresting(tree
);
708 add_pending_object(object
, "");
713 * Blob object? You know the drill by now..
715 if (object
->type
== blob_type
) {
716 struct blob
*blob
= (struct blob
*)object
;
719 if (flags
& UNINTERESTING
) {
720 mark_blob_uninteresting(blob
);
723 add_pending_object(object
, "");
726 die("%s is unknown object", name
);
729 static void handle_one_commit(struct commit
*com
, struct commit_list
**lst
)
731 if (!com
|| com
->object
.flags
& SEEN
)
733 com
->object
.flags
|= SEEN
;
734 commit_list_insert(com
, lst
);
737 /* for_each_ref() callback does not allow user data -- Yuck. */
738 static struct commit_list
**global_lst
;
740 static int include_one_commit(const char *path
, const unsigned char *sha1
)
742 struct commit
*com
= get_commit_reference(path
, sha1
, 0);
743 handle_one_commit(com
, global_lst
);
747 static void handle_all(struct commit_list
**lst
)
750 for_each_ref(include_one_commit
);
754 int main(int argc
, const char **argv
)
756 const char *prefix
= setup_git_directory();
757 struct commit_list
*list
= NULL
;
760 for (i
= 1 ; i
< argc
; i
++) {
762 const char *arg
= argv
[i
];
764 struct commit
*commit
;
765 unsigned char sha1
[20];
767 /* accept -<digit>, like traditilnal "head" */
768 if ((*arg
== '-') && isdigit(arg
[1])) {
769 max_count
= atoi(arg
+ 1);
772 if (!strcmp(arg
, "-n")) {
774 die("-n requires an argument");
775 max_count
= atoi(argv
[i
]);
778 if (!strncmp(arg
,"-n",2)) {
779 max_count
= atoi(arg
+ 2);
782 if (!strncmp(arg
, "--max-count=", 12)) {
783 max_count
= atoi(arg
+ 12);
786 if (!strncmp(arg
, "--max-age=", 10)) {
787 max_age
= atoi(arg
+ 10);
791 if (!strncmp(arg
, "--min-age=", 10)) {
792 min_age
= atoi(arg
+ 10);
796 if (!strcmp(arg
, "--header")) {
800 if (!strcmp(arg
, "--no-abbrev")) {
804 if (!strncmp(arg
, "--abbrev=", 9)) {
805 abbrev
= strtoul(arg
+ 9, NULL
, 10);
806 if (abbrev
&& abbrev
< MINIMUM_ABBREV
)
807 abbrev
= MINIMUM_ABBREV
;
808 else if (40 < abbrev
)
812 if (!strncmp(arg
, "--pretty", 8)) {
813 commit_format
= get_commit_format(arg
+8);
815 hdr_termination
= '\n';
816 if (commit_format
== CMIT_FMT_ONELINE
)
819 commit_prefix
= "commit ";
822 if (!strncmp(arg
, "--no-merges", 11)) {
826 if (!strcmp(arg
, "--parents")) {
830 if (!strcmp(arg
, "--bisect")) {
834 if (!strcmp(arg
, "--all")) {
838 if (!strcmp(arg
, "--objects")) {
844 if (!strcmp(arg
, "--unpacked")) {
849 if (!strcmp(arg
, "--merge-order")) {
853 if (!strcmp(arg
, "--show-breaks")) {
857 if (!strcmp(arg
, "--topo-order")) {
862 if (!strcmp(arg
, "--dense")) {
866 if (!strcmp(arg
, "--sparse")) {
870 if (!strcmp(arg
, "--remove-empty")) {
871 remove_empty_trees
= 1;
874 if (!strcmp(arg
, "--")) {
879 if (show_breaks
&& !merge_order
)
880 usage(rev_list_usage
);
883 dotdot
= strstr(arg
, "..");
885 unsigned char from_sha1
[20];
886 char *next
= dotdot
+ 2;
890 if (!get_sha1(arg
, from_sha1
) && !get_sha1(next
, sha1
)) {
891 struct commit
*exclude
;
892 struct commit
*include
;
894 exclude
= get_commit_reference(arg
, from_sha1
, UNINTERESTING
);
895 include
= get_commit_reference(next
, sha1
, 0);
896 if (!exclude
|| !include
)
897 die("Invalid revision range %s..%s", arg
, next
);
899 handle_one_commit(exclude
, &list
);
900 handle_one_commit(include
, &list
);
906 flags
= UNINTERESTING
;
910 if (get_sha1(arg
, sha1
) < 0) {
912 if (lstat(arg
, &st
) < 0)
913 die("'%s': %s", arg
, strerror(errno
));
916 commit
= get_commit_reference(arg
, sha1
, flags
);
917 handle_one_commit(commit
, &list
);
921 (!(tag_objects
||tree_objects
||blob_objects
) && !pending_objects
))
922 usage(rev_list_usage
);
924 paths
= get_pathspec(prefix
, argv
+ i
);
927 diff_tree_setup_paths(paths
);
930 save_commit_buffer
= verbose_header
;
931 track_object_refs
= 0;
935 if (list
&& !limited
&& max_count
== 1 &&
936 !tag_objects
&& !tree_objects
&& !blob_objects
) {
937 show_commit(list
->item
);
941 list
= limit_list(list
);
943 sort_in_topological_order(&list
);
944 show_commit_list(list
);
947 if (sort_list_in_merge_order(list
, &process_commit
)) {
948 die("merge order sort failed\n");
951 die("merge order sort unsupported, OpenSSL not linked");