#include "list-objects.h"
#include "quote.h"
#include "sha1-lookup.h"
+#include "run-command.h"
#include "bisect.h"
-static unsigned char (*skipped_sha1)[20];
-static int skipped_sha1_nr;
-static int skipped_sha1_alloc;
+struct sha1_array {
+ unsigned char (*sha1)[20];
+ int sha1_nr;
+ int sha1_alloc;
+};
+
+static struct sha1_array good_revs;
+static struct sha1_array skipped_revs;
+
+static const unsigned char *current_bad_sha1;
+
+struct argv_array {
+ const char **argv;
+ int argv_nr;
+ int argv_alloc;
+};
+
+struct argv_array rev_argv;
-static const char **rev_argv;
-static int rev_argv_nr;
-static int rev_argv_alloc;
+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 */
return best;
}
+static void argv_array_push(struct argv_array *array, const char *string)
+{
+ ALLOC_GROW(array->argv, array->argv_nr + 1, array->argv_alloc);
+ array->argv[array->argv_nr++] = string;
+}
+
+static void argv_array_push_sha1(struct argv_array *array,
+ const unsigned char *sha1,
+ const char *format)
+{
+ struct strbuf buf = STRBUF_INIT;
+ strbuf_addf(&buf, format, sha1_to_hex(sha1));
+ argv_array_push(array, strbuf_detach(&buf, NULL));
+}
+
+static void sha1_array_push(struct sha1_array *array,
+ const unsigned char *sha1)
+{
+ ALLOC_GROW(array->sha1, array->sha1_nr + 1, array->sha1_alloc);
+ hashcpy(array->sha1[array->sha1_nr++], sha1);
+}
+
static int register_ref(const char *refname, const unsigned char *sha1,
int flags, void *cb_data)
{
if (!strcmp(refname, "bad")) {
- ALLOC_GROW(rev_argv, rev_argv_nr + 1, rev_argv_alloc);
- rev_argv[rev_argv_nr++] = xstrdup(sha1_to_hex(sha1));
+ current_bad_sha1 = sha1;
} else if (!prefixcmp(refname, "good-")) {
- const char *hex = sha1_to_hex(sha1);
- char *good = xmalloc(strlen(hex) + 2);
- *good = '^';
- memcpy(good + 1, hex, strlen(hex) + 1);
- ALLOC_GROW(rev_argv, rev_argv_nr + 1, rev_argv_alloc);
- rev_argv[rev_argv_nr++] = good;
+ sha1_array_push(&good_revs, sha1);
} else if (!prefixcmp(refname, "skip-")) {
- ALLOC_GROW(skipped_sha1, skipped_sha1_nr + 1,
- skipped_sha1_alloc);
- hashcpy(skipped_sha1[skipped_sha1_nr++], sha1);
+ sha1_array_push(&skipped_revs, sha1);
}
return 0;
return for_each_ref_in("refs/bisect/", register_ref, NULL);
}
-void read_bisect_paths(void)
+void read_bisect_paths(struct argv_array *array)
{
struct strbuf str = STRBUF_INIT;
const char *filename = git_path("BISECT_NAMES");
strbuf_trim(&str);
quoted = strbuf_detach(&str, NULL);
- res = sq_dequote_to_argv(quoted, &rev_argv,
- &rev_argv_nr, &rev_argv_alloc);
+ res = sq_dequote_to_argv(quoted, &array->argv,
+ &array->argv_nr, &array->argv_alloc);
if (res)
die("Badly quoted content in file '%s': %s",
filename, quoted);
static void prepare_skipped(void)
{
- qsort(skipped_sha1, skipped_sha1_nr, sizeof(*skipped_sha1), skipcmp);
+ qsort(skipped_revs.sha1, skipped_revs.sha1_nr,
+ sizeof(*skipped_revs.sha1), skipcmp);
}
static const unsigned char *skipped_sha1_access(size_t index, void *table)
static int lookup_skipped(unsigned char *sha1)
{
- return sha1_pos(sha1, skipped_sha1, skipped_sha1_nr,
+ return sha1_pos(sha1, skipped_revs.sha1, skipped_revs.sha1_nr,
skipped_sha1_access);
}
*tried = NULL;
- if (!skipped_sha1_nr)
+ if (!skipped_revs.sha1_nr)
return list;
prepare_skipped();
static void bisect_rev_setup(struct rev_info *revs, const char *prefix)
{
+ int i;
+
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);
- rev_argv[rev_argv_nr++] = xstrdup("bisect_rev_setup");
-
if (read_bisect_refs())
die("reading bisect refs failed");
- ALLOC_GROW(rev_argv, rev_argv_nr + 1, rev_argv_alloc);
- rev_argv[rev_argv_nr++] = xstrdup("--");
+ /* rev_argv.argv[0] will be ignored by setup_revisions */
+ argv_array_push(&rev_argv, xstrdup("bisect_rev_setup"));
+ argv_array_push_sha1(&rev_argv, current_bad_sha1, "%s");
+ for (i = 0; i < good_revs.sha1_nr; i++)
+ argv_array_push_sha1(&rev_argv, good_revs.sha1[i], "^%s");
+ argv_array_push(&rev_argv, xstrdup("--"));
+ read_bisect_paths(&rev_argv);
+ argv_array_push(&rev_argv, NULL);
- read_bisect_paths();
+ setup_revisions(rev_argv.argv_nr, rev_argv.argv, revs, NULL);
+ revs->limited = 1;
+}
- ALLOC_GROW(rev_argv, rev_argv_nr + 1, rev_argv_alloc);
- rev_argv[rev_argv_nr++] = NULL;
+static void bisect_common(struct rev_info *revs, const char *prefix,
+ int *reaches, int *all)
+{
+ bisect_rev_setup(revs, prefix);
- setup_revisions(rev_argv_nr, rev_argv, revs, NULL);
+ if (prepare_revision_walk(revs))
+ die("revision walk setup failed");
+ if (revs->tree_objects)
+ mark_edges_uninteresting(revs->commits, revs, NULL);
- revs->limited = 1;
+ revs->commits = find_bisection(revs->commits, reaches, all,
+ !!skipped_revs.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));
+
+ 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));
}
-int bisect_next_vars(const char *prefix)
+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 rev_list_info info;
- int reaches = 0, all = 0;
+ struct commit_list *tried;
+ int reaches = 0, all = 0, nr;
+ const unsigned char *bisect_rev;
+ char bisect_rev_hex[41];
- memset(&info, 0, sizeof(info));
- info.revs = &revs;
- info.bisect_show_flags = BISECT_SHOW_TRIED | BISECT_SHOW_STRINGED;
+ bisect_common(&revs, prefix, &reaches, &all);
- bisect_rev_setup(&revs, prefix);
+ revs.commits = filter_skipped(revs.commits, &tried, 0);
- if (prepare_revision_walk(&revs))
- die("revision walk setup failed");
- if (revs.tree_objects)
- mark_edges_uninteresting(revs.commits, &revs, NULL);
+ if (!revs.commits) {
+ /*
+ * We should exit here only if the "bad"
+ * commit is also a "skip" commit.
+ */
+ exit_if_skipped_commits(tried, NULL);
- revs.commits = find_bisection(revs.commits, &reaches, &all,
- !!skipped_sha1_nr);
+ printf("%s was both good and bad\n",
+ sha1_to_hex(current_bad_sha1));
+ exit(1);
+ }
- return show_bisect_vars(&info, reaches, all);
+ 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);
}
+