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"
31 " formatting output:\n"
33 " --objects | --objects-edge\n"
35 " --header | --pretty\n"
36 " --abbrev=nr | --no-abbrev\n"
42 static int unpacked
= 0;
43 static int bisect_list
= 0;
44 static int tag_objects
= 0;
45 static int tree_objects
= 0;
46 static int blob_objects
= 0;
47 static int edge_hint
= 0;
48 static int verbose_header
= 0;
49 static int abbrev
= DEFAULT_ABBREV
;
50 static int show_parents
= 0;
51 static int hdr_termination
= 0;
52 static const char *commit_prefix
= "";
53 static unsigned long max_age
= -1;
54 static unsigned long min_age
= -1;
55 static int max_count
= -1;
56 static enum cmit_fmt commit_format
= CMIT_FMT_RAW
;
57 static int merge_order
= 0;
58 static int show_breaks
= 0;
59 static int stop_traversal
= 0;
60 static int topo_order
= 0;
62 static int no_merges
= 0;
63 static const char **paths
= NULL
;
64 static int remove_empty_trees
= 0;
72 static char *path_name(struct name_path
*path
, const char *name
)
76 int nlen
= strlen(name
);
79 for (p
= path
; p
; p
= p
->up
) {
81 len
+= p
->elem_len
+ 1;
84 m
= n
+ len
- (nlen
+ 1);
86 for (p
= path
; p
; p
= p
->up
) {
89 memcpy(m
, p
->elem
, p
->elem_len
);
96 static void show_commit(struct commit
*commit
)
98 commit
->object
.flags
|= SHOWN
;
100 commit_prefix
= "| ";
101 if (commit
->object
.flags
& DISCONTINUITY
) {
102 commit_prefix
= "^ ";
103 } else if (commit
->object
.flags
& BOUNDARY
) {
104 commit_prefix
= "= ";
107 printf("%s%s", commit_prefix
, sha1_to_hex(commit
->object
.sha1
));
109 struct commit_list
*parents
= commit
->parents
;
111 struct object
*o
= &(parents
->item
->object
);
112 parents
= parents
->next
;
113 if (o
->flags
& TMP_MARK
)
115 printf(" %s", sha1_to_hex(o
->sha1
));
116 o
->flags
|= TMP_MARK
;
118 /* TMP_MARK is a general purpose flag that can
119 * be used locally, but the user should clean
120 * things up after it is done with them.
122 for (parents
= commit
->parents
;
124 parents
= parents
->next
)
125 parents
->item
->object
.flags
&= ~TMP_MARK
;
127 if (commit_format
== CMIT_FMT_ONELINE
)
132 if (verbose_header
) {
133 static char pretty_header
[16384];
134 pretty_print_commit(commit_format
, commit
, ~0, pretty_header
, sizeof(pretty_header
), abbrev
);
135 printf("%s%c", pretty_header
, hdr_termination
);
140 static int rewrite_one(struct commit
**pp
)
143 struct commit
*p
= *pp
;
144 if (p
->object
.flags
& (TREECHANGE
| UNINTERESTING
))
148 *pp
= p
->parents
->item
;
152 static void rewrite_parents(struct commit
*commit
)
154 struct commit_list
**pp
= &commit
->parents
;
156 struct commit_list
*parent
= *pp
;
157 if (rewrite_one(&parent
->item
) < 0) {
165 static int filter_commit(struct commit
* commit
)
167 if (stop_traversal
&& (commit
->object
.flags
& BOUNDARY
))
169 if (commit
->object
.flags
& (UNINTERESTING
|SHOWN
))
171 if (min_age
!= -1 && (commit
->date
> min_age
))
173 if (max_age
!= -1 && (commit
->date
< max_age
)) {
177 if (no_merges
&& (commit
->parents
&& commit
->parents
->next
))
179 if (paths
&& dense
) {
180 if (!(commit
->object
.flags
& TREECHANGE
))
182 rewrite_parents(commit
);
187 static int process_commit(struct commit
* commit
)
189 int action
=filter_commit(commit
);
191 if (action
== STOP
) {
195 if (action
== CONTINUE
) {
199 if (max_count
!= -1 && !max_count
--)
207 static struct object_list
**add_object(struct object
*obj
,
208 struct object_list
**p
,
209 struct name_path
*path
,
212 struct object_list
*entry
= xmalloc(sizeof(*entry
));
215 entry
->name
= path_name(path
, name
);
220 static struct object_list
**process_blob(struct blob
*blob
,
221 struct object_list
**p
,
222 struct name_path
*path
,
225 struct object
*obj
= &blob
->object
;
229 if (obj
->flags
& (UNINTERESTING
| SEEN
))
232 return add_object(obj
, p
, path
, name
);
235 static struct object_list
**process_tree(struct tree
*tree
,
236 struct object_list
**p
,
237 struct name_path
*path
,
240 struct object
*obj
= &tree
->object
;
241 struct tree_entry_list
*entry
;
246 if (obj
->flags
& (UNINTERESTING
| SEEN
))
248 if (parse_tree(tree
) < 0)
249 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
251 p
= add_object(obj
, p
, path
, name
);
254 me
.elem_len
= strlen(name
);
255 entry
= tree
->entries
;
256 tree
->entries
= NULL
;
258 struct tree_entry_list
*next
= entry
->next
;
259 if (entry
->directory
)
260 p
= process_tree(entry
->item
.tree
, p
, &me
, entry
->name
);
262 p
= process_blob(entry
->item
.blob
, p
, &me
, entry
->name
);
269 static struct object_list
*pending_objects
= NULL
;
271 static void show_commit_list(struct commit_list
*list
)
273 struct object_list
*objects
= NULL
, **p
= &objects
, *pending
;
275 struct commit
*commit
= pop_most_recent_commit(&list
, SEEN
);
277 p
= process_tree(commit
->tree
, p
, NULL
, "");
278 if (process_commit(commit
) == STOP
)
281 for (pending
= pending_objects
; pending
; pending
= pending
->next
) {
282 struct object
*obj
= pending
->item
;
283 const char *name
= pending
->name
;
284 if (obj
->flags
& (UNINTERESTING
| SEEN
))
286 if (obj
->type
== tag_type
) {
288 p
= add_object(obj
, p
, NULL
, name
);
291 if (obj
->type
== tree_type
) {
292 p
= process_tree((struct tree
*)obj
, p
, NULL
, name
);
295 if (obj
->type
== blob_type
) {
296 p
= process_blob((struct blob
*)obj
, p
, NULL
, name
);
299 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
302 /* An object with name "foo\n0000000..." can be used to
303 * confuse downstream git-pack-objects very badly.
305 const char *ep
= strchr(objects
->name
, '\n');
307 printf("%s %.*s\n", sha1_to_hex(objects
->item
->sha1
),
308 (int) (ep
- objects
->name
),
312 printf("%s %s\n", sha1_to_hex(objects
->item
->sha1
), objects
->name
);
313 objects
= objects
->next
;
317 static void mark_blob_uninteresting(struct blob
*blob
)
321 if (blob
->object
.flags
& UNINTERESTING
)
323 blob
->object
.flags
|= UNINTERESTING
;
326 static void mark_tree_uninteresting(struct tree
*tree
)
328 struct object
*obj
= &tree
->object
;
329 struct tree_entry_list
*entry
;
333 if (obj
->flags
& UNINTERESTING
)
335 obj
->flags
|= UNINTERESTING
;
336 if (!has_sha1_file(obj
->sha1
))
338 if (parse_tree(tree
) < 0)
339 die("bad tree %s", sha1_to_hex(obj
->sha1
));
340 entry
= tree
->entries
;
341 tree
->entries
= NULL
;
343 struct tree_entry_list
*next
= entry
->next
;
344 if (entry
->directory
)
345 mark_tree_uninteresting(entry
->item
.tree
);
347 mark_blob_uninteresting(entry
->item
.blob
);
353 static void mark_parents_uninteresting(struct commit
*commit
)
355 struct commit_list
*parents
= commit
->parents
;
358 struct commit
*commit
= parents
->item
;
359 commit
->object
.flags
|= UNINTERESTING
;
362 * Normally we haven't parsed the parent
363 * yet, so we won't have a parent of a parent
364 * here. However, it may turn out that we've
365 * reached this commit some other way (where it
366 * wasn't uninteresting), in which case we need
367 * to mark its parents recursively too..
370 mark_parents_uninteresting(commit
);
373 * A missing commit is ok iff its parent is marked
376 * We just mark such a thing parsed, so that when
377 * it is popped next time around, we won't be trying
378 * to parse it and get an error.
380 if (!has_sha1_file(commit
->object
.sha1
))
381 commit
->object
.parsed
= 1;
382 parents
= parents
->next
;
386 static int everybody_uninteresting(struct commit_list
*orig
)
388 struct commit_list
*list
= orig
;
390 struct commit
*commit
= list
->item
;
392 if (commit
->object
.flags
& UNINTERESTING
)
400 * This is a truly stupid algorithm, but it's only
401 * used for bisection, and we just don't care enough.
403 * We care just barely enough to avoid recursing for
406 static int count_distance(struct commit_list
*entry
)
411 struct commit
*commit
= entry
->item
;
412 struct commit_list
*p
;
414 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
416 if (!paths
|| (commit
->object
.flags
& TREECHANGE
))
418 commit
->object
.flags
|= COUNTED
;
424 nr
+= count_distance(p
);
433 static void clear_distance(struct commit_list
*list
)
436 struct commit
*commit
= list
->item
;
437 commit
->object
.flags
&= ~COUNTED
;
442 static struct commit_list
*find_bisection(struct commit_list
*list
)
445 struct commit_list
*p
, *best
;
450 if (!paths
|| (p
->item
->object
.flags
& TREECHANGE
))
457 for (p
= list
; p
; p
= p
->next
) {
460 if (paths
&& !(p
->item
->object
.flags
& TREECHANGE
))
463 distance
= count_distance(p
);
464 clear_distance(list
);
465 if (nr
- distance
< distance
)
466 distance
= nr
- distance
;
467 if (distance
> closest
) {
477 static void mark_edge_parents_uninteresting(struct commit
*commit
)
479 struct commit_list
*parents
;
481 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
482 struct commit
*parent
= parents
->item
;
483 if (!(parent
->object
.flags
& UNINTERESTING
))
485 mark_tree_uninteresting(parent
->tree
);
486 if (edge_hint
&& !(parent
->object
.flags
& SHOWN
)) {
487 parent
->object
.flags
|= SHOWN
;
488 printf("-%s\n", sha1_to_hex(parent
->object
.sha1
));
493 static void mark_edges_uninteresting(struct commit_list
*list
)
495 for ( ; list
; list
= list
->next
) {
496 struct commit
*commit
= list
->item
;
498 if (commit
->object
.flags
& UNINTERESTING
) {
499 mark_tree_uninteresting(commit
->tree
);
502 mark_edge_parents_uninteresting(commit
);
508 #define TREE_DIFFERENT 2
509 static int tree_difference
= TREE_SAME
;
511 static void file_add_remove(struct diff_options
*options
,
512 int addremove
, unsigned mode
,
513 const unsigned char *sha1
,
514 const char *base
, const char *path
)
516 int diff
= TREE_DIFFERENT
;
519 * Is it an add of a new file? It means that
520 * the old tree didn't have it at all, so we
521 * will turn "TREE_SAME" -> "TREE_NEW", but
522 * leave any "TREE_DIFFERENT" alone (and if
523 * it already was "TREE_NEW", we'll keep it
524 * "TREE_NEW" of course).
526 if (addremove
== '+') {
527 diff
= tree_difference
;
528 if (diff
!= TREE_SAME
)
532 tree_difference
= diff
;
535 static void file_change(struct diff_options
*options
,
536 unsigned old_mode
, unsigned new_mode
,
537 const unsigned char *old_sha1
,
538 const unsigned char *new_sha1
,
539 const char *base
, const char *path
)
541 tree_difference
= TREE_DIFFERENT
;
544 static struct diff_options diff_opt
= {
546 .add_remove
= file_add_remove
,
547 .change
= file_change
,
550 static int compare_tree(struct tree
*t1
, struct tree
*t2
)
555 return TREE_DIFFERENT
;
556 tree_difference
= TREE_SAME
;
557 if (diff_tree_sha1(t1
->object
.sha1
, t2
->object
.sha1
, "", &diff_opt
) < 0)
558 return TREE_DIFFERENT
;
559 return tree_difference
;
562 static int same_tree_as_empty(struct tree
*t1
)
566 struct tree_desc empty
, real
;
571 tree
= read_object_with_reference(t1
->object
.sha1
, "tree", &real
.size
, NULL
);
580 retval
= diff_tree(&empty
, &real
, "", &diff_opt
);
583 return retval
>= 0 && !tree_difference
;
586 static void try_to_simplify_commit(struct commit
*commit
)
588 struct commit_list
**pp
, *parent
;
593 if (!commit
->parents
) {
594 if (!same_tree_as_empty(commit
->tree
))
595 commit
->object
.flags
|= TREECHANGE
;
599 pp
= &commit
->parents
;
600 while ((parent
= *pp
) != NULL
) {
601 struct commit
*p
= parent
->item
;
603 if (p
->object
.flags
& UNINTERESTING
) {
609 switch (compare_tree(p
->tree
, commit
->tree
)) {
612 commit
->parents
= parent
;
616 if (remove_empty_trees
&& same_tree_as_empty(p
->tree
)) {
625 die("bad tree compare for commit %s", sha1_to_hex(commit
->object
.sha1
));
627 commit
->object
.flags
|= TREECHANGE
;
630 static void add_parents_to_list(struct commit
*commit
, struct commit_list
**list
)
632 struct commit_list
*parent
= commit
->parents
;
635 * If the commit is uninteresting, don't try to
636 * prune parents - we want the maximal uninteresting
639 * Normally we haven't parsed the parent
640 * yet, so we won't have a parent of a parent
641 * here. However, it may turn out that we've
642 * reached this commit some other way (where it
643 * wasn't uninteresting), in which case we need
644 * to mark its parents recursively too..
646 if (commit
->object
.flags
& UNINTERESTING
) {
648 struct commit
*p
= parent
->item
;
649 parent
= parent
->next
;
651 p
->object
.flags
|= UNINTERESTING
;
653 mark_parents_uninteresting(p
);
654 if (p
->object
.flags
& SEEN
)
656 p
->object
.flags
|= SEEN
;
657 insert_by_date(p
, list
);
663 * Ok, the commit wasn't uninteresting. Try to
664 * simplify the commit history and find the parent
665 * that has no differences in the path set if one exists.
668 try_to_simplify_commit(commit
);
670 parent
= commit
->parents
;
672 struct commit
*p
= parent
->item
;
674 parent
= parent
->next
;
677 if (p
->object
.flags
& SEEN
)
679 p
->object
.flags
|= SEEN
;
680 insert_by_date(p
, list
);
684 static struct commit_list
*limit_list(struct commit_list
*list
)
686 struct commit_list
*newlist
= NULL
;
687 struct commit_list
**p
= &newlist
;
689 struct commit_list
*entry
= list
;
690 struct commit
*commit
= list
->item
;
691 struct object
*obj
= &commit
->object
;
696 if (max_age
!= -1 && (commit
->date
< max_age
))
697 obj
->flags
|= UNINTERESTING
;
698 if (unpacked
&& has_sha1_pack(obj
->sha1
))
699 obj
->flags
|= UNINTERESTING
;
700 add_parents_to_list(commit
, &list
);
701 if (obj
->flags
& UNINTERESTING
) {
702 mark_parents_uninteresting(commit
);
703 if (everybody_uninteresting(list
))
707 if (min_age
!= -1 && (commit
->date
> min_age
))
709 p
= &commit_list_insert(commit
, p
)->next
;
712 mark_edges_uninteresting(newlist
);
714 newlist
= find_bisection(newlist
);
718 static void add_pending_object(struct object
*obj
, const char *name
)
720 add_object(obj
, &pending_objects
, NULL
, name
);
723 static struct commit
*get_commit_reference(const char *name
, const unsigned char *sha1
, unsigned int flags
)
725 struct object
*object
;
727 object
= parse_object(sha1
);
729 die("bad object %s", name
);
732 * Tag object? Look what it points to..
734 while (object
->type
== tag_type
) {
735 struct tag
*tag
= (struct tag
*) object
;
736 object
->flags
|= flags
;
737 if (tag_objects
&& !(object
->flags
& UNINTERESTING
))
738 add_pending_object(object
, tag
->tag
);
739 object
= parse_object(tag
->tagged
->sha1
);
741 die("bad object %s", sha1_to_hex(tag
->tagged
->sha1
));
745 * Commit object? Just return it, we'll do all the complex
748 if (object
->type
== commit_type
) {
749 struct commit
*commit
= (struct commit
*)object
;
750 object
->flags
|= flags
;
751 if (parse_commit(commit
) < 0)
752 die("unable to parse commit %s", name
);
753 if (flags
& UNINTERESTING
)
754 mark_parents_uninteresting(commit
);
759 * Tree object? Either mark it uniniteresting, or add it
760 * to the list of objects to look at later..
762 if (object
->type
== tree_type
) {
763 struct tree
*tree
= (struct tree
*)object
;
766 if (flags
& UNINTERESTING
) {
767 mark_tree_uninteresting(tree
);
770 add_pending_object(object
, "");
775 * Blob object? You know the drill by now..
777 if (object
->type
== blob_type
) {
778 struct blob
*blob
= (struct blob
*)object
;
781 if (flags
& UNINTERESTING
) {
782 mark_blob_uninteresting(blob
);
785 add_pending_object(object
, "");
788 die("%s is unknown object", name
);
791 static void handle_one_commit(struct commit
*com
, struct commit_list
**lst
)
793 if (!com
|| com
->object
.flags
& SEEN
)
795 com
->object
.flags
|= SEEN
;
796 commit_list_insert(com
, lst
);
799 /* for_each_ref() callback does not allow user data -- Yuck. */
800 static struct commit_list
**global_lst
;
802 static int include_one_commit(const char *path
, const unsigned char *sha1
)
804 struct commit
*com
= get_commit_reference(path
, sha1
, 0);
805 handle_one_commit(com
, global_lst
);
809 static void handle_all(struct commit_list
**lst
)
812 for_each_ref(include_one_commit
);
816 int main(int argc
, const char **argv
)
818 const char *prefix
= setup_git_directory();
819 struct commit_list
*list
= NULL
;
822 for (i
= 1 ; i
< argc
; i
++) {
824 const char *arg
= argv
[i
];
826 struct commit
*commit
;
827 unsigned char sha1
[20];
829 /* accept -<digit>, like traditilnal "head" */
830 if ((*arg
== '-') && isdigit(arg
[1])) {
831 max_count
= atoi(arg
+ 1);
834 if (!strcmp(arg
, "-n")) {
836 die("-n requires an argument");
837 max_count
= atoi(argv
[i
]);
840 if (!strncmp(arg
,"-n",2)) {
841 max_count
= atoi(arg
+ 2);
844 if (!strncmp(arg
, "--max-count=", 12)) {
845 max_count
= atoi(arg
+ 12);
848 if (!strncmp(arg
, "--max-age=", 10)) {
849 max_age
= atoi(arg
+ 10);
853 if (!strncmp(arg
, "--min-age=", 10)) {
854 min_age
= atoi(arg
+ 10);
858 if (!strcmp(arg
, "--header")) {
862 if (!strcmp(arg
, "--no-abbrev")) {
866 if (!strncmp(arg
, "--abbrev=", 9)) {
867 abbrev
= strtoul(arg
+ 9, NULL
, 10);
868 if (abbrev
&& abbrev
< MINIMUM_ABBREV
)
869 abbrev
= MINIMUM_ABBREV
;
870 else if (40 < abbrev
)
874 if (!strncmp(arg
, "--pretty", 8)) {
875 commit_format
= get_commit_format(arg
+8);
877 hdr_termination
= '\n';
878 if (commit_format
== CMIT_FMT_ONELINE
)
881 commit_prefix
= "commit ";
884 if (!strncmp(arg
, "--no-merges", 11)) {
888 if (!strcmp(arg
, "--parents")) {
892 if (!strcmp(arg
, "--bisect")) {
896 if (!strcmp(arg
, "--all")) {
900 if (!strcmp(arg
, "--objects")) {
906 if (!strcmp(arg
, "--objects-edge")) {
913 if (!strcmp(arg
, "--unpacked")) {
918 if (!strcmp(arg
, "--merge-order")) {
922 if (!strcmp(arg
, "--show-breaks")) {
926 if (!strcmp(arg
, "--topo-order")) {
932 if (!strcmp(arg
, "--date-order")) {
938 if (!strcmp(arg
, "--dense")) {
942 if (!strcmp(arg
, "--sparse")) {
946 if (!strcmp(arg
, "--remove-empty")) {
947 remove_empty_trees
= 1;
950 if (!strcmp(arg
, "--")) {
955 if (show_breaks
&& !merge_order
)
956 usage(rev_list_usage
);
959 dotdot
= strstr(arg
, "..");
961 unsigned char from_sha1
[20];
962 char *next
= dotdot
+ 2;
966 if (!get_sha1(arg
, from_sha1
) && !get_sha1(next
, sha1
)) {
967 struct commit
*exclude
;
968 struct commit
*include
;
970 exclude
= get_commit_reference(arg
, from_sha1
, UNINTERESTING
);
971 include
= get_commit_reference(next
, sha1
, 0);
972 if (!exclude
|| !include
)
973 die("Invalid revision range %s..%s", arg
, next
);
975 handle_one_commit(exclude
, &list
);
976 handle_one_commit(include
, &list
);
982 flags
= UNINTERESTING
;
986 if (get_sha1(arg
, sha1
) < 0) {
988 if (lstat(arg
, &st
) < 0)
989 die("'%s': %s", arg
, strerror(errno
));
992 commit
= get_commit_reference(arg
, sha1
, flags
);
993 handle_one_commit(commit
, &list
);
997 (!(tag_objects
||tree_objects
||blob_objects
) && !pending_objects
))
998 usage(rev_list_usage
);
1000 paths
= get_pathspec(prefix
, argv
+ i
);
1003 diff_tree_setup_paths(paths
);
1006 save_commit_buffer
= verbose_header
;
1007 track_object_refs
= 0;
1010 sort_by_date(&list
);
1011 if (list
&& !limited
&& max_count
== 1 &&
1012 !tag_objects
&& !tree_objects
&& !blob_objects
) {
1013 show_commit(list
->item
);
1017 list
= limit_list(list
);
1019 sort_in_topological_order(&list
, lifo
);
1020 show_commit_list(list
);
1023 if (sort_list_in_merge_order(list
, &process_commit
)) {
1024 die("merge order sort failed\n");
1027 die("merge order sort unsupported, OpenSSL not linked");