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