3 #include "parse-options.h"
7 static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
9 static const char * const git_bisect_helper_usage[] = {
10 N_("git bisect--helper --next-all [--no-checkout]"),
11 N_("git bisect--helper --write-terms <bad_term> <good_term>"),
16 * Check whether the string `term` belongs to the set of strings
17 * included in the variable arguments.
20 static int one_of(const char *term, ...)
26 va_start(matches, term);
27 while (!res && (match = va_arg(matches, const char *)))
28 res = !strcmp(term, match);
34 static int check_term_format(const char *term, const char *orig_term)
37 char *new_term = xstrfmt("refs/bisect/%s", term);
39 res = check_refname_format(new_term, 0);
43 return error(_("'%s' is not a valid term"), term);
45 if (one_of(term, "help", "start", "skip", "next", "reset",
46 "visualize", "replay", "log", "run", "terms", NULL))
47 return error(_("can't use the builtin command '%s' as a term"), term);
50 * In theory, nothing prevents swapping completely good and bad,
51 * but this situation could be confusing and hasn't been tested
52 * enough. Forbid it for now.
55 if ((strcmp(orig_term, "bad") && one_of(term, "bad", "new", NULL)) ||
56 (strcmp(orig_term, "good") && one_of(term, "good", "old", NULL)))
57 return error(_("can't change the meaning of the term '%s'"), term);
62 static int write_terms(const char *bad, const char *good)
67 if (!strcmp(bad, good))
68 return error(_("please use two different terms"));
70 if (check_term_format(bad, "bad") || check_term_format(good, "good"))
73 fp = fopen(git_path_bisect_terms(), "w");
75 return error_errno(_("could not open the file BISECT_TERMS"));
77 res = fprintf(fp, "%s\n%s\n", bad, good);
79 return (res < 0) ? -1 : 0;
82 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
89 struct option options[] = {
90 OPT_CMDMODE(0, "next-all", &cmdmode,
91 N_("perform 'git bisect next'"), NEXT_ALL),
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")),
99 argc = parse_options(argc, argv, prefix, options,
100 git_bisect_helper_usage, 0);
103 usage_with_options(git_bisect_helper_usage, options);
107 return bisect_next_all(prefix, no_checkout);
110 return error(_("--write-terms requires two arguments"));
111 return write_terms(argv[0], argv[1]);
113 return error("BUG: unknown subcommand '%d'", cmdmode);