6 #define INTERESTING (1u << 1)
7 #define COUNTED (1u << 2)
8 #define SHOWN (LAST_EPOCH_FLAG << 2)
10 static const char rev_list_usage
[] =
11 "usage: git-rev-list [OPTION] commit-id <commit-id>\n"
17 " --merge-order [ --show-breaks ]";
19 static int bisect_list
= 0;
20 static int verbose_header
= 0;
21 static int show_parents
= 0;
22 static int hdr_termination
= 0;
23 static const char *prefix
= "";
24 static unsigned long max_age
= -1;
25 static unsigned long min_age
= -1;
26 static int max_count
= -1;
27 static enum cmit_fmt commit_format
= CMIT_FMT_RAW
;
28 static int merge_order
= 0;
29 static int show_breaks
= 0;
31 static void show_commit(struct commit
*commit
)
33 commit
->object
.flags
|= SHOWN
;
36 if (commit
->object
.flags
& DISCONTINUITY
) {
38 } else if (commit
->object
.flags
& BOUNDARY
) {
42 printf("%s%s", prefix
, sha1_to_hex(commit
->object
.sha1
));
44 struct commit_list
*parents
= commit
->parents
;
46 printf(" %s", sha1_to_hex(parents
->item
->object
.sha1
));
47 parents
= parents
->next
;
52 static char pretty_header
[16384];
53 pretty_print_commit(commit_format
, commit
->buffer
, ~0, pretty_header
, sizeof(pretty_header
));
54 printf("%s%c", pretty_header
, hdr_termination
);
58 static int filter_commit(struct commit
* commit
)
60 if (commit
->object
.flags
& (UNINTERESTING
|SHOWN
))
62 if (min_age
!= -1 && (commit
->date
> min_age
))
64 if (max_age
!= -1 && (commit
->date
< max_age
))
66 if (max_count
!= -1 && !max_count
--)
71 static int process_commit(struct commit
* commit
)
73 int action
=filter_commit(commit
);
79 if (action
== CONTINUE
) {
88 static void show_commit_list(struct commit_list
*list
)
91 struct commit
*commit
= pop_most_recent_commit(&list
, SEEN
);
93 if (process_commit(commit
) == STOP
)
98 static void mark_parents_uninteresting(struct commit
*commit
)
100 struct commit_list
*parents
= commit
->parents
;
103 struct commit
*commit
= parents
->item
;
104 commit
->object
.flags
|= UNINTERESTING
;
105 parents
= parents
->next
;
109 static int everybody_uninteresting(struct commit_list
*list
)
112 struct commit
*commit
= list
->item
;
114 if (commit
->object
.flags
& UNINTERESTING
)
122 * This is a truly stupid algorithm, but it's only
123 * used for bisection, and we just don't care enough.
125 * We care just barely enough to avoid recursing for
128 static int count_distance(struct commit_list
*entry
)
133 struct commit
*commit
= entry
->item
;
134 struct commit_list
*p
;
136 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
139 commit
->object
.flags
|= COUNTED
;
145 nr
+= count_distance(p
);
153 static void clear_distance(struct commit_list
*list
)
156 struct commit
*commit
= list
->item
;
157 commit
->object
.flags
&= ~COUNTED
;
162 static struct commit_list
*find_bisection(struct commit_list
*list
)
165 struct commit_list
*p
, *best
;
178 int distance
= count_distance(p
);
179 clear_distance(list
);
180 if (nr
- distance
< distance
)
181 distance
= nr
- distance
;
182 if (distance
> closest
) {
193 struct commit_list
*limit_list(struct commit_list
*list
)
195 struct commit_list
*newlist
= NULL
;
196 struct commit_list
**p
= &newlist
;
198 struct commit
*commit
= pop_most_recent_commit(&list
, SEEN
);
199 struct object
*obj
= &commit
->object
;
201 if (obj
->flags
& UNINTERESTING
) {
202 mark_parents_uninteresting(commit
);
203 if (everybody_uninteresting(list
))
207 p
= &commit_list_insert(commit
, p
)->next
;
210 newlist
= find_bisection(newlist
);
214 static enum cmit_fmt
get_commit_format(const char *arg
)
217 return CMIT_FMT_DEFAULT
;
218 if (!strcmp(arg
, "=raw"))
220 if (!strcmp(arg
, "=medium"))
221 return CMIT_FMT_MEDIUM
;
222 if (!strcmp(arg
, "=short"))
223 return CMIT_FMT_SHORT
;
224 usage(rev_list_usage
);
228 int main(int argc
, char **argv
)
230 struct commit_list
*list
= NULL
;
233 for (i
= 1 ; i
< argc
; i
++) {
236 unsigned char sha1
[20];
237 struct commit
*commit
;
239 if (!strncmp(arg
, "--max-count=", 12)) {
240 max_count
= atoi(arg
+ 12);
243 if (!strncmp(arg
, "--max-age=", 10)) {
244 max_age
= atoi(arg
+ 10);
247 if (!strncmp(arg
, "--min-age=", 10)) {
248 min_age
= atoi(arg
+ 10);
251 if (!strcmp(arg
, "--header")) {
255 if (!strncmp(arg
, "--pretty", 8)) {
256 commit_format
= get_commit_format(arg
+8);
258 hdr_termination
= '\n';
262 if (!strcmp(arg
, "--parents")) {
266 if (!strcmp(arg
, "--bisect")) {
270 if (!strncmp(arg
, "--merge-order", 13)) {
274 if (!strncmp(arg
, "--show-breaks", 13)) {
281 flags
= UNINTERESTING
;
285 if (get_sha1(arg
, sha1
) || (show_breaks
&& !merge_order
))
286 usage(rev_list_usage
);
287 commit
= lookup_commit_reference(sha1
);
288 if (!commit
|| parse_commit(commit
) < 0)
289 die("bad commit object %s", arg
);
290 commit
->object
.flags
|= flags
;
291 commit_list_insert(commit
, &list
);
295 usage(rev_list_usage
);
299 list
= limit_list(list
);
300 show_commit_list(list
);
302 if (sort_list_in_merge_order(list
, &process_commit
)) {
303 die("merge order sort failed\n");