Reimplement the `bisect_write` shell function in C and add a
`bisect-write` subcommand to `git bisect--helper` to call it from
git-bisect.sh
Using `--bisect-write` subcommand is a temporary measure to port shell
function in C so as to use the existing test suite. As more functions
are ported, this subcommand will be retired and will be called by some
other methods.
Note: bisect_write() uses two variables namely TERM_GOOD and TERM_BAD
from the global shell script thus we need to pass it to the subcommand
using the arguments. We then store them in a struct bisect_terms and
pass the memory address around functions.
This patch also introduces new methods namely bisect_state_init() and
bisect_terms_release() for easy memory management for the struct
bisect_terms.
Mentored-by: Lars Schneider
Mentored-by: Christian Couder
Signed-off-by: Pranit Bauva
---
builtin/bisect--helper.c | 97
git-bisect.sh| 25 ++---
2 files changed, 94 insertions(+), 28 deletions(-)
diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c
index c0f7091..70b953f 100644
--- a/builtin/bisect--helper.c
+++ b/builtin/bisect--helper.c
@@ -22,9 +22,27 @@ static const char * const git_bisect_helper_usage[] = {
N_("git bisect--helper --write-terms "),
N_("git bisect--helper --bisect-clean-state"),
N_("git bisect--helper --bisect-reset []"),
+ N_("git bisect--helper --bisect-write
[]"),
NULL
};
+struct bisect_terms {
+ struct strbuf term_good;
+ struct strbuf term_bad;
+};
+
+static void bisect_terms_init(struct bisect_terms *terms)
+{
+ strbuf_init(>term_good, 0);
+ strbuf_init(>term_bad, 0);
+}
+
+static void bisect_terms_release(struct bisect_terms *terms)
+{
+ strbuf_release(>term_good);
+ strbuf_release(>term_bad);
+}
+
/*
* Check whether the string `term` belongs to the set of strings
* included in the variable arguments.
@@ -188,6 +206,52 @@ static int check_expected_revs(const char **revs, int
rev_nr)
return 0;
}
+static int bisect_write(const char *state, const char *rev,
+ const struct bisect_terms *terms, int nolog)
+{
+ struct strbuf tag = STRBUF_INIT;
+ struct strbuf commit_name = STRBUF_INIT;
+ struct object_id oid;
+ struct commit *commit;
+ struct pretty_print_context pp = {0};
+ FILE *fp;
+
+ if (!strcmp(state, terms->term_bad.buf))
+ strbuf_addf(, "refs/bisect/%s", state);
+ else if (one_of(state, terms->term_good.buf, "skip", NULL))
+ strbuf_addf(, "refs/bisect/%s-%s", state, rev);
+ else
+ return error(_("Bad bisect_write argument: %s"), state);
+
+ if (get_oid(rev, )) {
+ strbuf_release();
+ return error(_("couldn't get the oid of the rev '%s'"), rev);
+ }
+
+ if (update_ref(NULL, tag.buf, oid.hash, NULL, 0,
+ UPDATE_REFS_MSG_ON_ERR)) {
+ strbuf_release();
+ return -1;
+ }
+ strbuf_release();
+
+ fp = fopen(git_path_bisect_log(), "a");
+ if (!fp)
+ return error_errno(_("couldn't open the file '%s'"),
git_path_bisect_log());
+
+ commit = lookup_commit_reference(oid.hash);
+ format_commit_message(commit, "%s", _name, );
+ fprintf(fp, "# %s: [%s] %s\n", state, sha1_to_hex(oid.hash),
+ commit_name.buf);
+ strbuf_release(_name);
+
+ if (!nolog)
+ fprintf(fp, "git bisect %s %s\n", state, rev);
+
+ fclose(fp);
+ return 0;
+}
+
int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
{
enum {
@@ -195,9 +259,10 @@ int cmd_bisect__helper(int argc, const char **argv, const
char *prefix)
WRITE_TERMS,
BISECT_CLEAN_STATE,
BISECT_RESET,
- CHECK_EXPECTED_REVS
+ CHECK_EXPECTED_REVS,
+ BISECT_WRITE
} cmdmode = 0;
- int no_checkout = 0;
+ int no_checkout = 0, res = 0;
struct option options[] = {
OPT_CMDMODE(0, "next-all", ,
N_("perform 'git bisect next'"), NEXT_ALL),
@@ -209,10 +274,14 @@ int cmd_bisect__helper(int argc, const char **argv, const
char *prefix)
N_("reset the bisection state"), BISECT_RESET),
OPT_CMDMODE(0, "check-expected-revs", ,
N_("check for expected revs"), CHECK_EXPECTED_REVS),
+ OPT_CMDMODE(0, "bisect-write", ,
+N_("write out the bisection state in BISECT_LOG"),
BISECT_WRITE),
OPT_BOOL(0, "no-checkout", _checkout,
N_("update BISECT_HEAD instead of checking out the
current commit")),
OPT_END()
};
+