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"
20 " --merge-order [ --show-breaks ]";
22 static int bisect_list
= 0;
23 static int tag_objects
= 0;
24 static int tree_objects
= 0;
25 static int blob_objects
= 0;
26 static int verbose_header
= 0;
27 static int show_parents
= 0;
28 static int hdr_termination
= 0;
29 static const char *prefix
= "";
30 static unsigned long max_age
= -1;
31 static unsigned long min_age
= -1;
32 static int max_count
= -1;
33 static enum cmit_fmt commit_format
= CMIT_FMT_RAW
;
34 static int merge_order
= 0;
35 static int show_breaks
= 0;
36 static int stop_traversal
= 0;
38 static void show_commit(struct commit
*commit
)
40 commit
->object
.flags
|= SHOWN
;
43 if (commit
->object
.flags
& DISCONTINUITY
) {
45 } else if (commit
->object
.flags
& BOUNDARY
) {
49 printf("%s%s", prefix
, sha1_to_hex(commit
->object
.sha1
));
51 struct commit_list
*parents
= commit
->parents
;
53 printf(" %s", sha1_to_hex(parents
->item
->object
.sha1
));
54 parents
= parents
->next
;
59 static char pretty_header
[16384];
60 pretty_print_commit(commit_format
, commit
->buffer
, ~0, pretty_header
, sizeof(pretty_header
));
61 printf("%s%c", pretty_header
, hdr_termination
);
65 static int filter_commit(struct commit
* commit
)
67 if (merge_order
&& stop_traversal
&& commit
->object
.flags
& BOUNDARY
)
69 if (commit
->object
.flags
& (UNINTERESTING
|SHOWN
))
71 if (min_age
!= -1 && (commit
->date
> min_age
))
73 if (max_age
!= -1 && (commit
->date
< max_age
)) {
81 if (max_count
!= -1 && !max_count
--)
86 static int process_commit(struct commit
* commit
)
88 int action
=filter_commit(commit
);
94 if (action
== CONTINUE
) {
103 static struct object_list
**add_object(struct object
*obj
, struct object_list
**p
, const char *name
)
105 struct object_list
*entry
= xmalloc(sizeof(*entry
));
113 static struct object_list
**process_blob(struct blob
*blob
, struct object_list
**p
, const char *name
)
115 struct object
*obj
= &blob
->object
;
119 if (obj
->flags
& (UNINTERESTING
| SEEN
))
122 return add_object(obj
, p
, name
);
125 static struct object_list
**process_tree(struct tree
*tree
, struct object_list
**p
, const char *name
)
127 struct object
*obj
= &tree
->object
;
128 struct tree_entry_list
*entry
;
132 if (obj
->flags
& (UNINTERESTING
| SEEN
))
134 if (parse_tree(tree
) < 0)
135 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
137 p
= add_object(obj
, p
, name
);
138 for (entry
= tree
->entries
; entry
; entry
= entry
->next
) {
139 if (entry
->directory
)
140 p
= process_tree(entry
->item
.tree
, p
, entry
->name
);
142 p
= process_blob(entry
->item
.blob
, p
, entry
->name
);
147 static struct object_list
*pending_objects
= NULL
;
149 static void show_commit_list(struct commit_list
*list
)
151 struct object_list
*objects
= NULL
, **p
= &objects
, *pending
;
153 struct commit
*commit
= pop_most_recent_commit(&list
, SEEN
);
155 p
= process_tree(commit
->tree
, p
, "");
156 if (process_commit(commit
) == STOP
)
159 for (pending
= pending_objects
; pending
; pending
= pending
->next
) {
160 struct object
*obj
= pending
->item
;
161 const char *name
= pending
->name
;
162 if (obj
->flags
& (UNINTERESTING
| SEEN
))
164 if (obj
->type
== tag_type
) {
166 p
= add_object(obj
, p
, name
);
169 if (obj
->type
== tree_type
) {
170 p
= process_tree((struct tree
*)obj
, p
, name
);
173 if (obj
->type
== blob_type
) {
174 p
= process_blob((struct blob
*)obj
, p
, name
);
177 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
180 printf("%s %s\n", sha1_to_hex(objects
->item
->sha1
), objects
->name
);
181 objects
= objects
->next
;
185 static void mark_blob_uninteresting(struct blob
*blob
)
189 if (blob
->object
.flags
& UNINTERESTING
)
191 blob
->object
.flags
|= UNINTERESTING
;
194 static void mark_tree_uninteresting(struct tree
*tree
)
196 struct object
*obj
= &tree
->object
;
197 struct tree_entry_list
*entry
;
201 if (obj
->flags
& UNINTERESTING
)
203 obj
->flags
|= UNINTERESTING
;
204 if (parse_tree(tree
) < 0)
205 die("bad tree %s", sha1_to_hex(obj
->sha1
));
206 entry
= tree
->entries
;
208 if (entry
->directory
)
209 mark_tree_uninteresting(entry
->item
.tree
);
211 mark_blob_uninteresting(entry
->item
.blob
);
216 static void mark_parents_uninteresting(struct commit
*commit
)
218 struct commit_list
*parents
= commit
->parents
;
221 mark_tree_uninteresting(commit
->tree
);
223 struct commit
*commit
= parents
->item
;
224 commit
->object
.flags
|= UNINTERESTING
;
225 parents
= parents
->next
;
229 static int everybody_uninteresting(struct commit_list
*list
)
232 struct commit
*commit
= list
->item
;
234 if (commit
->object
.flags
& UNINTERESTING
)
242 * This is a truly stupid algorithm, but it's only
243 * used for bisection, and we just don't care enough.
245 * We care just barely enough to avoid recursing for
248 static int count_distance(struct commit_list
*entry
)
253 struct commit
*commit
= entry
->item
;
254 struct commit_list
*p
;
256 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
259 commit
->object
.flags
|= COUNTED
;
265 nr
+= count_distance(p
);
273 static void clear_distance(struct commit_list
*list
)
276 struct commit
*commit
= list
->item
;
277 commit
->object
.flags
&= ~COUNTED
;
282 static struct commit_list
*find_bisection(struct commit_list
*list
)
285 struct commit_list
*p
, *best
;
298 int distance
= count_distance(p
);
299 clear_distance(list
);
300 if (nr
- distance
< distance
)
301 distance
= nr
- distance
;
302 if (distance
> closest
) {
313 static struct commit_list
*limit_list(struct commit_list
*list
)
315 struct commit_list
*newlist
= NULL
;
316 struct commit_list
**p
= &newlist
;
318 struct commit
*commit
= pop_most_recent_commit(&list
, SEEN
);
319 struct object
*obj
= &commit
->object
;
321 if (obj
->flags
& UNINTERESTING
) {
322 mark_parents_uninteresting(commit
);
323 if (everybody_uninteresting(list
))
327 p
= &commit_list_insert(commit
, p
)->next
;
330 newlist
= find_bisection(newlist
);
334 static void add_pending_object(struct object
*obj
, const char *name
)
336 add_object(obj
, &pending_objects
, name
);
339 static struct commit
*get_commit_reference(const char *name
, unsigned int flags
)
341 unsigned char sha1
[20];
342 struct object
*object
;
344 if (get_sha1(name
, sha1
))
345 usage(rev_list_usage
);
346 object
= parse_object(sha1
);
348 die("bad object %s", name
);
351 * Tag object? Look what it points to..
353 if (object
->type
== tag_type
) {
354 struct tag
*tag
= (struct tag
*) object
;
355 object
->flags
|= flags
;
356 if (tag_objects
&& !(object
->flags
& UNINTERESTING
))
357 add_pending_object(object
, tag
->tag
);
358 object
= tag
->tagged
;
362 * Commit object? Just return it, we'll do all the complex
365 if (object
->type
== commit_type
) {
366 struct commit
*commit
= (struct commit
*)object
;
367 object
->flags
|= flags
;
368 if (parse_commit(commit
) < 0)
369 die("unable to parse commit %s", name
);
374 * Tree object? Either mark it uniniteresting, or add it
375 * to the list of objects to look at later..
377 if (object
->type
== tree_type
) {
378 struct tree
*tree
= (struct tree
*)object
;
380 die("%s is a tree object, not a commit", name
);
381 if (flags
& UNINTERESTING
) {
382 mark_tree_uninteresting(tree
);
385 add_pending_object(object
, "");
390 * Blob object? You know the drill by now..
392 if (object
->type
== blob_type
) {
393 struct blob
*blob
= (struct blob
*)object
;
395 die("%s is a blob object, not a commit", name
);
396 if (flags
& UNINTERESTING
) {
397 mark_blob_uninteresting(blob
);
400 add_pending_object(object
, "");
403 die("%s is unknown object", name
);
406 int main(int argc
, char **argv
)
408 struct commit_list
*list
= NULL
;
411 for (i
= 1 ; i
< argc
; i
++) {
414 struct commit
*commit
;
416 if (!strncmp(arg
, "--max-count=", 12)) {
417 max_count
= atoi(arg
+ 12);
420 if (!strncmp(arg
, "--max-age=", 10)) {
421 max_age
= atoi(arg
+ 10);
424 if (!strncmp(arg
, "--min-age=", 10)) {
425 min_age
= atoi(arg
+ 10);
428 if (!strcmp(arg
, "--header")) {
432 if (!strncmp(arg
, "--pretty", 8)) {
433 commit_format
= get_commit_format(arg
+8);
435 hdr_termination
= '\n';
439 if (!strcmp(arg
, "--parents")) {
443 if (!strcmp(arg
, "--bisect")) {
447 if (!strcmp(arg
, "--objects")) {
453 if (!strncmp(arg
, "--merge-order", 13)) {
457 if (!strncmp(arg
, "--show-breaks", 13)) {
464 flags
= UNINTERESTING
;
468 if (show_breaks
&& !merge_order
)
469 usage(rev_list_usage
);
470 commit
= get_commit_reference(arg
, flags
);
473 commit_list_insert(commit
, &list
);
478 list
= limit_list(list
);
479 show_commit_list(list
);
481 if (sort_list_in_merge_order(list
, &process_commit
)) {
482 die("merge order sort failed\n");