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_log
, "BISECT_LOG")
17 static GIT_PATH_FUNC(git_path_head_name
, "head-name")
18 static GIT_PATH_FUNC(git_path_bisect_names
, "BISECT_NAMES")
20 static const char * const git_bisect_helper_usage
[] = {
21 N_("git bisect--helper --next-all [--no-checkout]"),
22 N_("git bisect--helper --write-terms <bad_term> <good_term>"),
23 N_("git bisect--helper --bisect-clean-state"),
24 N_("git bisect--helper --bisect-reset [<commit>]"),
25 N_("git bisect--helper --bisect-write [--no-log] <state> <revision> <good_term> <bad_term>"),
26 N_("git bisect--helper --bisect-check-and-set-terms <command> <good_term> <bad_term>"),
27 N_("git bisect--helper --bisect-next-check <good_term> <bad_term> [<term>]"),
28 N_("git bisect--helper --bisect-terms [--term-good | --term-old | --term-bad | --term-new]"),
29 N_("git bisect--helper --bisect-start [--term-{old,good}=<term> --term-{new,bad}=<term>]"
30 "[--no-checkout] [<bad> [<good>...]] [--] [<paths>...]"),
39 static void free_terms(struct bisect_terms
*terms
)
41 FREE_AND_NULL(terms
->term_good
);
42 FREE_AND_NULL(terms
->term_bad
);
45 static void set_terms(struct bisect_terms
*terms
, const char *bad
,
48 free((void *)terms
->term_good
);
49 terms
->term_good
= xstrdup(good
);
50 free((void *)terms
->term_bad
);
51 terms
->term_bad
= xstrdup(bad
);
54 static const char vocab_bad
[] = "bad|new";
55 static const char vocab_good
[] = "good|old";
58 * Check whether the string `term` belongs to the set of strings
59 * included in the variable arguments.
62 static int one_of(const char *term
, ...)
68 va_start(matches
, term
);
69 while (!res
&& (match
= va_arg(matches
, const char *)))
70 res
= !strcmp(term
, match
);
76 static int check_term_format(const char *term
, const char *orig_term
)
79 char *new_term
= xstrfmt("refs/bisect/%s", term
);
81 res
= check_refname_format(new_term
, 0);
85 return error(_("'%s' is not a valid term"), term
);
87 if (one_of(term
, "help", "start", "skip", "next", "reset",
88 "visualize", "view", "replay", "log", "run", "terms", NULL
))
89 return error(_("can't use the builtin command '%s' as a term"), term
);
92 * In theory, nothing prevents swapping completely good and bad,
93 * but this situation could be confusing and hasn't been tested
94 * enough. Forbid it for now.
97 if ((strcmp(orig_term
, "bad") && one_of(term
, "bad", "new", NULL
)) ||
98 (strcmp(orig_term
, "good") && one_of(term
, "good", "old", NULL
)))
99 return error(_("can't change the meaning of the term '%s'"), term
);
104 static int write_terms(const char *bad
, const char *good
)
109 if (!strcmp(bad
, good
))
110 return error(_("please use two different terms"));
112 if (check_term_format(bad
, "bad") || check_term_format(good
, "good"))
115 fp
= fopen(git_path_bisect_terms(), "w");
117 return error_errno(_("could not open the file BISECT_TERMS"));
119 res
= fprintf(fp
, "%s\n%s\n", bad
, good
);
121 return (res
< 0) ? -1 : 0;
124 static int is_expected_rev(const char *expected_hex
)
126 struct strbuf actual_hex
= STRBUF_INIT
;
128 if (strbuf_read_file(&actual_hex
, git_path_bisect_expected_rev(), 0) >= 40) {
129 strbuf_trim(&actual_hex
);
130 res
= !strcmp(actual_hex
.buf
, expected_hex
);
132 strbuf_release(&actual_hex
);
136 static void check_expected_revs(const char **revs
, int rev_nr
)
140 for (i
= 0; i
< rev_nr
; i
++) {
141 if (!is_expected_rev(revs
[i
])) {
142 unlink_or_warn(git_path_bisect_ancestors_ok());
143 unlink_or_warn(git_path_bisect_expected_rev());
148 static int bisect_reset(const char *commit
)
150 struct strbuf branch
= STRBUF_INIT
;
153 if (strbuf_read_file(&branch
, git_path_bisect_start(), 0) < 1) {
154 printf(_("We are not bisecting.\n"));
157 strbuf_rtrim(&branch
);
159 struct object_id oid
;
161 if (get_oid_commit(commit
, &oid
))
162 return error(_("'%s' is not a valid commit"), commit
);
163 strbuf_addstr(&branch
, commit
);
166 if (!ref_exists("BISECT_HEAD")) {
167 struct argv_array argv
= ARGV_ARRAY_INIT
;
169 argv_array_pushl(&argv
, "checkout", branch
.buf
, "--", NULL
);
170 if (run_command_v_opt(argv
.argv
, RUN_GIT_CMD
)) {
171 error(_("could not check out original"
172 " HEAD '%s'. Try 'git bisect"
173 " 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 res
= error(_("Bad bisect_write argument: %s"), state
);
219 if (get_oid(rev
, &oid
)) {
220 res
= 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
)) {
230 fp
= fopen(git_path_bisect_log(), "a");
232 res
= error_errno(_("couldn't open the file '%s'"), git_path_bisect_log());
236 commit
= lookup_commit_reference(the_repository
, &oid
);
237 log_commit(fp
, "%s", state
, commit
);
240 fprintf(fp
, "git bisect %s %s\n", state
, rev
);
245 strbuf_release(&tag
);
249 static int check_and_set_terms(struct bisect_terms
*terms
, const char *cmd
)
251 int has_term_file
= !is_empty_or_missing_file(git_path_bisect_terms());
253 if (one_of(cmd
, "skip", "start", "terms", NULL
))
256 if (has_term_file
&& strcmp(cmd
, terms
->term_bad
) &&
257 strcmp(cmd
, terms
->term_good
))
258 return error(_("Invalid command: you're currently in a "
259 "%s/%s bisect"), terms
->term_bad
,
262 if (!has_term_file
) {
263 if (one_of(cmd
, "bad", "good", NULL
)) {
264 set_terms(terms
, "bad", "good");
265 return write_terms(terms
->term_bad
, terms
->term_good
);
267 if (one_of(cmd
, "new", "old", NULL
)) {
268 set_terms(terms
, "new", "old");
269 return write_terms(terms
->term_bad
, terms
->term_good
);
276 static int mark_good(const char *refname
, const struct object_id
*oid
,
277 int flag
, void *cb_data
)
279 int *m_good
= (int *)cb_data
;
284 static const char need_bad_and_good_revision_warning
[] =
285 N_("You need to give me at least one %s and %s revision.\n"
286 "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
288 static const char need_bisect_start_warning
[] =
289 N_("You need to start by \"git bisect start\".\n"
290 "You then need to give me at least one %s and %s revision.\n"
291 "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
293 static int decide_next(const struct bisect_terms
*terms
,
294 const char *current_term
, int missing_good
,
297 if (!missing_good
&& !missing_bad
)
302 if (missing_good
&& !missing_bad
&&
303 !strcmp(current_term
, terms
->term_good
)) {
306 * have bad (or new) but not good (or old). We could bisect
307 * although this is less optimum.
309 warning(_("bisecting only with a %s commit"), terms
->term_bad
);
313 * TRANSLATORS: Make sure to include [Y] and [n] in your
314 * translation. The program will only accept English input
317 yesno
= git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO
);
318 if (starts_with(yesno
, "N") || starts_with(yesno
, "n"))
323 if (!is_empty_or_missing_file(git_path_bisect_start()))
324 return error(_(need_bad_and_good_revision_warning
),
325 vocab_bad
, vocab_good
, vocab_bad
, vocab_good
);
327 return error(_(need_bisect_start_warning
),
328 vocab_good
, vocab_bad
, vocab_good
, vocab_bad
);
331 static int bisect_next_check(const struct bisect_terms
*terms
,
332 const char *current_term
)
334 int missing_good
= 1, missing_bad
= 1;
335 char *bad_ref
= xstrfmt("refs/bisect/%s", terms
->term_bad
);
336 char *good_glob
= xstrfmt("%s-*", terms
->term_good
);
338 if (ref_exists(bad_ref
))
341 for_each_glob_ref_in(mark_good
, good_glob
, "refs/bisect/",
342 (void *) &missing_good
);
347 return decide_next(terms
, current_term
, missing_good
, missing_bad
);
350 static int get_terms(struct bisect_terms
*terms
)
352 struct strbuf str
= STRBUF_INIT
;
356 fp
= fopen(git_path_bisect_terms(), "r");
363 strbuf_getline_lf(&str
, fp
);
364 terms
->term_bad
= strbuf_detach(&str
, NULL
);
365 strbuf_getline_lf(&str
, fp
);
366 terms
->term_good
= strbuf_detach(&str
, NULL
);
371 strbuf_release(&str
);
375 static int bisect_terms(struct bisect_terms
*terms
, const char *option
)
377 if (get_terms(terms
))
378 return error(_("no terms defined"));
380 if (option
== NULL
) {
381 printf(_("Your current terms are %s for the old state\n"
382 "and %s for the new state.\n"),
383 terms
->term_good
, terms
->term_bad
);
386 if (one_of(option
, "--term-good", "--term-old", NULL
))
387 printf("%s\n", terms
->term_good
);
388 else if (one_of(option
, "--term-bad", "--term-new", NULL
))
389 printf("%s\n", terms
->term_bad
);
391 return error(_("invalid argument %s for 'git bisect terms'.\n"
392 "Supported options are: "
393 "--term-good|--term-old and "
394 "--term-bad|--term-new."), option
);
399 static int bisect_append_log_quoted(const char **argv
)
402 FILE *fp
= fopen(git_path_bisect_log(), "a");
403 struct strbuf orig_args
= STRBUF_INIT
;
408 if (fprintf(fp
, "git bisect start") < 1) {
413 sq_quote_argv(&orig_args
, argv
);
414 if (fprintf(fp
, "%s\n", orig_args
.buf
) < 1)
419 strbuf_release(&orig_args
);
423 static int bisect_start(struct bisect_terms
*terms
, int no_checkout
,
424 const char **argv
, int argc
)
426 int i
, has_double_dash
= 0, must_write_terms
= 0, bad_seen
= 0;
427 int flags
, pathspec_pos
, res
= 0;
428 struct string_list revs
= STRING_LIST_INIT_DUP
;
429 struct string_list states
= STRING_LIST_INIT_DUP
;
430 struct strbuf start_head
= STRBUF_INIT
;
431 struct strbuf bisect_names
= STRBUF_INIT
;
432 struct object_id head_oid
;
433 struct object_id oid
;
436 if (is_bare_repository())
440 * Check for one bad and then some good revisions
442 for (i
= 0; i
< argc
; i
++) {
443 if (!strcmp(argv
[i
], "--")) {
449 for (i
= 0; i
< argc
; i
++) {
450 const char *arg
= argv
[i
];
451 if (!strcmp(argv
[i
], "--")) {
453 } else if (!strcmp(arg
, "--no-checkout")) {
455 } else if (!strcmp(arg
, "--term-good") ||
456 !strcmp(arg
, "--term-old")) {
459 return error(_("'' is not a valid term"));
460 must_write_terms
= 1;
461 free((void *) terms
->term_good
);
462 terms
->term_good
= xstrdup(argv
[i
]);
463 } else if (skip_prefix(arg
, "--term-good=", &arg
) ||
464 skip_prefix(arg
, "--term-old=", &arg
)) {
465 must_write_terms
= 1;
466 free((void *) terms
->term_good
);
467 terms
->term_good
= xstrdup(arg
);
468 } else if (!strcmp(arg
, "--term-bad") ||
469 !strcmp(arg
, "--term-new")) {
472 return error(_("'' is not a valid term"));
473 must_write_terms
= 1;
474 free((void *) terms
->term_bad
);
475 terms
->term_bad
= xstrdup(argv
[i
]);
476 } else if (skip_prefix(arg
, "--term-bad=", &arg
) ||
477 skip_prefix(arg
, "--term-new=", &arg
)) {
478 must_write_terms
= 1;
479 free((void *) terms
->term_bad
);
480 terms
->term_bad
= xstrdup(arg
);
481 } else if (starts_with(arg
, "--")) {
482 return error(_("unrecognized option: '%s'"), arg
);
484 char *commit_id
= xstrfmt("%s^{commit}", arg
);
485 if (get_oid(commit_id
, &oid
) && has_double_dash
)
486 die(_("'%s' does not appear to be a valid "
489 string_list_append(&revs
, oid_to_hex(&oid
));
496 * The user ran "git bisect start <sha1> <sha1>", hence did not
497 * explicitly specify the terms, but we are already starting to
498 * set references named with the default terms, and won't be able
499 * to change afterwards.
502 must_write_terms
= 1;
503 for (i
= 0; i
< revs
.nr
; i
++) {
505 string_list_append(&states
, terms
->term_good
);
508 string_list_append(&states
, terms
->term_bad
);
515 head
= resolve_ref_unsafe("HEAD", 0, &head_oid
, &flags
);
517 if (get_oid("HEAD", &head_oid
))
518 return error(_("bad HEAD - I need a HEAD"));
521 * Check if we are bisecting
523 if (!is_empty_or_missing_file(git_path_bisect_start())) {
524 /* Reset to the rev from where we started */
525 strbuf_read_file(&start_head
, git_path_bisect_start(), 0);
526 strbuf_trim(&start_head
);
528 struct argv_array argv
= ARGV_ARRAY_INIT
;
530 argv_array_pushl(&argv
, "checkout", start_head
.buf
,
532 if (run_command_v_opt(argv
.argv
, RUN_GIT_CMD
)) {
533 res
= error(_("checking out '%s' failed."
534 " Try 'git bisect start "
541 /* Get the rev from where we start. */
542 if (!get_oid(head
, &head_oid
) &&
543 !starts_with(head
, "refs/heads/")) {
544 strbuf_reset(&start_head
);
545 strbuf_addstr(&start_head
, oid_to_hex(&head_oid
));
546 } else if (!get_oid(head
, &head_oid
) &&
547 skip_prefix(head
, "refs/heads/", &head
)) {
549 * This error message should only be triggered by
550 * cogito usage, and cogito users should understand
551 * it relates to cg-seek.
553 if (!is_empty_or_missing_file(git_path_head_name()))
554 return error(_("won't bisect on cg-seek'ed tree"));
555 strbuf_addstr(&start_head
, head
);
557 return error(_("bad HEAD - strange symbolic ref"));
562 * Get rid of any old bisect state.
564 if (bisect_clean_state())
568 * In case of mistaken revs or checkout error, or signals received,
569 * "bisect_auto_next" below may exit or misbehave.
570 * We have to trap this to be able to clean up using
571 * "bisect_clean_state".
575 * Write new start state
577 write_file(git_path_bisect_start(), "%s\n", start_head
.buf
);
580 if (get_oid(start_head
.buf
, &oid
) < 0) {
581 res
= error(_("invalid ref: '%s'"), start_head
.buf
);
584 if (update_ref(NULL
, "BISECT_HEAD", &oid
, NULL
, 0,
585 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)) {
602 if (must_write_terms
&& write_terms(terms
->term_bad
,
608 res
= 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, nolog
= 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_("write out the bisection state in 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")),
658 OPT_BOOL(0, "no-log", &nolog
,
659 N_("no log for BISECT_WRITE")),
662 struct bisect_terms terms
= { .term_good
= NULL
, .term_bad
= NULL
};
664 argc
= parse_options(argc
, argv
, prefix
, options
,
665 git_bisect_helper_usage
,
666 PARSE_OPT_KEEP_DASHDASH
| PARSE_OPT_KEEP_UNKNOWN
);
669 usage_with_options(git_bisect_helper_usage
, options
);
673 res
= bisect_next_all(the_repository
, prefix
, no_checkout
);
677 return error(_("--write-terms requires two arguments"));
678 return write_terms(argv
[0], argv
[1]);
679 case BISECT_CLEAN_STATE
:
681 return error(_("--bisect-clean-state requires no arguments"));
682 return bisect_clean_state();
683 case CHECK_EXPECTED_REVS
:
684 check_expected_revs(argv
, argc
);
688 return error(_("--bisect-reset requires either no argument or a commit"));
689 return !!bisect_reset(argc
? argv
[0] : NULL
);
691 if (argc
!= 4 && argc
!= 5)
692 return error(_("--bisect-write requires either 4 or 5 arguments"));
693 set_terms(&terms
, argv
[3], argv
[2]);
694 res
= bisect_write(argv
[0], argv
[1], &terms
, nolog
);
696 case CHECK_AND_SET_TERMS
:
698 return error(_("--check-and-set-terms requires 3 arguments"));
699 set_terms(&terms
, argv
[2], argv
[1]);
700 res
= check_and_set_terms(&terms
, argv
[0]);
702 case BISECT_NEXT_CHECK
:
703 if (argc
!= 2 && argc
!= 3)
704 return error(_("--bisect-next-check requires 2 or 3 arguments"));
705 set_terms(&terms
, argv
[1], argv
[0]);
706 res
= bisect_next_check(&terms
, argc
== 3 ? argv
[2] : NULL
);
710 return error(_("--bisect-terms requires 0 or 1 argument"));
711 res
= bisect_terms(&terms
, argc
== 1 ? argv
[0] : NULL
);
714 set_terms(&terms
, "bad", "good");
715 res
= bisect_start(&terms
, no_checkout
, argv
, argc
);
718 return error("BUG: unknown subcommand '%d'", cmdmode
);
723 * Handle early success
724 * From check_merge_bases > check_good_are_ancestors_of_bad > bisect_next_all
726 if (res
== BISECT_INTERNAL_SUCCESS_MERGE_BASE
)