Merge branch 'jh/strbuf-read-use-read-in-full'
[gitweb.git] / bisect.c
index 03d5cd9454207d0eb6fc87768e3dc432a5b921f5..f9da9d134680c7581231f27584b10e83a6110bcd 100644 (file)
--- a/bisect.c
+++ b/bisect.c
@@ -21,6 +21,9 @@ 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};
 
+static const char *term_bad;
+static const char *term_good;
+
 /* Remember to update object flag allocation in object.h */
 #define COUNTED                (1u<<16)
 
@@ -403,15 +406,21 @@ struct commit_list *find_bisection(struct commit_list *list,
 static int register_ref(const char *refname, const struct object_id *oid,
                        int flags, void *cb_data)
 {
-       if (!strcmp(refname, "bad")) {
+       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-")) {
+       } 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;
 }
 
@@ -420,10 +429,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)
@@ -634,7 +646,7 @@ static void exit_if_skipped_commits(struct commit_list *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", oid_to_hex(bad));
@@ -644,7 +656,7 @@ static void exit_if_skipped_commits(struct commit_list *tried,
 
 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;
@@ -732,18 +744,24 @@ static void handle_bad_merge_base(void)
        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 {
+                       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);
 }
 
@@ -755,10 +773,10 @@ static void handle_skipped_merge_base(const unsigned char *mb)
 
        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);
 }
 
@@ -839,7 +857,7 @@ static void check_good_are_ancestors_of_bad(const char *prefix, int no_checkout)
        int fd;
 
        if (!current_bad_oid)
-               die("a bad revision is needed");
+               die("a %s revision is needed", term_bad);
 
        /* Check if file BISECT_ANCESTORS_OK exists. */
        if (!stat(filename, &st) && S_ISREG(st.st_mode))
@@ -889,6 +907,36 @@ static void show_diff_tree(const char *prefix, struct commit *commit)
        log_tree_commit(&opt, 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.
@@ -905,6 +953,7 @@ int bisect_next_all(const char *prefix, int no_checkout)
        const unsigned char *bisect_rev;
        char bisect_rev_hex[GIT_SHA1_HEXSZ + 1];
 
+       read_bisect_terms(&term_bad, &term_good);
        if (read_bisect_refs())
                die("reading bisect refs failed");
 
@@ -926,8 +975,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",
-                      oid_to_hex(current_bad_oid));
+               printf("%s was both %s and %s\n",
+                      oid_to_hex(current_bad_oid),
+                      term_good,
+                      term_bad);
                exit(1);
        }
 
@@ -942,7 +993,8 @@ int bisect_next_all(const char *prefix, int no_checkout)
 
        if (!hashcmp(bisect_rev, current_bad_oid->hash)) {
                exit_if_skipped_commits(tried, current_bad_oid);
-               printf("%s is the first bad commit\n", bisect_rev_hex);
+               printf("%s is the first %s commit\n", bisect_rev_hex,
+                       term_bad);
                show_diff_tree(prefix, revs.commits->item);
                /* This means the bisection process succeeded. */
                exit(10);