rev-list: remove stringed output flag from "show_bisect_vars"
[gitweb.git] / bisect.c
index 94ec011786cdff03fd143926aea7835dc2839a30..4796aa919870d49f67ad23dded2ce5b654841c87 100644 (file)
--- a/bisect.c
+++ b/bisect.c
@@ -4,7 +4,9 @@
 #include "revision.h"
 #include "refs.h"
 #include "list-objects.h"
+#include "quote.h"
 #include "sha1-lookup.h"
+#include "run-command.h"
 #include "bisect.h"
 
 static unsigned char (*skipped_sha1)[20];
@@ -15,6 +17,12 @@ static const char **rev_argv;
 static int rev_argv_nr;
 static int rev_argv_alloc;
 
+static const unsigned char *current_bad_sha1;
+
+static const char *argv_diff_tree[] = {"diff-tree", "--pretty", NULL, NULL};
+static const char *argv_checkout[] = {"checkout", "-q", NULL, "--", NULL};
+static const char *argv_show_branch[] = {"show-branch", NULL, NULL};
+
 /* bits #0-15 in revision.h */
 
 #define COUNTED                (1u<<16)
@@ -402,6 +410,7 @@ static int register_ref(const char *refname, const unsigned char *sha1,
 {
        if (!strcmp(refname, "bad")) {
                ALLOC_GROW(rev_argv, rev_argv_nr + 1, rev_argv_alloc);
+               current_bad_sha1 = sha1;
                rev_argv[rev_argv_nr++] = xstrdup(sha1_to_hex(sha1));
        } else if (!prefixcmp(refname, "good-")) {
                const char *hex = sha1_to_hex(sha1);
@@ -424,6 +433,32 @@ static int read_bisect_refs(void)
        return for_each_ref_in("refs/bisect/", register_ref, NULL);
 }
 
+void read_bisect_paths(void)
+{
+       struct strbuf str = STRBUF_INIT;
+       const char *filename = git_path("BISECT_NAMES");
+       FILE *fp = fopen(filename, "r");
+
+       if (!fp)
+               die("Could not open file '%s': %s", filename, strerror(errno));
+
+       while (strbuf_getline(&str, fp, '\n') != EOF) {
+               char *quoted;
+               int res;
+
+               strbuf_trim(&str);
+               quoted = strbuf_detach(&str, NULL);
+               res = sq_dequote_to_argv(quoted, &rev_argv,
+                                        &rev_argv_nr, &rev_argv_alloc);
+               if (res)
+                       die("Badly quoted content in file '%s': %s",
+                           filename, quoted);
+       }
+
+       strbuf_release(&str);
+       fclose(fp);
+}
+
 static int skipcmp(const void *a, const void *b)
 {
        return hashcmp(a, b);
@@ -479,14 +514,11 @@ struct commit_list *filter_skipped(struct commit_list *list,
        return filtered;
 }
 
-int bisect_next_vars(const char *prefix)
+static void bisect_rev_setup(struct rev_info *revs, const char *prefix)
 {
-       struct rev_info revs;
-       int reaches = 0, all = 0;
-
-       init_revisions(&revs, prefix);
-       revs.abbrev = 0;
-       revs.commit_format = CMIT_FMT_UNSPECIFIED;
+       init_revisions(revs, prefix);
+       revs->abbrev = 0;
+       revs->commit_format = CMIT_FMT_UNSPECIFIED;
 
        /* argv[0] will be ignored by setup_revisions */
        ALLOC_GROW(rev_argv, rev_argv_nr + 1, rev_argv_alloc);
@@ -498,17 +530,123 @@ int bisect_next_vars(const char *prefix)
        ALLOC_GROW(rev_argv, rev_argv_nr + 1, rev_argv_alloc);
        rev_argv[rev_argv_nr++] = xstrdup("--");
 
-       setup_revisions(rev_argv_nr, rev_argv, &revs, NULL);
+       read_bisect_paths();
 
-       revs.limited = 1;
+       ALLOC_GROW(rev_argv, rev_argv_nr + 1, rev_argv_alloc);
+       rev_argv[rev_argv_nr++] = NULL;
+
+       setup_revisions(rev_argv_nr, rev_argv, revs, NULL);
 
-       if (prepare_revision_walk(&revs))
+       revs->limited = 1;
+}
+
+static void bisect_common(struct rev_info *revs, const char *prefix,
+                         int *reaches, int *all)
+{
+       bisect_rev_setup(revs, prefix);
+
+       if (prepare_revision_walk(revs))
                die("revision walk setup failed");
-       if (revs.tree_objects)
-               mark_edges_uninteresting(revs.commits, &revs, NULL);
+       if (revs->tree_objects)
+               mark_edges_uninteresting(revs->commits, revs, NULL);
+
+       revs->commits = find_bisection(revs->commits, reaches, all,
+                                      !!skipped_sha1_nr);
+}
+
+static void exit_if_skipped_commits(struct commit_list *tried,
+                                   const unsigned char *bad)
+{
+       if (!tried)
+               return;
+
+       printf("There are only 'skip'ped commits left to test.\n"
+              "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("We cannot bisect more!\n");
+       exit(2);
+}
+
+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("could not create file '%s': %s",
+                   filename, strerror(errno));
 
-       revs.commits = find_bisection(revs.commits, &reaches, &all,
-                                     !!skipped_sha1_nr);
+       bisect_rev_hex[len] = '\n';
+       write_or_die(fd, bisect_rev_hex, len + 1);
+       bisect_rev_hex[len] = '\0';
 
-       return show_bisect_vars(&revs, reaches, all, 0, 1);
+       if (close(fd) < 0)
+               die("closing file %s: %s", filename, strerror(errno));
 }
+
+static int bisect_checkout(char *bisect_rev_hex)
+{
+       int res;
+
+       mark_expected_rev(bisect_rev_hex);
+
+       argv_checkout[2] = bisect_rev_hex;
+       res = run_command_v_opt(argv_checkout, RUN_GIT_CMD);
+       if (res)
+               exit(res);
+
+       argv_show_branch[1] = bisect_rev_hex;
+       return run_command_v_opt(argv_show_branch, RUN_GIT_CMD);
+}
+
+/*
+ * 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.
+ */
+int bisect_next_exit(const char *prefix)
+{
+       struct rev_info revs;
+       struct commit_list *tried;
+       int reaches = 0, all = 0, nr;
+       const unsigned char *bisect_rev;
+       char bisect_rev_hex[41];
+
+       bisect_common(&revs, prefix, &reaches, &all);
+
+       revs.commits = filter_skipped(revs.commits, &tried, 0);
+
+       if (!revs.commits) {
+               /*
+                * We should exit here only if the "bad"
+                * commit is also a "skip" commit.
+                */
+               exit_if_skipped_commits(tried, NULL);
+
+               printf("%s was both good and bad\n",
+                      sha1_to_hex(current_bad_sha1));
+               exit(1);
+       }
+
+       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 first bad commit\n", bisect_rev_hex);
+               argv_diff_tree[2] = bisect_rev_hex;
+               run_command_v_opt(argv_diff_tree, RUN_GIT_CMD);
+               /* This means the bisection process succeeded. */
+               exit(10);
+       }
+
+       nr = all - reaches - 1;
+       printf("Bisecting: %d revisions left to test after this "
+              "(roughly %d steps)\n", nr, estimate_bisect_steps(all));
+
+       return bisect_checkout(bisect_rev_hex);
+}
+