Skip to content

Commit 4ba1e5c

Browse files
pranitbauva1997gitster
authored andcommitted
bisect--helper: rewrite check_term_format shell function in C
Reimplement the `check_term_format` shell function in C and add a `--check-term-format` subcommand to `git bisect--helper` to call it from git-bisect.sh Using `--check-term-format` 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/subcommand. For eg. In conversion of write_terms() of git-bisect.sh, the subcommand will be removed and instead check_term_format() will be called in its C implementation while a new subcommand will be introduced for write_terms(). Helped-by: Johannes Schindelein <[email protected]> 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 9e1c84d commit 4ba1e5c

File tree

2 files changed

+61
-30
lines changed

2 files changed

+61
-30
lines changed

builtin/bisect--helper.c

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,73 @@
22
#include "cache.h"
33
#include "parse-options.h"
44
#include "bisect.h"
5+
#include "refs.h"
56

67
static const char * const git_bisect_helper_usage[] = {
78
N_("git bisect--helper --next-all [--no-checkout]"),
9+
N_("git bisect--helper --check-term-format <term> <orig_term>"),
810
NULL
911
};
1012

13+
/*
14+
* Check whether the string `term` belongs to the set of strings
15+
* included in the variable arguments.
16+
*/
17+
LAST_ARG_MUST_BE_NULL
18+
static int one_of(const char *term, ...)
19+
{
20+
int res = 0;
21+
va_list matches;
22+
const char *match;
23+
24+
va_start(matches, term);
25+
while (!res && (match = va_arg(matches, const char *)))
26+
res = !strcmp(term, match);
27+
va_end(matches);
28+
29+
return res;
30+
}
31+
32+
static int check_term_format(const char *term, const char *orig_term)
33+
{
34+
int res;
35+
char *new_term = xstrfmt("refs/bisect/%s", term);
36+
37+
res = check_refname_format(new_term, 0);
38+
free(new_term);
39+
40+
if (res)
41+
return error(_("'%s' is not a valid term"), term);
42+
43+
if (one_of(term, "help", "start", "skip", "next", "reset",
44+
"visualize", "replay", "log", "run", "terms", NULL))
45+
return error(_("can't use the builtin command '%s' as a term"), term);
46+
47+
/*
48+
* In theory, nothing prevents swapping completely good and bad,
49+
* but this situation could be confusing and hasn't been tested
50+
* enough. Forbid it for now.
51+
*/
52+
53+
if ((strcmp(orig_term, "bad") && one_of(term, "bad", "new", NULL)) ||
54+
(strcmp(orig_term, "good") && one_of(term, "good", "old", NULL)))
55+
return error(_("can't change the meaning of the term '%s'"), term);
56+
57+
return 0;
58+
}
59+
1160
int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
1261
{
13-
enum { NEXT_ALL = 1 } cmdmode = 0;
62+
enum {
63+
NEXT_ALL = 1,
64+
CHECK_TERM_FMT
65+
} cmdmode = 0;
1466
int no_checkout = 0;
1567
struct option options[] = {
1668
OPT_CMDMODE(0, "next-all", &cmdmode,
1769
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),
1872
OPT_BOOL(0, "no-checkout", &no_checkout,
1973
N_("update BISECT_HEAD instead of checking out the current commit")),
2074
OPT_END()
@@ -29,6 +83,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
2983
switch (cmdmode) {
3084
case NEXT_ALL:
3185
return bisect_next_all(prefix, no_checkout);
86+
case CHECK_TERM_FMT:
87+
if (argc != 2)
88+
return error(_("--check-term-format requires two arguments"));
89+
return check_term_format(argv[0], argv[1]);
3290
default:
3391
return error("BUG: unknown subcommand '%d'", cmdmode);
3492
}

git-bisect.sh

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -564,38 +564,11 @@ write_terms () {
564564
then
565565
die "$(gettext "please use two different terms")"
566566
fi
567-
check_term_format "$TERM_BAD" bad
568-
check_term_format "$TERM_GOOD" good
567+
git bisect--helper --check-term-format "$TERM_BAD" bad || exit
568+
git bisect--helper --check-term-format "$TERM_GOOD" good || exit
569569
printf '%s\n%s\n' "$TERM_BAD" "$TERM_GOOD" >"$GIT_DIR/BISECT_TERMS"
570570
}
571571

572-
check_term_format () {
573-
term=$1
574-
git check-ref-format refs/bisect/"$term" ||
575-
die "$(eval_gettext "'\$term' is not a valid term")"
576-
case "$term" in
577-
help|start|terms|skip|next|reset|visualize|replay|log|run)
578-
die "$(eval_gettext "can't use the builtin command '\$term' as a term")"
579-
;;
580-
bad|new)
581-
if test "$2" != bad
582-
then
583-
# In theory, nothing prevents swapping
584-
# completely good and bad, but this situation
585-
# could be confusing and hasn't been tested
586-
# enough. Forbid it for now.
587-
die "$(eval_gettext "can't change the meaning of term '\$term'")"
588-
fi
589-
;;
590-
good|old)
591-
if test "$2" != good
592-
then
593-
die "$(eval_gettext "can't change the meaning of term '\$term'")"
594-
fi
595-
;;
596-
esac
597-
}
598-
599572
check_and_set_terms () {
600573
cmd="$1"
601574
case "$cmd" in

0 commit comments

Comments
 (0)