bisect--helper: add "--next-exit" to output bisect results
authorChristian Couder <chriscool@tuxfamily.org>
Sun, 19 Apr 2009 09:56:07 +0000 (11:56 +0200)
committerJunio C Hamano <gitster@pobox.com>
Sun, 10 May 2009 07:30:28 +0000 (00:30 -0700)
The goal of this patch is to port more shell code from the "bisect_next"
function in "git-bisect.sh" to C code in "builtin-bisect--helper.c".

So we port the code that interprets the bisection result and stops or
continues (by checking out the next revision) the bisection process.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
bisect.c
bisect.h
builtin-bisect--helper.c

index 0448eae3f8ffb80fd383db83507a9a4e7fdbe412..5902e83fac76cb419d62cfbbae09048872ba526e 100644 (file)
--- a/bisect.c
+++ b/bisect.c
@@ -6,6 +6,7 @@
 #include "list-objects.h"
 #include "quote.h"
 #include "sha1-lookup.h"
+#include "run-command.h"
 #include "bisect.h"
 
 static unsigned char (*skipped_sha1)[20];
@@ -16,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)
@@ -403,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);
@@ -560,3 +568,100 @@ int bisect_next_vars(const char *prefix)
 
        return show_bisect_vars(&info, reaches, all);
 }
+
+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));
+
+       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 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);
+}
+
index 89aa6cb0b30989ce8b1ac17859efdb9d11c97035..028eb85220bbb2432755ed481818b71e0c29b05d 100644 (file)
--- a/bisect.h
+++ b/bisect.h
@@ -29,6 +29,7 @@ struct rev_list_info {
 extern int show_bisect_vars(struct rev_list_info *info, int reaches, int all);
 
 extern int bisect_next_vars(const char *prefix);
+extern int bisect_next_exit(const char *prefix);
 
 extern int estimate_bisect_steps(int all);
 
index 8fe778766a45f8f7bfceeeef9f67a2433a892ffb..cb86a9a9e0cacbc64e1af8866b62d8f29dbc1776 100644 (file)
@@ -5,23 +5,29 @@
 
 static const char * const git_bisect_helper_usage[] = {
        "git bisect--helper --next-vars",
+       "git bisect--helper --next-exit",
        NULL
 };
 
 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 {
        int next_vars = 0;
+       int next_exit = 0;
        struct option options[] = {
                OPT_BOOLEAN(0, "next-vars", &next_vars,
                            "output next bisect step variables"),
+               OPT_BOOLEAN(0, "next-exit", &next_exit,
+                           "output bisect result and exit instuctions"),
                OPT_END()
        };
 
        argc = parse_options(argc, argv, options, git_bisect_helper_usage, 0);
 
-       if (!next_vars)
+       if ((next_vars && next_exit) || (!next_vars && !next_exit))
                usage_with_options(git_bisect_helper_usage, options);
 
-       /* next-vars */
-       return bisect_next_vars(prefix);
+       if (next_vars)
+               return bisect_next_vars(prefix);
+       else /* next-exit */
+               return bisect_next_exit(prefix);
 }