8 #define INTERESTING (1u << 1)
9 #define COUNTED (1u << 2)
10 #define SHOWN (LAST_EPOCH_FLAG << 2)
12 static const char rev_list_usage
[] =
13 "usage: git-rev-list [OPTION] commit-id <commit-id>\n"
19 " --merge-order [ --show-breaks ]";
21 static int bisect_list
= 0;
22 static int tree_objects
= 0;
23 static int blob_objects
= 0;
24 static int verbose_header
= 0;
25 static int show_parents
= 0;
26 static int hdr_termination
= 0;
27 static const char *prefix
= "";
28 static unsigned long max_age
= -1;
29 static unsigned long min_age
= -1;
30 static int max_count
= -1;
31 static enum cmit_fmt commit_format
= CMIT_FMT_RAW
;
32 static int merge_order
= 0;
33 static int show_breaks
= 0;
34 static int stop_traversal
= 0;
36 static void show_commit(struct commit
*commit
)
38 commit
->object
.flags
|= SHOWN
;
41 if (commit
->object
.flags
& DISCONTINUITY
) {
43 } else if (commit
->object
.flags
& BOUNDARY
) {
47 printf("%s%s", prefix
, sha1_to_hex(commit
->object
.sha1
));
49 struct commit_list
*parents
= commit
->parents
;
51 printf(" %s", sha1_to_hex(parents
->item
->object
.sha1
));
52 parents
= parents
->next
;
57 static char pretty_header
[16384];
58 pretty_print_commit(commit_format
, commit
->buffer
, ~0, pretty_header
, sizeof(pretty_header
));
59 printf("%s%c", pretty_header
, hdr_termination
);
63 static int filter_commit(struct commit
* commit
)
65 if (merge_order
&& stop_traversal
&& commit
->object
.flags
& BOUNDARY
)
67 if (commit
->object
.flags
& (UNINTERESTING
|SHOWN
))
69 if (min_age
!= -1 && (commit
->date
> min_age
))
71 if (max_age
!= -1 && (commit
->date
< max_age
)) {
79 if (max_count
!= -1 && !max_count
--)
84 static int process_commit(struct commit
* commit
)
86 int action
=filter_commit(commit
);
92 if (action
== CONTINUE
) {
101 static struct object_list
**add_object(struct object
*obj
, struct object_list
**p
, const char *name
)
103 struct object_list
*entry
= xmalloc(sizeof(*entry
));
111 static struct object_list
**process_blob(struct blob
*blob
, struct object_list
**p
, const char *name
)
113 struct object
*obj
= &blob
->object
;
117 if (obj
->flags
& (UNINTERESTING
| SEEN
))
120 return add_object(obj
, p
, name
);
123 static struct object_list
**process_tree(struct tree
*tree
, struct object_list
**p
, const char *name
)
125 struct object
*obj
= &tree
->object
;
126 struct tree_entry_list
*entry
;
130 if (obj
->flags
& (UNINTERESTING
| SEEN
))
132 if (parse_tree(tree
) < 0)
133 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
135 p
= add_object(obj
, p
, name
);
136 for (entry
= tree
->entries
; entry
; entry
= entry
->next
) {
137 if (entry
->directory
)
138 p
= process_tree(entry
->item
.tree
, p
, entry
->name
);
140 p
= process_blob(entry
->item
.blob
, p
, entry
->name
);
145 static void show_commit_list(struct commit_list
*list
)
147 struct object_list
*objects
= NULL
, **p
= &objects
;
149 struct commit
*commit
= pop_most_recent_commit(&list
, SEEN
);
151 p
= process_tree(commit
->tree
, p
, "");
152 if (process_commit(commit
) == STOP
)
156 printf("%s %s\n", sha1_to_hex(objects
->item
->sha1
), objects
->name
);
157 objects
= objects
->next
;
161 static void mark_blob_uninteresting(struct blob
*blob
)
165 if (blob
->object
.flags
& UNINTERESTING
)
167 blob
->object
.flags
|= UNINTERESTING
;
170 static void mark_tree_uninteresting(struct tree
*tree
)
172 struct object
*obj
= &tree
->object
;
173 struct tree_entry_list
*entry
;
177 if (obj
->flags
& UNINTERESTING
)
179 obj
->flags
|= UNINTERESTING
;
180 if (parse_tree(tree
) < 0)
181 die("bad tree %s", sha1_to_hex(obj
->sha1
));
182 entry
= tree
->entries
;
184 if (entry
->directory
)
185 mark_tree_uninteresting(entry
->item
.tree
);
187 mark_blob_uninteresting(entry
->item
.blob
);
192 static void mark_parents_uninteresting(struct commit
*commit
)
194 struct commit_list
*parents
= commit
->parents
;
197 mark_tree_uninteresting(commit
->tree
);
199 struct commit
*commit
= parents
->item
;
200 commit
->object
.flags
|= UNINTERESTING
;
201 parents
= parents
->next
;
205 static int everybody_uninteresting(struct commit_list
*list
)
208 struct commit
*commit
= list
->item
;
210 if (commit
->object
.flags
& UNINTERESTING
)
218 * This is a truly stupid algorithm, but it's only
219 * used for bisection, and we just don't care enough.
221 * We care just barely enough to avoid recursing for
224 static int count_distance(struct commit_list
*entry
)
229 struct commit
*commit
= entry
->item
;
230 struct commit_list
*p
;
232 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
235 commit
->object
.flags
|= COUNTED
;
241 nr
+= count_distance(p
);
249 static void clear_distance(struct commit_list
*list
)
252 struct commit
*commit
= list
->item
;
253 commit
->object
.flags
&= ~COUNTED
;
258 static struct commit_list
*find_bisection(struct commit_list
*list
)
261 struct commit_list
*p
, *best
;
274 int distance
= count_distance(p
);
275 clear_distance(list
);
276 if (nr
- distance
< distance
)
277 distance
= nr
- distance
;
278 if (distance
> closest
) {
289 struct commit_list
*limit_list(struct commit_list
*list
)
291 struct commit_list
*newlist
= NULL
;
292 struct commit_list
**p
= &newlist
;
294 struct commit
*commit
= pop_most_recent_commit(&list
, SEEN
);
295 struct object
*obj
= &commit
->object
;
297 if (obj
->flags
& UNINTERESTING
) {
298 mark_parents_uninteresting(commit
);
299 if (everybody_uninteresting(list
))
303 p
= &commit_list_insert(commit
, p
)->next
;
306 newlist
= find_bisection(newlist
);
310 int main(int argc
, char **argv
)
312 struct commit_list
*list
= NULL
;
315 for (i
= 1 ; i
< argc
; i
++) {
318 unsigned char sha1
[20];
319 struct commit
*commit
;
321 if (!strncmp(arg
, "--max-count=", 12)) {
322 max_count
= atoi(arg
+ 12);
325 if (!strncmp(arg
, "--max-age=", 10)) {
326 max_age
= atoi(arg
+ 10);
329 if (!strncmp(arg
, "--min-age=", 10)) {
330 min_age
= atoi(arg
+ 10);
333 if (!strcmp(arg
, "--header")) {
337 if (!strncmp(arg
, "--pretty", 8)) {
338 commit_format
= get_commit_format(arg
+8);
340 hdr_termination
= '\n';
344 if (!strcmp(arg
, "--parents")) {
348 if (!strcmp(arg
, "--bisect")) {
352 if (!strcmp(arg
, "--objects")) {
357 if (!strncmp(arg
, "--merge-order", 13)) {
361 if (!strncmp(arg
, "--show-breaks", 13)) {
368 flags
= UNINTERESTING
;
372 if (get_sha1(arg
, sha1
) || (show_breaks
&& !merge_order
))
373 usage(rev_list_usage
);
374 commit
= lookup_commit_reference(sha1
);
375 if (!commit
|| parse_commit(commit
) < 0)
376 die("bad commit object %s", arg
);
377 commit
->object
.flags
|= flags
;
378 commit_list_insert(commit
, &list
);
382 usage(rev_list_usage
);
386 list
= limit_list(list
);
387 show_commit_list(list
);
389 if (sort_list_in_merge_order(list
, &process_commit
)) {
390 die("merge order sort failed\n");