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