Skip to content

Commit 4fbdbd5

Browse files
pranitbauva1997gitster
authored andcommitted
bisect--helper: check_and_set_terms shell function in C
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 but its implementation 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. Mentored-by: Lars Schneider <[email protected]> Mentored-by: Christian Couder <[email protected]> Mentored-by: Johannes Schindelin <[email protected]> Signed-off-by: Pranit Bauva <[email protected]> Signed-off-by: Tanushree Tumane <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e3b1e3b commit 4fbdbd5

File tree

2 files changed

+42
-33
lines changed

2 files changed

+42
-33
lines changed

builtin/bisect--helper.c

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ static const char * const git_bisect_helper_usage[] = {
2020
N_("git bisect--helper --bisect-clean-state"),
2121
N_("git bisect--helper --bisect-reset [<commit>]"),
2222
N_("git bisect--helper --bisect-write [--no-log] <state> <revision> <good_term> <bad_term>"),
23+
N_("git bisect--helper --bisect-check-and-set-terms <command> <good_term> <bad_term>"),
2324
NULL
2425
};
2526

@@ -234,6 +235,33 @@ static int bisect_write(const char *state, const char *rev,
234235
return retval;
235236
}
236237

238+
static int check_and_set_terms(struct bisect_terms *terms, const char *cmd)
239+
{
240+
int has_term_file = !is_empty_or_missing_file(git_path_bisect_terms());
241+
242+
if (one_of(cmd, "skip", "start", "terms", NULL))
243+
return 0;
244+
245+
if (has_term_file && strcmp(cmd, terms->term_bad) &&
246+
strcmp(cmd, terms->term_good))
247+
return error(_("Invalid command: you're currently in a "
248+
"%s/%s bisect"), terms->term_bad,
249+
terms->term_good);
250+
251+
if (!has_term_file) {
252+
if (one_of(cmd, "bad", "good", NULL)) {
253+
set_terms(terms, "bad", "good");
254+
return write_terms(terms->term_bad, terms->term_good);
255+
}
256+
if (one_of(cmd, "new", "old", NULL)) {
257+
set_terms(terms, "new", "old");
258+
return write_terms(terms->term_bad, terms->term_good);
259+
}
260+
}
261+
262+
return 0;
263+
}
264+
237265
int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
238266
{
239267
enum {
@@ -242,7 +270,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
242270
BISECT_CLEAN_STATE,
243271
CHECK_EXPECTED_REVS,
244272
BISECT_RESET,
245-
BISECT_WRITE
273+
BISECT_WRITE,
274+
CHECK_AND_SET_TERMS
246275
} cmdmode = 0;
247276
int no_checkout = 0, res = 0, nolog = 0;
248277
struct option options[] = {
@@ -258,6 +287,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
258287
N_("reset the bisection state"), BISECT_RESET),
259288
OPT_CMDMODE(0, "bisect-write", &cmdmode,
260289
N_("write out the bisection state in BISECT_LOG"), BISECT_WRITE),
290+
OPT_CMDMODE(0, "check-and-set-terms", &cmdmode,
291+
N_("check and set terms in a bisection state"), CHECK_AND_SET_TERMS),
261292
OPT_BOOL(0, "no-checkout", &no_checkout,
262293
N_("update BISECT_HEAD instead of checking out the current commit")),
263294
OPT_BOOL(0, "no-log", &nolog,
@@ -296,6 +327,12 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
296327
set_terms(&terms, argv[3], argv[2]);
297328
res = bisect_write(argv[0], argv[1], &terms, nolog);
298329
break;
330+
case CHECK_AND_SET_TERMS:
331+
if (argc != 3)
332+
return error(_("--check-and-set-terms requires 3 arguments"));
333+
set_terms(&terms, argv[2], argv[1]);
334+
res = check_and_set_terms(&terms, argv[0]);
335+
break;
299336
default:
300337
return error("BUG: unknown subcommand '%d'", cmdmode);
301338
}

git-bisect.sh

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ bisect_skip() {
238238
bisect_state() {
239239
bisect_autostart
240240
state=$1
241-
check_and_set_terms $state
241+
git bisect--helper --check-and-set-terms $state $TERM_GOOD $TERM_BAD || exit
242+
get_terms
242243
case "$#,$state" in
243244
0,*)
244245
die "Please call 'bisect_state' with at least one argument." ;;
@@ -390,7 +391,8 @@ bisect_replay () {
390391
command="$bisect"
391392
fi
392393
get_terms
393-
check_and_set_terms "$command"
394+
git bisect--helper --check-and-set-terms "$command" "$TERM_GOOD" "$TERM_BAD" || exit
395+
get_terms
394396
case "$command" in
395397
start)
396398
cmd="bisect_start $rev"
@@ -482,36 +484,6 @@ get_terms () {
482484
fi
483485
}
484486

485-
check_and_set_terms () {
486-
cmd="$1"
487-
case "$cmd" in
488-
skip|start|terms) ;;
489-
*)
490-
if test -s "$GIT_DIR/BISECT_TERMS" && test "$cmd" != "$TERM_BAD" && test "$cmd" != "$TERM_GOOD"
491-
then
492-
die "$(eval_gettext "Invalid command: you're currently in a \$TERM_BAD/\$TERM_GOOD bisect.")"
493-
fi
494-
case "$cmd" in
495-
bad|good)
496-
if ! test -s "$GIT_DIR/BISECT_TERMS"
497-
then
498-
TERM_BAD=bad
499-
TERM_GOOD=good
500-
git bisect--helper --write-terms "$TERM_BAD" "$TERM_GOOD" || exit
501-
fi
502-
;;
503-
new|old)
504-
if ! test -s "$GIT_DIR/BISECT_TERMS"
505-
then
506-
TERM_BAD=new
507-
TERM_GOOD=old
508-
git bisect--helper --write-terms "$TERM_BAD" "$TERM_GOOD" || exit
509-
fi
510-
;;
511-
esac ;;
512-
esac
513-
}
514-
515487
bisect_voc () {
516488
case "$1" in
517489
bad) echo "bad|new" ;;

0 commit comments

Comments
 (0)