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>"),
12 N_("git bisect--helper --bisect-clean-state"),
17 * Check whether the string `term` belongs to the set of strings
18 * included in the variable arguments.
21 static int one_of(const char *term, ...)
27 va_start(matches, term);
28 while (!res && (match = va_arg(matches, const char *)))
29 res = !strcmp(term, match);
35 static int check_term_format(const char *term, const char *orig_term)
38 char *new_term = xstrfmt("refs/bisect/%s", term);
40 res = check_refname_format(new_term, 0);
44 return error(_("'%s' is not a valid term"), term);
46 if (one_of(term, "help", "start", "skip", "next", "reset",
47 "visualize", "replay", "log", "run", "terms", NULL))
48 return error(_("can't use the builtin command '%s' as a term"), term);
51 * In theory, nothing prevents swapping completely good and bad,
52 * but this situation could be confusing and hasn't been tested
53 * enough. Forbid it for now.
56 if ((strcmp(orig_term, "bad") && one_of(term, "bad", "new", NULL)) ||
57 (strcmp(orig_term, "good") && one_of(term, "good", "old", NULL)))
58 return error(_("can't change the meaning of the term '%s'"), term);
63 static int write_terms(const char *bad, const char *good)
68 if (!strcmp(bad, good))
69 return error(_("please use two different terms"));
71 if (check_term_format(bad, "bad") || check_term_format(good, "good"))
74 fp = fopen(git_path_bisect_terms(), "w");
76 return error_errno(_("could not open the file BISECT_TERMS"));
78 res = fprintf(fp, "%s\n%s\n", bad, good);
80 return (res < 0) ? -1 : 0;
83 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
91 struct option options[] = {
92 OPT_CMDMODE(0, "next-all", &cmdmode,
93 N_("perform 'git bisect next'"), NEXT_ALL),
94 OPT_CMDMODE(0, "write-terms", &cmdmode,
95 N_("write the terms to .git/BISECT_TERMS"), WRITE_TERMS),
96 OPT_CMDMODE(0, "bisect-clean-state", &cmdmode,
97 N_("cleanup the bisection state"), BISECT_CLEAN_STATE),
98 OPT_BOOL(0, "no-checkout", &no_checkout,
99 N_("update BISECT_HEAD instead of checking out the current commit")),
103 argc = parse_options(argc, argv, prefix, options,
104 git_bisect_helper_usage, 0);
107 usage_with_options(git_bisect_helper_usage, options);
111 return bisect_next_all(prefix, no_checkout);
114 return error(_("--write-terms requires two arguments"));
115 return write_terms(argv[0], argv[1]);
116 case BISECT_CLEAN_STATE:
118 return error(_("--bisect-clean-state requires no arguments"));
119 return bisect_clean_state();
121 return error("BUG: unknown subcommand '%d'", cmdmode);