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