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 static enum cmit_fmt
get_commit_format(const char *arg
)
313 return CMIT_FMT_DEFAULT
;
314 if (!strcmp(arg
, "=raw"))
316 if (!strcmp(arg
, "=medium"))
317 return CMIT_FMT_MEDIUM
;
318 if (!strcmp(arg
, "=short"))
319 return CMIT_FMT_SHORT
;
320 usage(rev_list_usage
);
324 int main(int argc
, char **argv
)
326 struct commit_list
*list
= NULL
;
329 for (i
= 1 ; i
< argc
; i
++) {
332 unsigned char sha1
[20];
333 struct commit
*commit
;
335 if (!strncmp(arg
, "--max-count=", 12)) {
336 max_count
= atoi(arg
+ 12);
339 if (!strncmp(arg
, "--max-age=", 10)) {
340 max_age
= atoi(arg
+ 10);
343 if (!strncmp(arg
, "--min-age=", 10)) {
344 min_age
= atoi(arg
+ 10);
347 if (!strcmp(arg
, "--header")) {
351 if (!strncmp(arg
, "--pretty", 8)) {
352 commit_format
= get_commit_format(arg
+8);
354 hdr_termination
= '\n';
358 if (!strcmp(arg
, "--parents")) {
362 if (!strcmp(arg
, "--bisect")) {
366 if (!strcmp(arg
, "--objects")) {
371 if (!strncmp(arg
, "--merge-order", 13)) {
375 if (!strncmp(arg
, "--show-breaks", 13)) {
382 flags
= UNINTERESTING
;
386 if (get_sha1(arg
, sha1
) || (show_breaks
&& !merge_order
))
387 usage(rev_list_usage
);
388 commit
= lookup_commit_reference(sha1
);
389 if (!commit
|| parse_commit(commit
) < 0)
390 die("bad commit object %s", arg
);
391 commit
->object
.flags
|= flags
;
392 commit_list_insert(commit
, &list
);
396 usage(rev_list_usage
);
400 list
= limit_list(list
);
401 show_commit_list(list
);
403 if (sort_list_in_merge_order(list
, &process_commit
)) {
404 die("merge order sort failed\n");