diff options
Diffstat (limited to 'bisect.c')
-rw-r--r-- | bisect.c | 241 |
1 files changed, 159 insertions, 82 deletions
@@ -15,14 +15,15 @@ static struct sha1_array good_revs; static struct sha1_array skipped_revs; -static const unsigned char *current_bad_sha1; +static struct object_id *current_bad_oid; static const char *argv_checkout[] = {"checkout", "-q", NULL, "--", NULL}; static const char *argv_show_branch[] = {"show-branch", NULL, NULL}; -static const char *argv_update_ref[] = {"update-ref", "--no-deref", "BISECT_HEAD", NULL, NULL}; -/* bits #0-15 in revision.h */ +static const char *term_bad; +static const char *term_good; +/* Remember to update object flag allocation in object.h */ #define COUNTED (1u<<16) /* @@ -216,11 +217,12 @@ static struct commit_list *best_bisection_sorted(struct commit_list *list, int n } qsort(array, cnt, sizeof(*array), compare_commit_dist); for (p = list, i = 0; i < cnt; i++) { - struct name_decoration *r = xmalloc(sizeof(*r) + 100); + char buf[100]; /* enough for dist=%d */ struct object *obj = &(array[i].commit->object); - sprintf(r->name, "dist=%d", array[i].distance); - r->next = add_decoration(&name_decoration, obj, r); + snprintf(buf, sizeof(buf), "dist=%d", array[i].distance); + add_name_decoration(DECORATION_NONE, buf, obj); + p->item = array[i].commit; p = p->next; } @@ -400,17 +402,24 @@ struct commit_list *find_bisection(struct commit_list *list, return best; } -static int register_ref(const char *refname, const unsigned char *sha1, +static int register_ref(const char *refname, const struct object_id *oid, int flags, void *cb_data) { - if (!strcmp(refname, "bad")) { - current_bad_sha1 = sha1; - } else if (!prefixcmp(refname, "good-")) { - sha1_array_append(&good_revs, sha1); - } else if (!prefixcmp(refname, "skip-")) { - sha1_array_append(&skipped_revs, sha1); + struct strbuf good_prefix = STRBUF_INIT; + strbuf_addstr(&good_prefix, term_good); + strbuf_addstr(&good_prefix, "-"); + + if (!strcmp(refname, term_bad)) { + current_bad_oid = xmalloc(sizeof(*current_bad_oid)); + oidcpy(current_bad_oid, oid); + } else if (starts_with(refname, good_prefix.buf)) { + sha1_array_append(&good_revs, oid->hash); + } else if (starts_with(refname, "skip-")) { + sha1_array_append(&skipped_revs, oid->hash); } + strbuf_release(&good_prefix); + return 0; } @@ -419,10 +428,13 @@ static int read_bisect_refs(void) return for_each_ref_in("refs/bisect/", register_ref, NULL); } +static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES") +static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV") + static void read_bisect_paths(struct argv_array *array) { struct strbuf str = STRBUF_INIT; - const char *filename = git_path("BISECT_NAMES"); + const char *filename = git_path_bisect_names(); FILE *fp = fopen(filename, "r"); if (!fp) @@ -525,9 +537,9 @@ struct commit_list *filter_skipped(struct commit_list *list, * is increased by one between each call, but that should not matter * for this application. */ -static int get_prn(int count) { +static unsigned get_prn(unsigned count) { count = count * 1103515245 + 12345; - return ((unsigned)(count/65536) % PRN_MODULO); + return (count/65536) % PRN_MODULO; } /* @@ -563,7 +575,7 @@ static struct commit_list *skip_away(struct commit_list *list, int count) for (i = 0; cur; cur = cur->next, i++) { if (i == index) { - if (hashcmp(cur->item->object.sha1, current_bad_sha1)) + if (hashcmp(cur->item->object.sha1, current_bad_oid->hash)) return cur; if (previous) return previous; @@ -606,7 +618,7 @@ static void bisect_rev_setup(struct rev_info *revs, const char *prefix, /* rev_argv.argv[0] will be ignored by setup_revisions */ argv_array_push(&rev_argv, "bisect_rev_setup"); - argv_array_pushf(&rev_argv, bad_format, sha1_to_hex(current_bad_sha1)); + argv_array_pushf(&rev_argv, bad_format, oid_to_hex(current_bad_oid)); for (i = 0; i < good_revs.nr; i++) argv_array_pushf(&rev_argv, good_format, sha1_to_hex(good_revs.sha1[i])); @@ -623,27 +635,27 @@ static void bisect_common(struct rev_info *revs) if (prepare_revision_walk(revs)) die("revision walk setup failed"); if (revs->tree_objects) - mark_edges_uninteresting(revs->commits, revs, NULL); + mark_edges_uninteresting(revs, NULL); } static void exit_if_skipped_commits(struct commit_list *tried, - const unsigned char *bad) + const struct object_id *bad) { if (!tried) return; printf("There are only 'skip'ped commits left to test.\n" - "The first bad commit could be any of:\n"); + "The first %s commit could be any of:\n", term_bad); print_commit_list(tried, "%s\n", "%s\n"); if (bad) - printf("%s\n", sha1_to_hex(bad)); + printf("%s\n", oid_to_hex(bad)); printf("We cannot bisect more!\n"); exit(2); } -static int is_expected_rev(const unsigned char *sha1) +static int is_expected_rev(const struct object_id *oid) { - const char *filename = git_path("BISECT_EXPECTED_REV"); + const char *filename = git_path_bisect_expected_rev(); struct stat st; struct strbuf str = STRBUF_INIT; FILE *fp; @@ -657,7 +669,7 @@ static int is_expected_rev(const unsigned char *sha1) return 0; if (strbuf_getline(&str, fp, '\n') != EOF) - res = !strcmp(str.buf, sha1_to_hex(sha1)); + res = !strcmp(str.buf, oid_to_hex(oid)); strbuf_release(&str); fclose(fp); @@ -665,36 +677,18 @@ static int is_expected_rev(const unsigned char *sha1) return res; } -static void mark_expected_rev(char *bisect_rev_hex) +static int bisect_checkout(const unsigned char *bisect_rev, int no_checkout) { - int len = strlen(bisect_rev_hex); - const char *filename = git_path("BISECT_EXPECTED_REV"); - int fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600); + char bisect_rev_hex[GIT_SHA1_HEXSZ + 1]; - if (fd < 0) - die_errno("could not create file '%s'", filename); - - bisect_rev_hex[len] = '\n'; - write_or_die(fd, bisect_rev_hex, len + 1); - bisect_rev_hex[len] = '\0'; - - if (close(fd) < 0) - die("closing file %s: %s", filename, strerror(errno)); -} - -static int bisect_checkout(char *bisect_rev_hex, int no_checkout) -{ - int res; - - mark_expected_rev(bisect_rev_hex); + memcpy(bisect_rev_hex, sha1_to_hex(bisect_rev), GIT_SHA1_HEXSZ + 1); + update_ref(NULL, "BISECT_EXPECTED_REV", bisect_rev, NULL, 0, UPDATE_REFS_DIE_ON_ERR); argv_checkout[2] = bisect_rev_hex; if (no_checkout) { - argv_update_ref[3] = bisect_rev_hex; - if (run_command_v_opt(argv_update_ref, RUN_GIT_CMD)) - die("update-ref --no-deref HEAD failed on %s", - bisect_rev_hex); + update_ref(NULL, "BISECT_HEAD", bisect_rev, NULL, 0, UPDATE_REFS_DIE_ON_ERR); } else { + int res; res = run_command_v_opt(argv_checkout, RUN_GIT_CMD); if (res) exit(res); @@ -718,7 +712,7 @@ static struct commit **get_bad_and_good_commits(int *rev_nr) struct commit **rev = xmalloc(len * sizeof(*rev)); int i, n = 0; - rev[n++] = get_commit_reference(current_bad_sha1); + rev[n++] = get_commit_reference(current_bad_oid->hash); for (i = 0; i < good_revs.nr; i++) rev[n++] = get_commit_reference(good_revs.sha1[i]); *rev_nr = n; @@ -728,45 +722,56 @@ static struct commit **get_bad_and_good_commits(int *rev_nr) static void handle_bad_merge_base(void) { - if (is_expected_rev(current_bad_sha1)) { - char *bad_hex = sha1_to_hex(current_bad_sha1); + if (is_expected_rev(current_bad_oid)) { + char *bad_hex = oid_to_hex(current_bad_oid); char *good_hex = join_sha1_array_hex(&good_revs, ' '); - - fprintf(stderr, "The merge base %s is bad.\n" - "This means the bug has been fixed " - "between %s and [%s].\n", - bad_hex, bad_hex, good_hex); - + if (!strcmp(term_bad, "bad") && !strcmp(term_good, "good")) { + fprintf(stderr, "The merge base %s is bad.\n" + "This means the bug has been fixed " + "between %s and [%s].\n", + bad_hex, bad_hex, good_hex); + } else if (!strcmp(term_bad, "new") && !strcmp(term_good, "old")) { + fprintf(stderr, "The merge base %s is new.\n" + "The property has changed " + "between %s and [%s].\n", + bad_hex, bad_hex, good_hex); + } else { + fprintf(stderr, "The merge base %s is %s.\n" + "This means the first '%s' commit is " + "between %s and [%s].\n", + bad_hex, term_bad, term_good, bad_hex, good_hex); + } exit(3); } - fprintf(stderr, "Some good revs are not ancestor of the bad rev.\n" + fprintf(stderr, "Some %s revs are not ancestor of the %s rev.\n" "git bisect cannot work properly in this case.\n" - "Maybe you mistake good and bad revs?\n"); + "Maybe you mistook %s and %s revs?\n", + term_good, term_bad, term_good, term_bad); exit(1); } static void handle_skipped_merge_base(const unsigned char *mb) { char *mb_hex = sha1_to_hex(mb); - char *bad_hex = sha1_to_hex(current_bad_sha1); + char *bad_hex = sha1_to_hex(current_bad_oid->hash); char *good_hex = join_sha1_array_hex(&good_revs, ' '); warning("the merge base between %s and [%s] " "must be skipped.\n" - "So we cannot be sure the first bad commit is " + "So we cannot be sure the first %s commit is " "between %s and %s.\n" "We continue anyway.", - bad_hex, good_hex, mb_hex, bad_hex); + bad_hex, good_hex, term_bad, mb_hex, bad_hex); free(good_hex); } /* - * "check_merge_bases" checks that merge bases are not "bad". + * "check_merge_bases" checks that merge bases are not "bad" (or "new"). * - * - If one is "bad", it means the user assumed something wrong + * - If one is "bad" (or "new"), it means the user assumed something wrong * and we must exit with a non 0 error code. - * - If one is "good", that's good, we have nothing to do. + * - If one is "good" (or "old"), that's good, we have nothing to do. * - If one is "skipped", we can't know but we should warn. * - If we don't know, we should check it out and ask the user to test. */ @@ -776,11 +781,11 @@ static void check_merge_bases(int no_checkout) int rev_nr; struct commit **rev = get_bad_and_good_commits(&rev_nr); - result = get_merge_bases_many(rev[0], rev_nr - 1, rev + 1, 0); + result = get_merge_bases_many(rev[0], rev_nr - 1, rev + 1); for (; result; result = result->next) { const unsigned char *mb = result->item->object.sha1; - if (!hashcmp(mb, current_bad_sha1)) { + if (!hashcmp(mb, current_bad_oid->hash)) { handle_bad_merge_base(); } else if (0 <= sha1_array_lookup(&good_revs, mb)) { continue; @@ -788,7 +793,7 @@ static void check_merge_bases(int no_checkout) handle_skipped_merge_base(mb); } else { printf("Bisecting: a merge base must be tested\n"); - exit(bisect_checkout(sha1_to_hex(mb), no_checkout)); + exit(bisect_checkout(mb, no_checkout)); } } @@ -833,20 +838,20 @@ static int check_ancestors(const char *prefix) */ static void check_good_are_ancestors_of_bad(const char *prefix, int no_checkout) { - const char *filename = git_path("BISECT_ANCESTORS_OK"); + char *filename = git_pathdup("BISECT_ANCESTORS_OK"); struct stat st; int fd; - if (!current_bad_sha1) - die("a bad revision is needed"); + if (!current_bad_oid) + die("a %s revision is needed", term_bad); /* Check if file BISECT_ANCESTORS_OK exists. */ if (!stat(filename, &st) && S_ISREG(st.st_mode)) - return; + goto done; /* Bisecting with no good rev is ok. */ if (good_revs.nr == 0) - return; + goto done; /* Check if all good revs are ancestor of the bad rev. */ if (check_ancestors(prefix)) @@ -859,6 +864,8 @@ static void check_good_are_ancestors_of_bad(const char *prefix, int no_checkout) filename, strerror(errno)); else close(fd); + done: + free(filename); } /* @@ -887,6 +894,36 @@ static void show_diff_tree(const char *prefix, struct commit *commit) } /* + * The terms used for this bisect session are stored in BISECT_TERMS. + * We read them and store them to adapt the messages accordingly. + * Default is bad/good. + */ +void read_bisect_terms(const char **read_bad, const char **read_good) +{ + struct strbuf str = STRBUF_INIT; + const char *filename = git_path("BISECT_TERMS"); + FILE *fp = fopen(filename, "r"); + + if (!fp) { + if (errno == ENOENT) { + *read_bad = "bad"; + *read_good = "good"; + return; + } else { + die("could not read file '%s': %s", filename, + strerror(errno)); + } + } else { + strbuf_getline(&str, fp, '\n'); + *read_bad = strbuf_detach(&str, NULL); + strbuf_getline(&str, fp, '\n'); + *read_good = strbuf_detach(&str, NULL); + } + strbuf_release(&str); + fclose(fp); +} + +/* * We use the convention that exiting with an exit code 10 means that * the bisection process finished successfully. * In this case the calling shell script should exit 0. @@ -900,8 +937,8 @@ int bisect_next_all(const char *prefix, int no_checkout) struct commit_list *tried; int reaches = 0, all = 0, nr, steps; const unsigned char *bisect_rev; - char bisect_rev_hex[41]; + read_bisect_terms(&term_bad, &term_good); if (read_bisect_refs()) die("reading bisect refs failed"); @@ -923,8 +960,10 @@ int bisect_next_all(const char *prefix, int no_checkout) */ exit_if_skipped_commits(tried, NULL); - printf("%s was both good and bad\n", - sha1_to_hex(current_bad_sha1)); + printf("%s was both %s and %s\n", + oid_to_hex(current_bad_oid), + term_good, + term_bad); exit(1); } @@ -935,11 +974,11 @@ int bisect_next_all(const char *prefix, int no_checkout) } bisect_rev = revs.commits->item->object.sha1; - memcpy(bisect_rev_hex, sha1_to_hex(bisect_rev), 41); - if (!hashcmp(bisect_rev, current_bad_sha1)) { - exit_if_skipped_commits(tried, current_bad_sha1); - printf("%s is the first bad commit\n", bisect_rev_hex); + if (!hashcmp(bisect_rev, current_bad_oid->hash)) { + exit_if_skipped_commits(tried, current_bad_oid); + printf("%s is the first %s commit\n", sha1_to_hex(bisect_rev), + term_bad); show_diff_tree(prefix, revs.commits->item); /* This means the bisection process succeeded. */ exit(10); @@ -951,6 +990,44 @@ int bisect_next_all(const char *prefix, int no_checkout) "(roughly %d step%s)\n", nr, (nr == 1 ? "" : "s"), steps, (steps == 1 ? "" : "s")); - return bisect_checkout(bisect_rev_hex, no_checkout); + return bisect_checkout(bisect_rev, no_checkout); } +static inline int log2i(int n) +{ + int log2 = 0; + + for (; n > 1; n >>= 1) + log2++; + + return log2; +} + +static inline int exp2i(int n) +{ + return 1 << n; +} + +/* + * Estimate the number of bisect steps left (after the current step) + * + * For any x between 0 included and 2^n excluded, the probability for + * n - 1 steps left looks like: + * + * P(2^n + x) == (2^n - x) / (2^n + x) + * + * and P(2^n + x) < 0.5 means 2^n < 3x + */ +int estimate_bisect_steps(int all) +{ + int n, x, e; + + if (all < 3) + return 0; + + n = log2i(all); + e = exp2i(n); + x = all - e; + + return (e < 3 * x) ? n : n - 1; +} |