Reimplement the `check_and_set_terms` shell function in C and add
`check-and-set-terms` subcommand to `git bisect--helper` to call it from
git-bisect.sh
Using `--check-and-set-terms` 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.
check_and_set_terms() sets and receives two global variables namely
TERM_GOOD and TERM_BAD in the shell script. Luckily the file BISECT_TERMS
also contains the value of those variables so its appropriate to evoke the
method get_terms() after calling the subcommand so that it retrieves the
value of TERM_GOOD and TERM_BAD from the file BISECT_TERMS. The two
global variables are passed as arguments to the subcommand.
Also introduce bisect_terms_reset() to empty the contents of `term_good`
and `term_bad` of `struct bisect_terms`.
Also introduce set_terms() to copy the `term_good` and `term_bad` into
`struct bisect_terms` and write it out to the file BISECT_TERMS.
Mentored-by: Lars Schneider
Mentored-by: Christian Couder
Signed-off-by: Pranit Bauva
---
builtin/bisect--helper.c | 52 +++-
git-bisect.sh| 36 -
2 files changed, 55 insertions(+), 33 deletions(-)
diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c
index 70b953f..99c9f90 100644
--- a/builtin/bisect--helper.c
+++ b/builtin/bisect--helper.c
@@ -23,6 +23,7 @@ static const char * const git_bisect_helper_usage[] = {
N_("git bisect--helper --bisect-clean-state"),
N_("git bisect--helper --bisect-reset []"),
N_("git bisect--helper --bisect-write
[]"),
+ N_("git bisect--helper --bisect-check-and-set-terms
"),
NULL
};
@@ -43,6 +44,12 @@ static void bisect_terms_release(struct bisect_terms *terms)
strbuf_release(>term_bad);
}
+static void bisect_terms_reset(struct bisect_terms *term)
+{
+ strbuf_reset(>term_good);
+ strbuf_reset(>term_bad);
+}
+
/*
* Check whether the string `term` belongs to the set of strings
* included in the variable arguments.
@@ -252,6 +259,39 @@ static int bisect_write(const char *state, const char *rev,
return 0;
}
+static int set_terms(struct bisect_terms *terms, const char *bad,
+const char *good)
+{
+ bisect_terms_reset(terms);
+ strbuf_addstr(>term_good, good);
+ strbuf_addstr(>term_bad, bad);
+ return write_terms(terms->term_bad.buf, terms->term_good.buf);
+}
+
+static int check_and_set_terms(struct bisect_terms *terms, const char *cmd)
+{
+ int no_term_file = is_empty_or_missing_file(git_path_bisect_terms());
+
+ if (one_of(cmd, "skip", "start", "terms", NULL))
+ return 0;
+
+ if (!no_term_file &&
+ strcmp(cmd, terms->term_bad.buf) &&
+ strcmp(cmd, terms->term_good.buf))
+ return error(_("Invalid command: you're currently in a "
+ "'%s' '%s' bisect"), terms->term_bad.buf,
+ terms->term_good.buf);
+
+ if (no_term_file) {
+ if (one_of(cmd, "bad", "good", NULL))
+ return set_terms(terms, "bad", "good");
+ if (one_of(cmd, "new", "old", NULL))
+ return set_terms(terms, "new", "old");
+ }
+
+ return 0;
+}
+
int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
{
enum {
@@ -260,7 +300,8 @@ int cmd_bisect__helper(int argc, const char **argv, const
char *prefix)
BISECT_CLEAN_STATE,
BISECT_RESET,
CHECK_EXPECTED_REVS,
- BISECT_WRITE
+ BISECT_WRITE,
+ CHECK_AND_SET_TERMS
} cmdmode = 0;
int no_checkout = 0, res = 0;
struct option options[] = {
@@ -276,6 +317,8 @@ int cmd_bisect__helper(int argc, const char **argv, const
char *prefix)
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_CMDMODE(0, "check-and-set-terms", ,
+N_("check and set terms in a bisection state"),
CHECK_AND_SET_TERMS),
OPT_BOOL(0, "no-checkout", _checkout,
N_("update BISECT_HEAD instead of checking out the
current commit")),
OPT_END()
@@ -319,6 +362,13 @@ int cmd_bisect__helper(int argc, const char **argv, const
char *prefix)
strbuf_addstr(_bad, argv[3]);
res = bisect_write(argv[0], argv[1], , nolog);
break;
+ case CHECK_AND_SET_TERMS:
+ if (argc != 3)
+