]> git.ipfire.org Git - thirdparty/git.git/blame - bisect.c
cmd_bisect__helper: defer parsing no-checkout flag
[thirdparty/git.git] / bisect.c
CommitLineData
a2ad79ce 1#include "cache.h"
b2141fc1 2#include "config.h"
a2ad79ce
CC
3#include "commit.h"
4#include "diff.h"
5#include "revision.h"
1bf072e3
CC
6#include "refs.h"
7#include "list-objects.h"
3b437b0d 8#include "quote.h"
4eb5b646 9#include "sha1-lookup.h"
ef24c7ca 10#include "run-command.h"
e22278c0 11#include "log-tree.h"
a2ad79ce 12#include "bisect.h"
fe299ec5 13#include "oid-array.h"
8a534b61 14#include "argv-array.h"
bb408ac9 15#include "commit-slab.h"
64043556 16#include "commit-reach.h"
b0eb92bb 17#include "object-store.h"
6212b1aa 18
910650d2 19static struct oid_array good_revs;
20static struct oid_array skipped_revs;
1bf072e3 21
3c5ff995 22static struct object_id *current_bad_oid;
ef24c7ca 23
ef24c7ca
CC
24static const char *argv_checkout[] = {"checkout", "-q", NULL, "--", NULL};
25static const char *argv_show_branch[] = {"show-branch", NULL, NULL};
26
43f9d9f3
AD
27static const char *term_bad;
28static const char *term_good;
29
208acbfb 30/* Remember to update object flag allocation in object.h */
a2ad79ce
CC
31#define COUNTED (1u<<16)
32
33/*
34 * This is a truly stupid algorithm, but it's only
35 * used for bisection, and we just don't care enough.
36 *
37 * We care just barely enough to avoid recursing for
38 * non-merge entries.
39 */
40static int count_distance(struct commit_list *entry)
41{
42 int nr = 0;
43
44 while (entry) {
45 struct commit *commit = entry->item;
46 struct commit_list *p;
47
48 if (commit->object.flags & (UNINTERESTING | COUNTED))
49 break;
50 if (!(commit->object.flags & TREESAME))
51 nr++;
52 commit->object.flags |= COUNTED;
53 p = commit->parents;
54 entry = p;
55 if (p) {
56 p = p->next;
57 while (p) {
58 nr += count_distance(p);
59 p = p->next;
60 }
61 }
62 }
63
64 return nr;
65}
66
67static void clear_distance(struct commit_list *list)
68{
69 while (list) {
70 struct commit *commit = list->item;
71 commit->object.flags &= ~COUNTED;
72 list = list->next;
73 }
74}
75
bb408ac9
NTND
76define_commit_slab(commit_weight, int *);
77static struct commit_weight commit_weight;
78
a2ad79ce
CC
79#define DEBUG_BISECT 0
80
81static inline int weight(struct commit_list *elem)
82{
bb408ac9 83 return **commit_weight_at(&commit_weight, elem->item);
a2ad79ce
CC
84}
85
86static inline void weight_set(struct commit_list *elem, int weight)
87{
bb408ac9 88 **commit_weight_at(&commit_weight, elem->item) = weight;
a2ad79ce
CC
89}
90
0fe305a5 91static int count_interesting_parents(struct commit *commit, int first_parent_only)
a2ad79ce
CC
92{
93 struct commit_list *p;
94 int count;
95
96 for (count = 0, p = commit->parents; p; p = p->next) {
0fe305a5
AL
97 if (!(p->item->object.flags & UNINTERESTING))
98 count++;
99 if (first_parent_only)
100 break;
a2ad79ce
CC
101 }
102 return count;
103}
104
105static inline int halfway(struct commit_list *p, int nr)
106{
107 /*
108 * Don't short-cut something we are not going to return!
109 */
110 if (p->item->object.flags & TREESAME)
111 return 0;
112 if (DEBUG_BISECT)
113 return 0;
114 /*
115 * 2 and 3 are halfway of 5.
116 * 3 is halfway of 6 but 2 and 4 are not.
117 */
118 switch (2 * weight(p) - nr) {
119 case -1: case 0: case 1:
120 return 1;
121 default:
122 return 0;
123 }
124}
125
a2ad79ce
CC
126static void show_list(const char *debug, int counted, int nr,
127 struct commit_list *list)
128{
129 struct commit_list *p;
130
b0eb92bb
NTND
131 if (!DEBUG_BISECT)
132 return;
133
a2ad79ce
CC
134 fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
135
136 for (p = list; p; p = p->next) {
137 struct commit_list *pp;
138 struct commit *commit = p->item;
139 unsigned flags = commit->object.flags;
140 enum object_type type;
141 unsigned long size;
b4f5aca4 142 char *buf = read_object_file(&commit->object.oid, &type,
143 &size);
56ff3794
CC
144 const char *subject_start;
145 int subject_len;
a2ad79ce
CC
146
147 fprintf(stderr, "%c%c%c ",
148 (flags & TREESAME) ? ' ' : 'T',
149 (flags & UNINTERESTING) ? 'U' : ' ',
150 (flags & COUNTED) ? 'C' : ' ');
b0eb92bb 151 if (*commit_weight_at(&commit_weight, p->item))
a2ad79ce
CC
152 fprintf(stderr, "%3d", weight(p));
153 else
154 fprintf(stderr, "---");
14ced556 155 fprintf(stderr, " %.*s", 8, oid_to_hex(&commit->object.oid));
a2ad79ce
CC
156 for (pp = commit->parents; pp; pp = pp->next)
157 fprintf(stderr, " %.*s", 8,
14ced556 158 oid_to_hex(&pp->item->object.oid));
a2ad79ce 159
56ff3794
CC
160 subject_len = find_commit_subject(buf, &subject_start);
161 if (subject_len)
162 fprintf(stderr, " %.*s", subject_len, subject_start);
a2ad79ce
CC
163 fprintf(stderr, "\n");
164 }
165}
a2ad79ce
CC
166
167static struct commit_list *best_bisection(struct commit_list *list, int nr)
168{
169 struct commit_list *p, *best;
170 int best_distance = -1;
171
172 best = list;
173 for (p = list; p; p = p->next) {
174 int distance;
175 unsigned flags = p->item->object.flags;
176
177 if (flags & TREESAME)
178 continue;
179 distance = weight(p);
180 if (nr - distance < distance)
181 distance = nr - distance;
182 if (distance > best_distance) {
183 best = p;
184 best_distance = distance;
185 }
186 }
187
188 return best;
189}
190
191struct commit_dist {
192 struct commit *commit;
193 int distance;
194};
195
196static int compare_commit_dist(const void *a_, const void *b_)
197{
198 struct commit_dist *a, *b;
199
200 a = (struct commit_dist *)a_;
201 b = (struct commit_dist *)b_;
202 if (a->distance != b->distance)
203 return b->distance - a->distance; /* desc sort */
f2fd0760 204 return oidcmp(&a->commit->object.oid, &b->commit->object.oid);
a2ad79ce
CC
205}
206
207static struct commit_list *best_bisection_sorted(struct commit_list *list, int nr)
208{
209 struct commit_list *p;
210 struct commit_dist *array = xcalloc(nr, sizeof(*array));
5b1ef2ce 211 struct strbuf buf = STRBUF_INIT;
a2ad79ce
CC
212 int cnt, i;
213
214 for (p = list, cnt = 0; p; p = p->next) {
215 int distance;
216 unsigned flags = p->item->object.flags;
217
218 if (flags & TREESAME)
219 continue;
220 distance = weight(p);
221 if (nr - distance < distance)
222 distance = nr - distance;
223 array[cnt].commit = p->item;
224 array[cnt].distance = distance;
225 cnt++;
226 }
9ed0d8d6 227 QSORT(array, cnt, compare_commit_dist);
a2ad79ce 228 for (p = list, i = 0; i < cnt; i++) {
a2ad79ce
CC
229 struct object *obj = &(array[i].commit->object);
230
5b1ef2ce
JK
231 strbuf_reset(&buf);
232 strbuf_addf(&buf, "dist=%d", array[i].distance);
233 add_name_decoration(DECORATION_NONE, buf.buf, obj);
662174d2 234
a2ad79ce 235 p->item = array[i].commit;
7c117184
236 if (i < cnt - 1)
237 p = p->next;
a2ad79ce 238 }
2e9fdc79
ÆAB
239 if (p) {
240 free_commit_list(p->next);
241 p->next = NULL;
242 }
5b1ef2ce 243 strbuf_release(&buf);
a2ad79ce
CC
244 free(array);
245 return list;
246}
247
248/*
249 * zero or positive weight is the number of interesting commits it can
250 * reach, including itself. Especially, weight = 0 means it does not
251 * reach any tree-changing commits (e.g. just above uninteresting one
252 * but traversal is with pathspec).
253 *
254 * weight = -1 means it has one parent and its distance is yet to
255 * be computed.
256 *
257 * weight = -2 means it has more than one parent and its distance is
258 * unknown. After running count_distance() first, they will get zero
259 * or positive distance.
260 */
261static struct commit_list *do_find_bisection(struct commit_list *list,
262 int nr, int *weights,
0fe305a5 263 int find_all, int first_parent_only)
a2ad79ce
CC
264{
265 int n, counted;
266 struct commit_list *p;
267
268 counted = 0;
269
270 for (n = 0, p = list; p; p = p->next) {
271 struct commit *commit = p->item;
272 unsigned flags = commit->object.flags;
273
bb408ac9 274 *commit_weight_at(&commit_weight, p->item) = &weights[n++];
0fe305a5 275 switch (count_interesting_parents(commit, first_parent_only)) {
a2ad79ce
CC
276 case 0:
277 if (!(flags & TREESAME)) {
278 weight_set(p, 1);
279 counted++;
280 show_list("bisection 2 count one",
281 counted, nr, list);
282 }
283 /*
284 * otherwise, it is known not to reach any
285 * tree-changing commit and gets weight 0.
286 */
287 break;
288 case 1:
289 weight_set(p, -1);
290 break;
291 default:
292 weight_set(p, -2);
293 break;
294 }
295 }
296
297 show_list("bisection 2 initialize", counted, nr, list);
298
299 /*
300 * If you have only one parent in the resulting set
301 * then you can reach one commit more than that parent
302 * can reach. So we do not have to run the expensive
303 * count_distance() for single strand of pearls.
304 *
305 * However, if you have more than one parents, you cannot
306 * just add their distance and one for yourself, since
307 * they usually reach the same ancestor and you would
308 * end up counting them twice that way.
309 *
310 * So we will first count distance of merges the usual
311 * way, and then fill the blanks using cheaper algorithm.
312 */
313 for (p = list; p; p = p->next) {
314 if (p->item->object.flags & UNINTERESTING)
315 continue;
316 if (weight(p) != -2)
317 continue;
0fe305a5
AL
318 if (first_parent_only)
319 BUG("shouldn't be calling count-distance in fp mode");
a2ad79ce
CC
320 weight_set(p, count_distance(p));
321 clear_distance(list);
322
323 /* Does it happen to be at exactly half-way? */
324 if (!find_all && halfway(p, nr))
325 return p;
326 counted++;
327 }
328
329 show_list("bisection 2 count_distance", counted, nr, list);
330
331 while (counted < nr) {
332 for (p = list; p; p = p->next) {
333 struct commit_list *q;
334 unsigned flags = p->item->object.flags;
335
336 if (0 <= weight(p))
337 continue;
0fe305a5
AL
338
339 for (q = p->item->parents;
340 q;
341 q = first_parent_only ? NULL : q->next) {
a2ad79ce
CC
342 if (q->item->object.flags & UNINTERESTING)
343 continue;
344 if (0 <= weight(q))
345 break;
346 }
347 if (!q)
348 continue;
349
350 /*
351 * weight for p is unknown but q is known.
352 * add one for p itself if p is to be counted,
353 * otherwise inherit it from q directly.
354 */
355 if (!(flags & TREESAME)) {
356 weight_set(p, weight(q)+1);
357 counted++;
358 show_list("bisection 2 count one",
359 counted, nr, list);
360 }
361 else
362 weight_set(p, weight(q));
363
364 /* Does it happen to be at exactly half-way? */
365 if (!find_all && halfway(p, nr))
366 return p;
367 }
368 }
369
370 show_list("bisection 2 counted all", counted, nr, list);
371
372 if (!find_all)
373 return best_bisection(list, nr);
374 else
375 return best_bisection_sorted(list, nr);
376}
377
24d707f6 378void find_bisection(struct commit_list **commit_list, int *reaches,
0fe305a5 379 int *all, int find_all, int first_parent_only)
a2ad79ce
CC
380{
381 int nr, on_list;
24d707f6 382 struct commit_list *list, *p, *best, *next, *last;
a2ad79ce
CC
383 int *weights;
384
24d707f6 385 show_list("bisection 2 entry", 0, 0, *commit_list);
bb408ac9 386 init_commit_weight(&commit_weight);
a2ad79ce
CC
387
388 /*
389 * Count the number of total and tree-changing items on the
390 * list, while reversing the list.
391 */
24d707f6 392 for (nr = on_list = 0, last = NULL, p = *commit_list;
a2ad79ce
CC
393 p;
394 p = next) {
395 unsigned flags = p->item->object.flags;
396
397 next = p->next;
fc5c40bb
398 if (flags & UNINTERESTING) {
399 free(p);
a2ad79ce 400 continue;
fc5c40bb 401 }
a2ad79ce
CC
402 p->next = last;
403 last = p;
404 if (!(flags & TREESAME))
405 nr++;
406 on_list++;
407 }
408 list = last;
409 show_list("bisection 2 sorted", 0, nr, list);
410
411 *all = nr;
412 weights = xcalloc(on_list, sizeof(*weights));
413
414 /* Do the real work of finding bisection commit. */
0fe305a5 415 best = do_find_bisection(list, nr, weights, find_all, first_parent_only);
a2ad79ce 416 if (best) {
f4e45cb3
417 if (!find_all) {
418 list->item = best->item;
419 free_commit_list(list->next);
420 best = list;
a2ad79ce 421 best->next = NULL;
f4e45cb3 422 }
a2ad79ce
CC
423 *reaches = weight(best);
424 }
425 free(weights);
24d707f6 426 *commit_list = best;
bb408ac9 427 clear_commit_weight(&commit_weight);
a2ad79ce
CC
428}
429
eed25148 430static int register_ref(const char *refname, const struct object_id *oid,
1bf072e3
CC
431 int flags, void *cb_data)
432{
43f9d9f3
AD
433 struct strbuf good_prefix = STRBUF_INIT;
434 strbuf_addstr(&good_prefix, term_good);
435 strbuf_addstr(&good_prefix, "-");
436
437 if (!strcmp(refname, term_bad)) {
3c5ff995 438 current_bad_oid = xmalloc(sizeof(*current_bad_oid));
eed25148 439 oidcpy(current_bad_oid, oid);
43f9d9f3 440 } else if (starts_with(refname, good_prefix.buf)) {
910650d2 441 oid_array_append(&good_revs, oid);
59556548 442 } else if (starts_with(refname, "skip-")) {
910650d2 443 oid_array_append(&skipped_revs, oid);
1bf072e3
CC
444 }
445
43f9d9f3
AD
446 strbuf_release(&good_prefix);
447
1bf072e3
CC
448 return 0;
449}
450
451static int read_bisect_refs(void)
452{
eed25148 453 return for_each_ref_in("refs/bisect/", register_ref, NULL);
1bf072e3
CC
454}
455
f932729c
JK
456static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES")
457static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
fb71a329
PB
458static GIT_PATH_FUNC(git_path_bisect_ancestors_ok, "BISECT_ANCESTORS_OK")
459static GIT_PATH_FUNC(git_path_bisect_run, "BISECT_RUN")
460static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START")
461static GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG")
f5d284f6 462static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
fb71a329 463static GIT_PATH_FUNC(git_path_head_name, "head-name")
f932729c 464
2af202be 465static void read_bisect_paths(struct argv_array *array)
3b437b0d
CC
466{
467 struct strbuf str = STRBUF_INIT;
f932729c 468 const char *filename = git_path_bisect_names();
23a9e071 469 FILE *fp = xfopen(filename, "r");
3b437b0d 470
8f309aeb 471 while (strbuf_getline_lf(&str, fp) != EOF) {
3b437b0d 472 strbuf_trim(&str);
8a534b61 473 if (sq_dequote_to_argv_array(str.buf, array))
14dc4899 474 die(_("Badly quoted content in file '%s': %s"),
8a534b61 475 filename, str.buf);
3b437b0d
CC
476 }
477
478 strbuf_release(&str);
479 fclose(fp);
480}
481
7383b25d 482static char *join_oid_array_hex(struct oid_array *array, char delim)
c0537662
CC
483{
484 struct strbuf joined_hexs = STRBUF_INIT;
485 int i;
486
902bb364 487 for (i = 0; i < array->nr; i++) {
ee3051bd 488 strbuf_addstr(&joined_hexs, oid_to_hex(array->oid + i));
902bb364 489 if (i + 1 < array->nr)
c0537662
CC
490 strbuf_addch(&joined_hexs, delim);
491 }
492
493 return strbuf_detach(&joined_hexs, NULL);
494}
495
9af3589e
CC
496/*
497 * In this function, passing a not NULL skipped_first is very special.
498 * It means that we want to know if the first commit in the list is
499 * skipped because we will want to test a commit away from it if it is
500 * indeed skipped.
501 * So if the first commit is skipped, we cannot take the shortcut to
502 * just "return list" when we find the first non skipped commit, we
503 * have to return a fully filtered list.
504 *
505 * We use (*skipped_first == -1) to mean "it has been found that the
506 * first commit is not skipped". In this case *skipped_first is set back
507 * to 0 just before the function returns.
508 */
95188648
CC
509struct commit_list *filter_skipped(struct commit_list *list,
510 struct commit_list **tried,
9af3589e
CC
511 int show_all,
512 int *count,
513 int *skipped_first)
95188648
CC
514{
515 struct commit_list *filtered = NULL, **f = &filtered;
516
517 *tried = NULL;
518
9af3589e
CC
519 if (skipped_first)
520 *skipped_first = 0;
521 if (count)
522 *count = 0;
523
902bb364 524 if (!skipped_revs.nr)
95188648
CC
525 return list;
526
95188648
CC
527 while (list) {
528 struct commit_list *next = list->next;
529 list->next = NULL;
910650d2 530 if (0 <= oid_array_lookup(&skipped_revs, &list->item->object.oid)) {
9af3589e
CC
531 if (skipped_first && !*skipped_first)
532 *skipped_first = 1;
95188648
CC
533 /* Move current to tried list */
534 *tried = list;
535 tried = &list->next;
536 } else {
9af3589e
CC
537 if (!show_all) {
538 if (!skipped_first || !*skipped_first)
539 return list;
540 } else if (skipped_first && !*skipped_first) {
541 /* This means we know it's not skipped */
542 *skipped_first = -1;
543 }
95188648
CC
544 /* Move current to filtered list */
545 *f = list;
546 f = &list->next;
9af3589e
CC
547 if (count)
548 (*count)++;
95188648
CC
549 }
550 list = next;
551 }
552
9af3589e
CC
553 if (skipped_first && *skipped_first == -1)
554 *skipped_first = 0;
555
95188648
CC
556 return filtered;
557}
1bf072e3 558
ebc9529f
CC
559#define PRN_MODULO 32768
560
561/*
562 * This is a pseudo random number generator based on "man 3 rand".
563 * It is not used properly because the seed is the argument and it
564 * is increased by one between each call, but that should not matter
565 * for this application.
566 */
3b335762
NTND
567static unsigned get_prn(unsigned count)
568{
ebc9529f 569 count = count * 1103515245 + 12345;
7b96d888 570 return (count/65536) % PRN_MODULO;
ebc9529f
CC
571}
572
573/*
574 * Custom integer square root from
5e68729f 575 * https://en.wikipedia.org/wiki/Integer_square_root
ebc9529f
CC
576 */
577static int sqrti(int val)
578{
579 float d, x = val;
580
b8e3b2f3 581 if (!val)
ebc9529f
CC
582 return 0;
583
584 do {
585 float y = (x + (float)val / x) / 2;
586 d = (y > x) ? y - x : x - y;
587 x = y;
588 } while (d >= 0.5);
589
590 return (int)x;
591}
592
593static struct commit_list *skip_away(struct commit_list *list, int count)
62d0b0da 594{
62d0b0da 595 struct commit_list *cur, *previous;
ebc9529f
CC
596 int prn, index, i;
597
598 prn = get_prn(count);
599 index = (count * prn / PRN_MODULO) * sqrti(prn) / sqrti(PRN_MODULO);
62d0b0da
CC
600
601 cur = list;
602 previous = NULL;
62d0b0da
CC
603
604 for (i = 0; cur; cur = cur->next, i++) {
605 if (i == index) {
9001dc2a 606 if (!oideq(&cur->item->object.oid, current_bad_oid))
62d0b0da
CC
607 return cur;
608 if (previous)
609 return previous;
610 return list;
611 }
612 previous = cur;
613 }
614
615 return list;
616}
617
618static struct commit_list *managed_skipped(struct commit_list *list,
619 struct commit_list **tried)
620{
621 int count, skipped_first;
62d0b0da
CC
622
623 *tried = NULL;
624
902bb364 625 if (!skipped_revs.nr)
62d0b0da
CC
626 return list;
627
628 list = filter_skipped(list, tried, 0, &count, &skipped_first);
629
630 if (!skipped_first)
631 return list;
632
ebc9529f 633 return skip_away(list, count);
62d0b0da
CC
634}
635
69d2cfe6
NTND
636static void bisect_rev_setup(struct repository *r, struct rev_info *revs,
637 const char *prefix,
a22347c6
CC
638 const char *bad_format, const char *good_format,
639 int read_paths)
1bf072e3 640{
8a534b61 641 struct argv_array rev_argv = ARGV_ARRAY_INIT;
fad2d31d
CC
642 int i;
643
69d2cfe6 644 repo_init_revisions(r, revs, prefix);
3b437b0d
CC
645 revs->abbrev = 0;
646 revs->commit_format = CMIT_FMT_UNSPECIFIED;
1bf072e3 647
1c953a1f 648 /* rev_argv.argv[0] will be ignored by setup_revisions */
8a534b61 649 argv_array_push(&rev_argv, "bisect_rev_setup");
3c5ff995 650 argv_array_pushf(&rev_argv, bad_format, oid_to_hex(current_bad_oid));
902bb364 651 for (i = 0; i < good_revs.nr; i++)
8a534b61 652 argv_array_pushf(&rev_argv, good_format,
ee3051bd 653 oid_to_hex(good_revs.oid + i));
8a534b61 654 argv_array_push(&rev_argv, "--");
a22347c6
CC
655 if (read_paths)
656 read_bisect_paths(&rev_argv);
3b437b0d 657
8a534b61
JK
658 setup_revisions(rev_argv.argc, rev_argv.argv, revs, NULL);
659 /* XXX leak rev_argv, as "revs" may still be pointing to it */
3b437b0d
CC
660}
661
a22347c6 662static void bisect_common(struct rev_info *revs)
2ace9727 663{
2ace9727
CC
664 if (prepare_revision_walk(revs))
665 die("revision walk setup failed");
666 if (revs->tree_objects)
4f6d26b1 667 mark_edges_uninteresting(revs, NULL, 0);
2ace9727
CC
668}
669
ce58b5d8 670static enum bisect_error error_if_skipped_commits(struct commit_list *tried,
3c5ff995 671 const struct object_id *bad)
ef24c7ca
CC
672{
673 if (!tried)
ce58b5d8 674 return BISECT_OK;
ef24c7ca
CC
675
676 printf("There are only 'skip'ped commits left to test.\n"
43f9d9f3 677 "The first %s commit could be any of:\n", term_bad);
54307ea7
JH
678
679 for ( ; tried; tried = tried->next)
680 printf("%s\n", oid_to_hex(&tried->item->object.oid));
681
ef24c7ca 682 if (bad)
3c5ff995 683 printf("%s\n", oid_to_hex(bad));
14dc4899 684 printf(_("We cannot bisect more!\n"));
ce58b5d8
PB
685
686 return BISECT_ONLY_SKIPPED_LEFT;
ef24c7ca
CC
687}
688
3c5ff995 689static int is_expected_rev(const struct object_id *oid)
c0537662 690{
f932729c 691 const char *filename = git_path_bisect_expected_rev();
c0537662
CC
692 struct stat st;
693 struct strbuf str = STRBUF_INIT;
694 FILE *fp;
695 int res = 0;
696
697 if (stat(filename, &st) || !S_ISREG(st.st_mode))
698 return 0;
699
e9d983f1 700 fp = fopen_or_warn(filename, "r");
c0537662
CC
701 if (!fp)
702 return 0;
703
8f309aeb 704 if (strbuf_getline_lf(&str, fp) != EOF)
3c5ff995 705 res = !strcmp(str.buf, oid_to_hex(oid));
c0537662
CC
706
707 strbuf_release(&str);
708 fclose(fp);
709
710 return res;
711}
712
e8e3ce67 713static enum bisect_error bisect_checkout(const struct object_id *bisect_rev, int no_checkout)
ef24c7ca 714{
dc01505f 715 char bisect_rev_hex[GIT_MAX_HEXSZ + 1];
e8e3ce67 716 enum bisect_error res = BISECT_OK;
ef24c7ca 717
95518faa 718 memcpy(bisect_rev_hex, oid_to_hex(bisect_rev), the_hash_algo->hexsz + 1);
ae077771 719 update_ref(NULL, "BISECT_EXPECTED_REV", bisect_rev, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
ef24c7ca
CC
720
721 argv_checkout[2] = bisect_rev_hex;
fee92fc1 722 if (no_checkout) {
ae077771 723 update_ref(NULL, "BISECT_HEAD", bisect_rev, NULL, 0,
724 UPDATE_REFS_DIE_ON_ERR);
fee92fc1
JS
725 } else {
726 res = run_command_v_opt(argv_checkout, RUN_GIT_CMD);
727 if (res)
e8e3ce67
PB
728 /*
729 * Errors in `run_command()` itself, signaled by res < 0,
730 * and errors in the child process, signaled by res > 0
731 * can both be treated as regular BISECT_FAILURE (-1).
732 */
733 return -abs(res);
fee92fc1 734 }
ef24c7ca
CC
735
736 argv_show_branch[1] = bisect_rev_hex;
e8e3ce67
PB
737 res = run_command_v_opt(argv_show_branch, RUN_GIT_CMD);
738 /*
739 * Errors in `run_command()` itself, signaled by res < 0,
740 * and errors in the child process, signaled by res > 0
741 * can both be treated as regular BISECT_FAILURE (-1).
742 */
743 return -abs(res);
ef24c7ca
CC
744}
745
69d2cfe6
NTND
746static struct commit *get_commit_reference(struct repository *r,
747 const struct object_id *oid)
c0537662 748{
69d2cfe6
NTND
749 struct commit *c = lookup_commit_reference(r, oid);
750 if (!c)
ee3051bd 751 die(_("Not a valid commit name %s"), oid_to_hex(oid));
69d2cfe6 752 return c;
c0537662
CC
753}
754
69d2cfe6
NTND
755static struct commit **get_bad_and_good_commits(struct repository *r,
756 int *rev_nr)
c0537662 757{
b32fa95f 758 struct commit **rev;
c0537662
CC
759 int i, n = 0;
760
b32fa95f 761 ALLOC_ARRAY(rev, 1 + good_revs.nr);
69d2cfe6 762 rev[n++] = get_commit_reference(r, current_bad_oid);
902bb364 763 for (i = 0; i < good_revs.nr; i++)
69d2cfe6 764 rev[n++] = get_commit_reference(r, good_revs.oid + i);
c0537662
CC
765 *rev_nr = n;
766
767 return rev;
768}
769
9ec598e0 770static enum bisect_error handle_bad_merge_base(void)
c0537662 771{
3c5ff995 772 if (is_expected_rev(current_bad_oid)) {
773 char *bad_hex = oid_to_hex(current_bad_oid);
7383b25d 774 char *good_hex = join_oid_array_hex(&good_revs, ' ');
43f9d9f3 775 if (!strcmp(term_bad, "bad") && !strcmp(term_good, "good")) {
14dc4899 776 fprintf(stderr, _("The merge base %s is bad.\n"
43f9d9f3 777 "This means the bug has been fixed "
14dc4899 778 "between %s and [%s].\n"),
43f9d9f3 779 bad_hex, bad_hex, good_hex);
21e5cfd8 780 } else if (!strcmp(term_bad, "new") && !strcmp(term_good, "old")) {
14dc4899 781 fprintf(stderr, _("The merge base %s is new.\n"
21e5cfd8 782 "The property has changed "
14dc4899 783 "between %s and [%s].\n"),
21e5cfd8 784 bad_hex, bad_hex, good_hex);
43f9d9f3 785 } else {
14dc4899 786 fprintf(stderr, _("The merge base %s is %s.\n"
43f9d9f3 787 "This means the first '%s' commit is "
14dc4899 788 "between %s and [%s].\n"),
43f9d9f3
AD
789 bad_hex, term_bad, term_good, bad_hex, good_hex);
790 }
9ec598e0 791 return BISECT_MERGE_BASE_CHECK;
c0537662
CC
792 }
793
3f407b76 794 fprintf(stderr, _("Some %s revs are not ancestors of the %s rev.\n"
c0537662 795 "git bisect cannot work properly in this case.\n"
14dc4899 796 "Maybe you mistook %s and %s revs?\n"),
43f9d9f3 797 term_good, term_bad, term_good, term_bad);
9ec598e0 798 return BISECT_FAILED;
c0537662
CC
799}
800
4ce3621a 801static void handle_skipped_merge_base(const struct object_id *mb)
c0537662 802{
4ce3621a 803 char *mb_hex = oid_to_hex(mb);
c368dde9 804 char *bad_hex = oid_to_hex(current_bad_oid);
7383b25d 805 char *good_hex = join_oid_array_hex(&good_revs, ' ');
c0537662 806
14dc4899 807 warning(_("the merge base between %s and [%s] "
c0537662 808 "must be skipped.\n"
43f9d9f3 809 "So we cannot be sure the first %s commit is "
c0537662 810 "between %s and %s.\n"
14dc4899 811 "We continue anyway."),
43f9d9f3 812 bad_hex, good_hex, term_bad, mb_hex, bad_hex);
c0537662
CC
813 free(good_hex);
814}
815
816/*
21e5cfd8 817 * "check_merge_bases" checks that merge bases are not "bad" (or "new").
c0537662 818 *
21e5cfd8 819 * - If one is "bad" (or "new"), it means the user assumed something wrong
cdd4dc2d 820 * and we must return error with a non 0 error code.
21e5cfd8 821 * - If one is "good" (or "old"), that's good, we have nothing to do.
c0537662
CC
822 * - If one is "skipped", we can't know but we should warn.
823 * - If we don't know, we should check it out and ask the user to test.
cdd4dc2d
PB
824 * - If a merge base must be tested, on success return
825 * BISECT_INTERNAL_SUCCESS_MERGE_BASE (-11) a special condition
826 * for early success, this will be converted back to 0 in
827 * check_good_are_ancestors_of_bad().
c0537662 828 */
cdd4dc2d 829static enum bisect_error check_merge_bases(int rev_nr, struct commit **rev, int no_checkout)
c0537662 830{
cdd4dc2d 831 enum bisect_error res = BISECT_OK;
c0537662 832 struct commit_list *result;
c0537662 833
2ce406cc 834 result = get_merge_bases_many(rev[0], rev_nr - 1, rev + 1);
c0537662
CC
835
836 for (; result; result = result->next) {
4ce3621a 837 const struct object_id *mb = &result->item->object.oid;
4a7e27e9 838 if (oideq(mb, current_bad_oid)) {
9ec598e0
PB
839 res = handle_bad_merge_base();
840 break;
910650d2 841 } else if (0 <= oid_array_lookup(&good_revs, mb)) {
c0537662 842 continue;
910650d2 843 } else if (0 <= oid_array_lookup(&skipped_revs, mb)) {
c0537662
CC
844 handle_skipped_merge_base(mb);
845 } else {
14dc4899 846 printf(_("Bisecting: a merge base must be tested\n"));
cdd4dc2d
PB
847 res = bisect_checkout(mb, no_checkout);
848 if (!res)
849 /* indicate early success */
850 res = BISECT_INTERNAL_SUCCESS_MERGE_BASE;
851 break;
c0537662
CC
852 }
853 }
854
c0537662 855 free_commit_list(result);
cdd4dc2d 856 return res;
c0537662
CC
857}
858
69d2cfe6
NTND
859static int check_ancestors(struct repository *r, int rev_nr,
860 struct commit **rev, const char *prefix)
d937d4ac 861{
2d938fc7 862 struct rev_info revs;
86a0a408 863 int res;
d937d4ac 864
69d2cfe6 865 bisect_rev_setup(r, &revs, prefix, "^%s", "%s", 0);
d937d4ac 866
2d938fc7
CC
867 bisect_common(&revs);
868 res = (revs.commits != NULL);
869
870 /* Clean up objects used, as they will be reused. */
148f14ab 871 clear_commit_marks_many(rev_nr, rev, ALL_REV_FLAGS);
d937d4ac 872
2d938fc7 873 return res;
d937d4ac
CC
874}
875
876/*
877 * "check_good_are_ancestors_of_bad" checks that all "good" revs are
878 * ancestor of the "bad" rev.
879 *
880 * If that's not the case, we need to check the merge bases.
881 * If a merge base must be tested by the user, its source code will be
45b63708 882 * checked out to be tested by the user and we will return.
d937d4ac 883 */
45b63708
PB
884
885static enum bisect_error check_good_are_ancestors_of_bad(struct repository *r,
69d2cfe6
NTND
886 const char *prefix,
887 int no_checkout)
d937d4ac 888{
45b63708 889 char *filename;
d937d4ac 890 struct stat st;
148f14ab 891 int fd, rev_nr;
cdd4dc2d 892 enum bisect_error res = BISECT_OK;
148f14ab 893 struct commit **rev;
d937d4ac 894
3c5ff995 895 if (!current_bad_oid)
45b63708
PB
896 return error(_("a %s revision is needed"), term_bad);
897
898 filename = git_pathdup("BISECT_ANCESTORS_OK");
d937d4ac
CC
899
900 /* Check if file BISECT_ANCESTORS_OK exists. */
901 if (!stat(filename, &st) && S_ISREG(st.st_mode))
144e7090 902 goto done;
d937d4ac
CC
903
904 /* Bisecting with no good rev is ok. */
b8e3b2f3 905 if (!good_revs.nr)
144e7090 906 goto done;
d937d4ac 907
2d938fc7 908 /* Check if all good revs are ancestor of the bad rev. */
cdd4dc2d 909
69d2cfe6
NTND
910 rev = get_bad_and_good_commits(r, &rev_nr);
911 if (check_ancestors(r, rev_nr, rev, prefix))
cdd4dc2d 912 res = check_merge_bases(rev_nr, rev, no_checkout);
148f14ab 913 free(rev);
d937d4ac 914
45b63708
PB
915 if (!res) {
916 /* Create file BISECT_ANCESTORS_OK. */
917 fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600);
918 if (fd < 0)
919 /*
920 * BISECT_ANCESTORS_OK file is not absolutely necessary,
921 * the bisection process will continue at the next
922 * bisection step.
923 * So, just signal with a warning that something
924 * might be wrong.
925 */
926 warning_errno(_("could not create file '%s'"),
927 filename);
928 else
929 close(fd);
930 }
144e7090
MH
931 done:
932 free(filename);
45b63708 933 return res;
d937d4ac
CC
934}
935
e22278c0
CC
936/*
937 * This does "git diff-tree --pretty COMMIT" without one fork+exec.
938 */
69d2cfe6
NTND
939static void show_diff_tree(struct repository *r,
940 const char *prefix,
941 struct commit *commit)
e22278c0 942{
2008f290 943 const char *argv[] = {
b02be8b9 944 "diff-tree", "--pretty", "--stat", "--summary", "--cc", NULL
2008f290 945 };
e22278c0
CC
946 struct rev_info opt;
947
b02be8b9 948 git_config(git_diff_ui_config, NULL);
40ae3d3e 949 repo_init_revisions(r, &opt, prefix);
e22278c0 950
2008f290 951 setup_revisions(ARRAY_SIZE(argv) - 1, argv, &opt, NULL);
e22278c0
CC
952 log_tree_commit(&opt, commit);
953}
954
cb46d630
AD
955/*
956 * The terms used for this bisect session are stored in BISECT_TERMS.
957 * We read them and store them to adapt the messages accordingly.
958 * Default is bad/good.
959 */
960void read_bisect_terms(const char **read_bad, const char **read_good)
961{
962 struct strbuf str = STRBUF_INIT;
f5d284f6 963 const char *filename = git_path_bisect_terms();
cb46d630
AD
964 FILE *fp = fopen(filename, "r");
965
966 if (!fp) {
967 if (errno == ENOENT) {
968 *read_bad = "bad";
969 *read_good = "good";
970 return;
971 } else {
14dc4899 972 die_errno(_("could not read file '%s'"), filename);
cb46d630
AD
973 }
974 } else {
8f309aeb 975 strbuf_getline_lf(&str, fp);
cb46d630 976 *read_bad = strbuf_detach(&str, NULL);
8f309aeb 977 strbuf_getline_lf(&str, fp);
cb46d630
AD
978 *read_good = strbuf_detach(&str, NULL);
979 }
980 strbuf_release(&str);
981 fclose(fp);
982}
983
ef24c7ca 984/*
6c69f222 985 * We use the convention that return BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND (-10) means
ef24c7ca 986 * the bisection process finished successfully.
6c69f222
PB
987 * In this case the calling function or command should not turn a
988 * BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND return code into an error or a non zero exit code.
fee92fc1
JS
989 * If no_checkout is non-zero, the bisection process does not
990 * checkout the trial commit but instead simply updates BISECT_HEAD.
ef24c7ca 991 */
be5fe200 992enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
ef24c7ca
CC
993{
994 struct rev_info revs;
995 struct commit_list *tried;
6329bade 996 int reaches = 0, all = 0, nr, steps;
ce58b5d8 997 enum bisect_error res = BISECT_OK;
4be0deec 998 struct object_id *bisect_rev;
2cfa8357 999 char *steps_msg;
be5fe200 1000 int no_checkout = ref_exists("BISECT_HEAD");
0fe305a5 1001 int first_parent_only = 0; /* TODO: pass --first-parent flag from git bisect start */
ef24c7ca 1002
cb46d630 1003 read_bisect_terms(&term_bad, &term_good);
2b020695 1004 if (read_bisect_refs())
14dc4899 1005 die(_("reading bisect refs failed"));
2b020695 1006
45b63708
PB
1007 res = check_good_are_ancestors_of_bad(r, prefix, no_checkout);
1008 if (res)
1009 return res;
0871984d 1010
69d2cfe6 1011 bisect_rev_setup(r, &revs, prefix, "%s", "^%s", 1);
0fe305a5 1012 revs.first_parent_only = first_parent_only;
a22347c6 1013 revs.limited = 1;
2b020695 1014
a22347c6 1015 bisect_common(&revs);
ef24c7ca 1016
0fe305a5 1017 find_bisection(&revs.commits, &reaches, &all, !!skipped_revs.nr, first_parent_only);
62d0b0da 1018 revs.commits = managed_skipped(revs.commits, &tried);
ef24c7ca
CC
1019
1020 if (!revs.commits) {
1021 /*
6c69f222 1022 * We should return error here only if the "bad"
ef24c7ca
CC
1023 * commit is also a "skip" commit.
1024 */
ce58b5d8
PB
1025 res = error_if_skipped_commits(tried, NULL);
1026 if (res < 0)
6c69f222 1027 return res;
14dc4899 1028 printf(_("%s was both %s and %s\n"),
43f9d9f3
AD
1029 oid_to_hex(current_bad_oid),
1030 term_good,
1031 term_bad);
6c69f222
PB
1032
1033 return BISECT_FAILED;
ef24c7ca
CC
1034 }
1035
8f69f72f 1036 if (!all) {
14dc4899
VA
1037 fprintf(stderr, _("No testable commit found.\n"
1038 "Maybe you started with bad path parameters?\n"));
6c69f222
PB
1039
1040 return BISECT_NO_TESTABLE_COMMIT;
8f69f72f
CC
1041 }
1042
4be0deec 1043 bisect_rev = &revs.commits->item->object.oid;
ef24c7ca 1044
4a7e27e9 1045 if (oideq(bisect_rev, current_bad_oid)) {
ce58b5d8
PB
1046 res = error_if_skipped_commits(tried, current_bad_oid);
1047 if (res)
6c69f222 1048 return res;
4be0deec 1049 printf("%s is the first %s commit\n", oid_to_hex(bisect_rev),
43f9d9f3 1050 term_bad);
6c69f222 1051
69d2cfe6 1052 show_diff_tree(r, prefix, revs.commits->item);
6c69f222
PB
1053 /*
1054 * This means the bisection process succeeded.
1055 * Using BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND (-10)
1056 * so that the call chain can simply check
1057 * for negative return values for early returns up
1058 * until the cmd_bisect__helper() caller.
1059 */
1060 return BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND;
ef24c7ca
CC
1061 }
1062
1063 nr = all - reaches - 1;
6329bade 1064 steps = estimate_bisect_steps(all);
2cfa8357
MM
1065
1066 steps_msg = xstrfmt(Q_("(roughly %d step)", "(roughly %d steps)",
1067 steps), steps);
66f5f6dc
ÆAB
1068 /*
1069 * TRANSLATORS: the last %s will be replaced with "(roughly %d
1070 * steps)" translation.
1071 */
14dc4899
VA
1072 printf(Q_("Bisecting: %d revision left to test after this %s\n",
1073 "Bisecting: %d revisions left to test after this %s\n",
1074 nr), nr, steps_msg);
2cfa8357 1075 free(steps_msg);
ef24c7ca 1076
f3a97718 1077 return bisect_checkout(bisect_rev, no_checkout);
ef24c7ca
CC
1078}
1079
c43cb386
NTND
1080static inline int log2i(int n)
1081{
1082 int log2 = 0;
1083
1084 for (; n > 1; n >>= 1)
1085 log2++;
1086
1087 return log2;
1088}
1089
1090static inline int exp2i(int n)
1091{
1092 return 1 << n;
1093}
1094
1095/*
1096 * Estimate the number of bisect steps left (after the current step)
1097 *
1098 * For any x between 0 included and 2^n excluded, the probability for
1099 * n - 1 steps left looks like:
1100 *
1101 * P(2^n + x) == (2^n - x) / (2^n + x)
1102 *
1103 * and P(2^n + x) < 0.5 means 2^n < 3x
1104 */
1105int estimate_bisect_steps(int all)
1106{
1107 int n, x, e;
1108
1109 if (all < 3)
1110 return 0;
1111
1112 n = log2i(all);
1113 e = exp2i(n);
1114 x = all - e;
1115
1116 return (e < 3 * x) ? n : n - 1;
1117}
fb71a329
PB
1118
1119static int mark_for_removal(const char *refname, const struct object_id *oid,
1120 int flag, void *cb_data)
1121{
1122 struct string_list *refs = cb_data;
1123 char *ref = xstrfmt("refs/bisect%s", refname);
1124 string_list_append(refs, ref);
1125 return 0;
1126}
1127
1128int bisect_clean_state(void)
1129{
1130 int result = 0;
1131
1132 /* There may be some refs packed during bisection */
1133 struct string_list refs_for_removal = STRING_LIST_INIT_NODUP;
1134 for_each_ref_in("refs/bisect", mark_for_removal, (void *) &refs_for_removal);
1135 string_list_append(&refs_for_removal, xstrdup("BISECT_HEAD"));
a9722297 1136 result = delete_refs("bisect: remove", &refs_for_removal, REF_NO_DEREF);
fb71a329
PB
1137 refs_for_removal.strdup_strings = 1;
1138 string_list_clear(&refs_for_removal, 0);
1139 unlink_or_warn(git_path_bisect_expected_rev());
1140 unlink_or_warn(git_path_bisect_ancestors_ok());
1141 unlink_or_warn(git_path_bisect_log());
1142 unlink_or_warn(git_path_bisect_names());
1143 unlink_or_warn(git_path_bisect_run());
1144 unlink_or_warn(git_path_bisect_terms());
1145 /* Cleanup head-name if it got left by an old version of git-bisect */
1146 unlink_or_warn(git_path_head_name());
1147 /*
1148 * Cleanup BISECT_START last to support the --no-checkout option
1149 * introduced in the commit 4796e823a.
1150 */
1151 unlink_or_warn(git_path_bisect_start());
1152
1153 return result;
1154}