9 #define INTERESTING (1u << 1)
10 #define COUNTED (1u << 2)
11 #define SHOWN (LAST_EPOCH_FLAG << 2)
13 static const char rev_list_usage
[] =
14 "usage: 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;
42 static void show_commit(struct commit
*commit
)
44 commit
->object
.flags
|= SHOWN
;
47 if (commit
->object
.flags
& DISCONTINUITY
) {
49 } else if (commit
->object
.flags
& BOUNDARY
) {
53 printf("%s%s", prefix
, sha1_to_hex(commit
->object
.sha1
));
55 struct commit_list
*parents
= commit
->parents
;
57 printf(" %s", sha1_to_hex(parents
->item
->object
.sha1
));
58 parents
= parents
->next
;
63 static char pretty_header
[16384];
64 pretty_print_commit(commit_format
, commit
->buffer
, ~0, pretty_header
, sizeof(pretty_header
));
65 printf("%s%c", pretty_header
, hdr_termination
);
70 static int filter_commit(struct commit
* commit
)
72 if (merge_order
&& stop_traversal
&& commit
->object
.flags
& BOUNDARY
)
74 if (commit
->object
.flags
& (UNINTERESTING
|SHOWN
))
76 if (min_age
!= -1 && (commit
->date
> min_age
))
78 if (max_age
!= -1 && (commit
->date
< max_age
)) {
86 if (max_count
!= -1 && !max_count
--)
91 static int process_commit(struct commit
* commit
)
93 int action
=filter_commit(commit
);
99 if (action
== CONTINUE
) {
108 static struct object_list
**add_object(struct object
*obj
, struct object_list
**p
, const char *name
)
110 struct object_list
*entry
= xmalloc(sizeof(*entry
));
118 static struct object_list
**process_blob(struct blob
*blob
, struct object_list
**p
, const char *name
)
120 struct object
*obj
= &blob
->object
;
124 if (obj
->flags
& (UNINTERESTING
| SEEN
))
127 return add_object(obj
, p
, name
);
130 static struct object_list
**process_tree(struct tree
*tree
, struct object_list
**p
, const char *name
)
132 struct object
*obj
= &tree
->object
;
133 struct tree_entry_list
*entry
;
137 if (obj
->flags
& (UNINTERESTING
| SEEN
))
139 if (parse_tree(tree
) < 0)
140 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
142 p
= add_object(obj
, p
, name
);
143 for (entry
= tree
->entries
; entry
; entry
= entry
->next
) {
144 if (entry
->directory
)
145 p
= process_tree(entry
->item
.tree
, p
, entry
->name
);
147 p
= process_blob(entry
->item
.blob
, p
, entry
->name
);
152 static struct object_list
*pending_objects
= NULL
;
154 static void show_commit_list(struct commit_list
*list
)
156 struct object_list
*objects
= NULL
, **p
= &objects
, *pending
;
158 struct commit
*commit
= pop_most_recent_commit(&list
, SEEN
);
160 p
= process_tree(commit
->tree
, p
, "");
161 if (process_commit(commit
) == STOP
)
164 for (pending
= pending_objects
; pending
; pending
= pending
->next
) {
165 struct object
*obj
= pending
->item
;
166 const char *name
= pending
->name
;
167 if (obj
->flags
& (UNINTERESTING
| SEEN
))
169 if (obj
->type
== tag_type
) {
171 p
= add_object(obj
, p
, name
);
174 if (obj
->type
== tree_type
) {
175 p
= process_tree((struct tree
*)obj
, p
, name
);
178 if (obj
->type
== blob_type
) {
179 p
= process_blob((struct blob
*)obj
, p
, name
);
182 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
185 printf("%s %s\n", sha1_to_hex(objects
->item
->sha1
), objects
->name
);
186 objects
= objects
->next
;
190 static void mark_blob_uninteresting(struct blob
*blob
)
194 if (blob
->object
.flags
& UNINTERESTING
)
196 blob
->object
.flags
|= UNINTERESTING
;
199 static void mark_tree_uninteresting(struct tree
*tree
)
201 struct object
*obj
= &tree
->object
;
202 struct tree_entry_list
*entry
;
206 if (obj
->flags
& UNINTERESTING
)
208 obj
->flags
|= UNINTERESTING
;
209 if (parse_tree(tree
) < 0)
210 die("bad tree %s", sha1_to_hex(obj
->sha1
));
211 entry
= tree
->entries
;
213 if (entry
->directory
)
214 mark_tree_uninteresting(entry
->item
.tree
);
216 mark_blob_uninteresting(entry
->item
.blob
);
221 static void mark_parents_uninteresting(struct commit
*commit
)
223 struct commit_list
*parents
= commit
->parents
;
226 mark_tree_uninteresting(commit
->tree
);
228 struct commit
*commit
= parents
->item
;
229 commit
->object
.flags
|= UNINTERESTING
;
230 parents
= parents
->next
;
234 static int everybody_uninteresting(struct commit_list
*list
)
237 struct commit
*commit
= list
->item
;
239 if (commit
->object
.flags
& UNINTERESTING
)
247 * This is a truly stupid algorithm, but it's only
248 * used for bisection, and we just don't care enough.
250 * We care just barely enough to avoid recursing for
253 static int count_distance(struct commit_list
*entry
)
258 struct commit
*commit
= entry
->item
;
259 struct commit_list
*p
;
261 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
264 commit
->object
.flags
|= COUNTED
;
270 nr
+= count_distance(p
);
278 static void clear_distance(struct commit_list
*list
)
281 struct commit
*commit
= list
->item
;
282 commit
->object
.flags
&= ~COUNTED
;
287 static struct commit_list
*find_bisection(struct commit_list
*list
)
290 struct commit_list
*p
, *best
;
303 int distance
= count_distance(p
);
304 clear_distance(list
);
305 if (nr
- distance
< distance
)
306 distance
= nr
- distance
;
307 if (distance
> closest
) {
318 static struct commit_list
*limit_list(struct commit_list
*list
)
320 struct commit_list
*newlist
= NULL
;
321 struct commit_list
**p
= &newlist
;
323 struct commit
*commit
= pop_most_recent_commit(&list
, SEEN
);
324 struct object
*obj
= &commit
->object
;
326 if (unpacked
&& has_sha1_pack(obj
->sha1
))
327 obj
->flags
|= UNINTERESTING
;
328 if (obj
->flags
& UNINTERESTING
) {
329 mark_parents_uninteresting(commit
);
330 if (everybody_uninteresting(list
))
334 p
= &commit_list_insert(commit
, p
)->next
;
337 newlist
= find_bisection(newlist
);
341 static void add_pending_object(struct object
*obj
, const char *name
)
343 add_object(obj
, &pending_objects
, name
);
346 static struct commit
*get_commit_reference(const char *name
, unsigned int flags
)
348 unsigned char sha1
[20];
349 struct object
*object
;
351 if (get_sha1(name
, sha1
))
352 usage(rev_list_usage
);
353 object
= parse_object(sha1
);
355 die("bad object %s", name
);
358 * Tag object? Look what it points to..
360 if (object
->type
== tag_type
) {
361 struct tag
*tag
= (struct tag
*) object
;
362 object
->flags
|= flags
;
363 if (tag_objects
&& !(object
->flags
& UNINTERESTING
))
364 add_pending_object(object
, tag
->tag
);
365 object
= tag
->tagged
;
369 * Commit object? Just return it, we'll do all the complex
372 if (object
->type
== commit_type
) {
373 struct commit
*commit
= (struct commit
*)object
;
374 object
->flags
|= flags
;
375 if (parse_commit(commit
) < 0)
376 die("unable to parse commit %s", name
);
381 * Tree object? Either mark it uniniteresting, or add it
382 * to the list of objects to look at later..
384 if (object
->type
== tree_type
) {
385 struct tree
*tree
= (struct tree
*)object
;
388 if (flags
& UNINTERESTING
) {
389 mark_tree_uninteresting(tree
);
392 add_pending_object(object
, "");
397 * Blob object? You know the drill by now..
399 if (object
->type
== blob_type
) {
400 struct blob
*blob
= (struct blob
*)object
;
403 if (flags
& UNINTERESTING
) {
404 mark_blob_uninteresting(blob
);
407 add_pending_object(object
, "");
410 die("%s is unknown object", name
);
413 int main(int argc
, char **argv
)
415 struct commit_list
*list
= NULL
;
418 for (i
= 1 ; i
< argc
; i
++) {
421 struct commit
*commit
;
423 if (!strncmp(arg
, "--max-count=", 12)) {
424 max_count
= atoi(arg
+ 12);
427 if (!strncmp(arg
, "--max-age=", 10)) {
428 max_age
= atoi(arg
+ 10);
431 if (!strncmp(arg
, "--min-age=", 10)) {
432 min_age
= atoi(arg
+ 10);
435 if (!strcmp(arg
, "--header")) {
439 if (!strncmp(arg
, "--pretty", 8)) {
440 commit_format
= get_commit_format(arg
+8);
442 hdr_termination
= '\n';
446 if (!strcmp(arg
, "--parents")) {
450 if (!strcmp(arg
, "--bisect")) {
454 if (!strcmp(arg
, "--objects")) {
460 if (!strcmp(arg
, "--unpacked")) {
465 if (!strncmp(arg
, "--merge-order", 13)) {
469 if (!strncmp(arg
, "--show-breaks", 13)) {
476 flags
= UNINTERESTING
;
480 if (show_breaks
&& !merge_order
)
481 usage(rev_list_usage
);
482 commit
= get_commit_reference(arg
, flags
);
485 commit_list_insert(commit
, &list
);
490 list
= limit_list(list
);
491 show_commit_list(list
);
493 if (sort_list_in_merge_order(list
, &process_commit
)) {
494 die("merge order sort failed\n");