]>
Commit | Line | Data |
---|---|---|
64745109 | 1 | #include "cache.h" |
e091eb93 | 2 | #include "refs.h" |
36f8d174 | 3 | #include "tag.h" |
64745109 | 4 | #include "commit.h" |
9de48752 LT |
5 | #include "tree.h" |
6 | #include "blob.h" | |
1b0c7174 | 7 | #include "tree-walk.h" |
c4e05b1a | 8 | #include "diff.h" |
ae563542 | 9 | #include "revision.h" |
c64ed70d | 10 | #include "list-objects.h" |
5fb61b8d | 11 | #include "builtin.h" |
50e62a8e | 12 | #include "log-tree.h" |
7fefda5c | 13 | #include "graph.h" |
ae563542 | 14 | |
1b65a5aa | 15 | /* bits #0-15 in revision.h */ |
64745109 | 16 | |
1b65a5aa | 17 | #define COUNTED (1u<<16) |
8906300f | 18 | |
a6f68d47 | 19 | static const char rev_list_usage[] = |
1b1dd23f | 20 | "git rev-list [OPTION] <commit-id>... [ -- paths... ]\n" |
69e0c256 JH |
21 | " limiting output:\n" |
22 | " --max-count=nr\n" | |
23 | " --max-age=epoch\n" | |
24 | " --min-age=epoch\n" | |
25 | " --sparse\n" | |
26 | " --no-merges\n" | |
93b74bca | 27 | " --remove-empty\n" |
69e0c256 | 28 | " --all\n" |
a5aa930d UKK |
29 | " --branches\n" |
30 | " --tags\n" | |
31 | " --remotes\n" | |
42cabc34 | 32 | " --stdin\n" |
27350891 | 33 | " --quiet\n" |
69e0c256 | 34 | " ordering output:\n" |
69e0c256 | 35 | " --topo-order\n" |
4c8725f1 | 36 | " --date-order\n" |
7ccd3667 | 37 | " --reverse\n" |
69e0c256 JH |
38 | " formatting output:\n" |
39 | " --parents\n" | |
72276a3e | 40 | " --children\n" |
c6496575 | 41 | " --objects | --objects-edge\n" |
69e0c256 JH |
42 | " --unpacked\n" |
43 | " --header | --pretty\n" | |
9da5c2f0 | 44 | " --abbrev=nr | --no-abbrev\n" |
5c51c985 | 45 | " --abbrev-commit\n" |
b24bace5 | 46 | " --left-right\n" |
69e0c256 | 47 | " special purpose:\n" |
457f08a0 | 48 | " --bisect\n" |
50e62a8e CC |
49 | " --bisect-vars\n" |
50 | " --bisect-all" | |
69e0c256 | 51 | ; |
a6f68d47 | 52 | |
5fb61b8d | 53 | static struct rev_info revs; |
ae563542 | 54 | |
96f1e58f DR |
55 | static int show_timestamp; |
56 | static int hdr_termination; | |
91539833 | 57 | static const char *header_prefix; |
e646de0d | 58 | |
27350891 | 59 | static void finish_commit(struct commit *commit); |
81f2bb1f LT |
60 | static void show_commit(struct commit *commit) |
61 | { | |
7fefda5c AS |
62 | graph_show_commit(revs.graph); |
63 | ||
dc68c4ff JH |
64 | if (show_timestamp) |
65 | printf("%lu ", commit->date); | |
91539833 LT |
66 | if (header_prefix) |
67 | fputs(header_prefix, stdout); | |
7528f27d AS |
68 | |
69 | if (!revs.graph) { | |
70 | if (commit->object.flags & BOUNDARY) | |
71 | putchar('-'); | |
72 | else if (commit->object.flags & UNINTERESTING) | |
73 | putchar('^'); | |
74 | else if (revs.left_right) { | |
75 | if (commit->object.flags & SYMMETRIC_LEFT) | |
76 | putchar('<'); | |
77 | else | |
78 | putchar('>'); | |
79 | } | |
577ed5c2 | 80 | } |
7594c4b2 JH |
81 | if (revs.abbrev_commit && revs.abbrev) |
82 | fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev), | |
83 | stdout); | |
5c51c985 JH |
84 | else |
85 | fputs(sha1_to_hex(commit->object.sha1), stdout); | |
885cf808 | 86 | if (revs.print_parents) { |
81f2bb1f LT |
87 | struct commit_list *parents = commit->parents; |
88 | while (parents) { | |
1ed84157 | 89 | printf(" %s", sha1_to_hex(parents->item->object.sha1)); |
81f2bb1f LT |
90 | parents = parents->next; |
91 | } | |
92 | } | |
72276a3e JH |
93 | if (revs.children.name) { |
94 | struct commit_list *children; | |
95 | ||
96 | children = lookup_decoration(&revs.children, &commit->object); | |
97 | while (children) { | |
98 | printf(" %s", sha1_to_hex(children->item->object.sha1)); | |
99 | children = children->next; | |
100 | } | |
101 | } | |
0f3a290b | 102 | show_decorations(&revs, commit); |
7594c4b2 | 103 | if (revs.commit_format == CMIT_FMT_ONELINE) |
d87449c5 JH |
104 | putchar(' '); |
105 | else | |
106 | putchar('\n'); | |
107 | ||
3131b713 | 108 | if (revs.verbose_header && commit->buffer) { |
f285a2d7 | 109 | struct strbuf buf = STRBUF_INIT; |
674d1727 | 110 | pretty_print_commit(revs.commit_format, commit, |
4593fb84 JH |
111 | &buf, revs.abbrev, NULL, NULL, |
112 | revs.date_mode, 0); | |
7fefda5c AS |
113 | if (revs.graph) { |
114 | if (buf.len) { | |
115 | if (revs.commit_format != CMIT_FMT_ONELINE) | |
116 | graph_show_oneline(revs.graph); | |
117 | ||
118 | graph_show_commit_msg(revs.graph, &buf); | |
119 | ||
120 | /* | |
121 | * Add a newline after the commit message. | |
122 | * | |
123 | * Usually, this newline produces a blank | |
124 | * padding line between entries, in which case | |
125 | * we need to add graph padding on this line. | |
126 | * | |
127 | * However, the commit message may not end in a | |
128 | * newline. In this case the newline simply | |
129 | * ends the last line of the commit message, | |
130 | * and we don't need any graph output. (This | |
131 | * always happens with CMIT_FMT_ONELINE, and it | |
132 | * happens with CMIT_FMT_USERFORMAT when the | |
133 | * format doesn't explicitly end in a newline.) | |
134 | */ | |
135 | if (buf.len && buf.buf[buf.len - 1] == '\n') | |
136 | graph_show_padding(revs.graph); | |
137 | putchar('\n'); | |
138 | } else { | |
139 | /* | |
140 | * If the message buffer is empty, just show | |
141 | * the rest of the graph output for this | |
142 | * commit. | |
143 | */ | |
144 | if (graph_show_remainder(revs.graph)) | |
145 | putchar('\n'); | |
146 | } | |
147 | } else { | |
148 | if (buf.len) | |
149 | printf("%s%c", buf.buf, hdr_termination); | |
150 | } | |
674d1727 | 151 | strbuf_release(&buf); |
7fefda5c AS |
152 | } else { |
153 | if (graph_show_remainder(revs.graph)) | |
154 | putchar('\n'); | |
7620d39f | 155 | } |
06f59e9f | 156 | maybe_flush_or_die(stdout, "stdout"); |
27350891 SP |
157 | finish_commit(commit); |
158 | } | |
159 | ||
160 | static void finish_commit(struct commit *commit) | |
161 | { | |
cb115748 LT |
162 | if (commit->parents) { |
163 | free_commit_list(commit->parents); | |
164 | commit->parents = NULL; | |
165 | } | |
4cac42b1 JH |
166 | free(commit->buffer); |
167 | commit->buffer = NULL; | |
a3437b8c JS |
168 | } |
169 | ||
27350891 SP |
170 | static void finish_object(struct object_array_entry *p) |
171 | { | |
172 | if (p->item->type == OBJ_BLOB && !has_sha1_file(p->item->sha1)) | |
173 | die("missing blob object '%s'", sha1_to_hex(p->item->sha1)); | |
174 | } | |
175 | ||
c64ed70d | 176 | static void show_object(struct object_array_entry *p) |
9de48752 | 177 | { |
c64ed70d | 178 | /* An object with name "foo\n0000000..." can be used to |
f18d244a | 179 | * confuse downstream "git pack-objects" very badly. |
c64ed70d JH |
180 | */ |
181 | const char *ep = strchr(p->name, '\n'); | |
b9849a1a | 182 | |
27350891 | 183 | finish_object(p); |
c64ed70d JH |
184 | if (ep) { |
185 | printf("%s %.*s\n", sha1_to_hex(p->item->sha1), | |
186 | (int) (ep - p->name), | |
187 | p->name); | |
9de48752 | 188 | } |
c64ed70d JH |
189 | else |
190 | printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name); | |
9de48752 LT |
191 | } |
192 | ||
8d1d8f83 JH |
193 | static void show_edge(struct commit *commit) |
194 | { | |
195 | printf("-%s\n", sha1_to_hex(commit->object.sha1)); | |
196 | } | |
197 | ||
8b3a1e05 LT |
198 | /* |
199 | * This is a truly stupid algorithm, but it's only | |
200 | * used for bisection, and we just don't care enough. | |
201 | * | |
202 | * We care just barely enough to avoid recursing for | |
203 | * non-merge entries. | |
204 | */ | |
205 | static int count_distance(struct commit_list *entry) | |
206 | { | |
207 | int nr = 0; | |
208 | ||
209 | while (entry) { | |
210 | struct commit *commit = entry->item; | |
211 | struct commit_list *p; | |
212 | ||
213 | if (commit->object.flags & (UNINTERESTING | COUNTED)) | |
214 | break; | |
7dc0fe3b | 215 | if (!(commit->object.flags & TREESAME)) |
b3cfd939 | 216 | nr++; |
8b3a1e05 LT |
217 | commit->object.flags |= COUNTED; |
218 | p = commit->parents; | |
219 | entry = p; | |
220 | if (p) { | |
221 | p = p->next; | |
222 | while (p) { | |
223 | nr += count_distance(p); | |
224 | p = p->next; | |
225 | } | |
226 | } | |
227 | } | |
b3cfd939 | 228 | |
8b3a1e05 LT |
229 | return nr; |
230 | } | |
231 | ||
3d958064 | 232 | static void clear_distance(struct commit_list *list) |
8b3a1e05 LT |
233 | { |
234 | while (list) { | |
235 | struct commit *commit = list->item; | |
236 | commit->object.flags &= ~COUNTED; | |
237 | list = list->next; | |
238 | } | |
239 | } | |
240 | ||
1daa09d9 | 241 | #define DEBUG_BISECT 0 |
1c4fea3a JH |
242 | |
243 | static inline int weight(struct commit_list *elem) | |
244 | { | |
245 | return *((int*)(elem->item->util)); | |
246 | } | |
247 | ||
248 | static inline void weight_set(struct commit_list *elem, int weight) | |
249 | { | |
250 | *((int*)(elem->item->util)) = weight; | |
251 | } | |
252 | ||
1daa09d9 | 253 | static int count_interesting_parents(struct commit *commit) |
1c4fea3a | 254 | { |
1daa09d9 JH |
255 | struct commit_list *p; |
256 | int count; | |
257 | ||
258 | for (count = 0, p = commit->parents; p; p = p->next) { | |
259 | if (p->item->object.flags & UNINTERESTING) | |
260 | continue; | |
261 | count++; | |
1c4fea3a | 262 | } |
1daa09d9 | 263 | return count; |
1c4fea3a JH |
264 | } |
265 | ||
53271411 | 266 | static inline int halfway(struct commit_list *p, int nr) |
2a464690 JH |
267 | { |
268 | /* | |
269 | * Don't short-cut something we are not going to return! | |
270 | */ | |
7dc0fe3b | 271 | if (p->item->object.flags & TREESAME) |
2a464690 | 272 | return 0; |
1daa09d9 JH |
273 | if (DEBUG_BISECT) |
274 | return 0; | |
2a464690 JH |
275 | /* |
276 | * 2 and 3 are halfway of 5. | |
277 | * 3 is halfway of 6 but 2 and 4 are not. | |
278 | */ | |
53271411 | 279 | switch (2 * weight(p) - nr) { |
2a464690 JH |
280 | case -1: case 0: case 1: |
281 | return 1; | |
282 | default: | |
283 | return 0; | |
284 | } | |
285 | } | |
286 | ||
1daa09d9 JH |
287 | #if !DEBUG_BISECT |
288 | #define show_list(a,b,c,d) do { ; } while (0) | |
289 | #else | |
290 | static void show_list(const char *debug, int counted, int nr, | |
291 | struct commit_list *list) | |
1c4fea3a | 292 | { |
1daa09d9 JH |
293 | struct commit_list *p; |
294 | ||
295 | fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr); | |
296 | ||
297 | for (p = list; p; p = p->next) { | |
298 | struct commit_list *pp; | |
299 | struct commit *commit = p->item; | |
300 | unsigned flags = commit->object.flags; | |
301 | enum object_type type; | |
302 | unsigned long size; | |
303 | char *buf = read_sha1_file(commit->object.sha1, &type, &size); | |
304 | char *ep, *sp; | |
305 | ||
306 | fprintf(stderr, "%c%c%c ", | |
7dc0fe3b | 307 | (flags & TREESAME) ? ' ' : 'T', |
1daa09d9 JH |
308 | (flags & UNINTERESTING) ? 'U' : ' ', |
309 | (flags & COUNTED) ? 'C' : ' '); | |
310 | if (commit->util) | |
311 | fprintf(stderr, "%3d", weight(p)); | |
312 | else | |
313 | fprintf(stderr, "---"); | |
314 | fprintf(stderr, " %.*s", 8, sha1_to_hex(commit->object.sha1)); | |
315 | for (pp = commit->parents; pp; pp = pp->next) | |
316 | fprintf(stderr, " %.*s", 8, | |
317 | sha1_to_hex(pp->item->object.sha1)); | |
318 | ||
319 | sp = strstr(buf, "\n\n"); | |
320 | if (sp) { | |
321 | sp += 2; | |
322 | for (ep = sp; *ep && *ep != '\n'; ep++) | |
323 | ; | |
324 | fprintf(stderr, " %.*s", (int)(ep - sp), sp); | |
325 | } | |
326 | fprintf(stderr, "\n"); | |
327 | } | |
328 | } | |
329 | #endif /* DEBUG_BISECT */ | |
330 | ||
77c11e06 CC |
331 | static struct commit_list *best_bisection(struct commit_list *list, int nr) |
332 | { | |
333 | struct commit_list *p, *best; | |
334 | int best_distance = -1; | |
335 | ||
336 | best = list; | |
337 | for (p = list; p; p = p->next) { | |
338 | int distance; | |
339 | unsigned flags = p->item->object.flags; | |
340 | ||
7dc0fe3b | 341 | if (flags & TREESAME) |
77c11e06 CC |
342 | continue; |
343 | distance = weight(p); | |
344 | if (nr - distance < distance) | |
345 | distance = nr - distance; | |
346 | if (distance > best_distance) { | |
347 | best = p; | |
348 | best_distance = distance; | |
349 | } | |
350 | } | |
351 | ||
352 | return best; | |
353 | } | |
354 | ||
50e62a8e CC |
355 | struct commit_dist { |
356 | struct commit *commit; | |
357 | int distance; | |
358 | }; | |
359 | ||
360 | static int compare_commit_dist(const void *a_, const void *b_) | |
361 | { | |
362 | struct commit_dist *a, *b; | |
363 | ||
364 | a = (struct commit_dist *)a_; | |
365 | b = (struct commit_dist *)b_; | |
366 | if (a->distance != b->distance) | |
367 | return b->distance - a->distance; /* desc sort */ | |
368 | return hashcmp(a->commit->object.sha1, b->commit->object.sha1); | |
369 | } | |
370 | ||
371 | static struct commit_list *best_bisection_sorted(struct commit_list *list, int nr) | |
372 | { | |
373 | struct commit_list *p; | |
374 | struct commit_dist *array = xcalloc(nr, sizeof(*array)); | |
375 | int cnt, i; | |
376 | ||
377 | for (p = list, cnt = 0; p; p = p->next) { | |
378 | int distance; | |
379 | unsigned flags = p->item->object.flags; | |
380 | ||
7dc0fe3b | 381 | if (flags & TREESAME) |
50e62a8e CC |
382 | continue; |
383 | distance = weight(p); | |
384 | if (nr - distance < distance) | |
385 | distance = nr - distance; | |
386 | array[cnt].commit = p->item; | |
387 | array[cnt].distance = distance; | |
388 | cnt++; | |
389 | } | |
390 | qsort(array, cnt, sizeof(*array), compare_commit_dist); | |
391 | for (p = list, i = 0; i < cnt; i++) { | |
392 | struct name_decoration *r = xmalloc(sizeof(*r) + 100); | |
393 | struct object *obj = &(array[i].commit->object); | |
394 | ||
395 | sprintf(r->name, "dist=%d", array[i].distance); | |
396 | r->next = add_decoration(&name_decoration, obj, r); | |
397 | p->item = array[i].commit; | |
398 | p = p->next; | |
399 | } | |
400 | if (p) | |
401 | p->next = NULL; | |
402 | free(array); | |
403 | return list; | |
404 | } | |
405 | ||
1daa09d9 JH |
406 | /* |
407 | * zero or positive weight is the number of interesting commits it can | |
408 | * reach, including itself. Especially, weight = 0 means it does not | |
409 | * reach any tree-changing commits (e.g. just above uninteresting one | |
410 | * but traversal is with pathspec). | |
411 | * | |
412 | * weight = -1 means it has one parent and its distance is yet to | |
413 | * be computed. | |
414 | * | |
415 | * weight = -2 means it has more than one parent and its distance is | |
416 | * unknown. After running count_distance() first, they will get zero | |
417 | * or positive distance. | |
418 | */ | |
ce0cbad7 | 419 | static struct commit_list *do_find_bisection(struct commit_list *list, |
50e62a8e CC |
420 | int nr, int *weights, |
421 | int find_all) | |
1daa09d9 | 422 | { |
53271411 | 423 | int n, counted; |
77c11e06 | 424 | struct commit_list *p; |
1daa09d9 | 425 | |
1c4fea3a JH |
426 | counted = 0; |
427 | ||
428 | for (n = 0, p = list; p; p = p->next) { | |
1daa09d9 JH |
429 | struct commit *commit = p->item; |
430 | unsigned flags = commit->object.flags; | |
431 | ||
432 | p->item->util = &weights[n++]; | |
433 | switch (count_interesting_parents(commit)) { | |
434 | case 0: | |
7dc0fe3b | 435 | if (!(flags & TREESAME)) { |
1c4fea3a JH |
436 | weight_set(p, 1); |
437 | counted++; | |
1daa09d9 JH |
438 | show_list("bisection 2 count one", |
439 | counted, nr, list); | |
1c4fea3a | 440 | } |
1daa09d9 JH |
441 | /* |
442 | * otherwise, it is known not to reach any | |
443 | * tree-changing commit and gets weight 0. | |
444 | */ | |
445 | break; | |
446 | case 1: | |
447 | weight_set(p, -1); | |
448 | break; | |
449 | default: | |
450 | weight_set(p, -2); | |
451 | break; | |
1c4fea3a JH |
452 | } |
453 | } | |
454 | ||
1daa09d9 JH |
455 | show_list("bisection 2 initialize", counted, nr, list); |
456 | ||
1c4fea3a JH |
457 | /* |
458 | * If you have only one parent in the resulting set | |
459 | * then you can reach one commit more than that parent | |
460 | * can reach. So we do not have to run the expensive | |
461 | * count_distance() for single strand of pearls. | |
462 | * | |
463 | * However, if you have more than one parents, you cannot | |
464 | * just add their distance and one for yourself, since | |
465 | * they usually reach the same ancestor and you would | |
466 | * end up counting them twice that way. | |
467 | * | |
468 | * So we will first count distance of merges the usual | |
469 | * way, and then fill the blanks using cheaper algorithm. | |
470 | */ | |
471 | for (p = list; p; p = p->next) { | |
1daa09d9 | 472 | if (p->item->object.flags & UNINTERESTING) |
1c4fea3a | 473 | continue; |
53271411 | 474 | if (weight(p) != -2) |
1c4fea3a | 475 | continue; |
53271411 | 476 | weight_set(p, count_distance(p)); |
1daa09d9 | 477 | clear_distance(list); |
1c4fea3a JH |
478 | |
479 | /* Does it happen to be at exactly half-way? */ | |
50e62a8e | 480 | if (!find_all && halfway(p, nr)) |
1c4fea3a | 481 | return p; |
1c4fea3a JH |
482 | counted++; |
483 | } | |
484 | ||
1daa09d9 JH |
485 | show_list("bisection 2 count_distance", counted, nr, list); |
486 | ||
1c4fea3a JH |
487 | while (counted < nr) { |
488 | for (p = list; p; p = p->next) { | |
489 | struct commit_list *q; | |
1daa09d9 | 490 | unsigned flags = p->item->object.flags; |
1c4fea3a | 491 | |
1daa09d9 | 492 | if (0 <= weight(p)) |
1c4fea3a | 493 | continue; |
1daa09d9 JH |
494 | for (q = p->item->parents; q; q = q->next) { |
495 | if (q->item->object.flags & UNINTERESTING) | |
496 | continue; | |
497 | if (0 <= weight(q)) | |
1c4fea3a | 498 | break; |
1daa09d9 | 499 | } |
1c4fea3a JH |
500 | if (!q) |
501 | continue; | |
1daa09d9 JH |
502 | |
503 | /* | |
504 | * weight for p is unknown but q is known. | |
505 | * add one for p itself if p is to be counted, | |
506 | * otherwise inherit it from q directly. | |
507 | */ | |
7dc0fe3b | 508 | if (!(flags & TREESAME)) { |
1daa09d9 JH |
509 | weight_set(p, weight(q)+1); |
510 | counted++; | |
511 | show_list("bisection 2 count one", | |
512 | counted, nr, list); | |
513 | } | |
514 | else | |
515 | weight_set(p, weight(q)); | |
1c4fea3a JH |
516 | |
517 | /* Does it happen to be at exactly half-way? */ | |
50e62a8e | 518 | if (!find_all && halfway(p, nr)) |
1c4fea3a | 519 | return p; |
1c4fea3a JH |
520 | } |
521 | } | |
522 | ||
1daa09d9 JH |
523 | show_list("bisection 2 counted all", counted, nr, list); |
524 | ||
50e62a8e CC |
525 | if (!find_all) |
526 | return best_bisection(list, nr); | |
527 | else | |
528 | return best_bisection_sorted(list, nr); | |
ce0cbad7 CC |
529 | } |
530 | ||
531 | static struct commit_list *find_bisection(struct commit_list *list, | |
50e62a8e CC |
532 | int *reaches, int *all, |
533 | int find_all) | |
ce0cbad7 CC |
534 | { |
535 | int nr, on_list; | |
536 | struct commit_list *p, *best, *next, *last; | |
537 | int *weights; | |
538 | ||
539 | show_list("bisection 2 entry", 0, 0, list); | |
540 | ||
541 | /* | |
542 | * Count the number of total and tree-changing items on the | |
543 | * list, while reversing the list. | |
544 | */ | |
545 | for (nr = on_list = 0, last = NULL, p = list; | |
546 | p; | |
547 | p = next) { | |
548 | unsigned flags = p->item->object.flags; | |
549 | ||
550 | next = p->next; | |
551 | if (flags & UNINTERESTING) | |
552 | continue; | |
553 | p->next = last; | |
554 | last = p; | |
7dc0fe3b | 555 | if (!(flags & TREESAME)) |
ce0cbad7 CC |
556 | nr++; |
557 | on_list++; | |
558 | } | |
559 | list = last; | |
560 | show_list("bisection 2 sorted", 0, nr, list); | |
561 | ||
562 | *all = nr; | |
563 | weights = xcalloc(on_list, sizeof(*weights)); | |
564 | ||
565 | /* Do the real work of finding bisection commit. */ | |
50e62a8e | 566 | best = do_find_bisection(list, nr, weights, find_all); |
17ed1580 | 567 | if (best) { |
50e62a8e CC |
568 | if (!find_all) |
569 | best->next = NULL; | |
17ed1580 CC |
570 | *reaches = weight(best); |
571 | } | |
1c4fea3a JH |
572 | free(weights); |
573 | return best; | |
574 | } | |
575 | ||
9f199b15 CC |
576 | static inline int log2i(int n) |
577 | { | |
578 | int log2 = 0; | |
579 | ||
580 | for (; n > 1; n >>= 1) | |
581 | log2++; | |
582 | ||
583 | return log2; | |
584 | } | |
585 | ||
586 | static inline int exp2i(int n) | |
587 | { | |
588 | return 1 << n; | |
589 | } | |
590 | ||
591 | /* | |
592 | * Estimate the number of bisect steps left (after the current step) | |
593 | * | |
594 | * For any x between 0 included and 2^n excluded, the probability for | |
595 | * n - 1 steps left looks like: | |
596 | * | |
597 | * P(2^n + x) == (2^n - x) / (2^n + x) | |
598 | * | |
599 | * and P(2^n + x) < 0.5 means 2^n < 3x | |
600 | */ | |
601 | static int estimate_bisect_steps(int all) | |
602 | { | |
603 | int n, x, e; | |
604 | ||
605 | if (all < 3) | |
606 | return 0; | |
607 | ||
608 | n = log2i(all); | |
609 | e = exp2i(n); | |
610 | x = all - e; | |
611 | ||
612 | return (e < 3 * x) ? n : n - 1; | |
613 | } | |
614 | ||
a633fca0 | 615 | int cmd_rev_list(int argc, const char **argv, const char *prefix) |
64745109 | 616 | { |
ae563542 | 617 | struct commit_list *list; |
d9a83684 | 618 | int i; |
42cabc34 | 619 | int read_from_stdin = 0; |
ff62d732 | 620 | int bisect_list = 0; |
457f08a0 | 621 | int bisect_show_vars = 0; |
50e62a8e | 622 | int bisect_find_all = 0; |
27350891 | 623 | int quiet = 0; |
64745109 | 624 | |
ef90d6d4 | 625 | git_config(git_default_config, NULL); |
a633fca0 | 626 | init_revisions(&revs, prefix); |
7594c4b2 JH |
627 | revs.abbrev = 0; |
628 | revs.commit_format = CMIT_FMT_UNSPECIFIED; | |
a4a88b2b | 629 | argc = setup_revisions(argc, argv, &revs, NULL); |
ae563542 | 630 | |
d85fe389 | 631 | quiet = DIFF_OPT_TST(&revs.diffopt, QUIET); |
fcfda02b | 632 | for (i = 1 ; i < argc; i++) { |
cf484544 | 633 | const char *arg = argv[i]; |
fcfda02b | 634 | |
a6f68d47 | 635 | if (!strcmp(arg, "--header")) { |
7594c4b2 | 636 | revs.verbose_header = 1; |
9d97aa64 LT |
637 | continue; |
638 | } | |
dc68c4ff JH |
639 | if (!strcmp(arg, "--timestamp")) { |
640 | show_timestamp = 1; | |
641 | continue; | |
642 | } | |
8b3a1e05 LT |
643 | if (!strcmp(arg, "--bisect")) { |
644 | bisect_list = 1; | |
645 | continue; | |
646 | } | |
50e62a8e CC |
647 | if (!strcmp(arg, "--bisect-all")) { |
648 | bisect_list = 1; | |
649 | bisect_find_all = 1; | |
6e46cc0d | 650 | revs.show_decorations = 1; |
50e62a8e CC |
651 | continue; |
652 | } | |
457f08a0 JH |
653 | if (!strcmp(arg, "--bisect-vars")) { |
654 | bisect_list = 1; | |
655 | bisect_show_vars = 1; | |
656 | continue; | |
657 | } | |
42cabc34 JH |
658 | if (!strcmp(arg, "--stdin")) { |
659 | if (read_from_stdin++) | |
660 | die("--stdin given twice?"); | |
661 | read_revisions_from_stdin(&revs); | |
662 | continue; | |
663 | } | |
ae563542 | 664 | usage(rev_list_usage); |
a6f68d47 | 665 | |
fcfda02b | 666 | } |
7594c4b2 JH |
667 | if (revs.commit_format != CMIT_FMT_UNSPECIFIED) { |
668 | /* The command line has a --pretty */ | |
669 | hdr_termination = '\n'; | |
670 | if (revs.commit_format == CMIT_FMT_ONELINE) | |
91539833 | 671 | header_prefix = ""; |
7594c4b2 | 672 | else |
91539833 | 673 | header_prefix = "commit "; |
7594c4b2 | 674 | } |
db89665f JH |
675 | else if (revs.verbose_header) |
676 | /* Only --header was specified */ | |
677 | revs.commit_format = CMIT_FMT_RAW; | |
fcfda02b | 678 | |
ae563542 | 679 | list = revs.commits; |
ae563542 | 680 | |
8c1f0b44 JH |
681 | if ((!list && |
682 | (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) && | |
1f1e895f | 683 | !revs.pending.nr)) || |
8c1f0b44 | 684 | revs.diff) |
7b34c2fa LT |
685 | usage(rev_list_usage); |
686 | ||
0843acfd JK |
687 | save_commit_buffer = revs.verbose_header || |
688 | revs.grep_filter.pattern_list; | |
4e1dc640 JH |
689 | if (bisect_list) |
690 | revs.limited = 1; | |
9181ca2c | 691 | |
3d51e1b5 MK |
692 | if (prepare_revision_walk(&revs)) |
693 | die("revision walk setup failed"); | |
a4a88b2b | 694 | if (revs.tree_objects) |
8d1d8f83 | 695 | mark_edges_uninteresting(revs.commits, &revs, show_edge); |
a4a88b2b | 696 | |
457f08a0 JH |
697 | if (bisect_list) { |
698 | int reaches = reaches, all = all; | |
699 | ||
50e62a8e CC |
700 | revs.commits = find_bisection(revs.commits, &reaches, &all, |
701 | bisect_find_all); | |
457f08a0 JH |
702 | if (bisect_show_vars) { |
703 | int cnt; | |
50e62a8e | 704 | char hex[41]; |
457f08a0 JH |
705 | if (!revs.commits) |
706 | return 1; | |
707 | /* | |
708 | * revs.commits can reach "reaches" commits among | |
709 | * "all" commits. If it is good, then there are | |
710 | * (all-reaches) commits left to be bisected. | |
711 | * On the other hand, if it is bad, then the set | |
712 | * to bisect is "reaches". | |
713 | * A bisect set of size N has (N-1) commits further | |
714 | * to test, as we already know one bad one. | |
715 | */ | |
50e62a8e | 716 | cnt = all - reaches; |
457f08a0 JH |
717 | if (cnt < reaches) |
718 | cnt = reaches; | |
50e62a8e CC |
719 | strcpy(hex, sha1_to_hex(revs.commits->item->object.sha1)); |
720 | ||
721 | if (bisect_find_all) { | |
722 | traverse_commit_list(&revs, show_commit, show_object); | |
723 | printf("------\n"); | |
724 | } | |
725 | ||
457f08a0 JH |
726 | printf("bisect_rev=%s\n" |
727 | "bisect_nr=%d\n" | |
728 | "bisect_good=%d\n" | |
729 | "bisect_bad=%d\n" | |
9f199b15 CC |
730 | "bisect_all=%d\n" |
731 | "bisect_steps=%d\n", | |
50e62a8e | 732 | hex, |
457f08a0 JH |
733 | cnt - 1, |
734 | all - reaches - 1, | |
735 | reaches - 1, | |
9f199b15 CC |
736 | all, |
737 | estimate_bisect_steps(all)); | |
457f08a0 JH |
738 | return 0; |
739 | } | |
740 | } | |
7b34c2fa | 741 | |
27350891 SP |
742 | traverse_commit_list(&revs, |
743 | quiet ? finish_commit : show_commit, | |
744 | quiet ? finish_object : show_object); | |
8906300f | 745 | |
64745109 LT |
746 | return 0; |
747 | } |