]>
Commit | Line | Data |
---|---|---|
a2ad79ce CC |
1 | #include "cache.h" |
2 | #include "commit.h" | |
3 | #include "diff.h" | |
4 | #include "revision.h" | |
1bf072e3 CC |
5 | #include "refs.h" |
6 | #include "list-objects.h" | |
3b437b0d | 7 | #include "quote.h" |
4eb5b646 | 8 | #include "sha1-lookup.h" |
ef24c7ca | 9 | #include "run-command.h" |
a2ad79ce CC |
10 | #include "bisect.h" |
11 | ||
6212b1aa CC |
12 | struct sha1_array { |
13 | unsigned char (*sha1)[20]; | |
14 | int sha1_nr; | |
15 | int sha1_alloc; | |
1da8c4fc | 16 | int sorted; |
6212b1aa CC |
17 | }; |
18 | ||
fad2d31d | 19 | static struct sha1_array good_revs; |
6212b1aa | 20 | static struct sha1_array skipped_revs; |
1bf072e3 | 21 | |
ef24c7ca CC |
22 | static const unsigned char *current_bad_sha1; |
23 | ||
1c953a1f CC |
24 | struct argv_array { |
25 | const char **argv; | |
26 | int argv_nr; | |
27 | int argv_alloc; | |
28 | }; | |
29 | ||
ef24c7ca CC |
30 | static const char *argv_diff_tree[] = {"diff-tree", "--pretty", NULL, NULL}; |
31 | static const char *argv_checkout[] = {"checkout", "-q", NULL, "--", NULL}; | |
32 | static const char *argv_show_branch[] = {"show-branch", NULL, NULL}; | |
33 | ||
a2ad79ce CC |
34 | /* bits #0-15 in revision.h */ |
35 | ||
36 | #define COUNTED (1u<<16) | |
37 | ||
38 | /* | |
39 | * This is a truly stupid algorithm, but it's only | |
40 | * used for bisection, and we just don't care enough. | |
41 | * | |
42 | * We care just barely enough to avoid recursing for | |
43 | * non-merge entries. | |
44 | */ | |
45 | static int count_distance(struct commit_list *entry) | |
46 | { | |
47 | int nr = 0; | |
48 | ||
49 | while (entry) { | |
50 | struct commit *commit = entry->item; | |
51 | struct commit_list *p; | |
52 | ||
53 | if (commit->object.flags & (UNINTERESTING | COUNTED)) | |
54 | break; | |
55 | if (!(commit->object.flags & TREESAME)) | |
56 | nr++; | |
57 | commit->object.flags |= COUNTED; | |
58 | p = commit->parents; | |
59 | entry = p; | |
60 | if (p) { | |
61 | p = p->next; | |
62 | while (p) { | |
63 | nr += count_distance(p); | |
64 | p = p->next; | |
65 | } | |
66 | } | |
67 | } | |
68 | ||
69 | return nr; | |
70 | } | |
71 | ||
72 | static void clear_distance(struct commit_list *list) | |
73 | { | |
74 | while (list) { | |
75 | struct commit *commit = list->item; | |
76 | commit->object.flags &= ~COUNTED; | |
77 | list = list->next; | |
78 | } | |
79 | } | |
80 | ||
81 | #define DEBUG_BISECT 0 | |
82 | ||
83 | static inline int weight(struct commit_list *elem) | |
84 | { | |
85 | return *((int*)(elem->item->util)); | |
86 | } | |
87 | ||
88 | static inline void weight_set(struct commit_list *elem, int weight) | |
89 | { | |
90 | *((int*)(elem->item->util)) = weight; | |
91 | } | |
92 | ||
93 | static int count_interesting_parents(struct commit *commit) | |
94 | { | |
95 | struct commit_list *p; | |
96 | int count; | |
97 | ||
98 | for (count = 0, p = commit->parents; p; p = p->next) { | |
99 | if (p->item->object.flags & UNINTERESTING) | |
100 | continue; | |
101 | count++; | |
102 | } | |
103 | return count; | |
104 | } | |
105 | ||
106 | static inline int halfway(struct commit_list *p, int nr) | |
107 | { | |
108 | /* | |
109 | * Don't short-cut something we are not going to return! | |
110 | */ | |
111 | if (p->item->object.flags & TREESAME) | |
112 | return 0; | |
113 | if (DEBUG_BISECT) | |
114 | return 0; | |
115 | /* | |
116 | * 2 and 3 are halfway of 5. | |
117 | * 3 is halfway of 6 but 2 and 4 are not. | |
118 | */ | |
119 | switch (2 * weight(p) - nr) { | |
120 | case -1: case 0: case 1: | |
121 | return 1; | |
122 | default: | |
123 | return 0; | |
124 | } | |
125 | } | |
126 | ||
127 | #if !DEBUG_BISECT | |
128 | #define show_list(a,b,c,d) do { ; } while (0) | |
129 | #else | |
130 | static void show_list(const char *debug, int counted, int nr, | |
131 | struct commit_list *list) | |
132 | { | |
133 | struct commit_list *p; | |
134 | ||
135 | fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr); | |
136 | ||
137 | for (p = list; p; p = p->next) { | |
138 | struct commit_list *pp; | |
139 | struct commit *commit = p->item; | |
140 | unsigned flags = commit->object.flags; | |
141 | enum object_type type; | |
142 | unsigned long size; | |
143 | char *buf = read_sha1_file(commit->object.sha1, &type, &size); | |
144 | char *ep, *sp; | |
145 | ||
146 | fprintf(stderr, "%c%c%c ", | |
147 | (flags & TREESAME) ? ' ' : 'T', | |
148 | (flags & UNINTERESTING) ? 'U' : ' ', | |
149 | (flags & COUNTED) ? 'C' : ' '); | |
150 | if (commit->util) | |
151 | fprintf(stderr, "%3d", weight(p)); | |
152 | else | |
153 | fprintf(stderr, "---"); | |
154 | fprintf(stderr, " %.*s", 8, sha1_to_hex(commit->object.sha1)); | |
155 | for (pp = commit->parents; pp; pp = pp->next) | |
156 | fprintf(stderr, " %.*s", 8, | |
157 | sha1_to_hex(pp->item->object.sha1)); | |
158 | ||
159 | sp = strstr(buf, "\n\n"); | |
160 | if (sp) { | |
161 | sp += 2; | |
162 | for (ep = sp; *ep && *ep != '\n'; ep++) | |
163 | ; | |
164 | fprintf(stderr, " %.*s", (int)(ep - sp), sp); | |
165 | } | |
166 | fprintf(stderr, "\n"); | |
167 | } | |
168 | } | |
169 | #endif /* DEBUG_BISECT */ | |
170 | ||
171 | static struct commit_list *best_bisection(struct commit_list *list, int nr) | |
172 | { | |
173 | struct commit_list *p, *best; | |
174 | int best_distance = -1; | |
175 | ||
176 | best = list; | |
177 | for (p = list; p; p = p->next) { | |
178 | int distance; | |
179 | unsigned flags = p->item->object.flags; | |
180 | ||
181 | if (flags & TREESAME) | |
182 | continue; | |
183 | distance = weight(p); | |
184 | if (nr - distance < distance) | |
185 | distance = nr - distance; | |
186 | if (distance > best_distance) { | |
187 | best = p; | |
188 | best_distance = distance; | |
189 | } | |
190 | } | |
191 | ||
192 | return best; | |
193 | } | |
194 | ||
195 | struct commit_dist { | |
196 | struct commit *commit; | |
197 | int distance; | |
198 | }; | |
199 | ||
200 | static int compare_commit_dist(const void *a_, const void *b_) | |
201 | { | |
202 | struct commit_dist *a, *b; | |
203 | ||
204 | a = (struct commit_dist *)a_; | |
205 | b = (struct commit_dist *)b_; | |
206 | if (a->distance != b->distance) | |
207 | return b->distance - a->distance; /* desc sort */ | |
208 | return hashcmp(a->commit->object.sha1, b->commit->object.sha1); | |
209 | } | |
210 | ||
211 | static struct commit_list *best_bisection_sorted(struct commit_list *list, int nr) | |
212 | { | |
213 | struct commit_list *p; | |
214 | struct commit_dist *array = xcalloc(nr, sizeof(*array)); | |
215 | int cnt, i; | |
216 | ||
217 | for (p = list, cnt = 0; p; p = p->next) { | |
218 | int distance; | |
219 | unsigned flags = p->item->object.flags; | |
220 | ||
221 | if (flags & TREESAME) | |
222 | continue; | |
223 | distance = weight(p); | |
224 | if (nr - distance < distance) | |
225 | distance = nr - distance; | |
226 | array[cnt].commit = p->item; | |
227 | array[cnt].distance = distance; | |
228 | cnt++; | |
229 | } | |
230 | qsort(array, cnt, sizeof(*array), compare_commit_dist); | |
231 | for (p = list, i = 0; i < cnt; i++) { | |
232 | struct name_decoration *r = xmalloc(sizeof(*r) + 100); | |
233 | struct object *obj = &(array[i].commit->object); | |
234 | ||
235 | sprintf(r->name, "dist=%d", array[i].distance); | |
236 | r->next = add_decoration(&name_decoration, obj, r); | |
237 | p->item = array[i].commit; | |
238 | p = p->next; | |
239 | } | |
240 | if (p) | |
241 | p->next = NULL; | |
242 | free(array); | |
243 | return list; | |
244 | } | |
245 | ||
246 | /* | |
247 | * zero or positive weight is the number of interesting commits it can | |
248 | * reach, including itself. Especially, weight = 0 means it does not | |
249 | * reach any tree-changing commits (e.g. just above uninteresting one | |
250 | * but traversal is with pathspec). | |
251 | * | |
252 | * weight = -1 means it has one parent and its distance is yet to | |
253 | * be computed. | |
254 | * | |
255 | * weight = -2 means it has more than one parent and its distance is | |
256 | * unknown. After running count_distance() first, they will get zero | |
257 | * or positive distance. | |
258 | */ | |
259 | static struct commit_list *do_find_bisection(struct commit_list *list, | |
260 | int nr, int *weights, | |
261 | int find_all) | |
262 | { | |
263 | int n, counted; | |
264 | struct commit_list *p; | |
265 | ||
266 | counted = 0; | |
267 | ||
268 | for (n = 0, p = list; p; p = p->next) { | |
269 | struct commit *commit = p->item; | |
270 | unsigned flags = commit->object.flags; | |
271 | ||
272 | p->item->util = &weights[n++]; | |
273 | switch (count_interesting_parents(commit)) { | |
274 | case 0: | |
275 | if (!(flags & TREESAME)) { | |
276 | weight_set(p, 1); | |
277 | counted++; | |
278 | show_list("bisection 2 count one", | |
279 | counted, nr, list); | |
280 | } | |
281 | /* | |
282 | * otherwise, it is known not to reach any | |
283 | * tree-changing commit and gets weight 0. | |
284 | */ | |
285 | break; | |
286 | case 1: | |
287 | weight_set(p, -1); | |
288 | break; | |
289 | default: | |
290 | weight_set(p, -2); | |
291 | break; | |
292 | } | |
293 | } | |
294 | ||
295 | show_list("bisection 2 initialize", counted, nr, list); | |
296 | ||
297 | /* | |
298 | * If you have only one parent in the resulting set | |
299 | * then you can reach one commit more than that parent | |
300 | * can reach. So we do not have to run the expensive | |
301 | * count_distance() for single strand of pearls. | |
302 | * | |
303 | * However, if you have more than one parents, you cannot | |
304 | * just add their distance and one for yourself, since | |
305 | * they usually reach the same ancestor and you would | |
306 | * end up counting them twice that way. | |
307 | * | |
308 | * So we will first count distance of merges the usual | |
309 | * way, and then fill the blanks using cheaper algorithm. | |
310 | */ | |
311 | for (p = list; p; p = p->next) { | |
312 | if (p->item->object.flags & UNINTERESTING) | |
313 | continue; | |
314 | if (weight(p) != -2) | |
315 | continue; | |
316 | weight_set(p, count_distance(p)); | |
317 | clear_distance(list); | |
318 | ||
319 | /* Does it happen to be at exactly half-way? */ | |
320 | if (!find_all && halfway(p, nr)) | |
321 | return p; | |
322 | counted++; | |
323 | } | |
324 | ||
325 | show_list("bisection 2 count_distance", counted, nr, list); | |
326 | ||
327 | while (counted < nr) { | |
328 | for (p = list; p; p = p->next) { | |
329 | struct commit_list *q; | |
330 | unsigned flags = p->item->object.flags; | |
331 | ||
332 | if (0 <= weight(p)) | |
333 | continue; | |
334 | for (q = p->item->parents; q; q = q->next) { | |
335 | if (q->item->object.flags & UNINTERESTING) | |
336 | continue; | |
337 | if (0 <= weight(q)) | |
338 | break; | |
339 | } | |
340 | if (!q) | |
341 | continue; | |
342 | ||
343 | /* | |
344 | * weight for p is unknown but q is known. | |
345 | * add one for p itself if p is to be counted, | |
346 | * otherwise inherit it from q directly. | |
347 | */ | |
348 | if (!(flags & TREESAME)) { | |
349 | weight_set(p, weight(q)+1); | |
350 | counted++; | |
351 | show_list("bisection 2 count one", | |
352 | counted, nr, list); | |
353 | } | |
354 | else | |
355 | weight_set(p, weight(q)); | |
356 | ||
357 | /* Does it happen to be at exactly half-way? */ | |
358 | if (!find_all && halfway(p, nr)) | |
359 | return p; | |
360 | } | |
361 | } | |
362 | ||
363 | show_list("bisection 2 counted all", counted, nr, list); | |
364 | ||
365 | if (!find_all) | |
366 | return best_bisection(list, nr); | |
367 | else | |
368 | return best_bisection_sorted(list, nr); | |
369 | } | |
370 | ||
371 | struct commit_list *find_bisection(struct commit_list *list, | |
372 | int *reaches, int *all, | |
373 | int find_all) | |
374 | { | |
375 | int nr, on_list; | |
376 | struct commit_list *p, *best, *next, *last; | |
377 | int *weights; | |
378 | ||
379 | show_list("bisection 2 entry", 0, 0, list); | |
380 | ||
381 | /* | |
382 | * Count the number of total and tree-changing items on the | |
383 | * list, while reversing the list. | |
384 | */ | |
385 | for (nr = on_list = 0, last = NULL, p = list; | |
386 | p; | |
387 | p = next) { | |
388 | unsigned flags = p->item->object.flags; | |
389 | ||
390 | next = p->next; | |
391 | if (flags & UNINTERESTING) | |
392 | continue; | |
393 | p->next = last; | |
394 | last = p; | |
395 | if (!(flags & TREESAME)) | |
396 | nr++; | |
397 | on_list++; | |
398 | } | |
399 | list = last; | |
400 | show_list("bisection 2 sorted", 0, nr, list); | |
401 | ||
402 | *all = nr; | |
403 | weights = xcalloc(on_list, sizeof(*weights)); | |
404 | ||
405 | /* Do the real work of finding bisection commit. */ | |
406 | best = do_find_bisection(list, nr, weights, find_all); | |
407 | if (best) { | |
408 | if (!find_all) | |
409 | best->next = NULL; | |
410 | *reaches = weight(best); | |
411 | } | |
412 | free(weights); | |
413 | return best; | |
414 | } | |
415 | ||
1c953a1f | 416 | static void argv_array_push(struct argv_array *array, const char *string) |
3755ccdb | 417 | { |
1c953a1f CC |
418 | ALLOC_GROW(array->argv, array->argv_nr + 1, array->argv_alloc); |
419 | array->argv[array->argv_nr++] = string; | |
420 | } | |
3755ccdb | 421 | |
1c953a1f CC |
422 | static void argv_array_push_sha1(struct argv_array *array, |
423 | const unsigned char *sha1, | |
424 | const char *format) | |
425 | { | |
426 | struct strbuf buf = STRBUF_INIT; | |
3755ccdb | 427 | strbuf_addf(&buf, format, sha1_to_hex(sha1)); |
1c953a1f | 428 | argv_array_push(array, strbuf_detach(&buf, NULL)); |
3755ccdb CC |
429 | } |
430 | ||
fad2d31d CC |
431 | static void sha1_array_push(struct sha1_array *array, |
432 | const unsigned char *sha1) | |
433 | { | |
434 | ALLOC_GROW(array->sha1, array->sha1_nr + 1, array->sha1_alloc); | |
435 | hashcpy(array->sha1[array->sha1_nr++], sha1); | |
436 | } | |
437 | ||
1bf072e3 CC |
438 | static int register_ref(const char *refname, const unsigned char *sha1, |
439 | int flags, void *cb_data) | |
440 | { | |
441 | if (!strcmp(refname, "bad")) { | |
ef24c7ca | 442 | current_bad_sha1 = sha1; |
1bf072e3 | 443 | } else if (!prefixcmp(refname, "good-")) { |
fad2d31d | 444 | sha1_array_push(&good_revs, sha1); |
1bf072e3 | 445 | } else if (!prefixcmp(refname, "skip-")) { |
fad2d31d | 446 | sha1_array_push(&skipped_revs, sha1); |
1bf072e3 CC |
447 | } |
448 | ||
449 | return 0; | |
450 | } | |
451 | ||
452 | static int read_bisect_refs(void) | |
453 | { | |
454 | return for_each_ref_in("refs/bisect/", register_ref, NULL); | |
455 | } | |
456 | ||
1c953a1f | 457 | void read_bisect_paths(struct argv_array *array) |
3b437b0d CC |
458 | { |
459 | struct strbuf str = STRBUF_INIT; | |
460 | const char *filename = git_path("BISECT_NAMES"); | |
461 | FILE *fp = fopen(filename, "r"); | |
462 | ||
463 | if (!fp) | |
464 | die("Could not open file '%s': %s", filename, strerror(errno)); | |
465 | ||
466 | while (strbuf_getline(&str, fp, '\n') != EOF) { | |
467 | char *quoted; | |
468 | int res; | |
469 | ||
470 | strbuf_trim(&str); | |
471 | quoted = strbuf_detach(&str, NULL); | |
1c953a1f CC |
472 | res = sq_dequote_to_argv(quoted, &array->argv, |
473 | &array->argv_nr, &array->argv_alloc); | |
3b437b0d CC |
474 | if (res) |
475 | die("Badly quoted content in file '%s': %s", | |
476 | filename, quoted); | |
477 | } | |
478 | ||
479 | strbuf_release(&str); | |
480 | fclose(fp); | |
481 | } | |
482 | ||
aaaff9e2 | 483 | static int array_cmp(const void *a, const void *b) |
95188648 CC |
484 | { |
485 | return hashcmp(a, b); | |
486 | } | |
487 | ||
aaaff9e2 | 488 | static void sort_sha1_array(struct sha1_array *array) |
95188648 | 489 | { |
aaaff9e2 | 490 | qsort(array->sha1, array->sha1_nr, sizeof(*array->sha1), array_cmp); |
1da8c4fc CC |
491 | |
492 | array->sorted = 1; | |
95188648 CC |
493 | } |
494 | ||
aaaff9e2 | 495 | static const unsigned char *sha1_access(size_t index, void *table) |
4eb5b646 | 496 | { |
aaaff9e2 CC |
497 | unsigned char (*array)[20] = table; |
498 | return array[index]; | |
4eb5b646 CC |
499 | } |
500 | ||
aaaff9e2 CC |
501 | static int lookup_sha1_array(struct sha1_array *array, |
502 | const unsigned char *sha1) | |
95188648 | 503 | { |
1da8c4fc CC |
504 | if (!array->sorted) |
505 | sort_sha1_array(array); | |
506 | ||
aaaff9e2 | 507 | return sha1_pos(sha1, array->sha1, array->sha1_nr, sha1_access); |
95188648 CC |
508 | } |
509 | ||
c0537662 CC |
510 | static char *join_sha1_array_hex(struct sha1_array *array, char delim) |
511 | { | |
512 | struct strbuf joined_hexs = STRBUF_INIT; | |
513 | int i; | |
514 | ||
515 | for (i = 0; i < array->sha1_nr; i++) { | |
516 | strbuf_addstr(&joined_hexs, sha1_to_hex(array->sha1[i])); | |
517 | if (i + 1 < array->sha1_nr) | |
518 | strbuf_addch(&joined_hexs, delim); | |
519 | } | |
520 | ||
521 | return strbuf_detach(&joined_hexs, NULL); | |
522 | } | |
523 | ||
95188648 CC |
524 | struct commit_list *filter_skipped(struct commit_list *list, |
525 | struct commit_list **tried, | |
526 | int show_all) | |
527 | { | |
528 | struct commit_list *filtered = NULL, **f = &filtered; | |
529 | ||
530 | *tried = NULL; | |
531 | ||
6212b1aa | 532 | if (!skipped_revs.sha1_nr) |
95188648 CC |
533 | return list; |
534 | ||
95188648 CC |
535 | while (list) { |
536 | struct commit_list *next = list->next; | |
537 | list->next = NULL; | |
aaaff9e2 CC |
538 | if (0 <= lookup_sha1_array(&skipped_revs, |
539 | list->item->object.sha1)) { | |
95188648 CC |
540 | /* Move current to tried list */ |
541 | *tried = list; | |
542 | tried = &list->next; | |
543 | } else { | |
544 | if (!show_all) | |
545 | return list; | |
546 | /* Move current to filtered list */ | |
547 | *f = list; | |
548 | f = &list->next; | |
549 | } | |
550 | list = next; | |
551 | } | |
552 | ||
553 | return filtered; | |
554 | } | |
1bf072e3 | 555 | |
3b437b0d | 556 | static void bisect_rev_setup(struct rev_info *revs, const char *prefix) |
1bf072e3 | 557 | { |
2b020695 | 558 | struct argv_array rev_argv = { NULL, 0, 0 }; |
fad2d31d CC |
559 | int i; |
560 | ||
3b437b0d CC |
561 | init_revisions(revs, prefix); |
562 | revs->abbrev = 0; | |
563 | revs->commit_format = CMIT_FMT_UNSPECIFIED; | |
1bf072e3 | 564 | |
1c953a1f CC |
565 | /* rev_argv.argv[0] will be ignored by setup_revisions */ |
566 | argv_array_push(&rev_argv, xstrdup("bisect_rev_setup")); | |
567 | argv_array_push_sha1(&rev_argv, current_bad_sha1, "%s"); | |
fad2d31d | 568 | for (i = 0; i < good_revs.sha1_nr; i++) |
1c953a1f CC |
569 | argv_array_push_sha1(&rev_argv, good_revs.sha1[i], "^%s"); |
570 | argv_array_push(&rev_argv, xstrdup("--")); | |
571 | read_bisect_paths(&rev_argv); | |
572 | argv_array_push(&rev_argv, NULL); | |
3b437b0d | 573 | |
1c953a1f | 574 | setup_revisions(rev_argv.argv_nr, rev_argv.argv, revs, NULL); |
3b437b0d CC |
575 | revs->limited = 1; |
576 | } | |
577 | ||
2b020695 | 578 | static void bisect_common(struct rev_info *revs, int *reaches, int *all) |
2ace9727 | 579 | { |
2ace9727 CC |
580 | if (prepare_revision_walk(revs)) |
581 | die("revision walk setup failed"); | |
582 | if (revs->tree_objects) | |
583 | mark_edges_uninteresting(revs->commits, revs, NULL); | |
584 | ||
585 | revs->commits = find_bisection(revs->commits, reaches, all, | |
6212b1aa | 586 | !!skipped_revs.sha1_nr); |
2ace9727 CC |
587 | } |
588 | ||
ef24c7ca CC |
589 | static void exit_if_skipped_commits(struct commit_list *tried, |
590 | const unsigned char *bad) | |
591 | { | |
592 | if (!tried) | |
593 | return; | |
594 | ||
595 | printf("There are only 'skip'ped commits left to test.\n" | |
596 | "The first bad commit could be any of:\n"); | |
597 | print_commit_list(tried, "%s\n", "%s\n"); | |
598 | if (bad) | |
599 | printf("%s\n", sha1_to_hex(bad)); | |
600 | printf("We cannot bisect more!\n"); | |
601 | exit(2); | |
602 | } | |
603 | ||
c0537662 CC |
604 | static int is_expected_rev(const unsigned char *sha1) |
605 | { | |
606 | const char *filename = git_path("BISECT_EXPECTED_REV"); | |
607 | struct stat st; | |
608 | struct strbuf str = STRBUF_INIT; | |
609 | FILE *fp; | |
610 | int res = 0; | |
611 | ||
612 | if (stat(filename, &st) || !S_ISREG(st.st_mode)) | |
613 | return 0; | |
614 | ||
615 | fp = fopen(filename, "r"); | |
616 | if (!fp) | |
617 | return 0; | |
618 | ||
619 | if (strbuf_getline(&str, fp, '\n') != EOF) | |
620 | res = !strcmp(str.buf, sha1_to_hex(sha1)); | |
621 | ||
622 | strbuf_release(&str); | |
623 | fclose(fp); | |
624 | ||
625 | return res; | |
626 | } | |
627 | ||
ef24c7ca CC |
628 | static void mark_expected_rev(char *bisect_rev_hex) |
629 | { | |
630 | int len = strlen(bisect_rev_hex); | |
631 | const char *filename = git_path("BISECT_EXPECTED_REV"); | |
632 | int fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600); | |
633 | ||
634 | if (fd < 0) | |
635 | die("could not create file '%s': %s", | |
636 | filename, strerror(errno)); | |
637 | ||
638 | bisect_rev_hex[len] = '\n'; | |
639 | write_or_die(fd, bisect_rev_hex, len + 1); | |
640 | bisect_rev_hex[len] = '\0'; | |
641 | ||
642 | if (close(fd) < 0) | |
643 | die("closing file %s: %s", filename, strerror(errno)); | |
644 | } | |
645 | ||
646 | static int bisect_checkout(char *bisect_rev_hex) | |
647 | { | |
648 | int res; | |
649 | ||
650 | mark_expected_rev(bisect_rev_hex); | |
651 | ||
652 | argv_checkout[2] = bisect_rev_hex; | |
653 | res = run_command_v_opt(argv_checkout, RUN_GIT_CMD); | |
654 | if (res) | |
655 | exit(res); | |
656 | ||
657 | argv_show_branch[1] = bisect_rev_hex; | |
658 | return run_command_v_opt(argv_show_branch, RUN_GIT_CMD); | |
659 | } | |
660 | ||
c0537662 CC |
661 | static struct commit *get_commit_reference(const unsigned char *sha1) |
662 | { | |
663 | struct commit *r = lookup_commit_reference(sha1); | |
664 | if (!r) | |
665 | die("Not a valid commit name %s", sha1_to_hex(sha1)); | |
666 | return r; | |
667 | } | |
668 | ||
669 | static struct commit **get_bad_and_good_commits(int *rev_nr) | |
670 | { | |
671 | int len = 1 + good_revs.sha1_nr; | |
672 | struct commit **rev = xmalloc(len * sizeof(*rev)); | |
673 | int i, n = 0; | |
674 | ||
675 | rev[n++] = get_commit_reference(current_bad_sha1); | |
676 | for (i = 0; i < good_revs.sha1_nr; i++) | |
677 | rev[n++] = get_commit_reference(good_revs.sha1[i]); | |
678 | *rev_nr = n; | |
679 | ||
680 | return rev; | |
681 | } | |
682 | ||
683 | static void handle_bad_merge_base(void) | |
684 | { | |
685 | if (is_expected_rev(current_bad_sha1)) { | |
686 | char *bad_hex = sha1_to_hex(current_bad_sha1); | |
687 | char *good_hex = join_sha1_array_hex(&good_revs, ' '); | |
688 | ||
689 | fprintf(stderr, "The merge base %s is bad.\n" | |
690 | "This means the bug has been fixed " | |
691 | "between %s and [%s].\n", | |
692 | bad_hex, bad_hex, good_hex); | |
693 | ||
694 | exit(3); | |
695 | } | |
696 | ||
697 | fprintf(stderr, "Some good revs are not ancestor of the bad rev.\n" | |
698 | "git bisect cannot work properly in this case.\n" | |
699 | "Maybe you mistake good and bad revs?\n"); | |
700 | exit(1); | |
701 | } | |
702 | ||
703 | void handle_skipped_merge_base(const unsigned char *mb) | |
704 | { | |
705 | char *mb_hex = sha1_to_hex(mb); | |
706 | char *bad_hex = sha1_to_hex(current_bad_sha1); | |
707 | char *good_hex = join_sha1_array_hex(&good_revs, ' '); | |
708 | ||
709 | fprintf(stderr, "Warning: the merge base between %s and [%s] " | |
710 | "must be skipped.\n" | |
711 | "So we cannot be sure the first bad commit is " | |
712 | "between %s and %s.\n" | |
713 | "We continue anyway.\n", | |
714 | bad_hex, good_hex, mb_hex, bad_hex); | |
715 | free(good_hex); | |
716 | } | |
717 | ||
718 | /* | |
719 | * "check_merge_bases" checks that merge bases are not "bad". | |
720 | * | |
721 | * - If one is "bad", it means the user assumed something wrong | |
722 | * and we must exit with a non 0 error code. | |
723 | * - If one is "good", that's good, we have nothing to do. | |
724 | * - If one is "skipped", we can't know but we should warn. | |
725 | * - If we don't know, we should check it out and ask the user to test. | |
726 | */ | |
727 | static void check_merge_bases(void) | |
728 | { | |
729 | struct commit_list *result; | |
730 | int rev_nr; | |
731 | struct commit **rev = get_bad_and_good_commits(&rev_nr); | |
732 | ||
733 | result = get_merge_bases_many(rev[0], rev_nr - 1, rev + 1, 0); | |
734 | ||
735 | for (; result; result = result->next) { | |
736 | const unsigned char *mb = result->item->object.sha1; | |
737 | if (!hashcmp(mb, current_bad_sha1)) { | |
738 | handle_bad_merge_base(); | |
739 | } else if (0 <= lookup_sha1_array(&good_revs, mb)) { | |
740 | continue; | |
741 | } else if (0 <= lookup_sha1_array(&skipped_revs, mb)) { | |
742 | handle_skipped_merge_base(mb); | |
743 | } else { | |
744 | printf("Bisecting: a merge base must be tested\n"); | |
745 | exit(bisect_checkout(sha1_to_hex(mb))); | |
746 | } | |
747 | } | |
748 | ||
749 | free(rev); | |
750 | free_commit_list(result); | |
751 | } | |
752 | ||
d937d4ac CC |
753 | /* |
754 | * This function runs the command "git rev-list $_good ^$_bad" | |
755 | * and returns 1 if it produces some output, 0 otherwise. | |
756 | */ | |
757 | static int check_ancestors(void) | |
758 | { | |
759 | struct argv_array rev_argv = { NULL, 0, 0 }; | |
760 | struct strbuf str = STRBUF_INIT; | |
761 | int i, result = 0; | |
762 | struct child_process rls; | |
763 | FILE *rls_fout; | |
764 | ||
765 | argv_array_push(&rev_argv, xstrdup("rev-list")); | |
766 | argv_array_push_sha1(&rev_argv, current_bad_sha1, "^%s"); | |
767 | for (i = 0; i < good_revs.sha1_nr; i++) | |
768 | argv_array_push_sha1(&rev_argv, good_revs.sha1[i], "%s"); | |
769 | argv_array_push(&rev_argv, NULL); | |
770 | ||
771 | memset(&rls, 0, sizeof(rls)); | |
772 | rls.argv = rev_argv.argv; | |
773 | rls.out = -1; | |
774 | rls.git_cmd = 1; | |
775 | if (start_command(&rls)) | |
776 | die("Could not launch 'git rev-list' command."); | |
777 | rls_fout = fdopen(rls.out, "r"); | |
778 | while (strbuf_getline(&str, rls_fout, '\n') != EOF) { | |
779 | strbuf_trim(&str); | |
780 | if (*str.buf) { | |
781 | result = 1; | |
782 | break; | |
783 | } | |
784 | } | |
785 | fclose(rls_fout); | |
786 | finish_command(&rls); | |
787 | ||
788 | return result; | |
789 | } | |
790 | ||
791 | /* | |
792 | * "check_good_are_ancestors_of_bad" checks that all "good" revs are | |
793 | * ancestor of the "bad" rev. | |
794 | * | |
795 | * If that's not the case, we need to check the merge bases. | |
796 | * If a merge base must be tested by the user, its source code will be | |
797 | * checked out to be tested by the user and we will exit. | |
798 | */ | |
799 | static void check_good_are_ancestors_of_bad(const char *prefix) | |
800 | { | |
801 | const char *filename = git_path("BISECT_ANCESTORS_OK"); | |
802 | struct stat st; | |
803 | int fd; | |
804 | ||
805 | if (!current_bad_sha1) | |
806 | die("a bad revision is needed"); | |
807 | ||
808 | /* Check if file BISECT_ANCESTORS_OK exists. */ | |
809 | if (!stat(filename, &st) && S_ISREG(st.st_mode)) | |
810 | return; | |
811 | ||
812 | /* Bisecting with no good rev is ok. */ | |
813 | if (good_revs.sha1_nr == 0) | |
814 | return; | |
815 | ||
816 | if (check_ancestors()) | |
817 | check_merge_bases(); | |
818 | ||
819 | /* Create file BISECT_ANCESTORS_OK. */ | |
820 | fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600); | |
821 | if (fd < 0) | |
822 | warning("could not create file '%s': %s", | |
823 | filename, strerror(errno)); | |
824 | else | |
825 | close(fd); | |
826 | } | |
827 | ||
ef24c7ca CC |
828 | /* |
829 | * We use the convention that exiting with an exit code 10 means that | |
830 | * the bisection process finished successfully. | |
831 | * In this case the calling shell script should exit 0. | |
832 | */ | |
0871984d | 833 | int bisect_next_all(const char *prefix) |
ef24c7ca CC |
834 | { |
835 | struct rev_info revs; | |
836 | struct commit_list *tried; | |
837 | int reaches = 0, all = 0, nr; | |
838 | const unsigned char *bisect_rev; | |
839 | char bisect_rev_hex[41]; | |
840 | ||
2b020695 CC |
841 | if (read_bisect_refs()) |
842 | die("reading bisect refs failed"); | |
843 | ||
0871984d CC |
844 | check_good_are_ancestors_of_bad(prefix); |
845 | ||
2b020695 CC |
846 | bisect_rev_setup(&revs, prefix); |
847 | ||
848 | bisect_common(&revs, &reaches, &all); | |
ef24c7ca CC |
849 | |
850 | revs.commits = filter_skipped(revs.commits, &tried, 0); | |
851 | ||
852 | if (!revs.commits) { | |
853 | /* | |
854 | * We should exit here only if the "bad" | |
855 | * commit is also a "skip" commit. | |
856 | */ | |
857 | exit_if_skipped_commits(tried, NULL); | |
858 | ||
859 | printf("%s was both good and bad\n", | |
860 | sha1_to_hex(current_bad_sha1)); | |
861 | exit(1); | |
862 | } | |
863 | ||
864 | bisect_rev = revs.commits->item->object.sha1; | |
865 | memcpy(bisect_rev_hex, sha1_to_hex(bisect_rev), 41); | |
866 | ||
867 | if (!hashcmp(bisect_rev, current_bad_sha1)) { | |
868 | exit_if_skipped_commits(tried, current_bad_sha1); | |
869 | printf("%s is first bad commit\n", bisect_rev_hex); | |
870 | argv_diff_tree[2] = bisect_rev_hex; | |
871 | run_command_v_opt(argv_diff_tree, RUN_GIT_CMD); | |
872 | /* This means the bisection process succeeded. */ | |
873 | exit(10); | |
874 | } | |
875 | ||
876 | nr = all - reaches - 1; | |
877 | printf("Bisecting: %d revisions left to test after this " | |
878 | "(roughly %d steps)\n", nr, estimate_bisect_steps(all)); | |
879 | ||
880 | return bisect_checkout(bisect_rev_hex); | |
881 | } | |
882 |