6 #define INTERESTING (1u << 1)
7 #define COUNTED (1u << 2)
9 static const char rev_list_usage
[] =
10 "usage: git-rev-list [OPTION] commit-id <commit-id>\n"
16 " --merge-order [ --show-breaks ]";
18 static int bisect_list
= 0;
19 static int verbose_header
= 0;
20 static int show_parents
= 0;
21 static int hdr_termination
= 0;
22 static const char *prefix
= "";
23 static unsigned long max_age
= -1;
24 static unsigned long min_age
= -1;
25 static int max_count
= -1;
26 static enum cmit_fmt commit_format
= CMIT_FMT_RAW
;
27 static int merge_order
= 0;
28 static int show_breaks
= 0;
30 static void show_commit(struct commit
*commit
)
34 if (commit
->object
.flags
& DISCONTINUITY
) {
36 } else if (commit
->object
.flags
& BOUNDARY
) {
40 printf("%s%s", prefix
, sha1_to_hex(commit
->object
.sha1
));
42 struct commit_list
*parents
= commit
->parents
;
44 printf(" %s", sha1_to_hex(parents
->item
->object
.sha1
));
45 parents
= parents
->next
;
50 static char pretty_header
[16384];
51 pretty_print_commit(commit_format
, commit
->buffer
, ~0, pretty_header
, sizeof(pretty_header
));
52 printf("%s%c", pretty_header
, hdr_termination
);
56 static int filter_commit(struct commit
* commit
)
58 if (commit
->object
.flags
& UNINTERESTING
)
60 if (min_age
!= -1 && (commit
->date
> min_age
))
62 if (max_age
!= -1 && (commit
->date
< max_age
))
64 if (max_count
!= -1 && !max_count
--)
70 static int process_commit(struct commit
* commit
)
72 int action
=filter_commit(commit
);
78 if (action
== CONTINUE
) {
87 static void show_commit_list(struct commit_list
*list
)
90 struct commit
*commit
= pop_most_recent_commit(&list
, SEEN
);
92 if (process_commit(commit
) == STOP
)
97 static void mark_parents_uninteresting(struct commit
*commit
)
99 struct commit_list
*parents
= commit
->parents
;
102 struct commit
*commit
= parents
->item
;
103 commit
->object
.flags
|= UNINTERESTING
;
104 parents
= parents
->next
;
108 static int everybody_uninteresting(struct commit_list
*list
)
111 struct commit
*commit
= list
->item
;
113 if (commit
->object
.flags
& UNINTERESTING
)
121 * This is a truly stupid algorithm, but it's only
122 * used for bisection, and we just don't care enough.
124 * We care just barely enough to avoid recursing for
127 static int count_distance(struct commit_list
*entry
)
132 struct commit
*commit
= entry
->item
;
133 struct commit_list
*p
;
135 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
138 commit
->object
.flags
|= COUNTED
;
144 nr
+= count_distance(p
);
152 static void clear_distance(struct commit_list
*list
)
155 struct commit
*commit
= list
->item
;
156 commit
->object
.flags
&= ~COUNTED
;
161 static struct commit_list
*find_bisection(struct commit_list
*list
)
164 struct commit_list
*p
, *best
;
177 int distance
= count_distance(p
);
178 clear_distance(list
);
179 if (nr
- distance
< distance
)
180 distance
= nr
- distance
;
181 if (distance
> closest
) {
192 struct commit_list
*limit_list(struct commit_list
*list
)
194 struct commit_list
*newlist
= NULL
;
195 struct commit_list
**p
= &newlist
;
197 struct commit
*commit
= pop_most_recent_commit(&list
, SEEN
);
198 struct object
*obj
= &commit
->object
;
200 if (obj
->flags
& UNINTERESTING
) {
201 mark_parents_uninteresting(commit
);
202 if (everybody_uninteresting(list
))
206 p
= &commit_list_insert(commit
, p
)->next
;
209 newlist
= find_bisection(newlist
);
213 static enum cmit_fmt
get_commit_format(const char *arg
)
216 return CMIT_FMT_DEFAULT
;
217 if (!strcmp(arg
, "=raw"))
219 if (!strcmp(arg
, "=medium"))
220 return CMIT_FMT_MEDIUM
;
221 if (!strcmp(arg
, "=short"))
222 return CMIT_FMT_SHORT
;
223 usage(rev_list_usage
);
227 int main(int argc
, char **argv
)
229 struct commit_list
*list
= NULL
;
232 for (i
= 1 ; i
< argc
; i
++) {
235 unsigned char sha1
[20];
236 struct commit
*commit
;
238 if (!strncmp(arg
, "--max-count=", 12)) {
239 max_count
= atoi(arg
+ 12);
242 if (!strncmp(arg
, "--max-age=", 10)) {
243 max_age
= atoi(arg
+ 10);
246 if (!strncmp(arg
, "--min-age=", 10)) {
247 min_age
= atoi(arg
+ 10);
250 if (!strcmp(arg
, "--header")) {
254 if (!strncmp(arg
, "--pretty", 8)) {
255 commit_format
= get_commit_format(arg
+8);
257 hdr_termination
= '\n';
261 if (!strcmp(arg
, "--parents")) {
265 if (!strcmp(arg
, "--bisect")) {
269 if (!strncmp(arg
, "--merge-order", 13)) {
273 if (!strncmp(arg
, "--show-breaks", 13)) {
280 flags
= UNINTERESTING
;
284 if (get_sha1(arg
, sha1
) || (show_breaks
&& !merge_order
))
285 usage(rev_list_usage
);
286 commit
= lookup_commit_reference(sha1
);
287 if (!commit
|| parse_commit(commit
) < 0)
288 die("bad commit object %s", arg
);
289 commit
->object
.flags
|= flags
;
290 commit_list_insert(commit
, &list
);
294 usage(rev_list_usage
);
298 list
= limit_list(list
);
299 show_commit_list(list
);
301 if (sort_list_in_merge_order(list
, &process_commit
)) {
302 die("merge order sort failed\n");