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"
23 " --merge-order [ --show-breaks ]";
25 static int unpacked
= 0;
26 static int bisect_list
= 0;
27 static int tag_objects
= 0;
28 static int tree_objects
= 0;
29 static int blob_objects
= 0;
30 static int verbose_header
= 0;
31 static int show_parents
= 0;
32 static int hdr_termination
= 0;
33 static const char *prefix
= "";
34 static unsigned long max_age
= -1;
35 static unsigned long min_age
= -1;
36 static int max_count
= -1;
37 static enum cmit_fmt commit_format
= CMIT_FMT_RAW
;
38 static int merge_order
= 0;
39 static int show_breaks
= 0;
40 static int stop_traversal
= 0;
41 static int topo_order
= 0;
43 static void show_commit(struct commit
*commit
)
45 commit
->object
.flags
|= SHOWN
;
48 if (commit
->object
.flags
& DISCONTINUITY
) {
50 } else if (commit
->object
.flags
& BOUNDARY
) {
54 printf("%s%s", prefix
, sha1_to_hex(commit
->object
.sha1
));
56 struct commit_list
*parents
= commit
->parents
;
58 printf(" %s", sha1_to_hex(parents
->item
->object
.sha1
));
59 parents
= parents
->next
;
64 static char pretty_header
[16384];
65 pretty_print_commit(commit_format
, commit
->buffer
, ~0, pretty_header
, sizeof(pretty_header
));
66 printf("%s%c", pretty_header
, hdr_termination
);
71 static int filter_commit(struct commit
* commit
)
73 if (stop_traversal
&& (commit
->object
.flags
& BOUNDARY
))
75 if (commit
->object
.flags
& (UNINTERESTING
|SHOWN
))
77 if (min_age
!= -1 && (commit
->date
> min_age
))
79 if (max_age
!= -1 && (commit
->date
< max_age
)) {
81 return merge_order
?CONTINUE
:STOP
;
83 if (max_count
!= -1 && !max_count
--)
88 static int process_commit(struct commit
* commit
)
90 int action
=filter_commit(commit
);
96 if (action
== CONTINUE
) {
105 static struct object_list
**add_object(struct object
*obj
, struct object_list
**p
, const char *name
)
107 struct object_list
*entry
= xmalloc(sizeof(*entry
));
115 static struct object_list
**process_blob(struct blob
*blob
, struct object_list
**p
, const char *name
)
117 struct object
*obj
= &blob
->object
;
121 if (obj
->flags
& (UNINTERESTING
| SEEN
))
124 return add_object(obj
, p
, name
);
127 static struct object_list
**process_tree(struct tree
*tree
, struct object_list
**p
, const char *name
)
129 struct object
*obj
= &tree
->object
;
130 struct tree_entry_list
*entry
;
134 if (obj
->flags
& (UNINTERESTING
| SEEN
))
136 if (parse_tree(tree
) < 0)
137 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
139 p
= add_object(obj
, p
, name
);
140 for (entry
= tree
->entries
; entry
; entry
= entry
->next
) {
141 if (entry
->directory
)
142 p
= process_tree(entry
->item
.tree
, p
, entry
->name
);
144 p
= process_blob(entry
->item
.blob
, p
, entry
->name
);
149 static struct object_list
*pending_objects
= NULL
;
151 static void show_commit_list(struct commit_list
*list
)
153 struct object_list
*objects
= NULL
, **p
= &objects
, *pending
;
155 struct commit
*commit
= pop_most_recent_commit(&list
, SEEN
);
157 p
= process_tree(commit
->tree
, p
, "");
158 if (process_commit(commit
) == STOP
)
161 for (pending
= pending_objects
; pending
; pending
= pending
->next
) {
162 struct object
*obj
= pending
->item
;
163 const char *name
= pending
->name
;
164 if (obj
->flags
& (UNINTERESTING
| SEEN
))
166 if (obj
->type
== tag_type
) {
168 p
= add_object(obj
, p
, name
);
171 if (obj
->type
== tree_type
) {
172 p
= process_tree((struct tree
*)obj
, p
, name
);
175 if (obj
->type
== blob_type
) {
176 p
= process_blob((struct blob
*)obj
, p
, name
);
179 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
182 printf("%s %s\n", sha1_to_hex(objects
->item
->sha1
), objects
->name
);
183 objects
= objects
->next
;
187 static void mark_blob_uninteresting(struct blob
*blob
)
191 if (blob
->object
.flags
& UNINTERESTING
)
193 blob
->object
.flags
|= UNINTERESTING
;
196 static void mark_tree_uninteresting(struct tree
*tree
)
198 struct object
*obj
= &tree
->object
;
199 struct tree_entry_list
*entry
;
203 if (obj
->flags
& UNINTERESTING
)
205 obj
->flags
|= UNINTERESTING
;
206 if (!has_sha1_file(obj
->sha1
))
208 if (parse_tree(tree
) < 0)
209 die("bad tree %s", sha1_to_hex(obj
->sha1
));
210 entry
= tree
->entries
;
212 if (entry
->directory
)
213 mark_tree_uninteresting(entry
->item
.tree
);
215 mark_blob_uninteresting(entry
->item
.blob
);
220 static void mark_parents_uninteresting(struct commit
*commit
)
222 struct commit_list
*parents
= commit
->parents
;
225 mark_tree_uninteresting(commit
->tree
);
227 struct commit
*commit
= parents
->item
;
228 commit
->object
.flags
|= UNINTERESTING
;
231 * Normally we haven't parsed the parent
232 * yet, so we won't have a parent of a parent
233 * here. However, it may turn out that we've
234 * reached this commit some other way (where it
235 * wasn't uninteresting), in which case we need
236 * to mark its parents recursively too..
239 mark_parents_uninteresting(commit
);
242 * A missing commit is ok iff its parent is marked
245 * We just mark such a thing parsed, so that when
246 * it is popped next time around, we won't be trying
247 * to parse it and get an error.
249 if (!has_sha1_file(commit
->object
.sha1
))
250 commit
->object
.parsed
= 1;
251 parents
= parents
->next
;
255 static int everybody_uninteresting(struct commit_list
*orig
)
257 struct commit_list
*list
= orig
;
259 struct commit
*commit
= list
->item
;
261 if (commit
->object
.flags
& UNINTERESTING
)
267 * Ok, go back and mark all the edge trees uninteresting,
268 * since otherwise we can have situations where a parent
269 * that was marked uninteresting (and we never even had
270 * to look at) had lots of objects that we don't want to
273 * NOTE! This still doesn't mean that the object list is
274 * "correct", since we may end up listing objects that
275 * even older commits (that we don't list) do actually
276 * reference, but it gets us to a minimal list (or very
277 * close) in practice.
283 struct commit
*commit
= orig
->item
;
284 if (!parse_commit(commit
) && commit
->tree
)
285 mark_tree_uninteresting(commit
->tree
);
292 * This is a truly stupid algorithm, but it's only
293 * used for bisection, and we just don't care enough.
295 * We care just barely enough to avoid recursing for
298 static int count_distance(struct commit_list
*entry
)
303 struct commit
*commit
= entry
->item
;
304 struct commit_list
*p
;
306 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
309 commit
->object
.flags
|= COUNTED
;
315 nr
+= count_distance(p
);
323 static void clear_distance(struct commit_list
*list
)
326 struct commit
*commit
= list
->item
;
327 commit
->object
.flags
&= ~COUNTED
;
332 static struct commit_list
*find_bisection(struct commit_list
*list
)
335 struct commit_list
*p
, *best
;
348 int distance
= count_distance(p
);
349 clear_distance(list
);
350 if (nr
- distance
< distance
)
351 distance
= nr
- distance
;
352 if (distance
> closest
) {
363 static struct commit_list
*limit_list(struct commit_list
*list
)
365 struct commit_list
*newlist
= NULL
;
366 struct commit_list
**p
= &newlist
;
368 struct commit
*commit
= pop_most_recent_commit(&list
, SEEN
);
369 struct object
*obj
= &commit
->object
;
371 if (unpacked
&& has_sha1_pack(obj
->sha1
))
372 obj
->flags
|= UNINTERESTING
;
373 if (obj
->flags
& UNINTERESTING
) {
374 mark_parents_uninteresting(commit
);
375 if (everybody_uninteresting(list
))
379 p
= &commit_list_insert(commit
, p
)->next
;
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
);
414 * Commit object? Just return it, we'll do all the complex
417 if (object
->type
== commit_type
) {
418 struct commit
*commit
= (struct commit
*)object
;
419 object
->flags
|= flags
;
420 if (parse_commit(commit
) < 0)
421 die("unable to parse commit %s", name
);
422 if (flags
& UNINTERESTING
)
423 mark_parents_uninteresting(commit
);
428 * Tree object? Either mark it uniniteresting, or add it
429 * to the list of objects to look at later..
431 if (object
->type
== tree_type
) {
432 struct tree
*tree
= (struct tree
*)object
;
435 if (flags
& UNINTERESTING
) {
436 mark_tree_uninteresting(tree
);
439 add_pending_object(object
, "");
444 * Blob object? You know the drill by now..
446 if (object
->type
== blob_type
) {
447 struct blob
*blob
= (struct blob
*)object
;
450 if (flags
& UNINTERESTING
) {
451 mark_blob_uninteresting(blob
);
454 add_pending_object(object
, "");
457 die("%s is unknown object", name
);
460 int main(int argc
, char **argv
)
462 struct commit_list
*list
= NULL
;
465 for (i
= 1 ; i
< argc
; i
++) {
468 struct commit
*commit
;
470 if (!strncmp(arg
, "--max-count=", 12)) {
471 max_count
= atoi(arg
+ 12);
474 if (!strncmp(arg
, "--max-age=", 10)) {
475 max_age
= atoi(arg
+ 10);
478 if (!strncmp(arg
, "--min-age=", 10)) {
479 min_age
= atoi(arg
+ 10);
482 if (!strcmp(arg
, "--header")) {
486 if (!strncmp(arg
, "--pretty", 8)) {
487 commit_format
= get_commit_format(arg
+8);
489 hdr_termination
= '\n';
493 if (!strcmp(arg
, "--parents")) {
497 if (!strcmp(arg
, "--bisect")) {
501 if (!strcmp(arg
, "--objects")) {
507 if (!strcmp(arg
, "--unpacked")) {
512 if (!strcmp(arg
, "--merge-order")) {
516 if (!strcmp(arg
, "--show-breaks")) {
520 if (!strcmp(arg
, "--topo-order")) {
528 flags
= UNINTERESTING
;
532 if (show_breaks
&& !merge_order
)
533 usage(rev_list_usage
);
534 commit
= get_commit_reference(arg
, flags
);
537 if (commit
->object
.flags
& SEEN
)
539 commit
->object
.flags
|= SEEN
;
540 commit_list_insert(commit
, &list
);
546 list
= limit_list(list
);
548 sort_in_topological_order(&list
);
549 show_commit_list(list
);
552 if (sort_list_in_merge_order(list
, &process_commit
)) {
553 die("merge order sort failed\n");
556 die("merge order sort unsupported, OpenSSL not linked");