bisect: use update_ref
[gitweb.git] / bisect.c
index 8c6d843699ab04bc0fe952268d14965943446a17..7eaa37a92986538a640671a8d607e47cccb2c759 100644 (file)
--- a/bisect.c
+++ b/bisect.c
 static struct sha1_array good_revs;
 static struct sha1_array skipped_revs;
 
-static 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};
 
 /* Remember to update object flag allocation in object.h */
 #define COUNTED                (1u<<16)
@@ -400,16 +399,16 @@ 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 = xmalloc(20);
-               hashcpy(current_bad_sha1, sha1);
+               current_bad_oid = xmalloc(sizeof(*current_bad_oid));
+               oidcpy(current_bad_oid, oid);
        } else if (starts_with(refname, "good-")) {
-               sha1_array_append(&good_revs, sha1);
+               sha1_array_append(&good_revs, oid->hash);
        } else if (starts_with(refname, "skip-")) {
-               sha1_array_append(&skipped_revs, sha1);
+               sha1_array_append(&skipped_revs, oid->hash);
        }
 
        return 0;
@@ -564,7 +563,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;
@@ -607,7 +606,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]));
@@ -628,7 +627,7 @@ static void bisect_common(struct rev_info *revs)
 }
 
 static void exit_if_skipped_commits(struct commit_list *tried,
-                                   const unsigned char *bad)
+                                   const struct object_id *bad)
 {
        if (!tried)
                return;
@@ -637,12 +636,12 @@ static void exit_if_skipped_commits(struct commit_list *tried,
               "The first bad commit could be any of:\n");
        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");
        struct stat st;
@@ -658,7 +657,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);
@@ -666,34 +665,16 @@ static int is_expected_rev(const unsigned char *sha1)
        return res;
 }
 
-static void mark_expected_rev(char *bisect_rev_hex)
-{
-       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);
-
-       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)
+static int bisect_checkout(const unsigned char *bisect_rev, int no_checkout)
 {
+       char bisect_rev_hex[GIT_SHA1_HEXSZ + 1];
 
-       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);
@@ -719,7 +700,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;
@@ -729,8 +710,8 @@ 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"
@@ -750,7 +731,7 @@ static void handle_bad_merge_base(void)
 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] "
@@ -781,7 +762,7 @@ static void check_merge_bases(int no_checkout)
 
        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;
@@ -789,7 +770,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));
                }
        }
 
@@ -838,7 +819,7 @@ static void check_good_are_ancestors_of_bad(const char *prefix, int no_checkout)
        struct stat st;
        int fd;
 
-       if (!current_bad_sha1)
+       if (!current_bad_oid)
                die("a bad revision is needed");
 
        /* Check if file BISECT_ANCESTORS_OK exists. */
@@ -903,7 +884,6 @@ 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];
 
        if (read_bisect_refs())
                die("reading bisect refs failed");
@@ -927,7 +907,7 @@ 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));
+                      oid_to_hex(current_bad_oid));
                exit(1);
        }
 
@@ -938,11 +918,10 @@ 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 bad commit\n", sha1_to_hex(bisect_rev));
                show_diff_tree(prefix, revs.commits->item);
                /* This means the bisection process succeeded. */
                exit(10);
@@ -954,7 +933,7 @@ 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)