bisect: add parameters to "filter_skipped"
[git/dscho.git] / bisect.c
blob115cf5fa41d91c047ee2081bf0d22cb6ae638970
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"
13 struct sha1_array {
14 unsigned char (*sha1)[20];
15 int sha1_nr;
16 int sha1_alloc;
17 int sorted;
20 static struct sha1_array good_revs;
21 static struct sha1_array skipped_revs;
23 static const unsigned char *current_bad_sha1;
25 struct argv_array {
26 const char **argv;
27 int argv_nr;
28 int argv_alloc;
31 static const char *argv_checkout[] = {"checkout", "-q", NULL, "--", NULL};
32 static const char *argv_show_branch[] = {"show-branch", NULL, NULL};
34 /* bits #0-15 in revision.h */
36 #define COUNTED (1u<<16)
39 * This is a truly stupid algorithm, but it's only
40 * used for bisection, and we just don't care enough.
42 * We care just barely enough to avoid recursing for
43 * non-merge entries.
45 static int count_distance(struct commit_list *entry)
47 int nr = 0;
49 while (entry) {
50 struct commit *commit = entry->item;
51 struct commit_list *p;
53 if (commit->object.flags & (UNINTERESTING | COUNTED))
54 break;
55 if (!(commit->object.flags & TREESAME))
56 nr++;
57 commit->object.flags |= COUNTED;
58 p = commit->parents;
59 entry = p;
60 if (p) {
61 p = p->next;
62 while (p) {
63 nr += count_distance(p);
64 p = p->next;
69 return nr;
72 static void clear_distance(struct commit_list *list)
74 while (list) {
75 struct commit *commit = list->item;
76 commit->object.flags &= ~COUNTED;
77 list = list->next;
81 #define DEBUG_BISECT 0
83 static inline int weight(struct commit_list *elem)
85 return *((int*)(elem->item->util));
88 static inline void weight_set(struct commit_list *elem, int weight)
90 *((int*)(elem->item->util)) = weight;
93 static int count_interesting_parents(struct commit *commit)
95 struct commit_list *p;
96 int count;
98 for (count = 0, p = commit->parents; p; p = p->next) {
99 if (p->item->object.flags & UNINTERESTING)
100 continue;
101 count++;
103 return count;
106 static inline int halfway(struct commit_list *p, int nr)
109 * Don't short-cut something we are not going to return!
111 if (p->item->object.flags & TREESAME)
112 return 0;
113 if (DEBUG_BISECT)
114 return 0;
116 * 2 and 3 are halfway of 5.
117 * 3 is halfway of 6 but 2 and 4 are not.
119 switch (2 * weight(p) - nr) {
120 case -1: case 0: case 1:
121 return 1;
122 default:
123 return 0;
127 #if !DEBUG_BISECT
128 #define show_list(a,b,c,d) do { ; } while (0)
129 #else
130 static void show_list(const char *debug, int counted, int nr,
131 struct commit_list *list)
133 struct commit_list *p;
135 fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
137 for (p = list; p; p = p->next) {
138 struct commit_list *pp;
139 struct commit *commit = p->item;
140 unsigned flags = commit->object.flags;
141 enum object_type type;
142 unsigned long size;
143 char *buf = read_sha1_file(commit->object.sha1, &type, &size);
144 char *ep, *sp;
146 fprintf(stderr, "%c%c%c ",
147 (flags & TREESAME) ? ' ' : 'T',
148 (flags & UNINTERESTING) ? 'U' : ' ',
149 (flags & COUNTED) ? 'C' : ' ');
150 if (commit->util)
151 fprintf(stderr, "%3d", weight(p));
152 else
153 fprintf(stderr, "---");
154 fprintf(stderr, " %.*s", 8, sha1_to_hex(commit->object.sha1));
155 for (pp = commit->parents; pp; pp = pp->next)
156 fprintf(stderr, " %.*s", 8,
157 sha1_to_hex(pp->item->object.sha1));
159 sp = strstr(buf, "\n\n");
160 if (sp) {
161 sp += 2;
162 for (ep = sp; *ep && *ep != '\n'; ep++)
164 fprintf(stderr, " %.*s", (int)(ep - sp), sp);
166 fprintf(stderr, "\n");
169 #endif /* DEBUG_BISECT */
171 static struct commit_list *best_bisection(struct commit_list *list, int nr)
173 struct commit_list *p, *best;
174 int best_distance = -1;
176 best = list;
177 for (p = list; p; p = p->next) {
178 int distance;
179 unsigned flags = p->item->object.flags;
181 if (flags & TREESAME)
182 continue;
183 distance = weight(p);
184 if (nr - distance < distance)
185 distance = nr - distance;
186 if (distance > best_distance) {
187 best = p;
188 best_distance = distance;
192 return best;
195 struct commit_dist {
196 struct commit *commit;
197 int distance;
200 static int compare_commit_dist(const void *a_, const void *b_)
202 struct commit_dist *a, *b;
204 a = (struct commit_dist *)a_;
205 b = (struct commit_dist *)b_;
206 if (a->distance != b->distance)
207 return b->distance - a->distance; /* desc sort */
208 return hashcmp(a->commit->object.sha1, b->commit->object.sha1);
211 static struct commit_list *best_bisection_sorted(struct commit_list *list, int nr)
213 struct commit_list *p;
214 struct commit_dist *array = xcalloc(nr, sizeof(*array));
215 int cnt, i;
217 for (p = list, cnt = 0; p; p = p->next) {
218 int distance;
219 unsigned flags = p->item->object.flags;
221 if (flags & TREESAME)
222 continue;
223 distance = weight(p);
224 if (nr - distance < distance)
225 distance = nr - distance;
226 array[cnt].commit = p->item;
227 array[cnt].distance = distance;
228 cnt++;
230 qsort(array, cnt, sizeof(*array), compare_commit_dist);
231 for (p = list, i = 0; i < cnt; i++) {
232 struct name_decoration *r = xmalloc(sizeof(*r) + 100);
233 struct object *obj = &(array[i].commit->object);
235 sprintf(r->name, "dist=%d", array[i].distance);
236 r->next = add_decoration(&name_decoration, obj, r);
237 p->item = array[i].commit;
238 p = p->next;
240 if (p)
241 p->next = NULL;
242 free(array);
243 return list;
247 * zero or positive weight is the number of interesting commits it can
248 * reach, including itself. Especially, weight = 0 means it does not
249 * reach any tree-changing commits (e.g. just above uninteresting one
250 * but traversal is with pathspec).
252 * weight = -1 means it has one parent and its distance is yet to
253 * be computed.
255 * weight = -2 means it has more than one parent and its distance is
256 * unknown. After running count_distance() first, they will get zero
257 * or positive distance.
259 static struct commit_list *do_find_bisection(struct commit_list *list,
260 int nr, int *weights,
261 int find_all)
263 int n, counted;
264 struct commit_list *p;
266 counted = 0;
268 for (n = 0, p = list; p; p = p->next) {
269 struct commit *commit = p->item;
270 unsigned flags = commit->object.flags;
272 p->item->util = &weights[n++];
273 switch (count_interesting_parents(commit)) {
274 case 0:
275 if (!(flags & TREESAME)) {
276 weight_set(p, 1);
277 counted++;
278 show_list("bisection 2 count one",
279 counted, nr, list);
282 * otherwise, it is known not to reach any
283 * tree-changing commit and gets weight 0.
285 break;
286 case 1:
287 weight_set(p, -1);
288 break;
289 default:
290 weight_set(p, -2);
291 break;
295 show_list("bisection 2 initialize", counted, nr, list);
298 * If you have only one parent in the resulting set
299 * then you can reach one commit more than that parent
300 * can reach. So we do not have to run the expensive
301 * count_distance() for single strand of pearls.
303 * However, if you have more than one parents, you cannot
304 * just add their distance and one for yourself, since
305 * they usually reach the same ancestor and you would
306 * end up counting them twice that way.
308 * So we will first count distance of merges the usual
309 * way, and then fill the blanks using cheaper algorithm.
311 for (p = list; p; p = p->next) {
312 if (p->item->object.flags & UNINTERESTING)
313 continue;
314 if (weight(p) != -2)
315 continue;
316 weight_set(p, count_distance(p));
317 clear_distance(list);
319 /* Does it happen to be at exactly half-way? */
320 if (!find_all && halfway(p, nr))
321 return p;
322 counted++;
325 show_list("bisection 2 count_distance", counted, nr, list);
327 while (counted < nr) {
328 for (p = list; p; p = p->next) {
329 struct commit_list *q;
330 unsigned flags = p->item->object.flags;
332 if (0 <= weight(p))
333 continue;
334 for (q = p->item->parents; q; q = q->next) {
335 if (q->item->object.flags & UNINTERESTING)
336 continue;
337 if (0 <= weight(q))
338 break;
340 if (!q)
341 continue;
344 * weight for p is unknown but q is known.
345 * add one for p itself if p is to be counted,
346 * otherwise inherit it from q directly.
348 if (!(flags & TREESAME)) {
349 weight_set(p, weight(q)+1);
350 counted++;
351 show_list("bisection 2 count one",
352 counted, nr, list);
354 else
355 weight_set(p, weight(q));
357 /* Does it happen to be at exactly half-way? */
358 if (!find_all && halfway(p, nr))
359 return p;
363 show_list("bisection 2 counted all", counted, nr, list);
365 if (!find_all)
366 return best_bisection(list, nr);
367 else
368 return best_bisection_sorted(list, nr);
371 struct commit_list *find_bisection(struct commit_list *list,
372 int *reaches, int *all,
373 int find_all)
375 int nr, on_list;
376 struct commit_list *p, *best, *next, *last;
377 int *weights;
379 show_list("bisection 2 entry", 0, 0, list);
382 * Count the number of total and tree-changing items on the
383 * list, while reversing the list.
385 for (nr = on_list = 0, last = NULL, p = list;
387 p = next) {
388 unsigned flags = p->item->object.flags;
390 next = p->next;
391 if (flags & UNINTERESTING)
392 continue;
393 p->next = last;
394 last = p;
395 if (!(flags & TREESAME))
396 nr++;
397 on_list++;
399 list = last;
400 show_list("bisection 2 sorted", 0, nr, list);
402 *all = nr;
403 weights = xcalloc(on_list, sizeof(*weights));
405 /* Do the real work of finding bisection commit. */
406 best = do_find_bisection(list, nr, weights, find_all);
407 if (best) {
408 if (!find_all)
409 best->next = NULL;
410 *reaches = weight(best);
412 free(weights);
413 return best;
416 static void argv_array_push(struct argv_array *array, const char *string)
418 ALLOC_GROW(array->argv, array->argv_nr + 1, array->argv_alloc);
419 array->argv[array->argv_nr++] = string;
422 static void argv_array_push_sha1(struct argv_array *array,
423 const unsigned char *sha1,
424 const char *format)
426 struct strbuf buf = STRBUF_INIT;
427 strbuf_addf(&buf, format, sha1_to_hex(sha1));
428 argv_array_push(array, strbuf_detach(&buf, NULL));
431 static void sha1_array_push(struct sha1_array *array,
432 const unsigned char *sha1)
434 ALLOC_GROW(array->sha1, array->sha1_nr + 1, array->sha1_alloc);
435 hashcpy(array->sha1[array->sha1_nr++], sha1);
438 static int register_ref(const char *refname, const unsigned char *sha1,
439 int flags, void *cb_data)
441 if (!strcmp(refname, "bad")) {
442 current_bad_sha1 = sha1;
443 } else if (!prefixcmp(refname, "good-")) {
444 sha1_array_push(&good_revs, sha1);
445 } else if (!prefixcmp(refname, "skip-")) {
446 sha1_array_push(&skipped_revs, sha1);
449 return 0;
452 static int read_bisect_refs(void)
454 return for_each_ref_in("refs/bisect/", register_ref, NULL);
457 void read_bisect_paths(struct argv_array *array)
459 struct strbuf str = STRBUF_INIT;
460 const char *filename = git_path("BISECT_NAMES");
461 FILE *fp = fopen(filename, "r");
463 if (!fp)
464 die("Could not open file '%s': %s", filename, strerror(errno));
466 while (strbuf_getline(&str, fp, '\n') != EOF) {
467 char *quoted;
468 int res;
470 strbuf_trim(&str);
471 quoted = strbuf_detach(&str, NULL);
472 res = sq_dequote_to_argv(quoted, &array->argv,
473 &array->argv_nr, &array->argv_alloc);
474 if (res)
475 die("Badly quoted content in file '%s': %s",
476 filename, quoted);
479 strbuf_release(&str);
480 fclose(fp);
483 static int array_cmp(const void *a, const void *b)
485 return hashcmp(a, b);
488 static void sort_sha1_array(struct sha1_array *array)
490 qsort(array->sha1, array->sha1_nr, sizeof(*array->sha1), array_cmp);
492 array->sorted = 1;
495 static const unsigned char *sha1_access(size_t index, void *table)
497 unsigned char (*array)[20] = table;
498 return array[index];
501 static int lookup_sha1_array(struct sha1_array *array,
502 const unsigned char *sha1)
504 if (!array->sorted)
505 sort_sha1_array(array);
507 return sha1_pos(sha1, array->sha1, array->sha1_nr, sha1_access);
510 static char *join_sha1_array_hex(struct sha1_array *array, char delim)
512 struct strbuf joined_hexs = STRBUF_INIT;
513 int i;
515 for (i = 0; i < array->sha1_nr; i++) {
516 strbuf_addstr(&joined_hexs, sha1_to_hex(array->sha1[i]));
517 if (i + 1 < array->sha1_nr)
518 strbuf_addch(&joined_hexs, delim);
521 return strbuf_detach(&joined_hexs, NULL);
525 * In this function, passing a not NULL skipped_first is very special.
526 * It means that we want to know if the first commit in the list is
527 * skipped because we will want to test a commit away from it if it is
528 * indeed skipped.
529 * So if the first commit is skipped, we cannot take the shortcut to
530 * just "return list" when we find the first non skipped commit, we
531 * have to return a fully filtered list.
533 * We use (*skipped_first == -1) to mean "it has been found that the
534 * first commit is not skipped". In this case *skipped_first is set back
535 * to 0 just before the function returns.
537 struct commit_list *filter_skipped(struct commit_list *list,
538 struct commit_list **tried,
539 int show_all,
540 int *count,
541 int *skipped_first)
543 struct commit_list *filtered = NULL, **f = &filtered;
545 *tried = NULL;
547 if (skipped_first)
548 *skipped_first = 0;
549 if (count)
550 *count = 0;
552 if (!skipped_revs.sha1_nr)
553 return list;
555 while (list) {
556 struct commit_list *next = list->next;
557 list->next = NULL;
558 if (0 <= lookup_sha1_array(&skipped_revs,
559 list->item->object.sha1)) {
560 if (skipped_first && !*skipped_first)
561 *skipped_first = 1;
562 /* Move current to tried list */
563 *tried = list;
564 tried = &list->next;
565 } else {
566 if (!show_all) {
567 if (!skipped_first || !*skipped_first)
568 return list;
569 } else if (skipped_first && !*skipped_first) {
570 /* This means we know it's not skipped */
571 *skipped_first = -1;
573 /* Move current to filtered list */
574 *f = list;
575 f = &list->next;
576 if (count)
577 (*count)++;
579 list = next;
582 if (skipped_first && *skipped_first == -1)
583 *skipped_first = 0;
585 return filtered;
588 static void bisect_rev_setup(struct rev_info *revs, const char *prefix,
589 const char *bad_format, const char *good_format,
590 int read_paths)
592 struct argv_array rev_argv = { NULL, 0, 0 };
593 int i;
595 init_revisions(revs, prefix);
596 revs->abbrev = 0;
597 revs->commit_format = CMIT_FMT_UNSPECIFIED;
599 /* rev_argv.argv[0] will be ignored by setup_revisions */
600 argv_array_push(&rev_argv, xstrdup("bisect_rev_setup"));
601 argv_array_push_sha1(&rev_argv, current_bad_sha1, bad_format);
602 for (i = 0; i < good_revs.sha1_nr; i++)
603 argv_array_push_sha1(&rev_argv, good_revs.sha1[i],
604 good_format);
605 argv_array_push(&rev_argv, xstrdup("--"));
606 if (read_paths)
607 read_bisect_paths(&rev_argv);
608 argv_array_push(&rev_argv, NULL);
610 setup_revisions(rev_argv.argv_nr, rev_argv.argv, revs, NULL);
613 static void bisect_common(struct rev_info *revs)
615 if (prepare_revision_walk(revs))
616 die("revision walk setup failed");
617 if (revs->tree_objects)
618 mark_edges_uninteresting(revs->commits, revs, NULL);
621 static void exit_if_skipped_commits(struct commit_list *tried,
622 const unsigned char *bad)
624 if (!tried)
625 return;
627 printf("There are only 'skip'ped commits left to test.\n"
628 "The first bad commit could be any of:\n");
629 print_commit_list(tried, "%s\n", "%s\n");
630 if (bad)
631 printf("%s\n", sha1_to_hex(bad));
632 printf("We cannot bisect more!\n");
633 exit(2);
636 static int is_expected_rev(const unsigned char *sha1)
638 const char *filename = git_path("BISECT_EXPECTED_REV");
639 struct stat st;
640 struct strbuf str = STRBUF_INIT;
641 FILE *fp;
642 int res = 0;
644 if (stat(filename, &st) || !S_ISREG(st.st_mode))
645 return 0;
647 fp = fopen(filename, "r");
648 if (!fp)
649 return 0;
651 if (strbuf_getline(&str, fp, '\n') != EOF)
652 res = !strcmp(str.buf, sha1_to_hex(sha1));
654 strbuf_release(&str);
655 fclose(fp);
657 return res;
660 static void mark_expected_rev(char *bisect_rev_hex)
662 int len = strlen(bisect_rev_hex);
663 const char *filename = git_path("BISECT_EXPECTED_REV");
664 int fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600);
666 if (fd < 0)
667 die("could not create file '%s': %s",
668 filename, strerror(errno));
670 bisect_rev_hex[len] = '\n';
671 write_or_die(fd, bisect_rev_hex, len + 1);
672 bisect_rev_hex[len] = '\0';
674 if (close(fd) < 0)
675 die("closing file %s: %s", filename, strerror(errno));
678 static int bisect_checkout(char *bisect_rev_hex)
680 int res;
682 mark_expected_rev(bisect_rev_hex);
684 argv_checkout[2] = bisect_rev_hex;
685 res = run_command_v_opt(argv_checkout, RUN_GIT_CMD);
686 if (res)
687 exit(res);
689 argv_show_branch[1] = bisect_rev_hex;
690 return run_command_v_opt(argv_show_branch, RUN_GIT_CMD);
693 static struct commit *get_commit_reference(const unsigned char *sha1)
695 struct commit *r = lookup_commit_reference(sha1);
696 if (!r)
697 die("Not a valid commit name %s", sha1_to_hex(sha1));
698 return r;
701 static struct commit **get_bad_and_good_commits(int *rev_nr)
703 int len = 1 + good_revs.sha1_nr;
704 struct commit **rev = xmalloc(len * sizeof(*rev));
705 int i, n = 0;
707 rev[n++] = get_commit_reference(current_bad_sha1);
708 for (i = 0; i < good_revs.sha1_nr; i++)
709 rev[n++] = get_commit_reference(good_revs.sha1[i]);
710 *rev_nr = n;
712 return rev;
715 static void handle_bad_merge_base(void)
717 if (is_expected_rev(current_bad_sha1)) {
718 char *bad_hex = sha1_to_hex(current_bad_sha1);
719 char *good_hex = join_sha1_array_hex(&good_revs, ' ');
721 fprintf(stderr, "The merge base %s is bad.\n"
722 "This means the bug has been fixed "
723 "between %s and [%s].\n",
724 bad_hex, bad_hex, good_hex);
726 exit(3);
729 fprintf(stderr, "Some good revs are not ancestor of the bad rev.\n"
730 "git bisect cannot work properly in this case.\n"
731 "Maybe you mistake good and bad revs?\n");
732 exit(1);
735 void handle_skipped_merge_base(const unsigned char *mb)
737 char *mb_hex = sha1_to_hex(mb);
738 char *bad_hex = sha1_to_hex(current_bad_sha1);
739 char *good_hex = join_sha1_array_hex(&good_revs, ' ');
741 fprintf(stderr, "Warning: the merge base between %s and [%s] "
742 "must be skipped.\n"
743 "So we cannot be sure the first bad commit is "
744 "between %s and %s.\n"
745 "We continue anyway.\n",
746 bad_hex, good_hex, mb_hex, bad_hex);
747 free(good_hex);
751 * "check_merge_bases" checks that merge bases are not "bad".
753 * - If one is "bad", it means the user assumed something wrong
754 * and we must exit with a non 0 error code.
755 * - If one is "good", that's good, we have nothing to do.
756 * - If one is "skipped", we can't know but we should warn.
757 * - If we don't know, we should check it out and ask the user to test.
759 static void check_merge_bases(void)
761 struct commit_list *result;
762 int rev_nr;
763 struct commit **rev = get_bad_and_good_commits(&rev_nr);
765 result = get_merge_bases_many(rev[0], rev_nr - 1, rev + 1, 0);
767 for (; result; result = result->next) {
768 const unsigned char *mb = result->item->object.sha1;
769 if (!hashcmp(mb, current_bad_sha1)) {
770 handle_bad_merge_base();
771 } else if (0 <= lookup_sha1_array(&good_revs, mb)) {
772 continue;
773 } else if (0 <= lookup_sha1_array(&skipped_revs, mb)) {
774 handle_skipped_merge_base(mb);
775 } else {
776 printf("Bisecting: a merge base must be tested\n");
777 exit(bisect_checkout(sha1_to_hex(mb)));
781 free(rev);
782 free_commit_list(result);
785 static int check_ancestors(const char *prefix)
787 struct rev_info revs;
788 struct object_array pending_copy;
789 int i, res;
791 bisect_rev_setup(&revs, prefix, "^%s", "%s", 0);
793 /* Save pending objects, so they can be cleaned up later. */
794 memset(&pending_copy, 0, sizeof(pending_copy));
795 for (i = 0; i < revs.pending.nr; i++)
796 add_object_array(revs.pending.objects[i].item,
797 revs.pending.objects[i].name,
798 &pending_copy);
800 bisect_common(&revs);
801 res = (revs.commits != NULL);
803 /* Clean up objects used, as they will be reused. */
804 for (i = 0; i < pending_copy.nr; i++) {
805 struct object *o = pending_copy.objects[i].item;
806 clear_commit_marks((struct commit *)o, ALL_REV_FLAGS);
809 return res;
813 * "check_good_are_ancestors_of_bad" checks that all "good" revs are
814 * ancestor of the "bad" rev.
816 * If that's not the case, we need to check the merge bases.
817 * If a merge base must be tested by the user, its source code will be
818 * checked out to be tested by the user and we will exit.
820 static void check_good_are_ancestors_of_bad(const char *prefix)
822 const char *filename = git_path("BISECT_ANCESTORS_OK");
823 struct stat st;
824 int fd;
826 if (!current_bad_sha1)
827 die("a bad revision is needed");
829 /* Check if file BISECT_ANCESTORS_OK exists. */
830 if (!stat(filename, &st) && S_ISREG(st.st_mode))
831 return;
833 /* Bisecting with no good rev is ok. */
834 if (good_revs.sha1_nr == 0)
835 return;
837 /* Check if all good revs are ancestor of the bad rev. */
838 if (check_ancestors(prefix))
839 check_merge_bases();
841 /* Create file BISECT_ANCESTORS_OK. */
842 fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600);
843 if (fd < 0)
844 warning("could not create file '%s': %s",
845 filename, strerror(errno));
846 else
847 close(fd);
851 * This does "git diff-tree --pretty COMMIT" without one fork+exec.
853 static void show_diff_tree(const char *prefix, struct commit *commit)
855 struct rev_info opt;
857 /* diff-tree init */
858 init_revisions(&opt, prefix);
859 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
860 opt.abbrev = 0;
861 opt.diff = 1;
863 /* This is what "--pretty" does */
864 opt.verbose_header = 1;
865 opt.use_terminator = 0;
866 opt.commit_format = CMIT_FMT_DEFAULT;
868 /* diff-tree init */
869 if (!opt.diffopt.output_format)
870 opt.diffopt.output_format = DIFF_FORMAT_RAW;
872 log_tree_commit(&opt, commit);
876 * We use the convention that exiting with an exit code 10 means that
877 * the bisection process finished successfully.
878 * In this case the calling shell script should exit 0.
880 int bisect_next_all(const char *prefix)
882 struct rev_info revs;
883 struct commit_list *tried;
884 int reaches = 0, all = 0, nr;
885 const unsigned char *bisect_rev;
886 char bisect_rev_hex[41];
888 if (read_bisect_refs())
889 die("reading bisect refs failed");
891 check_good_are_ancestors_of_bad(prefix);
893 bisect_rev_setup(&revs, prefix, "%s", "^%s", 1);
894 revs.limited = 1;
896 bisect_common(&revs);
898 revs.commits = find_bisection(revs.commits, &reaches, &all,
899 !!skipped_revs.sha1_nr);
900 revs.commits = filter_skipped(revs.commits, &tried, 0, NULL, NULL);
902 if (!revs.commits) {
904 * We should exit here only if the "bad"
905 * commit is also a "skip" commit.
907 exit_if_skipped_commits(tried, NULL);
909 printf("%s was both good and bad\n",
910 sha1_to_hex(current_bad_sha1));
911 exit(1);
914 bisect_rev = revs.commits->item->object.sha1;
915 memcpy(bisect_rev_hex, sha1_to_hex(bisect_rev), 41);
917 if (!hashcmp(bisect_rev, current_bad_sha1)) {
918 exit_if_skipped_commits(tried, current_bad_sha1);
919 printf("%s is first bad commit\n", bisect_rev_hex);
920 show_diff_tree(prefix, revs.commits->item);
921 /* This means the bisection process succeeded. */
922 exit(10);
925 nr = all - reaches - 1;
926 printf("Bisecting: %d revisions left to test after this "
927 "(roughly %d steps)\n", nr, estimate_bisect_steps(all));
929 return bisect_checkout(bisect_rev_hex);