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 <Johannes.Schindelein@gmx.de> 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 4ba1e5c4144bd268dafb252fd652dd37d9e4a1ed
2 files changed +61 -30
builtin/bisect--helper.c
+59 -1
@@ -2,19 +2,73 @@
2 #include "cache.h"
3 #include "parse-options.h"
4 #include "bisect.h"
5 +#include "refs.h"
6
7 static const char * const git_bisect_helper_usage[] = {
8 N_("git bisect--helper --next-all [--no-checkout]"),
9 + N_("git bisect--helper --check-term-format <term> <orig_term>"),
10 NULL
11 };
12
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 +
60 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
61 {
13 - enum { NEXT_ALL = 1 } cmdmode = 0;
62 + enum {
63 + NEXT_ALL = 1,
64 + CHECK_TERM_FMT
65 + } cmdmode = 0;
66 int no_checkout = 0;
67 struct option options[] = {
68 OPT_CMDMODE(0, "next-all", &cmdmode,
69 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),
72 OPT_BOOL(0, "no-checkout", &no_checkout,
73 N_("update BISECT_HEAD instead of checking out the current commit")),
74 OPT_END()
@@ -29,6 +83,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
83 switch (cmdmode) {
84 case NEXT_ALL:
85 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]);
90 default:
91 return error("BUG: unknown subcommand '%d'", cmdmode);
92 }
git-bisect.sh
+2 -29
@@ -564,38 +564,11 @@ write_terms () {
564 then
565 die "$(gettext "please use two different terms")"
566 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
569 printf '%s\n%s\n' "$TERM_BAD" "$TERM_GOOD" >"$GIT_DIR/BISECT_TERMS"
570 }
571
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 -
572 check_and_set_terms () {
573 cmd="$1"
574 case "$cmd" in