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"
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 verbose_header
= 0;
48 static int abbrev
= DEFAULT_ABBREV
;
49 static int show_parents
= 0;
50 static int hdr_termination
= 0;
51 static const char *commit_prefix
= "";
52 static unsigned long max_age
= -1;
53 static unsigned long min_age
= -1;
54 static int max_count
= -1;
55 static enum cmit_fmt commit_format
= CMIT_FMT_RAW
;
56 static int merge_order
= 0;
57 static int show_breaks
= 0;
58 static int stop_traversal
= 0;
59 static int topo_order
= 0;
61 static int no_merges
= 0;
62 static const char **paths
= NULL
;
63 static int remove_empty_trees
= 0;
65 static void show_commit(struct commit
*commit
)
67 commit
->object
.flags
|= SHOWN
;
70 if (commit
->object
.flags
& DISCONTINUITY
) {
72 } else if (commit
->object
.flags
& BOUNDARY
) {
76 printf("%s%s", commit_prefix
, sha1_to_hex(commit
->object
.sha1
));
78 struct commit_list
*parents
= commit
->parents
;
80 struct object
*o
= &(parents
->item
->object
);
81 parents
= parents
->next
;
82 if (o
->flags
& TMP_MARK
)
84 printf(" %s", sha1_to_hex(o
->sha1
));
87 /* TMP_MARK is a general purpose flag that can
88 * be used locally, but the user should clean
89 * things up after it is done with them.
91 for (parents
= commit
->parents
;
93 parents
= parents
->next
)
94 parents
->item
->object
.flags
&= ~TMP_MARK
;
96 if (commit_format
== CMIT_FMT_ONELINE
)
101 if (verbose_header
) {
102 static char pretty_header
[16384];
103 pretty_print_commit(commit_format
, commit
, ~0, pretty_header
, sizeof(pretty_header
), abbrev
);
104 printf("%s%c", pretty_header
, hdr_termination
);
109 static int rewrite_one(struct commit
**pp
)
112 struct commit
*p
= *pp
;
113 if (p
->object
.flags
& (TREECHANGE
| UNINTERESTING
))
117 *pp
= p
->parents
->item
;
121 static void rewrite_parents(struct commit
*commit
)
123 struct commit_list
**pp
= &commit
->parents
;
125 struct commit_list
*parent
= *pp
;
126 if (rewrite_one(&parent
->item
) < 0) {
134 static int filter_commit(struct commit
* commit
)
136 if (stop_traversal
&& (commit
->object
.flags
& BOUNDARY
))
138 if (commit
->object
.flags
& (UNINTERESTING
|SHOWN
))
140 if (min_age
!= -1 && (commit
->date
> min_age
))
142 if (max_age
!= -1 && (commit
->date
< max_age
)) {
146 if (no_merges
&& (commit
->parents
&& commit
->parents
->next
))
148 if (paths
&& dense
) {
149 if (!(commit
->object
.flags
& TREECHANGE
))
151 rewrite_parents(commit
);
156 static int process_commit(struct commit
* commit
)
158 int action
=filter_commit(commit
);
160 if (action
== STOP
) {
164 if (action
== CONTINUE
) {
168 if (max_count
!= -1 && !max_count
--)
176 static struct object_list
**add_object(struct object
*obj
, struct object_list
**p
, const char *name
)
178 struct object_list
*entry
= xmalloc(sizeof(*entry
));
186 static struct object_list
**process_blob(struct blob
*blob
, struct object_list
**p
, const char *name
)
188 struct object
*obj
= &blob
->object
;
192 if (obj
->flags
& (UNINTERESTING
| SEEN
))
195 return add_object(obj
, p
, name
);
198 static struct object_list
**process_tree(struct tree
*tree
, struct object_list
**p
, const char *name
)
200 struct object
*obj
= &tree
->object
;
201 struct tree_entry_list
*entry
;
205 if (obj
->flags
& (UNINTERESTING
| SEEN
))
207 if (parse_tree(tree
) < 0)
208 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
210 p
= add_object(obj
, p
, name
);
211 entry
= tree
->entries
;
212 tree
->entries
= NULL
;
214 struct tree_entry_list
*next
= entry
->next
;
215 if (entry
->directory
)
216 p
= process_tree(entry
->item
.tree
, p
, entry
->name
);
218 p
= process_blob(entry
->item
.blob
, p
, entry
->name
);
225 static struct object_list
*pending_objects
= NULL
;
227 static void show_commit_list(struct commit_list
*list
)
229 struct object_list
*objects
= NULL
, **p
= &objects
, *pending
;
231 struct commit
*commit
= pop_most_recent_commit(&list
, SEEN
);
233 p
= process_tree(commit
->tree
, p
, "");
234 if (process_commit(commit
) == STOP
)
237 for (pending
= pending_objects
; pending
; pending
= pending
->next
) {
238 struct object
*obj
= pending
->item
;
239 const char *name
= pending
->name
;
240 if (obj
->flags
& (UNINTERESTING
| SEEN
))
242 if (obj
->type
== tag_type
) {
244 p
= add_object(obj
, p
, name
);
247 if (obj
->type
== tree_type
) {
248 p
= process_tree((struct tree
*)obj
, p
, name
);
251 if (obj
->type
== blob_type
) {
252 p
= process_blob((struct blob
*)obj
, p
, name
);
255 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
258 /* An object with name "foo\n0000000000000000000000000000000000000000"
259 * can be used confuse downstream git-pack-objects very badly.
261 const char *ep
= strchr(objects
->name
, '\n');
263 printf("%s %.*s\n", sha1_to_hex(objects
->item
->sha1
),
264 (int) (ep
- objects
->name
),
268 printf("%s %s\n", sha1_to_hex(objects
->item
->sha1
), objects
->name
);
269 objects
= objects
->next
;
273 static void mark_blob_uninteresting(struct blob
*blob
)
277 if (blob
->object
.flags
& UNINTERESTING
)
279 blob
->object
.flags
|= UNINTERESTING
;
282 static void mark_tree_uninteresting(struct tree
*tree
)
284 struct object
*obj
= &tree
->object
;
285 struct tree_entry_list
*entry
;
289 if (obj
->flags
& UNINTERESTING
)
291 obj
->flags
|= UNINTERESTING
;
292 if (!has_sha1_file(obj
->sha1
))
294 if (parse_tree(tree
) < 0)
295 die("bad tree %s", sha1_to_hex(obj
->sha1
));
296 entry
= tree
->entries
;
297 tree
->entries
= NULL
;
299 struct tree_entry_list
*next
= entry
->next
;
300 if (entry
->directory
)
301 mark_tree_uninteresting(entry
->item
.tree
);
303 mark_blob_uninteresting(entry
->item
.blob
);
309 static void mark_parents_uninteresting(struct commit
*commit
)
311 struct commit_list
*parents
= commit
->parents
;
314 struct commit
*commit
= parents
->item
;
315 commit
->object
.flags
|= UNINTERESTING
;
318 * Normally we haven't parsed the parent
319 * yet, so we won't have a parent of a parent
320 * here. However, it may turn out that we've
321 * reached this commit some other way (where it
322 * wasn't uninteresting), in which case we need
323 * to mark its parents recursively too..
326 mark_parents_uninteresting(commit
);
329 * A missing commit is ok iff its parent is marked
332 * We just mark such a thing parsed, so that when
333 * it is popped next time around, we won't be trying
334 * to parse it and get an error.
336 if (!has_sha1_file(commit
->object
.sha1
))
337 commit
->object
.parsed
= 1;
338 parents
= parents
->next
;
342 static int everybody_uninteresting(struct commit_list
*orig
)
344 struct commit_list
*list
= orig
;
346 struct commit
*commit
= list
->item
;
348 if (commit
->object
.flags
& UNINTERESTING
)
356 * This is a truly stupid algorithm, but it's only
357 * used for bisection, and we just don't care enough.
359 * We care just barely enough to avoid recursing for
362 static int count_distance(struct commit_list
*entry
)
367 struct commit
*commit
= entry
->item
;
368 struct commit_list
*p
;
370 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
372 if (!paths
|| (commit
->object
.flags
& TREECHANGE
))
374 commit
->object
.flags
|= COUNTED
;
380 nr
+= count_distance(p
);
389 static void clear_distance(struct commit_list
*list
)
392 struct commit
*commit
= list
->item
;
393 commit
->object
.flags
&= ~COUNTED
;
398 static struct commit_list
*find_bisection(struct commit_list
*list
)
401 struct commit_list
*p
, *best
;
406 if (!paths
|| (p
->item
->object
.flags
& TREECHANGE
))
413 for (p
= list
; p
; p
= p
->next
) {
416 if (paths
&& !(p
->item
->object
.flags
& TREECHANGE
))
419 distance
= count_distance(p
);
420 clear_distance(list
);
421 if (nr
- distance
< distance
)
422 distance
= nr
- distance
;
423 if (distance
> closest
) {
433 static void mark_edges_uninteresting(struct commit_list
*list
)
435 for ( ; list
; list
= list
->next
) {
436 struct commit_list
*parents
= list
->item
->parents
;
438 for ( ; parents
; parents
= parents
->next
) {
439 struct commit
*commit
= parents
->item
;
440 if (commit
->object
.flags
& UNINTERESTING
)
441 mark_tree_uninteresting(commit
->tree
);
448 #define TREE_DIFFERENT 2
449 static int tree_difference
= TREE_SAME
;
451 static void file_add_remove(struct diff_options
*options
,
452 int addremove
, unsigned mode
,
453 const unsigned char *sha1
,
454 const char *base
, const char *path
)
456 int diff
= TREE_DIFFERENT
;
459 * Is it an add of a new file? It means that
460 * the old tree didn't have it at all, so we
461 * will turn "TREE_SAME" -> "TREE_NEW", but
462 * leave any "TREE_DIFFERENT" alone (and if
463 * it already was "TREE_NEW", we'll keep it
464 * "TREE_NEW" of course).
466 if (addremove
== '+') {
467 diff
= tree_difference
;
468 if (diff
!= TREE_SAME
)
472 tree_difference
= diff
;
475 static void file_change(struct diff_options
*options
,
476 unsigned old_mode
, unsigned new_mode
,
477 const unsigned char *old_sha1
,
478 const unsigned char *new_sha1
,
479 const char *base
, const char *path
)
481 tree_difference
= TREE_DIFFERENT
;
484 static struct diff_options diff_opt
= {
486 .add_remove
= file_add_remove
,
487 .change
= file_change
,
490 static int compare_tree(struct tree
*t1
, struct tree
*t2
)
495 return TREE_DIFFERENT
;
496 tree_difference
= TREE_SAME
;
497 if (diff_tree_sha1(t1
->object
.sha1
, t2
->object
.sha1
, "", &diff_opt
) < 0)
498 return TREE_DIFFERENT
;
499 return tree_difference
;
502 static int same_tree_as_empty(struct tree
*t1
)
506 struct tree_desc empty
, real
;
511 tree
= read_object_with_reference(t1
->object
.sha1
, "tree", &real
.size
, NULL
);
520 retval
= diff_tree(&empty
, &real
, "", &diff_opt
);
523 return retval
>= 0 && !tree_difference
;
526 static void try_to_simplify_commit(struct commit
*commit
)
528 struct commit_list
**pp
, *parent
;
533 if (!commit
->parents
) {
534 if (!same_tree_as_empty(commit
->tree
))
535 commit
->object
.flags
|= TREECHANGE
;
539 pp
= &commit
->parents
;
540 while ((parent
= *pp
) != NULL
) {
541 struct commit
*p
= parent
->item
;
543 if (p
->object
.flags
& UNINTERESTING
) {
549 switch (compare_tree(p
->tree
, commit
->tree
)) {
552 commit
->parents
= parent
;
556 if (remove_empty_trees
&& same_tree_as_empty(p
->tree
)) {
565 die("bad tree compare for commit %s", sha1_to_hex(commit
->object
.sha1
));
567 commit
->object
.flags
|= TREECHANGE
;
570 static void add_parents_to_list(struct commit
*commit
, struct commit_list
**list
)
572 struct commit_list
*parent
= commit
->parents
;
575 * If the commit is uninteresting, don't try to
576 * prune parents - we want the maximal uninteresting
579 * Normally we haven't parsed the parent
580 * yet, so we won't have a parent of a parent
581 * here. However, it may turn out that we've
582 * reached this commit some other way (where it
583 * wasn't uninteresting), in which case we need
584 * to mark its parents recursively too..
586 if (commit
->object
.flags
& UNINTERESTING
) {
588 struct commit
*p
= parent
->item
;
589 parent
= parent
->next
;
591 p
->object
.flags
|= UNINTERESTING
;
593 mark_parents_uninteresting(p
);
594 if (p
->object
.flags
& SEEN
)
596 p
->object
.flags
|= SEEN
;
597 insert_by_date(p
, list
);
603 * Ok, the commit wasn't uninteresting. Try to
604 * simplify the commit history and find the parent
605 * that has no differences in the path set if one exists.
608 try_to_simplify_commit(commit
);
610 parent
= commit
->parents
;
612 struct commit
*p
= parent
->item
;
614 parent
= parent
->next
;
617 if (p
->object
.flags
& SEEN
)
619 p
->object
.flags
|= SEEN
;
620 insert_by_date(p
, list
);
624 static struct commit_list
*limit_list(struct commit_list
*list
)
626 struct commit_list
*newlist
= NULL
;
627 struct commit_list
**p
= &newlist
;
629 struct commit_list
*entry
= list
;
630 struct commit
*commit
= list
->item
;
631 struct object
*obj
= &commit
->object
;
636 if (max_age
!= -1 && (commit
->date
< max_age
))
637 obj
->flags
|= UNINTERESTING
;
638 if (unpacked
&& has_sha1_pack(obj
->sha1
))
639 obj
->flags
|= UNINTERESTING
;
640 add_parents_to_list(commit
, &list
);
641 if (obj
->flags
& UNINTERESTING
) {
642 mark_parents_uninteresting(commit
);
643 if (everybody_uninteresting(list
))
647 if (min_age
!= -1 && (commit
->date
> min_age
))
649 p
= &commit_list_insert(commit
, p
)->next
;
652 mark_edges_uninteresting(newlist
);
654 newlist
= find_bisection(newlist
);
658 static void add_pending_object(struct object
*obj
, const char *name
)
660 add_object(obj
, &pending_objects
, name
);
663 static struct commit
*get_commit_reference(const char *name
, const unsigned char *sha1
, unsigned int flags
)
665 struct object
*object
;
667 object
= parse_object(sha1
);
669 die("bad object %s", name
);
672 * Tag object? Look what it points to..
674 while (object
->type
== tag_type
) {
675 struct tag
*tag
= (struct tag
*) object
;
676 object
->flags
|= flags
;
677 if (tag_objects
&& !(object
->flags
& UNINTERESTING
))
678 add_pending_object(object
, tag
->tag
);
679 object
= parse_object(tag
->tagged
->sha1
);
681 die("bad object %s", sha1_to_hex(tag
->tagged
->sha1
));
685 * Commit object? Just return it, we'll do all the complex
688 if (object
->type
== commit_type
) {
689 struct commit
*commit
= (struct commit
*)object
;
690 object
->flags
|= flags
;
691 if (parse_commit(commit
) < 0)
692 die("unable to parse commit %s", name
);
693 if (flags
& UNINTERESTING
)
694 mark_parents_uninteresting(commit
);
699 * Tree object? Either mark it uniniteresting, or add it
700 * to the list of objects to look at later..
702 if (object
->type
== tree_type
) {
703 struct tree
*tree
= (struct tree
*)object
;
706 if (flags
& UNINTERESTING
) {
707 mark_tree_uninteresting(tree
);
710 add_pending_object(object
, "");
715 * Blob object? You know the drill by now..
717 if (object
->type
== blob_type
) {
718 struct blob
*blob
= (struct blob
*)object
;
721 if (flags
& UNINTERESTING
) {
722 mark_blob_uninteresting(blob
);
725 add_pending_object(object
, "");
728 die("%s is unknown object", name
);
731 static void handle_one_commit(struct commit
*com
, struct commit_list
**lst
)
733 if (!com
|| com
->object
.flags
& SEEN
)
735 com
->object
.flags
|= SEEN
;
736 commit_list_insert(com
, lst
);
739 /* for_each_ref() callback does not allow user data -- Yuck. */
740 static struct commit_list
**global_lst
;
742 static int include_one_commit(const char *path
, const unsigned char *sha1
)
744 struct commit
*com
= get_commit_reference(path
, sha1
, 0);
745 handle_one_commit(com
, global_lst
);
749 static void handle_all(struct commit_list
**lst
)
752 for_each_ref(include_one_commit
);
756 int main(int argc
, const char **argv
)
758 const char *prefix
= setup_git_directory();
759 struct commit_list
*list
= NULL
;
762 for (i
= 1 ; i
< argc
; i
++) {
764 const char *arg
= argv
[i
];
766 struct commit
*commit
;
767 unsigned char sha1
[20];
769 /* accept -<digit>, like traditilnal "head" */
770 if ((*arg
== '-') && isdigit(arg
[1])) {
771 max_count
= atoi(arg
+ 1);
774 if (!strcmp(arg
, "-n")) {
776 die("-n requires an argument");
777 max_count
= atoi(argv
[i
]);
780 if (!strncmp(arg
,"-n",2)) {
781 max_count
= atoi(arg
+ 2);
784 if (!strncmp(arg
, "--max-count=", 12)) {
785 max_count
= atoi(arg
+ 12);
788 if (!strncmp(arg
, "--max-age=", 10)) {
789 max_age
= atoi(arg
+ 10);
793 if (!strncmp(arg
, "--min-age=", 10)) {
794 min_age
= atoi(arg
+ 10);
798 if (!strcmp(arg
, "--header")) {
802 if (!strcmp(arg
, "--no-abbrev")) {
806 if (!strncmp(arg
, "--abbrev=", 9)) {
807 abbrev
= strtoul(arg
+ 9, NULL
, 10);
808 if (abbrev
&& abbrev
< MINIMUM_ABBREV
)
809 abbrev
= MINIMUM_ABBREV
;
810 else if (40 < abbrev
)
814 if (!strncmp(arg
, "--pretty", 8)) {
815 commit_format
= get_commit_format(arg
+8);
817 hdr_termination
= '\n';
818 if (commit_format
== CMIT_FMT_ONELINE
)
821 commit_prefix
= "commit ";
824 if (!strncmp(arg
, "--no-merges", 11)) {
828 if (!strcmp(arg
, "--parents")) {
832 if (!strcmp(arg
, "--bisect")) {
836 if (!strcmp(arg
, "--all")) {
840 if (!strcmp(arg
, "--objects")) {
846 if (!strcmp(arg
, "--unpacked")) {
851 if (!strcmp(arg
, "--merge-order")) {
855 if (!strcmp(arg
, "--show-breaks")) {
859 if (!strcmp(arg
, "--topo-order")) {
865 if (!strcmp(arg
, "--date-order")) {
871 if (!strcmp(arg
, "--dense")) {
875 if (!strcmp(arg
, "--sparse")) {
879 if (!strcmp(arg
, "--remove-empty")) {
880 remove_empty_trees
= 1;
883 if (!strcmp(arg
, "--")) {
888 if (show_breaks
&& !merge_order
)
889 usage(rev_list_usage
);
892 dotdot
= strstr(arg
, "..");
894 unsigned char from_sha1
[20];
895 char *next
= dotdot
+ 2;
899 if (!get_sha1(arg
, from_sha1
) && !get_sha1(next
, sha1
)) {
900 struct commit
*exclude
;
901 struct commit
*include
;
903 exclude
= get_commit_reference(arg
, from_sha1
, UNINTERESTING
);
904 include
= get_commit_reference(next
, sha1
, 0);
905 if (!exclude
|| !include
)
906 die("Invalid revision range %s..%s", arg
, next
);
908 handle_one_commit(exclude
, &list
);
909 handle_one_commit(include
, &list
);
915 flags
= UNINTERESTING
;
919 if (get_sha1(arg
, sha1
) < 0) {
921 if (lstat(arg
, &st
) < 0)
922 die("'%s': %s", arg
, strerror(errno
));
925 commit
= get_commit_reference(arg
, sha1
, flags
);
926 handle_one_commit(commit
, &list
);
930 (!(tag_objects
||tree_objects
||blob_objects
) && !pending_objects
))
931 usage(rev_list_usage
);
933 paths
= get_pathspec(prefix
, argv
+ i
);
936 diff_tree_setup_paths(paths
);
939 save_commit_buffer
= verbose_header
;
940 track_object_refs
= 0;
944 if (list
&& !limited
&& max_count
== 1 &&
945 !tag_objects
&& !tree_objects
&& !blob_objects
) {
946 show_commit(list
->item
);
950 list
= limit_list(list
);
952 sort_in_topological_order(&list
, lifo
);
953 show_commit_list(list
);
956 if (sort_list_in_merge_order(list
, &process_commit
)) {
957 die("merge order sort failed\n");
960 die("merge order sort unsupported, OpenSSL not linked");