Reimplement the `get_terms` and `bisect_terms` shell function in C and add `bisect-terms` subcommand to `git bisect--helper` to call it from git-bisect.sh . Using `--bisect-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. Also use error() to report "no terms defined" and accordingly change the test in t6030. Mentored-by: Lars Schneider <larsxschneider@xxxxxxxxx> Mentored-by: Christian Couder <chriscool@xxxxxxxxxxxxx> Signed-off-by: Pranit Bauva <pranit.bauva@xxxxxxxxx> --- builtin/bisect--helper.c | 69 +++++++++++++++++++++++++++++++++++++++++++-- git-bisect.sh | 35 ++--------------------- t/t6030-bisect-porcelain.sh | 2 +- 3 files changed, 70 insertions(+), 36 deletions(-) diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c index 0f9c3e63821b8..ab0580ce0089a 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-write <state> <revision> <good_term> <bad_term> [<nolog>]"), N_("git bisect--helper --bisect-check-and-set-terms <command> <good_term> <bad_term>"), N_("git bisect--helper --bisect-next-check [<term>] <good_term> <bad_term>"), + N_("git bisect--helper --bisect-terms [--term-good | --term-old | --term-bad | --term-new]"), NULL }; @@ -344,6 +345,62 @@ static int bisect_next_check(const struct bisect_terms *terms, return retval; } +static int get_terms(struct bisect_terms *terms) +{ + struct strbuf str = STRBUF_INIT; + FILE *fp = NULL; + int res = 0; + + fp = fopen(git_path_bisect_terms(), "r"); + if (!fp) + goto fail; + + free_terms(terms); + strbuf_getline_lf(&str, fp); + terms->term_bad = strbuf_detach(&str, NULL); + strbuf_getline_lf(&str, fp); + terms->term_good = strbuf_detach(&str, NULL); + goto finish; + +fail: + res = -1; +finish: + if (fp) + fclose(fp); + strbuf_release(&str); + return res; +} + +static int bisect_terms(struct bisect_terms *terms, const char **argv, int argc) +{ + int i; + + if (get_terms(terms)) + return error(_("no terms defined")); + + if (argc > 1) + return error(_("--bisect-term requires exactly one argument")); + + if (argc == 0) + return !printf(_("Your current terms are %s for the old state\n" + "and %s for the new state.\n"), + terms->term_good, terms->term_bad); + + for (i = 0; i < argc; i++) { + if (!strcmp(argv[i], "--term-good")) + printf(_("%s\n"), terms->term_good); + else if (!strcmp(argv[i], "--term-bad")) + printf(_("%s\n"), terms->term_bad); + else + error(_("BUG: invalid argument %s for 'git bisect terms'.\n" + "Supported options are: " + "--term-good|--term-old and " + "--term-bad|--term-new."), argv[i]); + } + + return 0; +} + int cmd_bisect__helper(int argc, const char **argv, const char *prefix) { enum { @@ -354,7 +411,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) BISECT_RESET, BISECT_WRITE, CHECK_AND_SET_TERMS, - BISECT_NEXT_CHECK + BISECT_NEXT_CHECK, + BISECT_TERMS } cmdmode = 0; int no_checkout = 0, res = 0; struct option options[] = { @@ -374,6 +432,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) N_("check and set terms in a bisection state"), CHECK_AND_SET_TERMS), OPT_CMDMODE(0, "bisect-next-check", &cmdmode, N_("check whether bad or good terms exist"), BISECT_NEXT_CHECK), + OPT_CMDMODE(0, "bisect-terms", &cmdmode, + N_("print out the bisect terms"), BISECT_TERMS), OPT_BOOL(0, "no-checkout", &no_checkout, N_("update BISECT_HEAD instead of checking out the current commit")), OPT_END() @@ -381,7 +441,7 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) struct bisect_terms terms; argc = parse_options(argc, argv, prefix, options, - git_bisect_helper_usage, 0); + git_bisect_helper_usage, PARSE_OPT_KEEP_UNKNOWN); if (!cmdmode) usage_with_options(git_bisect_helper_usage, options); @@ -424,6 +484,11 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix) set_terms(&terms, argv[1], argv[0]); res = bisect_next_check(&terms, argc == 3 ? argv[2] : NULL); break; + case BISECT_TERMS: + if (argc > 1) + return error(_("--bisect-terms requires 0 or 1 argument")); + res = bisect_terms(&terms, argv, argc); + break; default: return error("BUG: unknown subcommand '%d'", cmdmode); } diff --git a/git-bisect.sh b/git-bisect.sh index c21fafc62d71f..1bc5749f78484 100755 --- a/git-bisect.sh +++ b/git-bisect.sh @@ -355,7 +355,7 @@ bisect_replay () { "$TERM_GOOD"|"$TERM_BAD"|skip) git bisect--helper --bisect-write "$command" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit;; terms) - bisect_terms $rev ;; + git bisect--helper --bisect-terms $rev || exit;; *) die "$(gettext "?? what are you talking about?")" ;; esac @@ -437,37 +437,6 @@ get_terms () { fi } -bisect_terms () { - get_terms - if ! test -s "$GIT_DIR/BISECT_TERMS" - then - die "$(gettext "no terms defined")" - fi - case "$#" in - 0) - gettextln "Your current terms are $TERM_GOOD for the old state -and $TERM_BAD for the new state." - ;; - 1) - arg=$1 - case "$arg" in - --term-good|--term-old) - printf '%s\n' "$TERM_GOOD" - ;; - --term-bad|--term-new) - printf '%s\n' "$TERM_BAD" - ;; - *) - die "$(eval_gettext "invalid argument \$arg for 'git bisect terms'. -Supported options are: --term-good|--term-old and --term-bad|--term-new.")" - ;; - esac - ;; - *) - usage ;; - esac -} - case "$#" in 0) usage ;; @@ -498,7 +467,7 @@ case "$#" in run) bisect_run "$@" ;; terms) - bisect_terms "$@" ;; + git bisect--helper --bisect-terms "$@" || exit;; *) usage ;; esac diff --git a/t/t6030-bisect-porcelain.sh b/t/t6030-bisect-porcelain.sh index f84ff941c3624..55835ee4a4715 100755 --- a/t/t6030-bisect-porcelain.sh +++ b/t/t6030-bisect-porcelain.sh @@ -802,7 +802,7 @@ test_expect_success 'bisect terms needs 0 or 1 argument' ' test_must_fail git bisect terms only-one && test_must_fail git bisect terms 1 2 && test_must_fail git bisect terms 2>actual && - echo "no terms defined" >expected && + echo "error: no terms defined" >expected && test_i18ncmp expected actual ' -- https://github.com/git/git/pull/420