Skip to content

Commit ecb3f37

Browse files
pranitbauva1997gitster
authored andcommitted
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 <[email protected]> Mentored-by: Christian Couder <[email protected]> Signed-off-by: Pranit Bauva <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4ba1e5c commit ecb3f37

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

builtin/bisect--helper.c

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
#include "bisect.h"
55
#include "refs.h"
66

7+
static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
8+
79
static const char * const git_bisect_helper_usage[] = {
810
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>"),
1012
NULL
1113
};
1214

@@ -57,18 +59,38 @@ static int check_term_format(const char *term, const char *orig_term)
5759
return 0;
5860
}
5961

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+
6082
int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
6183
{
6284
enum {
6385
NEXT_ALL = 1,
64-
CHECK_TERM_FMT
86+
WRITE_TERMS
6587
} cmdmode = 0;
6688
int no_checkout = 0;
6789
struct option options[] = {
6890
OPT_CMDMODE(0, "next-all", &cmdmode,
6991
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),
7294
OPT_BOOL(0, "no-checkout", &no_checkout,
7395
N_("update BISECT_HEAD instead of checking out the current commit")),
7496
OPT_END()
@@ -83,10 +105,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
83105
switch (cmdmode) {
84106
case NEXT_ALL:
85107
return bisect_next_all(prefix, no_checkout);
86-
case CHECK_TERM_FMT:
108+
case WRITE_TERMS:
87109
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]);
90112
default:
91113
return error("BUG: unknown subcommand '%d'", cmdmode);
92114
}

git-bisect.sh

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ bisect_start() {
209209
eval "$eval true" &&
210210
if test $must_write_terms -eq 1
211211
then
212-
write_terms "$TERM_BAD" "$TERM_GOOD"
212+
git bisect--helper --write-terms "$TERM_BAD" "$TERM_GOOD" || exit
213213
fi &&
214214
echo "git bisect start$orig_args" >>"$GIT_DIR/BISECT_LOG" || exit
215215
#
@@ -557,18 +557,6 @@ get_terms () {
557557
fi
558558
}
559559

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-
572560
check_and_set_terms () {
573561
cmd="$1"
574562
case "$cmd" in
@@ -582,13 +570,17 @@ check_and_set_terms () {
582570
bad|good)
583571
if ! test -s "$GIT_DIR/BISECT_TERMS"
584572
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
586576
fi
587577
;;
588578
new|old)
589579
if ! test -s "$GIT_DIR/BISECT_TERMS"
590580
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
592584
fi
593585
;;
594586
esac ;;

0 commit comments

Comments
 (0)