3 #include "parse-options.h"
7 #include "argv-array.h"
8 #include "run-command.h"
11 static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
12 static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
13 static GIT_PATH_FUNC(git_path_bisect_ancestors_ok, "BISECT_ANCESTORS_OK")
14 static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START")
15 static GIT_PATH_FUNC(git_path_bisect_head, "BISECT_HEAD")
16 static GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG")
18 static const char * const git_bisect_helper_usage[] = {
19 N_("git bisect--helper --next-all [--no-checkout]"),
20 N_("git bisect--helper --write-terms <bad_term> <good_term>"),
21 N_("git bisect--helper --bisect-clean-state"),
22 N_("git bisect--helper --bisect-reset [<commit>]"),
23 N_("git bisect--helper --bisect-write [--no-log] <state> <revision> <good_term> <bad_term>"),
24 N_("git bisect--helper --bisect-check-and-set-terms <command> <good_term> <bad_term>"),
25 N_("git bisect--helper --bisect-next-check <good_term> <bad_term> [<term>]"),
34 static void free_terms(struct bisect_terms *terms)
36 FREE_AND_NULL(terms->term_good);
37 FREE_AND_NULL(terms->term_bad);
40 static void set_terms(struct bisect_terms *terms, const char *bad,
43 free((void *)terms->term_good);
44 terms->term_good = xstrdup(good);
45 free((void *)terms->term_bad);
46 terms->term_bad = xstrdup(bad);
49 static const char *vocab_bad = "bad|new";
50 static const char *vocab_good = "good|old";
53 * Check whether the string `term` belongs to the set of strings
54 * included in the variable arguments.
57 static int one_of(const char *term, ...)
63 va_start(matches, term);
64 while (!res && (match = va_arg(matches, const char *)))
65 res = !strcmp(term, match);
71 static int check_term_format(const char *term, const char *orig_term)
74 char *new_term = xstrfmt("refs/bisect/%s", term);
76 res = check_refname_format(new_term, 0);
80 return error(_("'%s' is not a valid term"), term);
82 if (one_of(term, "help", "start", "skip", "next", "reset",
83 "visualize", "view", "replay", "log", "run", "terms", NULL))
84 return error(_("can't use the builtin command '%s' as a term"), term);
87 * In theory, nothing prevents swapping completely good and bad,
88 * but this situation could be confusing and hasn't been tested
89 * enough. Forbid it for now.
92 if ((strcmp(orig_term, "bad") && one_of(term, "bad", "new", NULL)) ||
93 (strcmp(orig_term, "good") && one_of(term, "good", "old", NULL)))
94 return error(_("can't change the meaning of the term '%s'"), term);
99 static int write_terms(const char *bad, const char *good)
104 if (!strcmp(bad, good))
105 return error(_("please use two different terms"));
107 if (check_term_format(bad, "bad") || check_term_format(good, "good"))
110 fp = fopen(git_path_bisect_terms(), "w");
112 return error_errno(_("could not open the file BISECT_TERMS"));
114 res = fprintf(fp, "%s\n%s\n", bad, good);
116 return (res < 0) ? -1 : 0;
119 static int is_expected_rev(const char *expected_hex)
121 struct strbuf actual_hex = STRBUF_INIT;
123 if (strbuf_read_file(&actual_hex, git_path_bisect_expected_rev(), 0) >= 40) {
124 strbuf_trim(&actual_hex);
125 res = !strcmp(actual_hex.buf, expected_hex);
127 strbuf_release(&actual_hex);
131 static void check_expected_revs(const char **revs, int rev_nr)
135 for (i = 0; i < rev_nr; i++) {
136 if (!is_expected_rev(revs[i])) {
137 unlink_or_warn(git_path_bisect_ancestors_ok());
138 unlink_or_warn(git_path_bisect_expected_rev());
143 static int bisect_reset(const char *commit)
145 struct strbuf branch = STRBUF_INIT;
148 if (strbuf_read_file(&branch, git_path_bisect_start(), 0) < 1) {
149 printf(_("We are not bisecting.\n"));
152 strbuf_rtrim(&branch);
154 struct object_id oid;
156 if (get_oid_commit(commit, &oid))
157 return error(_("'%s' is not a valid commit"), commit);
158 strbuf_addstr(&branch, commit);
161 if (!file_exists(git_path_bisect_head())) {
162 struct argv_array argv = ARGV_ARRAY_INIT;
164 argv_array_pushl(&argv, "checkout", branch.buf, "--", NULL);
165 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
166 strbuf_release(&branch);
167 argv_array_clear(&argv);
168 return error(_("could not check out original"
169 " HEAD '%s'. Try 'git bisect"
170 "reset <commit>'."), branch.buf);
172 argv_array_clear(&argv);
175 strbuf_release(&branch);
176 return bisect_clean_state();
179 static void log_commit(FILE *fp, char *fmt, const char *state,
180 struct commit *commit)
182 struct pretty_print_context pp = {0};
183 struct strbuf commit_msg = STRBUF_INIT;
184 char *label = xstrfmt(fmt, state);
186 format_commit_message(commit, "%s", &commit_msg, &pp);
188 fprintf(fp, "# %s: [%s] %s\n", label, oid_to_hex(&commit->object.oid),
191 strbuf_release(&commit_msg);
195 static int bisect_write(const char *state, const char *rev,
196 const struct bisect_terms *terms, int nolog)
198 struct strbuf tag = STRBUF_INIT;
199 struct object_id oid;
200 struct commit *commit;
204 if (!strcmp(state, terms->term_bad)) {
205 strbuf_addf(&tag, "refs/bisect/%s", state);
206 } else if (one_of(state, terms->term_good, "skip", NULL)) {
207 strbuf_addf(&tag, "refs/bisect/%s-%s", state, rev);
209 retval = error(_("Bad bisect_write argument: %s"), state);
213 if (get_oid(rev, &oid)) {
214 retval = error(_("couldn't get the oid of the rev '%s'"), rev);
218 if (update_ref(NULL, tag.buf, &oid, NULL, 0,
219 UPDATE_REFS_MSG_ON_ERR)) {
224 fp = fopen(git_path_bisect_log(), "a");
226 retval = error_errno(_("couldn't open the file '%s'"), git_path_bisect_log());
230 commit = lookup_commit_reference(the_repository, &oid);
231 log_commit(fp, "%s", state, commit);
234 fprintf(fp, "git bisect %s %s\n", state, rev);
239 strbuf_release(&tag);
243 static int check_and_set_terms(struct bisect_terms *terms, const char *cmd)
245 int has_term_file = !is_empty_or_missing_file(git_path_bisect_terms());
247 if (one_of(cmd, "skip", "start", "terms", NULL))
250 if (has_term_file && strcmp(cmd, terms->term_bad) &&
251 strcmp(cmd, terms->term_good))
252 return error(_("Invalid command: you're currently in a "
253 "%s/%s bisect"), terms->term_bad,
256 if (!has_term_file) {
257 if (one_of(cmd, "bad", "good", NULL)) {
258 set_terms(terms, "bad", "good");
259 return write_terms(terms->term_bad, terms->term_good);
261 if (one_of(cmd, "new", "old", NULL)) {
262 set_terms(terms, "new", "old");
263 return write_terms(terms->term_bad, terms->term_good);
270 static int mark_good(const char *refname, const struct object_id *oid,
271 int flag, void *cb_data)
273 int *m_good = (int *)cb_data;
278 static const char *need_bad_and_good_revision_warning =
279 N_("You need to give me at least one %s and %s revision.\n"
280 "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
282 static const char *need_bisect_start_warning =
283 N_("You need to start by \"git bisect start\".\n"
284 "You then need to give me at least one %s and %s revision.\n"
285 "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
287 static int bisect_next_check(const struct bisect_terms *terms,
288 const char *current_term)
290 int missing_good = 1, missing_bad = 1, retval = 0;
291 const char *bad_ref = xstrfmt("refs/bisect/%s", terms->term_bad);
292 const char *good_glob = xstrfmt("%s-*", terms->term_good);
294 if (ref_exists(bad_ref))
297 for_each_glob_ref_in(mark_good, good_glob, "refs/bisect/",
298 (void *) &missing_good);
300 if (!missing_good && !missing_bad)
308 if (missing_good && !missing_bad &&
309 !strcmp(current_term, terms->term_good)) {
312 * have bad (or new) but not good (or old). We could bisect
313 * although this is less optimum.
315 warning(_("bisecting only with a %s commit"), terms->term_bad);
319 * TRANSLATORS: Make sure to include [Y] and [n] in your
320 * translation. The program will only accept English input
323 yesno = git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO);
324 if (starts_with(yesno, "N") || starts_with(yesno, "n"))
328 if (!is_empty_or_missing_file(git_path_bisect_start())) {
329 retval = error(_(need_bad_and_good_revision_warning),
330 vocab_bad, vocab_good, vocab_bad, vocab_good);
332 retval = error(_(need_bisect_start_warning),
333 vocab_good, vocab_bad, vocab_good, vocab_bad);
337 free((void *) good_glob);
338 free((void *) bad_ref);
342 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
354 int no_checkout = 0, res = 0, nolog = 0;
355 struct option options[] = {
356 OPT_CMDMODE(0, "next-all", &cmdmode,
357 N_("perform 'git bisect next'"), NEXT_ALL),
358 OPT_CMDMODE(0, "write-terms", &cmdmode,
359 N_("write the terms to .git/BISECT_TERMS"), WRITE_TERMS),
360 OPT_CMDMODE(0, "bisect-clean-state", &cmdmode,
361 N_("cleanup the bisection state"), BISECT_CLEAN_STATE),
362 OPT_CMDMODE(0, "check-expected-revs", &cmdmode,
363 N_("check for expected revs"), CHECK_EXPECTED_REVS),
364 OPT_CMDMODE(0, "bisect-reset", &cmdmode,
365 N_("reset the bisection state"), BISECT_RESET),
366 OPT_CMDMODE(0, "bisect-write", &cmdmode,
367 N_("write out the bisection state in BISECT_LOG"), BISECT_WRITE),
368 OPT_CMDMODE(0, "check-and-set-terms", &cmdmode,
369 N_("check and set terms in a bisection state"), CHECK_AND_SET_TERMS),
370 OPT_CMDMODE(0, "bisect-next-check", &cmdmode,
371 N_("check whether bad or good terms exist"), BISECT_NEXT_CHECK),
372 OPT_BOOL(0, "no-checkout", &no_checkout,
373 N_("update BISECT_HEAD instead of checking out the current commit")),
374 OPT_BOOL(0, "no-log", &nolog,
375 N_("no log for BISECT_WRITE ")),
378 struct bisect_terms terms = { .term_good = NULL, .term_bad = NULL };
380 argc = parse_options(argc, argv, prefix, options,
381 git_bisect_helper_usage, 0);
384 usage_with_options(git_bisect_helper_usage, options);
388 return bisect_next_all(prefix, no_checkout);
391 return error(_("--write-terms requires two arguments"));
392 return write_terms(argv[0], argv[1]);
393 case BISECT_CLEAN_STATE:
395 return error(_("--bisect-clean-state requires no arguments"));
396 return bisect_clean_state();
397 case CHECK_EXPECTED_REVS:
398 check_expected_revs(argv, argc);
402 return error(_("--bisect-reset requires either no argument or a commit"));
403 return !!bisect_reset(argc ? argv[0] : NULL);
405 if (argc != 4 && argc != 5)
406 return error(_("--bisect-write requires either 4 or 5 arguments"));
407 set_terms(&terms, argv[3], argv[2]);
408 res = bisect_write(argv[0], argv[1], &terms, nolog);
410 case CHECK_AND_SET_TERMS:
412 return error(_("--check-and-set-terms requires 3 arguments"));
413 set_terms(&terms, argv[2], argv[1]);
414 res = check_and_set_terms(&terms, argv[0]);
416 case BISECT_NEXT_CHECK:
417 if (argc != 2 && argc != 3)
418 return error(_("--bisect-next-check requires 2 or 3 arguments"));
419 set_terms(&terms, argv[1], argv[0]);
420 res = bisect_next_check(&terms, argc == 3 ? argv[2] : NULL);
423 return error("BUG: unknown subcommand '%d'", cmdmode);