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