bisect--helper: `write_terms` shell function in C

Reimplement the `write_terms` shell function in C and add a `write-terms` subcommand to `git bisect--helper` to call it from git-bisect.sh . Also remove the subcommand `--check-term-format` as it can now be called from inside the function write_terms() C implementation. Also `|| exit` is added when calling write-terms subcommand from git-bisect.sh so as to exit whenever there is an error. Using `--write-terms` subcommand is a temporary measure to port shell function to C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and its implementation will be called by some other method. Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Pranit Bauva committed Sep 29, 2017 at 06:49 UTC ecb3f3733cbeaf514508f97429863d33a6ac0d57
2 files changed +36 -22
builtin/bisect--helper.c
+29 -7
@@ -4,9 +4,11 @@
4 #include "bisect.h"
5 #include "refs.h"
6
7 +static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
8 +
9 static const char * const git_bisect_helper_usage[] = {
10 N_("git bisect--helper --next-all [--no-checkout]"),
9 - N_("git bisect--helper --check-term-format <term> <orig_term>"),
11 + N_("git bisect--helper --write-terms <bad_term> <good_term>"),
12 NULL
13 };
14
@@ -57,18 +59,38 @@ static int check_term_format(const char *term, const char *orig_term)
59 return 0;
60 }
61
62 +static int write_terms(const char *bad, const char *good)
63 +{
64 + FILE *fp = NULL;
65 + int res;
66 +
67 + if (!strcmp(bad, good))
68 + return error(_("please use two different terms"));
69 +
70 + if (check_term_format(bad, "bad") || check_term_format(good, "good"))
71 + return -1;
72 +
73 + fp = fopen(git_path_bisect_terms(), "w");
74 + if (!fp)
75 + return error_errno(_("could not open the file BISECT_TERMS"));
76 +
77 + res = fprintf(fp, "%s\n%s\n", bad, good);
78 + res |= fclose(fp);
79 + return (res < 0) ? -1 : 0;
80 +}
81 +
82 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
83 {
84 enum {
85 NEXT_ALL = 1,
64 - CHECK_TERM_FMT
86 + WRITE_TERMS
87 } cmdmode = 0;
88 int no_checkout = 0;
89 struct option options[] = {
90 OPT_CMDMODE(0, "next-all", &cmdmode,
91 N_("perform 'git bisect next'"), NEXT_ALL),
70 - OPT_CMDMODE(0, "check-term-format", &cmdmode,
71 - N_("check format of the term"), CHECK_TERM_FMT),
92 + OPT_CMDMODE(0, "write-terms", &cmdmode,
93 + N_("write the terms to .git/BISECT_TERMS"), WRITE_TERMS),
94 OPT_BOOL(0, "no-checkout", &no_checkout,
95 N_("update BISECT_HEAD instead of checking out the current commit")),
96 OPT_END()
@@ -83,10 +105,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
105 switch (cmdmode) {
106 case NEXT_ALL:
107 return bisect_next_all(prefix, no_checkout);
86 - case CHECK_TERM_FMT:
108 + case WRITE_TERMS:
109 if (argc != 2)
88 - return error(_("--check-term-format requires two arguments"));
89 - return check_term_format(argv[0], argv[1]);
110 + return error(_("--write-terms requires two arguments"));
111 + return write_terms(argv[0], argv[1]);
112 default:
113 return error("BUG: unknown subcommand '%d'", cmdmode);
114 }
git-bisect.sh
+7 -15
@@ -209,7 +209,7 @@ bisect_start() {
209 eval "$eval true" &&
210 if test $must_write_terms -eq 1
211 then
212 - write_terms "$TERM_BAD" "$TERM_GOOD"
212 + git bisect--helper --write-terms "$TERM_BAD" "$TERM_GOOD" || exit
213 fi &&
214 echo "git bisect start$orig_args" >>"$GIT_DIR/BISECT_LOG" || exit
215 #
@@ -557,18 +557,6 @@ get_terms () {
557 fi
558 }
559
560 -write_terms () {
561 - TERM_BAD=$1
562 - TERM_GOOD=$2
563 - if test "$TERM_BAD" = "$TERM_GOOD"
564 - then
565 - die "$(gettext "please use two different terms")"
566 - fi
567 - git bisect--helper --check-term-format "$TERM_BAD" bad || exit
568 - git bisect--helper --check-term-format "$TERM_GOOD" good || exit
569 - printf '%s\n%s\n' "$TERM_BAD" "$TERM_GOOD" >"$GIT_DIR/BISECT_TERMS"
570 -}
571 -
560 check_and_set_terms () {
561 cmd="$1"
562 case "$cmd" in
@@ -582,13 +570,17 @@ check_and_set_terms () {
570 bad|good)
571 if ! test -s "$GIT_DIR/BISECT_TERMS"
572 then
585 - write_terms bad good
573 + TERM_BAD=bad
574 + TERM_GOOD=good
575 + git bisect--helper --write-terms "$TERM_BAD" "$TERM_GOOD" || exit
576 fi
577 ;;
578 new|old)
579 if ! test -s "$GIT_DIR/BISECT_TERMS"
580 then
591 - write_terms new old
581 + TERM_BAD=new
582 + TERM_GOOD=old
583 + git bisect--helper --write-terms "$TERM_BAD" "$TERM_GOOD" || exit
584 fi
585 ;;
586 esac ;;