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>... [ -- paths... ]\n"
26 " --merge-order [ --show-breaks ]\n"
28 " formatting output:\n"
32 " --header | --pretty\n"
38 static int unpacked
= 0;
39 static int bisect_list
= 0;
40 static int tag_objects
= 0;
41 static int tree_objects
= 0;
42 static int blob_objects
= 0;
43 static int verbose_header
= 0;
44 static int show_parents
= 0;
45 static int hdr_termination
= 0;
46 static const char *commit_prefix
= "";
47 static unsigned long max_age
= -1;
48 static unsigned long min_age
= -1;
49 static int max_count
= -1;
50 static enum cmit_fmt commit_format
= CMIT_FMT_RAW
;
51 static int merge_order
= 0;
52 static int show_breaks
= 0;
53 static int stop_traversal
= 0;
54 static int topo_order
= 0;
55 static int no_merges
= 0;
56 static const char **paths
= NULL
;
57 static int remove_empty_trees
= 0;
59 static void show_commit(struct commit
*commit
)
61 commit
->object
.flags
|= SHOWN
;
64 if (commit
->object
.flags
& DISCONTINUITY
) {
66 } else if (commit
->object
.flags
& BOUNDARY
) {
70 printf("%s%s", commit_prefix
, sha1_to_hex(commit
->object
.sha1
));
72 struct commit_list
*parents
= commit
->parents
;
74 printf(" %s", sha1_to_hex(parents
->item
->object
.sha1
));
75 parents
= parents
->next
;
78 if (commit_format
== CMIT_FMT_ONELINE
)
84 static char pretty_header
[16384];
85 pretty_print_commit(commit_format
, commit
->buffer
, ~0, pretty_header
, sizeof(pretty_header
));
86 printf("%s%c", pretty_header
, hdr_termination
);
91 static int rewrite_one(struct commit
**pp
)
94 struct commit
*p
= *pp
;
95 if (p
->object
.flags
& (TREECHANGE
| UNINTERESTING
))
99 *pp
= p
->parents
->item
;
103 static void rewrite_parents(struct commit
*commit
)
105 struct commit_list
**pp
= &commit
->parents
;
107 struct commit_list
*parent
= *pp
;
108 if (rewrite_one(&parent
->item
) < 0) {
116 static int filter_commit(struct commit
* commit
)
118 if (stop_traversal
&& (commit
->object
.flags
& BOUNDARY
))
120 if (commit
->object
.flags
& (UNINTERESTING
|SHOWN
))
122 if (min_age
!= -1 && (commit
->date
> min_age
))
124 if (max_age
!= -1 && (commit
->date
< max_age
)) {
128 if (no_merges
&& (commit
->parents
&& commit
->parents
->next
))
130 if (paths
&& dense
) {
131 if (!(commit
->object
.flags
& TREECHANGE
))
133 rewrite_parents(commit
);
138 static int process_commit(struct commit
* commit
)
140 int action
=filter_commit(commit
);
142 if (action
== STOP
) {
146 if (action
== CONTINUE
) {
150 if (max_count
!= -1 && !max_count
--)
158 static struct object_list
**add_object(struct object
*obj
, struct object_list
**p
, const char *name
)
160 struct object_list
*entry
= xmalloc(sizeof(*entry
));
168 static struct object_list
**process_blob(struct blob
*blob
, struct object_list
**p
, const char *name
)
170 struct object
*obj
= &blob
->object
;
174 if (obj
->flags
& (UNINTERESTING
| SEEN
))
177 return add_object(obj
, p
, name
);
180 static struct object_list
**process_tree(struct tree
*tree
, struct object_list
**p
, const char *name
)
182 struct object
*obj
= &tree
->object
;
183 struct tree_entry_list
*entry
;
187 if (obj
->flags
& (UNINTERESTING
| SEEN
))
189 if (parse_tree(tree
) < 0)
190 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
192 p
= add_object(obj
, p
, name
);
193 entry
= tree
->entries
;
194 tree
->entries
= NULL
;
196 struct tree_entry_list
*next
= entry
->next
;
197 if (entry
->directory
)
198 p
= process_tree(entry
->item
.tree
, p
, entry
->name
);
200 p
= process_blob(entry
->item
.blob
, p
, entry
->name
);
207 static struct object_list
*pending_objects
= NULL
;
209 static void show_commit_list(struct commit_list
*list
)
211 struct object_list
*objects
= NULL
, **p
= &objects
, *pending
;
213 struct commit
*commit
= pop_most_recent_commit(&list
, SEEN
);
215 p
= process_tree(commit
->tree
, p
, "");
216 if (process_commit(commit
) == STOP
)
219 for (pending
= pending_objects
; pending
; pending
= pending
->next
) {
220 struct object
*obj
= pending
->item
;
221 const char *name
= pending
->name
;
222 if (obj
->flags
& (UNINTERESTING
| SEEN
))
224 if (obj
->type
== tag_type
) {
226 p
= add_object(obj
, p
, name
);
229 if (obj
->type
== tree_type
) {
230 p
= process_tree((struct tree
*)obj
, p
, name
);
233 if (obj
->type
== blob_type
) {
234 p
= process_blob((struct blob
*)obj
, p
, name
);
237 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
240 /* An object with name "foo\n0000000000000000000000000000000000000000"
241 * can be used confuse downstream git-pack-objects very badly.
243 const char *ep
= strchr(objects
->name
, '\n');
245 printf("%s %.*s\n", sha1_to_hex(objects
->item
->sha1
),
246 (int) (ep
- objects
->name
),
250 printf("%s %s\n", sha1_to_hex(objects
->item
->sha1
), objects
->name
);
251 objects
= objects
->next
;
255 static void mark_blob_uninteresting(struct blob
*blob
)
259 if (blob
->object
.flags
& UNINTERESTING
)
261 blob
->object
.flags
|= UNINTERESTING
;
264 static void mark_tree_uninteresting(struct tree
*tree
)
266 struct object
*obj
= &tree
->object
;
267 struct tree_entry_list
*entry
;
271 if (obj
->flags
& UNINTERESTING
)
273 obj
->flags
|= UNINTERESTING
;
274 if (!has_sha1_file(obj
->sha1
))
276 if (parse_tree(tree
) < 0)
277 die("bad tree %s", sha1_to_hex(obj
->sha1
));
278 entry
= tree
->entries
;
279 tree
->entries
= NULL
;
281 struct tree_entry_list
*next
= entry
->next
;
282 if (entry
->directory
)
283 mark_tree_uninteresting(entry
->item
.tree
);
285 mark_blob_uninteresting(entry
->item
.blob
);
291 static void mark_parents_uninteresting(struct commit
*commit
)
293 struct commit_list
*parents
= commit
->parents
;
296 struct commit
*commit
= parents
->item
;
297 commit
->object
.flags
|= UNINTERESTING
;
300 * Normally we haven't parsed the parent
301 * yet, so we won't have a parent of a parent
302 * here. However, it may turn out that we've
303 * reached this commit some other way (where it
304 * wasn't uninteresting), in which case we need
305 * to mark its parents recursively too..
308 mark_parents_uninteresting(commit
);
311 * A missing commit is ok iff its parent is marked
314 * We just mark such a thing parsed, so that when
315 * it is popped next time around, we won't be trying
316 * to parse it and get an error.
318 if (!has_sha1_file(commit
->object
.sha1
))
319 commit
->object
.parsed
= 1;
320 parents
= parents
->next
;
324 static int everybody_uninteresting(struct commit_list
*orig
)
326 struct commit_list
*list
= orig
;
328 struct commit
*commit
= list
->item
;
330 if (commit
->object
.flags
& UNINTERESTING
)
338 * This is a truly stupid algorithm, but it's only
339 * used for bisection, and we just don't care enough.
341 * We care just barely enough to avoid recursing for
344 static int count_distance(struct commit_list
*entry
)
349 struct commit
*commit
= entry
->item
;
350 struct commit_list
*p
;
352 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
354 if (!paths
|| (commit
->object
.flags
& TREECHANGE
))
356 commit
->object
.flags
|= COUNTED
;
362 nr
+= count_distance(p
);
371 static void clear_distance(struct commit_list
*list
)
374 struct commit
*commit
= list
->item
;
375 commit
->object
.flags
&= ~COUNTED
;
380 static struct commit_list
*find_bisection(struct commit_list
*list
)
383 struct commit_list
*p
, *best
;
388 if (!paths
|| (p
->item
->object
.flags
& TREECHANGE
))
395 for (p
= list
; p
; p
= p
->next
) {
398 if (paths
&& !(p
->item
->object
.flags
& TREECHANGE
))
401 distance
= count_distance(p
);
402 clear_distance(list
);
403 if (nr
- distance
< distance
)
404 distance
= nr
- distance
;
405 if (distance
> closest
) {
415 static void mark_edges_uninteresting(struct commit_list
*list
)
417 for ( ; list
; list
= list
->next
) {
418 struct commit_list
*parents
= list
->item
->parents
;
420 for ( ; parents
; parents
= parents
->next
) {
421 struct commit
*commit
= parents
->item
;
422 if (commit
->object
.flags
& UNINTERESTING
)
423 mark_tree_uninteresting(commit
->tree
);
430 #define TREE_DIFFERENT 2
431 static int tree_difference
= TREE_SAME
;
433 static void file_add_remove(struct diff_options
*options
,
434 int addremove
, unsigned mode
,
435 const unsigned char *sha1
,
436 const char *base
, const char *path
)
438 int diff
= TREE_DIFFERENT
;
441 * Is it an add of a new file? It means that
442 * the old tree didn't have it at all, so we
443 * will turn "TREE_SAME" -> "TREE_NEW", but
444 * leave any "TREE_DIFFERENT" alone (and if
445 * it already was "TREE_NEW", we'll keep it
446 * "TREE_NEW" of course).
448 if (addremove
== '+') {
449 diff
= tree_difference
;
450 if (diff
!= TREE_SAME
)
454 tree_difference
= diff
;
457 static void file_change(struct diff_options
*options
,
458 unsigned old_mode
, unsigned new_mode
,
459 const unsigned char *old_sha1
,
460 const unsigned char *new_sha1
,
461 const char *base
, const char *path
)
463 tree_difference
= TREE_DIFFERENT
;
466 static struct diff_options diff_opt
= {
468 .add_remove
= file_add_remove
,
469 .change
= file_change
,
472 static int compare_tree(struct tree
*t1
, struct tree
*t2
)
477 return TREE_DIFFERENT
;
478 tree_difference
= TREE_SAME
;
479 if (diff_tree_sha1(t1
->object
.sha1
, t2
->object
.sha1
, "", &diff_opt
) < 0)
480 return TREE_DIFFERENT
;
481 return tree_difference
;
484 static int same_tree_as_empty(struct tree
*t1
)
488 struct tree_desc empty
, real
;
493 tree
= read_object_with_reference(t1
->object
.sha1
, "tree", &real
.size
, NULL
);
502 retval
= diff_tree(&empty
, &real
, "", &diff_opt
);
505 return retval
>= 0 && !tree_difference
;
508 static void try_to_simplify_commit(struct commit
*commit
)
510 struct commit_list
**pp
, *parent
;
515 if (!commit
->parents
) {
516 if (!same_tree_as_empty(commit
->tree
))
517 commit
->object
.flags
|= TREECHANGE
;
521 pp
= &commit
->parents
;
522 while ((parent
= *pp
) != NULL
) {
523 struct commit
*p
= parent
->item
;
525 if (p
->object
.flags
& UNINTERESTING
) {
531 switch (compare_tree(p
->tree
, commit
->tree
)) {
534 commit
->parents
= parent
;
538 if (remove_empty_trees
&& same_tree_as_empty(p
->tree
)) {
547 die("bad tree compare for commit %s", sha1_to_hex(commit
->object
.sha1
));
549 commit
->object
.flags
|= TREECHANGE
;
552 static void add_parents_to_list(struct commit
*commit
, struct commit_list
**list
)
554 struct commit_list
*parent
= commit
->parents
;
557 * If the commit is uninteresting, don't try to
558 * prune parents - we want the maximal uninteresting
561 * Normally we haven't parsed the parent
562 * yet, so we won't have a parent of a parent
563 * here. However, it may turn out that we've
564 * reached this commit some other way (where it
565 * wasn't uninteresting), in which case we need
566 * to mark its parents recursively too..
568 if (commit
->object
.flags
& UNINTERESTING
) {
570 struct commit
*p
= parent
->item
;
571 parent
= parent
->next
;
573 p
->object
.flags
|= UNINTERESTING
;
575 mark_parents_uninteresting(p
);
576 if (p
->object
.flags
& SEEN
)
578 p
->object
.flags
|= SEEN
;
579 insert_by_date(p
, list
);
585 * Ok, the commit wasn't uninteresting. Try to
586 * simplify the commit history and find the parent
587 * that has no differences in the path set if one exists.
590 try_to_simplify_commit(commit
);
592 parent
= commit
->parents
;
594 struct commit
*p
= parent
->item
;
596 parent
= parent
->next
;
599 if (p
->object
.flags
& SEEN
)
601 p
->object
.flags
|= SEEN
;
602 insert_by_date(p
, list
);
606 static struct commit_list
*limit_list(struct commit_list
*list
)
608 struct commit_list
*newlist
= NULL
;
609 struct commit_list
**p
= &newlist
;
611 struct commit_list
*entry
= list
;
612 struct commit
*commit
= list
->item
;
613 struct object
*obj
= &commit
->object
;
618 if (max_age
!= -1 && (commit
->date
< max_age
))
619 obj
->flags
|= UNINTERESTING
;
620 if (unpacked
&& has_sha1_pack(obj
->sha1
))
621 obj
->flags
|= UNINTERESTING
;
622 add_parents_to_list(commit
, &list
);
623 if (obj
->flags
& UNINTERESTING
) {
624 mark_parents_uninteresting(commit
);
625 if (everybody_uninteresting(list
))
629 if (min_age
!= -1 && (commit
->date
> min_age
))
631 p
= &commit_list_insert(commit
, p
)->next
;
634 mark_edges_uninteresting(newlist
);
636 newlist
= find_bisection(newlist
);
640 static void add_pending_object(struct object
*obj
, const char *name
)
642 add_object(obj
, &pending_objects
, name
);
645 static struct commit
*get_commit_reference(const char *name
, const unsigned char *sha1
, unsigned int flags
)
647 struct object
*object
;
649 object
= parse_object(sha1
);
651 die("bad object %s", name
);
654 * Tag object? Look what it points to..
656 while (object
->type
== tag_type
) {
657 struct tag
*tag
= (struct tag
*) object
;
658 object
->flags
|= flags
;
659 if (tag_objects
&& !(object
->flags
& UNINTERESTING
))
660 add_pending_object(object
, tag
->tag
);
661 object
= parse_object(tag
->tagged
->sha1
);
663 die("bad object %s", sha1_to_hex(tag
->tagged
->sha1
));
667 * Commit object? Just return it, we'll do all the complex
670 if (object
->type
== commit_type
) {
671 struct commit
*commit
= (struct commit
*)object
;
672 object
->flags
|= flags
;
673 if (parse_commit(commit
) < 0)
674 die("unable to parse commit %s", name
);
675 if (flags
& UNINTERESTING
)
676 mark_parents_uninteresting(commit
);
681 * Tree object? Either mark it uniniteresting, or add it
682 * to the list of objects to look at later..
684 if (object
->type
== tree_type
) {
685 struct tree
*tree
= (struct tree
*)object
;
688 if (flags
& UNINTERESTING
) {
689 mark_tree_uninteresting(tree
);
692 add_pending_object(object
, "");
697 * Blob object? You know the drill by now..
699 if (object
->type
== blob_type
) {
700 struct blob
*blob
= (struct blob
*)object
;
703 if (flags
& UNINTERESTING
) {
704 mark_blob_uninteresting(blob
);
707 add_pending_object(object
, "");
710 die("%s is unknown object", name
);
713 static void handle_one_commit(struct commit
*com
, struct commit_list
**lst
)
715 if (!com
|| com
->object
.flags
& SEEN
)
717 com
->object
.flags
|= SEEN
;
718 commit_list_insert(com
, lst
);
721 /* for_each_ref() callback does not allow user data -- Yuck. */
722 static struct commit_list
**global_lst
;
724 static int include_one_commit(const char *path
, const unsigned char *sha1
)
726 struct commit
*com
= get_commit_reference(path
, sha1
, 0);
727 handle_one_commit(com
, global_lst
);
731 static void handle_all(struct commit_list
**lst
)
734 for_each_ref(include_one_commit
);
738 int main(int argc
, const char **argv
)
740 const char *prefix
= setup_git_directory();
741 struct commit_list
*list
= NULL
;
744 for (i
= 1 ; i
< argc
; i
++) {
746 const char *arg
= argv
[i
];
748 struct commit
*commit
;
749 unsigned char sha1
[20];
751 if (!strncmp(arg
, "--max-count=", 12)) {
752 max_count
= atoi(arg
+ 12);
755 if (!strncmp(arg
, "--max-age=", 10)) {
756 max_age
= atoi(arg
+ 10);
760 if (!strncmp(arg
, "--min-age=", 10)) {
761 min_age
= atoi(arg
+ 10);
765 if (!strcmp(arg
, "--header")) {
769 if (!strncmp(arg
, "--pretty", 8)) {
770 commit_format
= get_commit_format(arg
+8);
772 hdr_termination
= '\n';
773 if (commit_format
== CMIT_FMT_ONELINE
)
776 commit_prefix
= "commit ";
779 if (!strncmp(arg
, "--no-merges", 11)) {
783 if (!strcmp(arg
, "--parents")) {
787 if (!strcmp(arg
, "--bisect")) {
791 if (!strcmp(arg
, "--all")) {
795 if (!strcmp(arg
, "--objects")) {
801 if (!strcmp(arg
, "--unpacked")) {
806 if (!strcmp(arg
, "--merge-order")) {
810 if (!strcmp(arg
, "--show-breaks")) {
814 if (!strcmp(arg
, "--topo-order")) {
819 if (!strcmp(arg
, "--dense")) {
823 if (!strcmp(arg
, "--sparse")) {
827 if (!strcmp(arg
, "--remove-empty")) {
828 remove_empty_trees
= 1;
831 if (!strcmp(arg
, "--")) {
836 if (show_breaks
&& !merge_order
)
837 usage(rev_list_usage
);
840 dotdot
= strstr(arg
, "..");
842 unsigned char from_sha1
[20];
843 char *next
= dotdot
+ 2;
847 if (!get_sha1(arg
, from_sha1
) && !get_sha1(next
, sha1
)) {
848 struct commit
*exclude
;
849 struct commit
*include
;
851 exclude
= get_commit_reference(arg
, from_sha1
, UNINTERESTING
);
852 include
= get_commit_reference(next
, sha1
, 0);
853 if (!exclude
|| !include
)
854 die("Invalid revision range %s..%s", arg
, next
);
856 handle_one_commit(exclude
, &list
);
857 handle_one_commit(include
, &list
);
863 flags
= UNINTERESTING
;
867 if (get_sha1(arg
, sha1
) < 0) {
869 if (lstat(arg
, &st
) < 0)
870 die("'%s': %s", arg
, strerror(errno
));
873 commit
= get_commit_reference(arg
, sha1
, flags
);
874 handle_one_commit(commit
, &list
);
878 (!(tag_objects
||tree_objects
||blob_objects
) && !pending_objects
))
879 usage(rev_list_usage
);
881 paths
= get_pathspec(prefix
, argv
+ i
);
884 diff_tree_setup_paths(paths
);
887 save_commit_buffer
= verbose_header
;
888 track_object_refs
= 0;
892 if (list
&& !limited
&& max_count
== 1 &&
893 !tag_objects
&& !tree_objects
&& !blob_objects
) {
894 show_commit(list
->item
);
898 list
= limit_list(list
);
900 sort_in_topological_order(&list
);
901 show_commit_list(list
);
904 if (sort_list_in_merge_order(list
, &process_commit
)) {
905 die("merge order sort failed\n");
908 die("merge order sort unsupported, OpenSSL not linked");