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
)) {
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 entry
= tree
->entries
;
151 tree
->entries
= NULL
;
153 struct tree_entry_list
*next
= entry
->next
;
154 if (entry
->directory
)
155 p
= process_tree(entry
->item
.tree
, p
, entry
->name
);
157 p
= process_blob(entry
->item
.blob
, p
, entry
->name
);
164 static struct object_list
*pending_objects
= NULL
;
166 static void show_commit_list(struct commit_list
*list
)
168 struct object_list
*objects
= NULL
, **p
= &objects
, *pending
;
170 struct commit
*commit
= pop_most_recent_commit(&list
, SEEN
);
172 p
= process_tree(commit
->tree
, p
, "");
173 if (process_commit(commit
) == STOP
)
176 for (pending
= pending_objects
; pending
; pending
= pending
->next
) {
177 struct object
*obj
= pending
->item
;
178 const char *name
= pending
->name
;
179 if (obj
->flags
& (UNINTERESTING
| SEEN
))
181 if (obj
->type
== tag_type
) {
183 p
= add_object(obj
, p
, name
);
186 if (obj
->type
== tree_type
) {
187 p
= process_tree((struct tree
*)obj
, p
, name
);
190 if (obj
->type
== blob_type
) {
191 p
= process_blob((struct blob
*)obj
, p
, name
);
194 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
197 printf("%s %s\n", sha1_to_hex(objects
->item
->sha1
), objects
->name
);
198 objects
= objects
->next
;
202 static void mark_blob_uninteresting(struct blob
*blob
)
206 if (blob
->object
.flags
& UNINTERESTING
)
208 blob
->object
.flags
|= UNINTERESTING
;
211 static void mark_tree_uninteresting(struct tree
*tree
)
213 struct object
*obj
= &tree
->object
;
214 struct tree_entry_list
*entry
;
218 if (obj
->flags
& UNINTERESTING
)
220 obj
->flags
|= UNINTERESTING
;
221 if (!has_sha1_file(obj
->sha1
))
223 if (parse_tree(tree
) < 0)
224 die("bad tree %s", sha1_to_hex(obj
->sha1
));
225 entry
= tree
->entries
;
226 tree
->entries
= NULL
;
228 struct tree_entry_list
*next
= entry
->next
;
229 if (entry
->directory
)
230 mark_tree_uninteresting(entry
->item
.tree
);
232 mark_blob_uninteresting(entry
->item
.blob
);
238 static void mark_parents_uninteresting(struct commit
*commit
)
240 struct commit_list
*parents
= commit
->parents
;
243 struct commit
*commit
= parents
->item
;
244 commit
->object
.flags
|= UNINTERESTING
;
247 * Normally we haven't parsed the parent
248 * yet, so we won't have a parent of a parent
249 * here. However, it may turn out that we've
250 * reached this commit some other way (where it
251 * wasn't uninteresting), in which case we need
252 * to mark its parents recursively too..
255 mark_parents_uninteresting(commit
);
258 * A missing commit is ok iff its parent is marked
261 * We just mark such a thing parsed, so that when
262 * it is popped next time around, we won't be trying
263 * to parse it and get an error.
265 if (!has_sha1_file(commit
->object
.sha1
))
266 commit
->object
.parsed
= 1;
267 parents
= parents
->next
;
271 static int everybody_uninteresting(struct commit_list
*orig
)
273 struct commit_list
*list
= orig
;
275 struct commit
*commit
= list
->item
;
277 if (commit
->object
.flags
& UNINTERESTING
)
285 * This is a truly stupid algorithm, but it's only
286 * used for bisection, and we just don't care enough.
288 * We care just barely enough to avoid recursing for
291 static int count_distance(struct commit_list
*entry
)
296 struct commit
*commit
= entry
->item
;
297 struct commit_list
*p
;
299 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
302 commit
->object
.flags
|= COUNTED
;
308 nr
+= count_distance(p
);
316 static void clear_distance(struct commit_list
*list
)
319 struct commit
*commit
= list
->item
;
320 commit
->object
.flags
&= ~COUNTED
;
325 static struct commit_list
*find_bisection(struct commit_list
*list
)
328 struct commit_list
*p
, *best
;
341 int distance
= count_distance(p
);
342 clear_distance(list
);
343 if (nr
- distance
< distance
)
344 distance
= nr
- distance
;
345 if (distance
> closest
) {
356 static void mark_edges_uninteresting(struct commit_list
*list
)
358 for ( ; list
; list
= list
->next
) {
359 struct commit_list
*parents
= list
->item
->parents
;
361 for ( ; parents
; parents
= parents
->next
) {
362 struct commit
*commit
= parents
->item
;
363 if (commit
->object
.flags
& UNINTERESTING
)
364 mark_tree_uninteresting(commit
->tree
);
369 static struct commit_list
*limit_list(struct commit_list
*list
)
371 struct commit_list
*newlist
= NULL
;
372 struct commit_list
**p
= &newlist
;
374 struct commit
*commit
= pop_most_recent_commit(&list
, SEEN
);
375 struct object
*obj
= &commit
->object
;
377 if (max_age
!= -1 && (commit
->date
< max_age
))
378 obj
->flags
|= UNINTERESTING
;
379 if (unpacked
&& has_sha1_pack(obj
->sha1
))
380 obj
->flags
|= UNINTERESTING
;
381 if (obj
->flags
& UNINTERESTING
) {
382 mark_parents_uninteresting(commit
);
383 if (everybody_uninteresting(list
))
387 if (min_age
!= -1 && (commit
->date
> min_age
))
389 p
= &commit_list_insert(commit
, p
)->next
;
392 mark_edges_uninteresting(newlist
);
394 newlist
= find_bisection(newlist
);
398 static void add_pending_object(struct object
*obj
, const char *name
)
400 add_object(obj
, &pending_objects
, name
);
403 static struct commit
*get_commit_reference(const char *name
, unsigned int flags
)
405 unsigned char sha1
[20];
406 struct object
*object
;
408 if (get_sha1(name
, sha1
))
409 usage(rev_list_usage
);
410 object
= parse_object(sha1
);
412 die("bad object %s", name
);
415 * Tag object? Look what it points to..
417 while (object
->type
== tag_type
) {
418 struct tag
*tag
= (struct tag
*) object
;
419 object
->flags
|= flags
;
420 if (tag_objects
&& !(object
->flags
& UNINTERESTING
))
421 add_pending_object(object
, tag
->tag
);
422 object
= parse_object(tag
->tagged
->sha1
);
424 die("bad object %s", sha1_to_hex(tag
->tagged
->sha1
));
428 * Commit object? Just return it, we'll do all the complex
431 if (object
->type
== commit_type
) {
432 struct commit
*commit
= (struct commit
*)object
;
433 object
->flags
|= flags
;
434 if (parse_commit(commit
) < 0)
435 die("unable to parse commit %s", name
);
436 if (flags
& UNINTERESTING
)
437 mark_parents_uninteresting(commit
);
442 * Tree object? Either mark it uniniteresting, or add it
443 * to the list of objects to look at later..
445 if (object
->type
== tree_type
) {
446 struct tree
*tree
= (struct tree
*)object
;
449 if (flags
& UNINTERESTING
) {
450 mark_tree_uninteresting(tree
);
453 add_pending_object(object
, "");
458 * Blob object? You know the drill by now..
460 if (object
->type
== blob_type
) {
461 struct blob
*blob
= (struct blob
*)object
;
464 if (flags
& UNINTERESTING
) {
465 mark_blob_uninteresting(blob
);
468 add_pending_object(object
, "");
471 die("%s is unknown object", name
);
474 static void handle_one_commit(struct commit
*com
, struct commit_list
**lst
)
476 if (!com
|| com
->object
.flags
& SEEN
)
478 com
->object
.flags
|= SEEN
;
479 commit_list_insert(com
, lst
);
483 int main(int argc
, char **argv
)
485 struct commit_list
*list
= NULL
;
488 setup_git_directory();
489 for (i
= 1 ; i
< argc
; i
++) {
493 struct commit
*commit
;
495 if (!strncmp(arg
, "--max-count=", 12)) {
496 max_count
= atoi(arg
+ 12);
499 if (!strncmp(arg
, "--max-age=", 10)) {
500 max_age
= atoi(arg
+ 10);
504 if (!strncmp(arg
, "--min-age=", 10)) {
505 min_age
= atoi(arg
+ 10);
509 if (!strcmp(arg
, "--header")) {
513 if (!strncmp(arg
, "--pretty", 8)) {
514 commit_format
= get_commit_format(arg
+8);
516 hdr_termination
= '\n';
517 if (commit_format
== CMIT_FMT_ONELINE
)
520 commit_prefix
= "commit ";
523 if (!strncmp(arg
, "--no-merges", 11)) {
527 if (!strcmp(arg
, "--parents")) {
531 if (!strcmp(arg
, "--bisect")) {
535 if (!strcmp(arg
, "--objects")) {
541 if (!strcmp(arg
, "--unpacked")) {
546 if (!strcmp(arg
, "--merge-order")) {
550 if (!strcmp(arg
, "--show-breaks")) {
554 if (!strcmp(arg
, "--topo-order")) {
560 if (show_breaks
&& !merge_order
)
561 usage(rev_list_usage
);
564 dotdot
= strstr(arg
, "..");
566 char *next
= dotdot
+ 2;
567 struct commit
*exclude
= NULL
;
568 struct commit
*include
= NULL
;
572 exclude
= get_commit_reference(arg
, UNINTERESTING
);
573 include
= get_commit_reference(next
, 0);
574 if (exclude
&& include
) {
576 handle_one_commit(exclude
, &list
);
577 handle_one_commit(include
, &list
);
583 flags
= UNINTERESTING
;
587 commit
= get_commit_reference(arg
, flags
);
588 handle_one_commit(commit
, &list
);
591 save_commit_buffer
= verbose_header
;
592 track_object_refs
= 0;
597 list
= limit_list(list
);
599 sort_in_topological_order(&list
);
600 show_commit_list(list
);
603 if (sort_list_in_merge_order(list
, &process_commit
)) {
604 die("merge order sort failed\n");
607 die("merge order sort unsupported, OpenSSL not linked");