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