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 <commit-id>\n"
28 " --merge-order [ --show-breaks ]\n"
32 static int unpacked
= 0;
33 static int bisect_list
= 0;
34 static int tag_objects
= 0;
35 static int tree_objects
= 0;
36 static int blob_objects
= 0;
37 static int verbose_header
= 0;
38 static int show_parents
= 0;
39 static int hdr_termination
= 0;
40 static const char *commit_prefix
= "";
41 static unsigned long max_age
= -1;
42 static unsigned long min_age
= -1;
43 static int max_count
= -1;
44 static enum cmit_fmt commit_format
= CMIT_FMT_RAW
;
45 static int merge_order
= 0;
46 static int show_breaks
= 0;
47 static int stop_traversal
= 0;
48 static int topo_order
= 0;
49 static int no_merges
= 0;
50 static const char **paths
= NULL
;
52 static void show_commit(struct commit
*commit
)
54 commit
->object
.flags
|= SHOWN
;
57 if (commit
->object
.flags
& DISCONTINUITY
) {
59 } else if (commit
->object
.flags
& BOUNDARY
) {
63 printf("%s%s", commit_prefix
, sha1_to_hex(commit
->object
.sha1
));
65 struct commit_list
*parents
= commit
->parents
;
67 printf(" %s", sha1_to_hex(parents
->item
->object
.sha1
));
68 parents
= parents
->next
;
71 if (commit_format
== CMIT_FMT_ONELINE
)
77 static char pretty_header
[16384];
78 pretty_print_commit(commit_format
, commit
->buffer
, ~0, pretty_header
, sizeof(pretty_header
));
79 printf("%s%c", pretty_header
, hdr_termination
);
84 static int rewrite_one(struct commit
**pp
)
87 struct commit
*p
= *pp
;
88 if (p
->object
.flags
& (TREECHANGE
| UNINTERESTING
))
92 *pp
= p
->parents
->item
;
96 static void rewrite_parents(struct commit
*commit
)
98 struct commit_list
**pp
= &commit
->parents
;
100 struct commit_list
*parent
= *pp
;
101 if (rewrite_one(&parent
->item
) < 0) {
109 static int filter_commit(struct commit
* commit
)
111 if (stop_traversal
&& (commit
->object
.flags
& BOUNDARY
))
113 if (commit
->object
.flags
& (UNINTERESTING
|SHOWN
))
115 if (min_age
!= -1 && (commit
->date
> min_age
))
117 if (max_age
!= -1 && (commit
->date
< max_age
)) {
121 if (max_count
!= -1 && !max_count
--)
123 if (no_merges
&& (commit
->parents
&& commit
->parents
->next
))
125 if (paths
&& dense
) {
126 if (!(commit
->object
.flags
& TREECHANGE
))
128 rewrite_parents(commit
);
133 static int process_commit(struct commit
* commit
)
135 int action
=filter_commit(commit
);
137 if (action
== STOP
) {
141 if (action
== CONTINUE
) {
150 static struct object_list
**add_object(struct object
*obj
, struct object_list
**p
, const char *name
)
152 struct object_list
*entry
= xmalloc(sizeof(*entry
));
160 static struct object_list
**process_blob(struct blob
*blob
, struct object_list
**p
, const char *name
)
162 struct object
*obj
= &blob
->object
;
166 if (obj
->flags
& (UNINTERESTING
| SEEN
))
169 return add_object(obj
, p
, name
);
172 static struct object_list
**process_tree(struct tree
*tree
, struct object_list
**p
, const char *name
)
174 struct object
*obj
= &tree
->object
;
175 struct tree_entry_list
*entry
;
179 if (obj
->flags
& (UNINTERESTING
| SEEN
))
181 if (parse_tree(tree
) < 0)
182 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
184 p
= add_object(obj
, p
, name
);
185 entry
= tree
->entries
;
186 tree
->entries
= NULL
;
188 struct tree_entry_list
*next
= entry
->next
;
189 if (entry
->directory
)
190 p
= process_tree(entry
->item
.tree
, p
, entry
->name
);
192 p
= process_blob(entry
->item
.blob
, p
, entry
->name
);
199 static struct object_list
*pending_objects
= NULL
;
201 static void show_commit_list(struct commit_list
*list
)
203 struct object_list
*objects
= NULL
, **p
= &objects
, *pending
;
205 struct commit
*commit
= pop_most_recent_commit(&list
, SEEN
);
207 p
= process_tree(commit
->tree
, p
, "");
208 if (process_commit(commit
) == STOP
)
211 for (pending
= pending_objects
; pending
; pending
= pending
->next
) {
212 struct object
*obj
= pending
->item
;
213 const char *name
= pending
->name
;
214 if (obj
->flags
& (UNINTERESTING
| SEEN
))
216 if (obj
->type
== tag_type
) {
218 p
= add_object(obj
, p
, name
);
221 if (obj
->type
== tree_type
) {
222 p
= process_tree((struct tree
*)obj
, p
, name
);
225 if (obj
->type
== blob_type
) {
226 p
= process_blob((struct blob
*)obj
, p
, name
);
229 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
232 /* An object with name "foo\n0000000000000000000000000000000000000000"
233 * can be used confuse downstream git-pack-objects very badly.
235 const char *ep
= strchr(objects
->name
, '\n');
237 printf("%s %.*s\n", sha1_to_hex(objects
->item
->sha1
),
238 (int) (ep
- objects
->name
),
242 printf("%s %s\n", sha1_to_hex(objects
->item
->sha1
), objects
->name
);
243 objects
= objects
->next
;
247 static void mark_blob_uninteresting(struct blob
*blob
)
251 if (blob
->object
.flags
& UNINTERESTING
)
253 blob
->object
.flags
|= UNINTERESTING
;
256 static void mark_tree_uninteresting(struct tree
*tree
)
258 struct object
*obj
= &tree
->object
;
259 struct tree_entry_list
*entry
;
263 if (obj
->flags
& UNINTERESTING
)
265 obj
->flags
|= UNINTERESTING
;
266 if (!has_sha1_file(obj
->sha1
))
268 if (parse_tree(tree
) < 0)
269 die("bad tree %s", sha1_to_hex(obj
->sha1
));
270 entry
= tree
->entries
;
271 tree
->entries
= NULL
;
273 struct tree_entry_list
*next
= entry
->next
;
274 if (entry
->directory
)
275 mark_tree_uninteresting(entry
->item
.tree
);
277 mark_blob_uninteresting(entry
->item
.blob
);
283 static void mark_parents_uninteresting(struct commit
*commit
)
285 struct commit_list
*parents
= commit
->parents
;
288 struct commit
*commit
= parents
->item
;
289 commit
->object
.flags
|= UNINTERESTING
;
292 * Normally we haven't parsed the parent
293 * yet, so we won't have a parent of a parent
294 * here. However, it may turn out that we've
295 * reached this commit some other way (where it
296 * wasn't uninteresting), in which case we need
297 * to mark its parents recursively too..
300 mark_parents_uninteresting(commit
);
303 * A missing commit is ok iff its parent is marked
306 * We just mark such a thing parsed, so that when
307 * it is popped next time around, we won't be trying
308 * to parse it and get an error.
310 if (!has_sha1_file(commit
->object
.sha1
))
311 commit
->object
.parsed
= 1;
312 parents
= parents
->next
;
316 static int everybody_uninteresting(struct commit_list
*orig
)
318 struct commit_list
*list
= orig
;
320 struct commit
*commit
= list
->item
;
322 if (commit
->object
.flags
& UNINTERESTING
)
330 * This is a truly stupid algorithm, but it's only
331 * used for bisection, and we just don't care enough.
333 * We care just barely enough to avoid recursing for
336 static int count_distance(struct commit_list
*entry
)
341 struct commit
*commit
= entry
->item
;
342 struct commit_list
*p
;
344 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
347 commit
->object
.flags
|= COUNTED
;
353 nr
+= count_distance(p
);
361 static void clear_distance(struct commit_list
*list
)
364 struct commit
*commit
= list
->item
;
365 commit
->object
.flags
&= ~COUNTED
;
370 static struct commit_list
*find_bisection(struct commit_list
*list
)
373 struct commit_list
*p
, *best
;
386 int distance
= count_distance(p
);
387 clear_distance(list
);
388 if (nr
- distance
< distance
)
389 distance
= nr
- distance
;
390 if (distance
> closest
) {
401 static void mark_edges_uninteresting(struct commit_list
*list
)
403 for ( ; list
; list
= list
->next
) {
404 struct commit_list
*parents
= list
->item
->parents
;
406 for ( ; parents
; parents
= parents
->next
) {
407 struct commit
*commit
= parents
->item
;
408 if (commit
->object
.flags
& UNINTERESTING
)
409 mark_tree_uninteresting(commit
->tree
);
414 static int is_different
= 0;
416 static void file_add_remove(struct diff_options
*options
,
417 int addremove
, unsigned mode
,
418 const unsigned char *sha1
,
419 const char *base
, const char *path
)
424 static void file_change(struct diff_options
*options
,
425 unsigned old_mode
, unsigned new_mode
,
426 const unsigned char *old_sha1
,
427 const unsigned char *new_sha1
,
428 const char *base
, const char *path
)
433 static struct diff_options diff_opt
= {
435 .add_remove
= file_add_remove
,
436 .change
= file_change
,
439 static int same_tree(struct tree
*t1
, struct tree
*t2
)
442 if (diff_tree_sha1(t1
->object
.sha1
, t2
->object
.sha1
, "", &diff_opt
) < 0)
444 return !is_different
;
447 static int same_tree_as_empty(struct tree
*t1
)
451 struct tree_desc empty
, real
;
456 tree
= read_object_with_reference(t1
->object
.sha1
, "tree", &real
.size
, NULL
);
465 retval
= diff_tree(&empty
, &real
, "", &diff_opt
);
468 return retval
>= 0 && !is_different
;
471 static struct commit
*try_to_simplify_merge(struct commit
*commit
, struct commit_list
*parent
)
477 struct commit
*p
= parent
->item
;
478 parent
= parent
->next
;
482 if (same_tree(commit
->tree
, p
->tree
))
488 static void add_parents_to_list(struct commit
*commit
, struct commit_list
**list
)
490 struct commit_list
*parent
= commit
->parents
;
493 * If the commit is uninteresting, don't try to
494 * prune parents - we want the maximal uninteresting
497 * Normally we haven't parsed the parent
498 * yet, so we won't have a parent of a parent
499 * here. However, it may turn out that we've
500 * reached this commit some other way (where it
501 * wasn't uninteresting), in which case we need
502 * to mark its parents recursively too..
504 if (commit
->object
.flags
& UNINTERESTING
) {
506 struct commit
*p
= parent
->item
;
507 parent
= parent
->next
;
509 p
->object
.flags
|= UNINTERESTING
;
511 mark_parents_uninteresting(p
);
512 if (p
->object
.flags
& SEEN
)
514 p
->object
.flags
|= SEEN
;
515 insert_by_date(p
, list
);
521 * Ok, the commit wasn't uninteresting. If it
522 * is a merge, try to find the parent that has
523 * no differences in the path set if one exists.
525 if (paths
&& parent
&& parent
->next
) {
526 struct commit
*preferred
;
528 preferred
= try_to_simplify_merge(commit
, parent
);
530 parent
->item
= preferred
;
536 struct commit
*p
= parent
->item
;
538 parent
= parent
->next
;
541 if (p
->object
.flags
& SEEN
)
543 p
->object
.flags
|= SEEN
;
544 insert_by_date(p
, list
);
548 static void compress_list(struct commit_list
*list
)
551 struct commit
*commit
= list
->item
;
552 struct commit_list
*parent
= commit
->parents
;
556 if (!same_tree_as_empty(commit
->tree
))
557 commit
->object
.flags
|= TREECHANGE
;
562 * Exactly one parent? Check if it leaves the tree
566 struct tree
*t1
= commit
->tree
;
567 struct tree
*t2
= parent
->item
->tree
;
568 if (!t1
|| !t2
|| same_tree(t1
, t2
))
571 commit
->object
.flags
|= TREECHANGE
;
575 static struct commit_list
*limit_list(struct commit_list
*list
)
577 struct commit_list
*newlist
= NULL
;
578 struct commit_list
**p
= &newlist
;
580 struct commit_list
*entry
= list
;
581 struct commit
*commit
= list
->item
;
582 struct object
*obj
= &commit
->object
;
587 if (max_age
!= -1 && (commit
->date
< max_age
))
588 obj
->flags
|= UNINTERESTING
;
589 if (unpacked
&& has_sha1_pack(obj
->sha1
))
590 obj
->flags
|= UNINTERESTING
;
591 add_parents_to_list(commit
, &list
);
592 if (obj
->flags
& UNINTERESTING
) {
593 mark_parents_uninteresting(commit
);
594 if (everybody_uninteresting(list
))
598 if (min_age
!= -1 && (commit
->date
> min_age
))
600 p
= &commit_list_insert(commit
, p
)->next
;
603 mark_edges_uninteresting(newlist
);
605 compress_list(newlist
);
607 newlist
= find_bisection(newlist
);
611 static void add_pending_object(struct object
*obj
, const char *name
)
613 add_object(obj
, &pending_objects
, name
);
616 static struct commit
*get_commit_reference(const char *name
, const unsigned char *sha1
, unsigned int flags
)
618 struct object
*object
;
620 object
= parse_object(sha1
);
622 die("bad object %s", name
);
625 * Tag object? Look what it points to..
627 while (object
->type
== tag_type
) {
628 struct tag
*tag
= (struct tag
*) object
;
629 object
->flags
|= flags
;
630 if (tag_objects
&& !(object
->flags
& UNINTERESTING
))
631 add_pending_object(object
, tag
->tag
);
632 object
= parse_object(tag
->tagged
->sha1
);
634 die("bad object %s", sha1_to_hex(tag
->tagged
->sha1
));
638 * Commit object? Just return it, we'll do all the complex
641 if (object
->type
== commit_type
) {
642 struct commit
*commit
= (struct commit
*)object
;
643 object
->flags
|= flags
;
644 if (parse_commit(commit
) < 0)
645 die("unable to parse commit %s", name
);
646 if (flags
& UNINTERESTING
)
647 mark_parents_uninteresting(commit
);
652 * Tree object? Either mark it uniniteresting, or add it
653 * to the list of objects to look at later..
655 if (object
->type
== tree_type
) {
656 struct tree
*tree
= (struct tree
*)object
;
659 if (flags
& UNINTERESTING
) {
660 mark_tree_uninteresting(tree
);
663 add_pending_object(object
, "");
668 * Blob object? You know the drill by now..
670 if (object
->type
== blob_type
) {
671 struct blob
*blob
= (struct blob
*)object
;
674 if (flags
& UNINTERESTING
) {
675 mark_blob_uninteresting(blob
);
678 add_pending_object(object
, "");
681 die("%s is unknown object", name
);
684 static void handle_one_commit(struct commit
*com
, struct commit_list
**lst
)
686 if (!com
|| com
->object
.flags
& SEEN
)
688 com
->object
.flags
|= SEEN
;
689 commit_list_insert(com
, lst
);
692 /* for_each_ref() callback does not allow user data -- Yuck. */
693 static struct commit_list
**global_lst
;
695 static int include_one_commit(const char *path
, const unsigned char *sha1
)
697 struct commit
*com
= get_commit_reference(path
, sha1
, 0);
698 handle_one_commit(com
, global_lst
);
702 static void handle_all(struct commit_list
**lst
)
705 for_each_ref(include_one_commit
);
709 int main(int argc
, const char **argv
)
711 const char *prefix
= setup_git_directory();
712 struct commit_list
*list
= NULL
;
715 for (i
= 1 ; i
< argc
; i
++) {
717 const char *arg
= argv
[i
];
719 struct commit
*commit
;
720 unsigned char sha1
[20];
722 if (!strncmp(arg
, "--max-count=", 12)) {
723 max_count
= atoi(arg
+ 12);
726 if (!strncmp(arg
, "--max-age=", 10)) {
727 max_age
= atoi(arg
+ 10);
731 if (!strncmp(arg
, "--min-age=", 10)) {
732 min_age
= atoi(arg
+ 10);
736 if (!strcmp(arg
, "--header")) {
740 if (!strncmp(arg
, "--pretty", 8)) {
741 commit_format
= get_commit_format(arg
+8);
743 hdr_termination
= '\n';
744 if (commit_format
== CMIT_FMT_ONELINE
)
747 commit_prefix
= "commit ";
750 if (!strncmp(arg
, "--no-merges", 11)) {
754 if (!strcmp(arg
, "--parents")) {
758 if (!strcmp(arg
, "--bisect")) {
762 if (!strcmp(arg
, "--all")) {
766 if (!strcmp(arg
, "--objects")) {
772 if (!strcmp(arg
, "--unpacked")) {
777 if (!strcmp(arg
, "--merge-order")) {
781 if (!strcmp(arg
, "--show-breaks")) {
785 if (!strcmp(arg
, "--topo-order")) {
790 if (!strcmp(arg
, "--dense")) {
794 if (!strcmp(arg
, "--sparse")) {
798 if (!strcmp(arg
, "--")) {
803 if (show_breaks
&& !merge_order
)
804 usage(rev_list_usage
);
807 dotdot
= strstr(arg
, "..");
809 unsigned char from_sha1
[20];
810 char *next
= dotdot
+ 2;
814 if (!get_sha1(arg
, from_sha1
) && !get_sha1(next
, sha1
)) {
815 struct commit
*exclude
;
816 struct commit
*include
;
818 exclude
= get_commit_reference(arg
, from_sha1
, UNINTERESTING
);
819 include
= get_commit_reference(next
, sha1
, 0);
820 if (!exclude
|| !include
)
821 die("Invalid revision range %s..%s", arg
, next
);
823 handle_one_commit(exclude
, &list
);
824 handle_one_commit(include
, &list
);
830 flags
= UNINTERESTING
;
834 if (get_sha1(arg
, sha1
) < 0)
836 commit
= get_commit_reference(arg
, sha1
, flags
);
837 handle_one_commit(commit
, &list
);
841 usage(rev_list_usage
);
843 paths
= get_pathspec(prefix
, argv
+ i
);
846 diff_tree_setup_paths(paths
);
849 save_commit_buffer
= verbose_header
;
850 track_object_refs
= 0;
854 if (list
&& !limited
&& max_count
== 1 &&
855 !tag_objects
&& !tree_objects
&& !blob_objects
) {
856 show_commit(list
->item
);
860 list
= limit_list(list
);
862 sort_in_topological_order(&list
);
863 show_commit_list(list
);
866 if (sort_list_in_merge_order(list
, &process_commit
)) {
867 die("merge order sort failed\n");
870 die("merge order sort unsupported, OpenSSL not linked");