6 #include "list-objects.h"
8 #include "sha1-lookup.h"
9 #include "run-command.h"
12 #include "sha1-array.h"
14 static struct sha1_array good_revs;
15 static struct sha1_array skipped_revs;
17 static const unsigned char *current_bad_sha1;
25 static const char *argv_checkout[] = {"checkout", "-q", NULL, "--", NULL};
26 static const char *argv_show_branch[] = {"show-branch", NULL, NULL};
27 static const char *argv_update_ref[] = {"update-ref", "--no-deref", "BISECT_HEAD", NULL, NULL};
29 /* bits #0-15 in revision.h */
31 #define COUNTED (1u<<16)
34 * This is a truly stupid algorithm, but it's only
35 * used for bisection, and we just don't care enough.
37 * We care just barely enough to avoid recursing for
40 static int count_distance(struct commit_list *entry)
45 struct commit *commit = entry->item;
46 struct commit_list *p;
48 if (commit->object.flags & (UNINTERESTING | COUNTED))
50 if (!(commit->object.flags & TREESAME))
52 commit->object.flags |= COUNTED;
58 nr += count_distance(p);
67 static void clear_distance(struct commit_list *list)
70 struct commit *commit = list->item;
71 commit->object.flags &= ~COUNTED;
76 #define DEBUG_BISECT 0
78 static inline int weight(struct commit_list *elem)
80 return *((int*)(elem->item->util));
83 static inline void weight_set(struct commit_list *elem, int weight)
85 *((int*)(elem->item->util)) = weight;
88 static int count_interesting_parents(struct commit *commit)
90 struct commit_list *p;
93 for (count = 0, p = commit->parents; p; p = p->next) {
94 if (p->item->object.flags & UNINTERESTING)
101 static inline int halfway(struct commit_list *p, int nr)
104 * Don't short-cut something we are not going to return!
106 if (p->item->object.flags & TREESAME)
111 * 2 and 3 are halfway of 5.
112 * 3 is halfway of 6 but 2 and 4 are not.
114 switch (2 * weight(p) - nr) {
115 case -1: case 0: case 1:
123 #define show_list(a,b,c,d) do { ; } while (0)
125 static void show_list(const char *debug, int counted, int nr,
126 struct commit_list *list)
128 struct commit_list *p;
130 fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
132 for (p = list; p; p = p->next) {
133 struct commit_list *pp;
134 struct commit *commit = p->item;
135 unsigned flags = commit->object.flags;
136 enum object_type type;
138 char *buf = read_sha1_file(commit->object.sha1, &type, &size);
139 const char *subject_start;
142 fprintf(stderr, "%c%c%c ",
143 (flags & TREESAME) ? ' ' : 'T',
144 (flags & UNINTERESTING) ? 'U' : ' ',
145 (flags & COUNTED) ? 'C' : ' ');
147 fprintf(stderr, "%3d", weight(p));
149 fprintf(stderr, "---");
150 fprintf(stderr, " %.*s", 8, sha1_to_hex(commit->object.sha1));
151 for (pp = commit->parents; pp; pp = pp->next)
152 fprintf(stderr, " %.*s", 8,
153 sha1_to_hex(pp->item->object.sha1));
155 subject_len = find_commit_subject(buf, &subject_start);
157 fprintf(stderr, " %.*s", subject_len, subject_start);
158 fprintf(stderr, "\n");
161 #endif /* DEBUG_BISECT */
163 static struct commit_list *best_bisection(struct commit_list *list, int nr)
165 struct commit_list *p, *best;
166 int best_distance = -1;
169 for (p = list; p; p = p->next) {
171 unsigned flags = p->item->object.flags;
173 if (flags & TREESAME)
175 distance = weight(p);
176 if (nr - distance < distance)
177 distance = nr - distance;
178 if (distance > best_distance) {
180 best_distance = distance;
188 struct commit *commit;
192 static int compare_commit_dist(const void *a_, const void *b_)
194 struct commit_dist *a, *b;
196 a = (struct commit_dist *)a_;
197 b = (struct commit_dist *)b_;
198 if (a->distance != b->distance)
199 return b->distance - a->distance; /* desc sort */
200 return hashcmp(a->commit->object.sha1, b->commit->object.sha1);
203 static struct commit_list *best_bisection_sorted(struct commit_list *list, int nr)
205 struct commit_list *p;
206 struct commit_dist *array = xcalloc(nr, sizeof(*array));
209 for (p = list, cnt = 0; p; p = p->next) {
211 unsigned flags = p->item->object.flags;
213 if (flags & TREESAME)
215 distance = weight(p);
216 if (nr - distance < distance)
217 distance = nr - distance;
218 array[cnt].commit = p->item;
219 array[cnt].distance = distance;
222 qsort(array, cnt, sizeof(*array), compare_commit_dist);
223 for (p = list, i = 0; i < cnt; i++) {
224 struct name_decoration *r = xmalloc(sizeof(*r) + 100);
225 struct object *obj = &(array[i].commit->object);
227 sprintf(r->name, "dist=%d", array[i].distance);
228 r->next = add_decoration(&name_decoration, obj, r);
229 p->item = array[i].commit;
239 * zero or positive weight is the number of interesting commits it can
240 * reach, including itself. Especially, weight = 0 means it does not
241 * reach any tree-changing commits (e.g. just above uninteresting one
242 * but traversal is with pathspec).
244 * weight = -1 means it has one parent and its distance is yet to
247 * weight = -2 means it has more than one parent and its distance is
248 * unknown. After running count_distance() first, they will get zero
249 * or positive distance.
251 static struct commit_list *do_find_bisection(struct commit_list *list,
252 int nr, int *weights,
256 struct commit_list *p;
260 for (n = 0, p = list; p; p = p->next) {
261 struct commit *commit = p->item;
262 unsigned flags = commit->object.flags;
264 p->item->util = &weights[n++];
265 switch (count_interesting_parents(commit)) {
267 if (!(flags & TREESAME)) {
270 show_list("bisection 2 count one",
274 * otherwise, it is known not to reach any
275 * tree-changing commit and gets weight 0.
287 show_list("bisection 2 initialize", counted, nr, list);
290 * If you have only one parent in the resulting set
291 * then you can reach one commit more than that parent
292 * can reach. So we do not have to run the expensive
293 * count_distance() for single strand of pearls.
295 * However, if you have more than one parents, you cannot
296 * just add their distance and one for yourself, since
297 * they usually reach the same ancestor and you would
298 * end up counting them twice that way.
300 * So we will first count distance of merges the usual
301 * way, and then fill the blanks using cheaper algorithm.
303 for (p = list; p; p = p->next) {
304 if (p->item->object.flags & UNINTERESTING)
308 weight_set(p, count_distance(p));
309 clear_distance(list);
311 /* Does it happen to be at exactly half-way? */
312 if (!find_all && halfway(p, nr))
317 show_list("bisection 2 count_distance", counted, nr, list);
319 while (counted < nr) {
320 for (p = list; p; p = p->next) {
321 struct commit_list *q;
322 unsigned flags = p->item->object.flags;
326 for (q = p->item->parents; q; q = q->next) {
327 if (q->item->object.flags & UNINTERESTING)
336 * weight for p is unknown but q is known.
337 * add one for p itself if p is to be counted,
338 * otherwise inherit it from q directly.
340 if (!(flags & TREESAME)) {
341 weight_set(p, weight(q)+1);
343 show_list("bisection 2 count one",
347 weight_set(p, weight(q));
349 /* Does it happen to be at exactly half-way? */
350 if (!find_all && halfway(p, nr))
355 show_list("bisection 2 counted all", counted, nr, list);
358 return best_bisection(list, nr);
360 return best_bisection_sorted(list, nr);
363 struct commit_list *find_bisection(struct commit_list *list,
364 int *reaches, int *all,
368 struct commit_list *p, *best, *next, *last;
371 show_list("bisection 2 entry", 0, 0, list);
374 * Count the number of total and tree-changing items on the
375 * list, while reversing the list.
377 for (nr = on_list = 0, last = NULL, p = list;
380 unsigned flags = p->item->object.flags;
383 if (flags & UNINTERESTING)
387 if (!(flags & TREESAME))
392 show_list("bisection 2 sorted", 0, nr, list);
395 weights = xcalloc(on_list, sizeof(*weights));
397 /* Do the real work of finding bisection commit. */
398 best = do_find_bisection(list, nr, weights, find_all);
402 *reaches = weight(best);
408 static void argv_array_push(struct argv_array *array, const char *string)
410 ALLOC_GROW(array->argv, array->argv_nr + 1, array->argv_alloc);
411 array->argv[array->argv_nr++] = string;
414 static void argv_array_push_sha1(struct argv_array *array,
415 const unsigned char *sha1,
418 struct strbuf buf = STRBUF_INIT;
419 strbuf_addf(&buf, format, sha1_to_hex(sha1));
420 argv_array_push(array, strbuf_detach(&buf, NULL));
423 static int register_ref(const char *refname, const unsigned char *sha1,
424 int flags, void *cb_data)
426 if (!strcmp(refname, "bad")) {
427 current_bad_sha1 = sha1;
428 } else if (!prefixcmp(refname, "good-")) {
429 sha1_array_append(&good_revs, sha1);
430 } else if (!prefixcmp(refname, "skip-")) {
431 sha1_array_append(&skipped_revs, sha1);
437 static int read_bisect_refs(void)
439 return for_each_ref_in("refs/bisect/", register_ref, NULL);
442 static void read_bisect_paths(struct argv_array *array)
444 struct strbuf str = STRBUF_INIT;
445 const char *filename = git_path("BISECT_NAMES");
446 FILE *fp = fopen(filename, "r");
449 die_errno("Could not open file '%s'", filename);
451 while (strbuf_getline(&str, fp, '\n') != EOF) {
456 quoted = strbuf_detach(&str, NULL);
457 res = sq_dequote_to_argv(quoted, &array->argv,
458 &array->argv_nr, &array->argv_alloc);
460 die("Badly quoted content in file '%s': %s",
464 strbuf_release(&str);
468 static char *join_sha1_array_hex(struct sha1_array *array, char delim)
470 struct strbuf joined_hexs = STRBUF_INIT;
473 for (i = 0; i < array->nr; i++) {
474 strbuf_addstr(&joined_hexs, sha1_to_hex(array->sha1[i]));
475 if (i + 1 < array->nr)
476 strbuf_addch(&joined_hexs, delim);
479 return strbuf_detach(&joined_hexs, NULL);
483 * In this function, passing a not NULL skipped_first is very special.
484 * It means that we want to know if the first commit in the list is
485 * skipped because we will want to test a commit away from it if it is
487 * So if the first commit is skipped, we cannot take the shortcut to
488 * just "return list" when we find the first non skipped commit, we
489 * have to return a fully filtered list.
491 * We use (*skipped_first == -1) to mean "it has been found that the
492 * first commit is not skipped". In this case *skipped_first is set back
493 * to 0 just before the function returns.
495 struct commit_list *filter_skipped(struct commit_list *list,
496 struct commit_list **tried,
501 struct commit_list *filtered = NULL, **f = &filtered;
510 if (!skipped_revs.nr)
514 struct commit_list *next = list->next;
516 if (0 <= sha1_array_lookup(&skipped_revs,
517 list->item->object.sha1)) {
518 if (skipped_first && !*skipped_first)
520 /* Move current to tried list */
525 if (!skipped_first || !*skipped_first)
527 } else if (skipped_first && !*skipped_first) {
528 /* This means we know it's not skipped */
531 /* Move current to filtered list */
540 if (skipped_first && *skipped_first == -1)
546 #define PRN_MODULO 32768
549 * This is a pseudo random number generator based on "man 3 rand".
550 * It is not used properly because the seed is the argument and it
551 * is increased by one between each call, but that should not matter
552 * for this application.
554 static int get_prn(int count) {
555 count = count * 1103515245 + 12345;
556 return ((unsigned)(count/65536) % PRN_MODULO);
560 * Custom integer square root from
561 * http://en.wikipedia.org/wiki/Integer_square_root
563 static int sqrti(int val)
571 float y = (x + (float)val / x) / 2;
572 d = (y > x) ? y - x : x - y;
579 static struct commit_list *skip_away(struct commit_list *list, int count)
581 struct commit_list *cur, *previous;
584 prn = get_prn(count);
585 index = (count * prn / PRN_MODULO) * sqrti(prn) / sqrti(PRN_MODULO);
590 for (i = 0; cur; cur = cur->next, i++) {
592 if (hashcmp(cur->item->object.sha1, current_bad_sha1))
604 static struct commit_list *managed_skipped(struct commit_list *list,
605 struct commit_list **tried)
607 int count, skipped_first;
611 if (!skipped_revs.nr)
614 list = filter_skipped(list, tried, 0, &count, &skipped_first);
619 return skip_away(list, count);
622 static void bisect_rev_setup(struct rev_info *revs, const char *prefix,
623 const char *bad_format, const char *good_format,
626 struct argv_array rev_argv = { NULL, 0, 0 };
629 init_revisions(revs, prefix);
631 revs->commit_format = CMIT_FMT_UNSPECIFIED;
633 /* rev_argv.argv[0] will be ignored by setup_revisions */
634 argv_array_push(&rev_argv, xstrdup("bisect_rev_setup"));
635 argv_array_push_sha1(&rev_argv, current_bad_sha1, bad_format);
636 for (i = 0; i < good_revs.nr; i++)
637 argv_array_push_sha1(&rev_argv, good_revs.sha1[i],
639 argv_array_push(&rev_argv, xstrdup("--"));
641 read_bisect_paths(&rev_argv);
642 argv_array_push(&rev_argv, NULL);
644 setup_revisions(rev_argv.argv_nr, rev_argv.argv, revs, NULL);
647 static void bisect_common(struct rev_info *revs)
649 if (prepare_revision_walk(revs))
650 die("revision walk setup failed");
651 if (revs->tree_objects)
652 mark_edges_uninteresting(revs->commits, revs, NULL);
655 static void exit_if_skipped_commits(struct commit_list *tried,
656 const unsigned char *bad)
661 printf("There are only 'skip'ped commits left to test.\n"
662 "The first bad commit could be any of:\n");
663 print_commit_list(tried, "%s\n", "%s\n");
665 printf("%s\n", sha1_to_hex(bad));
666 printf("We cannot bisect more!\n");
670 static int is_expected_rev(const unsigned char *sha1)
672 const char *filename = git_path("BISECT_EXPECTED_REV");
674 struct strbuf str = STRBUF_INIT;
678 if (stat(filename, &st) || !S_ISREG(st.st_mode))
681 fp = fopen(filename, "r");
685 if (strbuf_getline(&str, fp, '\n') != EOF)
686 res = !strcmp(str.buf, sha1_to_hex(sha1));
688 strbuf_release(&str);
694 static void mark_expected_rev(char *bisect_rev_hex)
696 int len = strlen(bisect_rev_hex);
697 const char *filename = git_path("BISECT_EXPECTED_REV");
698 int fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600);
701 die_errno("could not create file '%s'", filename);
703 bisect_rev_hex[len] = '\n';
704 write_or_die(fd, bisect_rev_hex, len + 1);
705 bisect_rev_hex[len] = '\0';
708 die("closing file %s: %s", filename, strerror(errno));
711 static int bisect_checkout(char *bisect_rev_hex, int no_checkout)
715 mark_expected_rev(bisect_rev_hex);
717 argv_checkout[2] = bisect_rev_hex;
719 argv_update_ref[3] = bisect_rev_hex;
720 if (run_command_v_opt(argv_update_ref, RUN_GIT_CMD))
721 die("update-ref --no-deref HEAD failed on %s",
724 res = run_command_v_opt(argv_checkout, RUN_GIT_CMD);
729 argv_show_branch[1] = bisect_rev_hex;
730 return run_command_v_opt(argv_show_branch, RUN_GIT_CMD);
733 static struct commit *get_commit_reference(const unsigned char *sha1)
735 struct commit *r = lookup_commit_reference(sha1);
737 die("Not a valid commit name %s", sha1_to_hex(sha1));
741 static struct commit **get_bad_and_good_commits(int *rev_nr)
743 int len = 1 + good_revs.nr;
744 struct commit **rev = xmalloc(len * sizeof(*rev));
747 rev[n++] = get_commit_reference(current_bad_sha1);
748 for (i = 0; i < good_revs.nr; i++)
749 rev[n++] = get_commit_reference(good_revs.sha1[i]);
755 static void handle_bad_merge_base(void)
757 if (is_expected_rev(current_bad_sha1)) {
758 char *bad_hex = sha1_to_hex(current_bad_sha1);
759 char *good_hex = join_sha1_array_hex(&good_revs, ' ');
761 fprintf(stderr, "The merge base %s is bad.\n"
762 "This means the bug has been fixed "
763 "between %s and [%s].\n",
764 bad_hex, bad_hex, good_hex);
769 fprintf(stderr, "Some good revs are not ancestor of the bad rev.\n"
770 "git bisect cannot work properly in this case.\n"
771 "Maybe you mistake good and bad revs?\n");
775 static void handle_skipped_merge_base(const unsigned char *mb)
777 char *mb_hex = sha1_to_hex(mb);
778 char *bad_hex = sha1_to_hex(current_bad_sha1);
779 char *good_hex = join_sha1_array_hex(&good_revs, ' ');
781 warning("the merge base between %s and [%s] "
783 "So we cannot be sure the first bad commit is "
784 "between %s and %s.\n"
785 "We continue anyway.",
786 bad_hex, good_hex, mb_hex, bad_hex);
791 * "check_merge_bases" checks that merge bases are not "bad".
793 * - If one is "bad", it means the user assumed something wrong
794 * and we must exit with a non 0 error code.
795 * - If one is "good", that's good, we have nothing to do.
796 * - If one is "skipped", we can't know but we should warn.
797 * - If we don't know, we should check it out and ask the user to test.
799 static void check_merge_bases(int no_checkout)
801 struct commit_list *result;
803 struct commit **rev = get_bad_and_good_commits(&rev_nr);
805 result = get_merge_bases_many(rev[0], rev_nr - 1, rev + 1, 0);
807 for (; result; result = result->next) {
808 const unsigned char *mb = result->item->object.sha1;
809 if (!hashcmp(mb, current_bad_sha1)) {
810 handle_bad_merge_base();
811 } else if (0 <= sha1_array_lookup(&good_revs, mb)) {
813 } else if (0 <= sha1_array_lookup(&skipped_revs, mb)) {
814 handle_skipped_merge_base(mb);
816 printf("Bisecting: a merge base must be tested\n");
817 exit(bisect_checkout(sha1_to_hex(mb), no_checkout));
822 free_commit_list(result);
825 static int check_ancestors(const char *prefix)
827 struct rev_info revs;
828 struct object_array pending_copy;
831 bisect_rev_setup(&revs, prefix, "^%s", "%s", 0);
833 /* Save pending objects, so they can be cleaned up later. */
834 memset(&pending_copy, 0, sizeof(pending_copy));
835 for (i = 0; i < revs.pending.nr; i++)
836 add_object_array(revs.pending.objects[i].item,
837 revs.pending.objects[i].name,
840 bisect_common(&revs);
841 res = (revs.commits != NULL);
843 /* Clean up objects used, as they will be reused. */
844 for (i = 0; i < pending_copy.nr; i++) {
845 struct object *o = pending_copy.objects[i].item;
846 clear_commit_marks((struct commit *)o, ALL_REV_FLAGS);
853 * "check_good_are_ancestors_of_bad" checks that all "good" revs are
854 * ancestor of the "bad" rev.
856 * If that's not the case, we need to check the merge bases.
857 * If a merge base must be tested by the user, its source code will be
858 * checked out to be tested by the user and we will exit.
860 static void check_good_are_ancestors_of_bad(const char *prefix, int no_checkout)
862 const char *filename = git_path("BISECT_ANCESTORS_OK");
866 if (!current_bad_sha1)
867 die("a bad revision is needed");
869 /* Check if file BISECT_ANCESTORS_OK exists. */
870 if (!stat(filename, &st) && S_ISREG(st.st_mode))
873 /* Bisecting with no good rev is ok. */
874 if (good_revs.nr == 0)
877 /* Check if all good revs are ancestor of the bad rev. */
878 if (check_ancestors(prefix))
879 check_merge_bases(no_checkout);
881 /* Create file BISECT_ANCESTORS_OK. */
882 fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600);
884 warning("could not create file '%s': %s",
885 filename, strerror(errno));
891 * This does "git diff-tree --pretty COMMIT" without one fork+exec.
893 static void show_diff_tree(const char *prefix, struct commit *commit)
898 init_revisions(&opt, prefix);
899 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
903 /* This is what "--pretty" does */
904 opt.verbose_header = 1;
905 opt.use_terminator = 0;
906 opt.commit_format = CMIT_FMT_DEFAULT;
909 if (!opt.diffopt.output_format)
910 opt.diffopt.output_format = DIFF_FORMAT_RAW;
912 log_tree_commit(&opt, commit);
916 * We use the convention that exiting with an exit code 10 means that
917 * the bisection process finished successfully.
918 * In this case the calling shell script should exit 0.
920 * If no_checkout is non-zero, the bisection process does not
921 * checkout the trial commit but instead simply updates BISECT_HEAD.
923 int bisect_next_all(const char *prefix, int no_checkout)
925 struct rev_info revs;
926 struct commit_list *tried;
927 int reaches = 0, all = 0, nr, steps;
928 const unsigned char *bisect_rev;
929 char bisect_rev_hex[41];
931 if (read_bisect_refs())
932 die("reading bisect refs failed");
934 check_good_are_ancestors_of_bad(prefix, no_checkout);
936 bisect_rev_setup(&revs, prefix, "%s", "^%s", 1);
939 bisect_common(&revs);
941 revs.commits = find_bisection(revs.commits, &reaches, &all,
943 revs.commits = managed_skipped(revs.commits, &tried);
947 * We should exit here only if the "bad"
948 * commit is also a "skip" commit.
950 exit_if_skipped_commits(tried, NULL);
952 printf("%s was both good and bad\n",
953 sha1_to_hex(current_bad_sha1));
958 fprintf(stderr, "No testable commit found.\n"
959 "Maybe you started with bad path parameters?\n");
963 bisect_rev = revs.commits->item->object.sha1;
964 memcpy(bisect_rev_hex, sha1_to_hex(bisect_rev), 41);
966 if (!hashcmp(bisect_rev, current_bad_sha1)) {
967 exit_if_skipped_commits(tried, current_bad_sha1);
968 printf("%s is the first bad commit\n", bisect_rev_hex);
969 show_diff_tree(prefix, revs.commits->item);
970 /* This means the bisection process succeeded. */
974 nr = all - reaches - 1;
975 steps = estimate_bisect_steps(all);
976 printf("Bisecting: %d revision%s left to test after this "
977 "(roughly %d step%s)\n", nr, (nr == 1 ? "" : "s"),
978 steps, (steps == 1 ? "" : "s"));
980 return bisect_checkout(bisect_rev_hex, no_checkout);