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 <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Mentored-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> Signed-off-by: Tanushree Tumane <tanushreetumane@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Pranit Bauva committed Jan 2, 2019 at 07:38 UTC 4fbdbd5bfff874330115f8d7556cf7c9f7f138c9
2 files changed +42 -33
builtin/bisect--helper.c
+38 -1
@@ -20,6 +20,7 @@ static const char * const git_bisect_helper_usage[] = {
20 N_("git bisect--helper --bisect-clean-state"),
21 N_("git bisect--helper --bisect-reset [<commit>]"),
22 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>"),
24 NULL
25 };
26
@@ -234,6 +235,33 @@ finish:
235 return retval;
236 }
237
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 +
265 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
266 {
267 enum {
@@ -242,7 +270,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
270 BISECT_CLEAN_STATE,
271 CHECK_EXPECTED_REVS,
272 BISECT_RESET,
245 - BISECT_WRITE
273 + BISECT_WRITE,
274 + CHECK_AND_SET_TERMS
275 } cmdmode = 0;
276 int no_checkout = 0, res = 0, nolog = 0;
277 struct option options[] = {
@@ -258,6 +287,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
287 N_("reset the bisection state"), BISECT_RESET),
288 OPT_CMDMODE(0, "bisect-write", &cmdmode,
289 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),
292 OPT_BOOL(0, "no-checkout", &no_checkout,
293 N_("update BISECT_HEAD instead of checking out the current commit")),
294 OPT_BOOL(0, "no-log", &nolog,
@@ -296,6 +327,12 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
327 set_terms(&terms, argv[3], argv[2]);
328 res = bisect_write(argv[0], argv[1], &terms, nolog);
329 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;
336 default:
337 return error("BUG: unknown subcommand '%d'", cmdmode);
338 }
git-bisect.sh
+4 -32
@@ -238,7 +238,8 @@ bisect_skip() {
238 bisect_state() {
239 bisect_autostart
240 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
243 case "$#,$state" in
244 0,*)
245 die "Please call 'bisect_state' with at least one argument." ;;
@@ -390,7 +391,8 @@ bisect_replay () {
391 command="$bisect"
392 fi
393 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
396 case "$command" in
397 start)
398 cmd="bisect_start $rev"
@@ -482,36 +484,6 @@ get_terms () {
484 fi
485 }
486
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 -
487 bisect_voc () {
488 case "$1" in
489 bad) echo "bad|new" ;;