9 #define INTERESTING (1u << 1)
10 #define COUNTED (1u << 2)
11 #define SHOWN (1u << 3)
13 static const char rev_list_usage
[] =
14 "git-rev-list [OPTION] commit-id <commit-id>\n"
25 " --merge-order [ --show-breaks ]\n"
28 static int unpacked
= 0;
29 static int bisect_list
= 0;
30 static int tag_objects
= 0;
31 static int tree_objects
= 0;
32 static int blob_objects
= 0;
33 static int verbose_header
= 0;
34 static int show_parents
= 0;
35 static int hdr_termination
= 0;
36 static const char *commit_prefix
= "";
37 static unsigned long max_age
= -1;
38 static unsigned long min_age
= -1;
39 static int max_count
= -1;
40 static enum cmit_fmt commit_format
= CMIT_FMT_RAW
;
41 static int merge_order
= 0;
42 static int show_breaks
= 0;
43 static int stop_traversal
= 0;
44 static int topo_order
= 0;
45 static int no_merges
= 0;
47 static void show_commit(struct commit
*commit
)
49 commit
->object
.flags
|= SHOWN
;
52 if (commit
->object
.flags
& DISCONTINUITY
) {
54 } else if (commit
->object
.flags
& BOUNDARY
) {
58 printf("%s%s", commit_prefix
, sha1_to_hex(commit
->object
.sha1
));
60 struct commit_list
*parents
= commit
->parents
;
62 printf(" %s", sha1_to_hex(parents
->item
->object
.sha1
));
63 parents
= parents
->next
;
66 if (commit_format
== CMIT_FMT_ONELINE
)
72 static char pretty_header
[16384];
73 pretty_print_commit(commit_format
, commit
->buffer
, ~0, pretty_header
, sizeof(pretty_header
));
74 printf("%s%c", pretty_header
, hdr_termination
);
79 static int filter_commit(struct commit
* commit
)
81 if (stop_traversal
&& (commit
->object
.flags
& BOUNDARY
))
83 if (commit
->object
.flags
& (UNINTERESTING
|SHOWN
))
85 if (min_age
!= -1 && (commit
->date
> min_age
))
87 if (max_age
!= -1 && (commit
->date
< max_age
)) {
89 return merge_order
?CONTINUE
:STOP
;
91 if (max_count
!= -1 && !max_count
--)
93 if (no_merges
&& (commit
->parents
&& commit
->parents
->next
))
98 static int process_commit(struct commit
* commit
)
100 int action
=filter_commit(commit
);
102 if (action
== STOP
) {
106 if (action
== CONTINUE
) {
115 static struct object_list
**add_object(struct object
*obj
, struct object_list
**p
, const char *name
)
117 struct object_list
*entry
= xmalloc(sizeof(*entry
));
125 static struct object_list
**process_blob(struct blob
*blob
, struct object_list
**p
, const char *name
)
127 struct object
*obj
= &blob
->object
;
131 if (obj
->flags
& (UNINTERESTING
| SEEN
))
134 return add_object(obj
, p
, name
);
137 static struct object_list
**process_tree(struct tree
*tree
, struct object_list
**p
, const char *name
)
139 struct object
*obj
= &tree
->object
;
140 struct tree_entry_list
*entry
;
144 if (obj
->flags
& (UNINTERESTING
| SEEN
))
146 if (parse_tree(tree
) < 0)
147 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
149 p
= add_object(obj
, p
, name
);
150 for (entry
= tree
->entries
; entry
; entry
= entry
->next
) {
151 if (entry
->directory
)
152 p
= process_tree(entry
->item
.tree
, p
, entry
->name
);
154 p
= process_blob(entry
->item
.blob
, p
, entry
->name
);
159 static struct object_list
*pending_objects
= NULL
;
161 static void show_commit_list(struct commit_list
*list
)
163 struct object_list
*objects
= NULL
, **p
= &objects
, *pending
;
165 struct commit
*commit
= pop_most_recent_commit(&list
, SEEN
);
167 p
= process_tree(commit
->tree
, p
, "");
168 if (process_commit(commit
) == STOP
)
171 for (pending
= pending_objects
; pending
; pending
= pending
->next
) {
172 struct object
*obj
= pending
->item
;
173 const char *name
= pending
->name
;
174 if (obj
->flags
& (UNINTERESTING
| SEEN
))
176 if (obj
->type
== tag_type
) {
178 p
= add_object(obj
, p
, name
);
181 if (obj
->type
== tree_type
) {
182 p
= process_tree((struct tree
*)obj
, p
, name
);
185 if (obj
->type
== blob_type
) {
186 p
= process_blob((struct blob
*)obj
, p
, name
);
189 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
192 printf("%s %s\n", sha1_to_hex(objects
->item
->sha1
), objects
->name
);
193 objects
= objects
->next
;
197 static void mark_blob_uninteresting(struct blob
*blob
)
201 if (blob
->object
.flags
& UNINTERESTING
)
203 blob
->object
.flags
|= UNINTERESTING
;
206 static void mark_tree_uninteresting(struct tree
*tree
)
208 struct object
*obj
= &tree
->object
;
209 struct tree_entry_list
*entry
;
213 if (obj
->flags
& UNINTERESTING
)
215 obj
->flags
|= UNINTERESTING
;
216 if (!has_sha1_file(obj
->sha1
))
218 if (parse_tree(tree
) < 0)
219 die("bad tree %s", sha1_to_hex(obj
->sha1
));
220 entry
= tree
->entries
;
222 if (entry
->directory
)
223 mark_tree_uninteresting(entry
->item
.tree
);
225 mark_blob_uninteresting(entry
->item
.blob
);
230 static void mark_parents_uninteresting(struct commit
*commit
)
232 struct commit_list
*parents
= commit
->parents
;
235 struct commit
*commit
= parents
->item
;
236 commit
->object
.flags
|= UNINTERESTING
;
239 * Normally we haven't parsed the parent
240 * yet, so we won't have a parent of a parent
241 * here. However, it may turn out that we've
242 * reached this commit some other way (where it
243 * wasn't uninteresting), in which case we need
244 * to mark its parents recursively too..
247 mark_parents_uninteresting(commit
);
250 * A missing commit is ok iff its parent is marked
253 * We just mark such a thing parsed, so that when
254 * it is popped next time around, we won't be trying
255 * to parse it and get an error.
257 if (!has_sha1_file(commit
->object
.sha1
))
258 commit
->object
.parsed
= 1;
259 parents
= parents
->next
;
263 static int everybody_uninteresting(struct commit_list
*orig
)
265 struct commit_list
*list
= orig
;
267 struct commit
*commit
= list
->item
;
269 if (commit
->object
.flags
& UNINTERESTING
)
277 * This is a truly stupid algorithm, but it's only
278 * used for bisection, and we just don't care enough.
280 * We care just barely enough to avoid recursing for
283 static int count_distance(struct commit_list
*entry
)
288 struct commit
*commit
= entry
->item
;
289 struct commit_list
*p
;
291 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
294 commit
->object
.flags
|= COUNTED
;
300 nr
+= count_distance(p
);
308 static void clear_distance(struct commit_list
*list
)
311 struct commit
*commit
= list
->item
;
312 commit
->object
.flags
&= ~COUNTED
;
317 static struct commit_list
*find_bisection(struct commit_list
*list
)
320 struct commit_list
*p
, *best
;
333 int distance
= count_distance(p
);
334 clear_distance(list
);
335 if (nr
- distance
< distance
)
336 distance
= nr
- distance
;
337 if (distance
> closest
) {
348 static void mark_edges_uninteresting(struct commit_list
*list
)
350 for ( ; list
; list
= list
->next
) {
351 struct commit_list
*parents
= list
->item
->parents
;
353 for ( ; parents
; parents
= parents
->next
) {
354 struct commit
*commit
= parents
->item
;
355 if (commit
->object
.flags
& UNINTERESTING
)
356 mark_tree_uninteresting(commit
->tree
);
361 static struct commit_list
*limit_list(struct commit_list
*list
)
363 struct commit_list
*newlist
= NULL
;
364 struct commit_list
**p
= &newlist
;
366 struct commit
*commit
= pop_most_recent_commit(&list
, SEEN
);
367 struct object
*obj
= &commit
->object
;
369 if (unpacked
&& has_sha1_pack(obj
->sha1
))
370 obj
->flags
|= UNINTERESTING
;
371 if (obj
->flags
& UNINTERESTING
) {
372 mark_parents_uninteresting(commit
);
373 if (everybody_uninteresting(list
))
377 p
= &commit_list_insert(commit
, p
)->next
;
380 mark_edges_uninteresting(newlist
);
382 newlist
= find_bisection(newlist
);
386 static void add_pending_object(struct object
*obj
, const char *name
)
388 add_object(obj
, &pending_objects
, name
);
391 static struct commit
*get_commit_reference(const char *name
, unsigned int flags
)
393 unsigned char sha1
[20];
394 struct object
*object
;
396 if (get_sha1(name
, sha1
))
397 usage(rev_list_usage
);
398 object
= parse_object(sha1
);
400 die("bad object %s", name
);
403 * Tag object? Look what it points to..
405 while (object
->type
== tag_type
) {
406 struct tag
*tag
= (struct tag
*) object
;
407 object
->flags
|= flags
;
408 if (tag_objects
&& !(object
->flags
& UNINTERESTING
))
409 add_pending_object(object
, tag
->tag
);
410 object
= parse_object(tag
->tagged
->sha1
);
412 die("bad object %s", sha1_to_hex(tag
->tagged
->sha1
));
416 * Commit object? Just return it, we'll do all the complex
419 if (object
->type
== commit_type
) {
420 struct commit
*commit
= (struct commit
*)object
;
421 object
->flags
|= flags
;
422 if (parse_commit(commit
) < 0)
423 die("unable to parse commit %s", name
);
424 if (flags
& UNINTERESTING
)
425 mark_parents_uninteresting(commit
);
430 * Tree object? Either mark it uniniteresting, or add it
431 * to the list of objects to look at later..
433 if (object
->type
== tree_type
) {
434 struct tree
*tree
= (struct tree
*)object
;
437 if (flags
& UNINTERESTING
) {
438 mark_tree_uninteresting(tree
);
441 add_pending_object(object
, "");
446 * Blob object? You know the drill by now..
448 if (object
->type
== blob_type
) {
449 struct blob
*blob
= (struct blob
*)object
;
452 if (flags
& UNINTERESTING
) {
453 mark_blob_uninteresting(blob
);
456 add_pending_object(object
, "");
459 die("%s is unknown object", name
);
462 static void handle_one_commit(struct commit
*com
, struct commit_list
**lst
)
464 if (!com
|| com
->object
.flags
& SEEN
)
466 com
->object
.flags
|= SEEN
;
467 commit_list_insert(com
, lst
);
471 int main(int argc
, char **argv
)
473 struct commit_list
*list
= NULL
;
476 setup_git_directory();
477 for (i
= 1 ; i
< argc
; i
++) {
481 struct commit
*commit
;
483 if (!strncmp(arg
, "--max-count=", 12)) {
484 max_count
= atoi(arg
+ 12);
487 if (!strncmp(arg
, "--max-age=", 10)) {
488 max_age
= atoi(arg
+ 10);
491 if (!strncmp(arg
, "--min-age=", 10)) {
492 min_age
= atoi(arg
+ 10);
495 if (!strcmp(arg
, "--header")) {
499 if (!strncmp(arg
, "--pretty", 8)) {
500 commit_format
= get_commit_format(arg
+8);
502 hdr_termination
= '\n';
503 if (commit_format
== CMIT_FMT_ONELINE
)
506 commit_prefix
= "commit ";
509 if (!strncmp(arg
, "--no-merges", 11)) {
513 if (!strcmp(arg
, "--parents")) {
517 if (!strcmp(arg
, "--bisect")) {
521 if (!strcmp(arg
, "--objects")) {
527 if (!strcmp(arg
, "--unpacked")) {
532 if (!strcmp(arg
, "--merge-order")) {
536 if (!strcmp(arg
, "--show-breaks")) {
540 if (!strcmp(arg
, "--topo-order")) {
546 if (show_breaks
&& !merge_order
)
547 usage(rev_list_usage
);
550 dotdot
= strstr(arg
, "..");
552 char *next
= dotdot
+ 2;
553 struct commit
*exclude
= NULL
;
554 struct commit
*include
= NULL
;
556 exclude
= get_commit_reference(arg
, UNINTERESTING
);
557 include
= get_commit_reference(next
, 0);
558 if (exclude
&& include
) {
560 handle_one_commit(exclude
, &list
);
561 handle_one_commit(include
, &list
);
567 flags
= UNINTERESTING
;
571 commit
= get_commit_reference(arg
, flags
);
572 handle_one_commit(commit
, &list
);
575 save_commit_buffer
= verbose_header
;
580 list
= limit_list(list
);
582 sort_in_topological_order(&list
);
583 show_commit_list(list
);
586 if (sort_list_in_merge_order(list
, &process_commit
)) {
587 die("merge order sort failed\n");
590 die("merge order sort unsupported, OpenSSL not linked");