3 #include "parse-options.h"
7 #include "argv-array.h"
8 #include "run-command.h"
12 static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
13 static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
14 static GIT_PATH_FUNC(git_path_bisect_ancestors_ok, "BISECT_ANCESTORS_OK")
15 static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START")
16 static GIT_PATH_FUNC(git_path_bisect_head, "BISECT_HEAD")
17 static GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG")
18 static GIT_PATH_FUNC(git_path_head_name, "head-name")
19 static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES")
21 static const char * const git_bisect_helper_usage[] = {
22 N_("git bisect--helper --next-all [--no-checkout]"),
23 N_("git bisect--helper --write-terms <bad_term> <good_term>"),
24 N_("git bisect--helper --bisect-clean-state"),
25 N_("git bisect--helper --bisect-reset [<commit>]"),
26 N_("git bisect--helper --bisect-write <state> <revision> <good_term> <bad_term> [<nolog>]"),
27 N_("git bisect--helper --bisect-check-and-set-terms <command> <good_term> <bad_term>"),
28 N_("git bisect--helper --bisect-next-check [<term>] <good_term> <bad_term>"),
29 N_("git bisect--helper --bisect-terms [--term-good | --term-old | --term-bad | --term-new]"),
30 N_("git bisect--helper --bisect-start [--term-{old,good}=<term> --term-{new,bad}=<term>]"
31 "[--no-checkout] [<bad> [<good>...]] [--] [<paths>...]"),
36 const char *term_good;
40 static void free_terms(struct bisect_terms *terms)
42 if (!terms->term_good)
43 free((void *) terms->term_good);
45 free((void *) terms->term_bad);
48 static void set_terms(struct bisect_terms *terms, const char *bad,
51 terms->term_good = xstrdup(good);
52 terms->term_bad = xstrdup(bad);
55 static const char *voc[] = {
61 * Check whether the string `term` belongs to the set of strings
62 * included in the variable arguments.
65 static int one_of(const char *term, ...)
71 va_start(matches, term);
72 while (!res && (match = va_arg(matches, const char *)))
73 res = !strcmp(term, match);
79 static int check_term_format(const char *term, const char *orig_term)
82 char *new_term = xstrfmt("refs/bisect/%s", term);
84 res = check_refname_format(new_term, 0);
88 return error(_("'%s' is not a valid term"), term);
90 if (one_of(term, "help", "start", "skip", "next", "reset",
91 "visualize", "view", "replay", "log", "run", "terms", NULL))
92 return error(_("can't use the builtin command '%s' as a term"), term);
95 * In theory, nothing prevents swapping completely good and bad,
96 * but this situation could be confusing and hasn't been tested
97 * enough. Forbid it for now.
100 if ((strcmp(orig_term, "bad") && one_of(term, "bad", "new", NULL)) ||
101 (strcmp(orig_term, "good") && one_of(term, "good", "old", NULL)))
102 return error(_("can't change the meaning of the term '%s'"), term);
107 static int write_terms(const char *bad, const char *good)
112 if (!strcmp(bad, good))
113 return error(_("please use two different terms"));
115 if (check_term_format(bad, "bad") || check_term_format(good, "good"))
118 fp = fopen(git_path_bisect_terms(), "w");
120 return error_errno(_("could not open the file BISECT_TERMS"));
122 res = fprintf(fp, "%s\n%s\n", bad, good);
124 return (res < 0) ? -1 : 0;
127 static int is_expected_rev(const char *expected_hex)
129 struct strbuf actual_hex = STRBUF_INIT;
131 if (strbuf_read_file(&actual_hex, git_path_bisect_expected_rev(), 0) >= 40) {
132 strbuf_trim(&actual_hex);
133 res = !strcmp(actual_hex.buf, expected_hex);
135 strbuf_release(&actual_hex);
139 static void check_expected_revs(const char **revs, int rev_nr)
143 for (i = 0; i < rev_nr; i++) {
144 if (!is_expected_rev(revs[i])) {
145 unlink_or_warn(git_path_bisect_ancestors_ok());
146 unlink_or_warn(git_path_bisect_expected_rev());
151 static int bisect_reset(const char *commit)
153 struct strbuf branch = STRBUF_INIT;
156 if (strbuf_read_file(&branch, git_path_bisect_start(), 0) < 1)
157 return !printf(_("We are not bisecting.\n"));
158 strbuf_rtrim(&branch);
160 struct object_id oid;
162 if (get_oid_commit(commit, &oid))
163 return error(_("'%s' is not a valid commit"), commit);
164 strbuf_addstr(&branch, commit);
167 if (!file_exists(git_path_bisect_head())) {
168 struct argv_array argv = ARGV_ARRAY_INIT;
170 argv_array_pushl(&argv, "checkout", branch.buf, "--", NULL);
171 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
172 error(_("Could not check out original HEAD '%s'. Try "
173 "'git bisect reset <commit>'."), branch.buf);
174 strbuf_release(&branch);
175 argv_array_clear(&argv);
178 argv_array_clear(&argv);
181 strbuf_release(&branch);
182 return bisect_clean_state();
185 static void log_commit(FILE *fp, char *fmt, const char *state,
186 struct commit *commit)
188 struct pretty_print_context pp = {0};
189 struct strbuf commit_msg = STRBUF_INIT;
190 char *label = xstrfmt(fmt, state);
192 format_commit_message(commit, "%s", &commit_msg, &pp);
194 fprintf(fp, "# %s: [%s] %s\n", label, oid_to_hex(&commit->object.oid),
197 strbuf_release(&commit_msg);
201 static int bisect_write(const char *state, const char *rev,
202 const struct bisect_terms *terms, int nolog)
204 struct strbuf tag = STRBUF_INIT;
205 struct object_id oid;
206 struct commit *commit;
210 if (!strcmp(state, terms->term_bad)) {
211 strbuf_addf(&tag, "refs/bisect/%s", state);
212 } else if (one_of(state, terms->term_good, "skip", NULL)) {
213 strbuf_addf(&tag, "refs/bisect/%s-%s", state, rev);
215 error(_("Bad bisect_write argument: %s"), state);
219 if (get_oid(rev, &oid)) {
220 error(_("couldn't get the oid of the rev '%s'"), rev);
224 if (update_ref(NULL, tag.buf, &oid, NULL, 0,
225 UPDATE_REFS_MSG_ON_ERR))
228 fp = fopen(git_path_bisect_log(), "a");
230 error_errno(_("couldn't open the file '%s'"), git_path_bisect_log());
234 commit = lookup_commit_reference(&oid);
235 log_commit(fp, "%s", state, commit);
238 fprintf(fp, "git bisect %s %s\n", state, rev);
247 strbuf_release(&tag);
251 static int check_and_set_terms(struct bisect_terms *terms, const char *cmd)
253 int has_term_file = !is_empty_or_missing_file(git_path_bisect_terms());
255 if (one_of(cmd, "skip", "start", "terms", NULL))
258 if (has_term_file && strcmp(cmd, terms->term_bad) &&
259 strcmp(cmd, terms->term_good))
260 return error(_("Invalid command: you're currently in a "
261 "%s/%s bisect"), terms->term_bad,
264 if (!has_term_file) {
265 if (one_of(cmd, "bad", "good", NULL)) {
267 set_terms(terms, "bad", "good");
268 return write_terms(terms->term_bad, terms->term_good);
270 else if (one_of(cmd, "new", "old", NULL)) {
272 set_terms(terms, "new", "old");
273 return write_terms(terms->term_bad, terms->term_good);
280 static int mark_good(const char *refname, const struct object_id *oid,
281 int flag, void *cb_data)
283 int *m_good = (int *)cb_data;
288 static int bisect_next_check(const struct bisect_terms *terms,
289 const char *current_term)
291 int missing_good = 1, missing_bad = 1, retval = 0;
292 const char *bad_ref = xstrfmt("refs/bisect/%s", terms->term_bad);
293 const char *good_glob = xstrfmt("%s-*", terms->term_good);
295 if (ref_exists(bad_ref))
298 for_each_glob_ref_in(mark_good, good_glob, "refs/bisect/",
299 (void *) &missing_good);
301 if (!missing_good && !missing_bad)
307 if (missing_good && !missing_bad && current_term &&
308 !strcmp(current_term, terms->term_good)) {
311 * have bad (or new) but not good (or old). We could bisect
312 * although this is less optimum.
314 fprintf(stderr, _("Warning: bisecting only with a %s commit\n"),
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"))
329 if (!is_empty_or_missing_file(git_path_bisect_start())) {
330 error(_("You need to give me at least one %s and "
331 "%s revision. You can use \"git bisect %s\" "
332 "and \"git bisect %s\" for that.\n"),
333 voc[0], voc[1], voc[0], voc[1]);
336 error(_("You need to start by \"git bisect start\". You "
337 "then need to give me at least one %s and %s "
338 "revision. You can use \"git bisect %s\" and "
339 "\"git bisect %s\" for that.\n"),
340 voc[1], voc[0], voc[1], voc[0]);
348 free((void *) good_glob);
349 free((void *) bad_ref);
353 static int get_terms(struct bisect_terms *terms)
355 struct strbuf str = STRBUF_INIT;
359 fp = fopen(git_path_bisect_terms(), "r");
364 strbuf_getline_lf(&str, fp);
365 terms->term_bad = strbuf_detach(&str, NULL);
366 strbuf_getline_lf(&str, fp);
367 terms->term_good = strbuf_detach(&str, NULL);
375 strbuf_release(&str);
379 static int bisect_terms(struct bisect_terms *terms, const char **argv, int argc)
383 if (get_terms(terms))
384 return error(_("no terms defined"));
387 return error(_("--bisect-term requires exactly one argument"));
390 return !printf(_("Your current terms are %s for the old state\n"
391 "and %s for the new state.\n"),
392 terms->term_good, terms->term_bad);
394 for (i = 0; i < argc; i++) {
395 if (!strcmp(argv[i], "--term-good"))
396 printf(_("%s\n"), terms->term_good);
397 else if (!strcmp(argv[i], "--term-bad"))
398 printf(_("%s\n"), terms->term_bad);
400 error(_("BUG: invalid argument %s for 'git bisect terms'.\n"
401 "Supported options are: "
402 "--term-good|--term-old and "
403 "--term-bad|--term-new."), argv[i]);
409 static int bisect_append_log_quoted(const char **argv)
412 FILE *fp = fopen(git_path_bisect_log(), "a");
413 struct strbuf orig_args = STRBUF_INIT;
418 if (fprintf(fp, "git bisect start") < 1)
421 sq_quote_argv(&orig_args, argv);
422 if (fprintf(fp, "%s\n", orig_args.buf) < 1)
431 strbuf_release(&orig_args);
435 static int bisect_start(struct bisect_terms *terms, int no_checkout,
436 const char **argv, int argc)
438 int i, has_double_dash = 0, must_write_terms = 0, bad_seen = 0;
439 int flags, pathspec_pos, retval = 0;
440 struct string_list revs = STRING_LIST_INIT_DUP;
441 struct string_list states = STRING_LIST_INIT_DUP;
442 struct strbuf start_head = STRBUF_INIT;
443 struct strbuf bisect_names = STRBUF_INIT;
444 struct object_id head_oid;
445 struct object_id oid;
448 if (is_bare_repository())
452 * Check for one bad and then some good revisions
454 for (i = 0; i < argc; i++) {
455 if (!strcmp(argv[i], "--")) {
461 for (i = 0; i < argc; i++) {
462 const char *arg = argv[i];
463 if (!strcmp(argv[i], "--")) {
465 } else if (!strcmp(arg, "--no-checkout")) {
467 } else if (!strcmp(arg, "--term-good") ||
468 !strcmp(arg, "--term-old")) {
469 must_write_terms = 1;
470 free((void *) terms->term_good);
471 terms->term_good = xstrdup(argv[++i]);
472 } else if (skip_prefix(arg, "--term-good=", &arg) ||
473 skip_prefix(arg, "--term-old=", &arg)) {
474 must_write_terms = 1;
475 free((void *) terms->term_good);
476 terms->term_good = xstrdup(arg);
477 } else if (!strcmp(arg, "--term-bad") ||
478 !strcmp(arg, "--term-new")) {
479 must_write_terms = 1;
480 free((void *) terms->term_bad);
481 terms->term_bad = xstrdup(argv[++i]);
482 } else if (skip_prefix(arg, "--term-bad=", &arg) ||
483 skip_prefix(arg, "--term-new=", &arg)) {
484 must_write_terms = 1;
485 free((void *) terms->term_bad);
486 terms->term_bad = xstrdup(arg);
487 } else if (starts_with(arg, "--") &&
488 !one_of(arg, "--term-good", "--term-bad", NULL)) {
489 return error(_("unrecognised option: '%s'"), arg);
491 char *commit_id = xstrfmt("%s^{commit}", arg);
492 if (get_oid(commit_id, &oid) && has_double_dash)
493 die(_("'%s' does not appear to be a valid "
496 string_list_append(&revs, oid_to_hex(&oid));
503 * The user ran "git bisect start <sha1> <sha1>", hence did not
504 * explicitly specify the terms, but we are already starting to
505 * set references named with the default terms, and won't be able
506 * to change afterwards.
508 must_write_terms |= !!revs.nr;
509 for (i = 0; i < revs.nr; i++) {
511 string_list_append(&states, terms->term_good);
514 string_list_append(&states, terms->term_bad);
521 head = resolve_ref_unsafe("HEAD", 0, &head_oid, &flags);
523 if (get_oid("HEAD", &head_oid))
524 return error(_("Bad HEAD - I need a HEAD"));
527 * Check if we are bisecting
529 if (!is_empty_or_missing_file(git_path_bisect_start())) {
530 /* Reset to the rev from where we started */
531 strbuf_read_file(&start_head, git_path_bisect_start(), 0);
532 strbuf_trim(&start_head);
534 struct argv_array argv = ARGV_ARRAY_INIT;
536 argv_array_pushl(&argv, "checkout", start_head.buf,
538 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
539 error(_("checking out '%s' failed. Try 'git "
540 "bisect start <valid-branch>'."),
546 /* Get the rev from where we start. */
547 if (!get_oid(head, &head_oid) &&
548 !starts_with(head, "refs/heads/")) {
549 strbuf_reset(&start_head);
550 strbuf_addstr(&start_head, sha1_to_hex(head_oid.hash));
551 } else if (!get_oid(head, &head_oid) &&
552 skip_prefix(head, "refs/heads/", &head)) {
554 * This error message should only be triggered by
555 * cogito usage, and cogito users should understand
556 * it relates to cg-seek.
558 if (!is_empty_or_missing_file(git_path_head_name()))
559 return error(_("won't bisect on cg-seek'ed tree"));
560 strbuf_addstr(&start_head, head);
562 return error(_("Bad HEAD - strange symbolic ref"));
567 * Get rid of any old bisect state.
569 if (bisect_clean_state())
573 * In case of mistaken revs or checkout error, or signals received,
574 * "bisect_auto_next" below may exit or misbehave.
575 * We have to trap this to be able to clean up using
576 * "bisect_clean_state".
580 * Write new start state
582 write_file(git_path_bisect_start(), "%s\n", start_head.buf);
585 get_oid(start_head.buf, &oid);
586 if (update_ref(NULL, "BISECT_HEAD", &oid, NULL, 0,
587 UPDATE_REFS_MSG_ON_ERR))
591 if (pathspec_pos < argc - 1)
592 sq_quote_argv(&bisect_names, argv + pathspec_pos);
593 write_file(git_path_bisect_names(), "%s\n", bisect_names.buf);
595 for (i = 0; i < states.nr; i++)
596 if (bisect_write(states.items[i].string,
597 revs.items[i].string, terms, 1))
600 if (must_write_terms)
601 if (write_terms(terms->term_bad, terms->term_good))
604 retval = bisect_append_log_quoted(argv);
613 string_list_clear(&revs, 0);
614 string_list_clear(&states, 0);
615 strbuf_release(&start_head);
616 strbuf_release(&bisect_names);
620 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
634 int no_checkout = 0, res = 0;
635 struct option options[] = {
636 OPT_CMDMODE(0, "next-all", &cmdmode,
637 N_("perform 'git bisect next'"), NEXT_ALL),
638 OPT_CMDMODE(0, "write-terms", &cmdmode,
639 N_("write the terms to .git/BISECT_TERMS"), WRITE_TERMS),
640 OPT_CMDMODE(0, "bisect-clean-state", &cmdmode,
641 N_("cleanup the bisection state"), BISECT_CLEAN_STATE),
642 OPT_CMDMODE(0, "check-expected-revs", &cmdmode,
643 N_("check for expected revs"), CHECK_EXPECTED_REVS),
644 OPT_CMDMODE(0, "bisect-reset", &cmdmode,
645 N_("reset the bisection state"), BISECT_RESET),
646 OPT_CMDMODE(0, "bisect-write", &cmdmode,
647 N_("update the refs according to the bisection state and may write it to BISECT_LOG"), BISECT_WRITE),
648 OPT_CMDMODE(0, "check-and-set-terms", &cmdmode,
649 N_("check and set terms in a bisection state"), CHECK_AND_SET_TERMS),
650 OPT_CMDMODE(0, "bisect-next-check", &cmdmode,
651 N_("check whether bad or good terms exist"), BISECT_NEXT_CHECK),
652 OPT_CMDMODE(0, "bisect-terms", &cmdmode,
653 N_("print out the bisect terms"), BISECT_TERMS),
654 OPT_CMDMODE(0, "bisect-start", &cmdmode,
655 N_("start the bisect session"), BISECT_START),
656 OPT_BOOL(0, "no-checkout", &no_checkout,
657 N_("update BISECT_HEAD instead of checking out the current commit")),
660 struct bisect_terms terms;
662 argc = parse_options(argc, argv, prefix, options,
663 git_bisect_helper_usage,
664 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_UNKNOWN);
667 usage_with_options(git_bisect_helper_usage, options);
672 return bisect_next_all(prefix, no_checkout);
675 return error(_("--write-terms requires two arguments"));
676 return write_terms(argv[0], argv[1]);
677 case BISECT_CLEAN_STATE:
679 return error(_("--bisect-clean-state requires no arguments"));
680 return bisect_clean_state();
681 case CHECK_EXPECTED_REVS:
682 check_expected_revs(argv, argc);
686 return error(_("--bisect-reset requires either no argument or a commit"));
687 return bisect_reset(argc ? argv[0] : NULL);
689 if (argc != 4 && argc != 5)
690 return error(_("--bisect-write requires either 4 or 5 arguments"));
691 nolog = (argc == 5) && !strcmp(argv[4], "nolog");
692 set_terms(&terms, argv[3], argv[2]);
693 res = bisect_write(argv[0], argv[1], &terms, nolog);
695 case CHECK_AND_SET_TERMS:
697 return error(_("--check-and-set-terms requires 3 arguments"));
698 set_terms(&terms, argv[2], argv[1]);
699 res = check_and_set_terms(&terms, argv[0]);
701 case BISECT_NEXT_CHECK:
702 if (argc != 2 && argc != 3)
703 return error(_("--bisect-next-check requires 2 or 3 arguments"));
704 set_terms(&terms, argv[1], argv[0]);
705 res = bisect_next_check(&terms, argc == 3 ? argv[2] : NULL);
709 return error(_("--bisect-terms requires 0 or 1 argument"));
710 res = bisect_terms(&terms, argv, argc);
713 set_terms(&terms, "bad", "good");
714 res = bisect_start(&terms, no_checkout, argv, argc);
717 return error("BUG: unknown subcommand '%d'", cmdmode);