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 [--no-log] <state> <revision> <good_term> <bad_term>"),
27 N_("git bisect--helper --bisect-check-and-set-terms <command> <good_term> <bad_term>"),
28 N_("git bisect--helper --bisect-next-check <good_term> <bad_term> [<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>...]"),
40 static void free_terms(struct bisect_terms
*terms
)
42 FREE_AND_NULL(terms
->term_good
);
43 FREE_AND_NULL(terms
->term_bad
);
46 static void set_terms(struct bisect_terms
*terms
, const char *bad
,
49 free((void *)terms
->term_good
);
50 terms
->term_good
= xstrdup(good
);
51 free((void *)terms
->term_bad
);
52 terms
->term_bad
= xstrdup(bad
);
55 static const char *vocab_bad
= "bad|new";
56 static const char *vocab_good
= "good|old";
59 * Check whether the string `term` belongs to the set of strings
60 * included in the variable arguments.
63 static int one_of(const char *term
, ...)
69 va_start(matches
, term
);
70 while (!res
&& (match
= va_arg(matches
, const char *)))
71 res
= !strcmp(term
, match
);
77 static int check_term_format(const char *term
, const char *orig_term
)
80 char *new_term
= xstrfmt("refs/bisect/%s", term
);
82 res
= check_refname_format(new_term
, 0);
86 return error(_("'%s' is not a valid term"), term
);
88 if (one_of(term
, "help", "start", "skip", "next", "reset",
89 "visualize", "view", "replay", "log", "run", "terms", NULL
))
90 return error(_("can't use the builtin command '%s' as a term"), term
);
93 * In theory, nothing prevents swapping completely good and bad,
94 * but this situation could be confusing and hasn't been tested
95 * enough. Forbid it for now.
98 if ((strcmp(orig_term
, "bad") && one_of(term
, "bad", "new", NULL
)) ||
99 (strcmp(orig_term
, "good") && one_of(term
, "good", "old", NULL
)))
100 return error(_("can't change the meaning of the term '%s'"), term
);
105 static int write_terms(const char *bad
, const char *good
)
110 if (!strcmp(bad
, good
))
111 return error(_("please use two different terms"));
113 if (check_term_format(bad
, "bad") || check_term_format(good
, "good"))
116 fp
= fopen(git_path_bisect_terms(), "w");
118 return error_errno(_("could not open the file BISECT_TERMS"));
120 res
= fprintf(fp
, "%s\n%s\n", bad
, good
);
122 return (res
< 0) ? -1 : 0;
125 static int is_expected_rev(const char *expected_hex
)
127 struct strbuf actual_hex
= STRBUF_INIT
;
129 if (strbuf_read_file(&actual_hex
, git_path_bisect_expected_rev(), 0) >= 40) {
130 strbuf_trim(&actual_hex
);
131 res
= !strcmp(actual_hex
.buf
, expected_hex
);
133 strbuf_release(&actual_hex
);
137 static void check_expected_revs(const char **revs
, int rev_nr
)
141 for (i
= 0; i
< rev_nr
; i
++) {
142 if (!is_expected_rev(revs
[i
])) {
143 unlink_or_warn(git_path_bisect_ancestors_ok());
144 unlink_or_warn(git_path_bisect_expected_rev());
149 static int bisect_reset(const char *commit
)
151 struct strbuf branch
= STRBUF_INIT
;
154 if (strbuf_read_file(&branch
, git_path_bisect_start(), 0) < 1) {
155 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"
173 " HEAD '%s'. Try 'git bisect"
174 " reset <commit>'."), branch
.buf
);
175 strbuf_release(&branch
);
176 argv_array_clear(&argv
);
179 argv_array_clear(&argv
);
182 strbuf_release(&branch
);
183 return bisect_clean_state();
186 static void log_commit(FILE *fp
, char *fmt
, const char *state
,
187 struct commit
*commit
)
189 struct pretty_print_context pp
= {0};
190 struct strbuf commit_msg
= STRBUF_INIT
;
191 char *label
= xstrfmt(fmt
, state
);
193 format_commit_message(commit
, "%s", &commit_msg
, &pp
);
195 fprintf(fp
, "# %s: [%s] %s\n", label
, oid_to_hex(&commit
->object
.oid
),
198 strbuf_release(&commit_msg
);
202 static int bisect_write(const char *state
, const char *rev
,
203 const struct bisect_terms
*terms
, int nolog
)
205 struct strbuf tag
= STRBUF_INIT
;
206 struct object_id oid
;
207 struct commit
*commit
;
211 if (!strcmp(state
, terms
->term_bad
)) {
212 strbuf_addf(&tag
, "refs/bisect/%s", state
);
213 } else if (one_of(state
, terms
->term_good
, "skip", NULL
)) {
214 strbuf_addf(&tag
, "refs/bisect/%s-%s", state
, rev
);
216 retval
= error(_("Bad bisect_write argument: %s"), state
);
220 if (get_oid(rev
, &oid
)) {
221 retval
= error(_("couldn't get the oid of the rev '%s'"), rev
);
225 if (update_ref(NULL
, tag
.buf
, &oid
, NULL
, 0,
226 UPDATE_REFS_MSG_ON_ERR
)) {
231 fp
= fopen(git_path_bisect_log(), "a");
233 retval
= error_errno(_("couldn't open the file '%s'"), git_path_bisect_log());
237 commit
= lookup_commit_reference(the_repository
, &oid
);
238 log_commit(fp
, "%s", state
, commit
);
241 fprintf(fp
, "git bisect %s %s\n", state
, rev
);
246 strbuf_release(&tag
);
250 static int check_and_set_terms(struct bisect_terms
*terms
, const char *cmd
)
252 int has_term_file
= !is_empty_or_missing_file(git_path_bisect_terms());
254 if (one_of(cmd
, "skip", "start", "terms", NULL
))
257 if (has_term_file
&& strcmp(cmd
, terms
->term_bad
) &&
258 strcmp(cmd
, terms
->term_good
))
259 return error(_("Invalid command: you're currently in a "
260 "%s/%s bisect"), terms
->term_bad
,
263 if (!has_term_file
) {
264 if (one_of(cmd
, "bad", "good", NULL
)) {
265 set_terms(terms
, "bad", "good");
266 return write_terms(terms
->term_bad
, terms
->term_good
);
268 if (one_of(cmd
, "new", "old", NULL
)) {
269 set_terms(terms
, "new", "old");
270 return write_terms(terms
->term_bad
, terms
->term_good
);
277 static int mark_good(const char *refname
, const struct object_id
*oid
,
278 int flag
, void *cb_data
)
280 int *m_good
= (int *)cb_data
;
285 static const char need_bad_and_good_revision_warning
[] =
286 N_("You need to give me at least one %s and %s revision.\n"
287 "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
289 static const char need_bisect_start_warning
[] =
290 N_("You need to start by \"git bisect start\".\n"
291 "You then need to give me at least one %s and %s revision.\n"
292 "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
294 static int bisect_next_check(const struct bisect_terms
*terms
,
295 const char *current_term
)
297 int missing_good
= 1, missing_bad
= 1, retval
= 0;
298 const char *bad_ref
= xstrfmt("refs/bisect/%s", terms
->term_bad
);
299 const char *good_glob
= xstrfmt("%s-*", terms
->term_good
);
301 if (ref_exists(bad_ref
))
304 for_each_glob_ref_in(mark_good
, good_glob
, "refs/bisect/",
305 (void *) &missing_good
);
307 if (!missing_good
&& !missing_bad
)
315 if (missing_good
&& !missing_bad
&&
316 !strcmp(current_term
, terms
->term_good
)) {
319 * have bad (or new) but not good (or old). We could bisect
320 * although this is less optimum.
322 warning(_("bisecting only with a %s commit"), terms
->term_bad
);
326 * TRANSLATORS: Make sure to include [Y] and [n] in your
327 * translation. The program will only accept English input
330 yesno
= git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO
);
331 if (starts_with(yesno
, "N") || starts_with(yesno
, "n"))
335 if (!is_empty_or_missing_file(git_path_bisect_start())) {
336 retval
= error(_(need_bad_and_good_revision_warning
),
337 vocab_bad
, vocab_good
, vocab_bad
, vocab_good
);
339 retval
= error(_(need_bisect_start_warning
),
340 vocab_good
, vocab_bad
, vocab_good
, vocab_bad
);
344 free((void *) good_glob
);
345 free((void *) bad_ref
);
349 static int get_terms(struct bisect_terms
*terms
)
351 struct strbuf str
= STRBUF_INIT
;
355 fp
= fopen(git_path_bisect_terms(), "r");
362 strbuf_getline_lf(&str
, fp
);
363 terms
->term_bad
= strbuf_detach(&str
, NULL
);
364 strbuf_getline_lf(&str
, fp
);
365 terms
->term_good
= strbuf_detach(&str
, NULL
);
370 strbuf_release(&str
);
374 static int bisect_terms(struct bisect_terms
*terms
, const char *option
)
376 if (get_terms(terms
))
377 return error(_("no terms defined"));
379 if (option
== NULL
) {
380 printf(_("Your current terms are %s for the old state\n"
381 "and %s for the new state.\n"),
382 terms
->term_good
, terms
->term_bad
);
385 if (one_of(option
, "--term-good", "--term-old", NULL
))
386 printf("%s\n", terms
->term_good
);
387 else if (one_of(option
, "--term-bad", "--term-new", NULL
))
388 printf("%s\n", terms
->term_bad
);
390 return error(_("invalid argument %s for 'git bisect terms'.\n"
391 "Supported options are: "
392 "--term-good|--term-old and "
393 "--term-bad|--term-new."), option
);
398 static int bisect_append_log_quoted(const char **argv
)
401 FILE *fp
= fopen(git_path_bisect_log(), "a");
402 struct strbuf orig_args
= STRBUF_INIT
;
407 if (fprintf(fp
, "git bisect start") < 1) {
412 sq_quote_argv(&orig_args
, argv
);
413 if (fprintf(fp
, "%s\n", orig_args
.buf
) < 1)
418 strbuf_release(&orig_args
);
422 static int bisect_start(struct bisect_terms
*terms
, int no_checkout
,
423 const char **argv
, int argc
)
425 int i
, has_double_dash
= 0, must_write_terms
= 0, bad_seen
= 0;
426 int flags
, pathspec_pos
, retval
= 0;
427 struct string_list revs
= STRING_LIST_INIT_DUP
;
428 struct string_list states
= STRING_LIST_INIT_DUP
;
429 struct strbuf start_head
= STRBUF_INIT
;
430 struct strbuf bisect_names
= STRBUF_INIT
;
431 struct object_id head_oid
;
432 struct object_id oid
;
435 if (is_bare_repository())
439 * Check for one bad and then some good revisions
441 for (i
= 0; i
< argc
; i
++) {
442 if (!strcmp(argv
[i
], "--")) {
448 for (i
= 0; i
< argc
; i
++) {
449 const char *arg
= argv
[i
];
450 if (!strcmp(argv
[i
], "--")) {
452 } else if (!strcmp(arg
, "--no-checkout")) {
454 } else if (!strcmp(arg
, "--term-good") ||
455 !strcmp(arg
, "--term-old")) {
456 must_write_terms
= 1;
457 free((void *) terms
->term_good
);
458 terms
->term_good
= xstrdup(argv
[++i
]);
459 } else if (skip_prefix(arg
, "--term-good=", &arg
) ||
460 skip_prefix(arg
, "--term-old=", &arg
)) {
461 must_write_terms
= 1;
462 free((void *) terms
->term_good
);
463 terms
->term_good
= xstrdup(arg
);
464 } else if (!strcmp(arg
, "--term-bad") ||
465 !strcmp(arg
, "--term-new")) {
466 must_write_terms
= 1;
467 free((void *) terms
->term_bad
);
468 terms
->term_bad
= xstrdup(argv
[++i
]);
469 } else if (skip_prefix(arg
, "--term-bad=", &arg
) ||
470 skip_prefix(arg
, "--term-new=", &arg
)) {
471 must_write_terms
= 1;
472 free((void *) terms
->term_bad
);
473 terms
->term_bad
= xstrdup(arg
);
474 } else if (starts_with(arg
, "--") &&
475 !one_of(arg
, "--term-good", "--term-bad", NULL
)) {
476 return error(_("unrecognized option: '%s'"), arg
);
478 char *commit_id
= xstrfmt("%s^{commit}", arg
);
479 if (get_oid(commit_id
, &oid
) && has_double_dash
)
480 die(_("'%s' does not appear to be a valid "
483 string_list_append(&revs
, oid_to_hex(&oid
));
490 * The user ran "git bisect start <sha1> <sha1>", hence did not
491 * explicitly specify the terms, but we are already starting to
492 * set references named with the default terms, and won't be able
493 * to change afterwards.
496 must_write_terms
= 1;
497 for (i
= 0; i
< revs
.nr
; i
++) {
499 string_list_append(&states
, terms
->term_good
);
502 string_list_append(&states
, terms
->term_bad
);
509 head
= resolve_ref_unsafe("HEAD", 0, &head_oid
, &flags
);
511 if (get_oid("HEAD", &head_oid
))
512 return error(_("bad HEAD - I need a HEAD"));
515 * Check if we are bisecting
517 if (!is_empty_or_missing_file(git_path_bisect_start())) {
518 /* Reset to the rev from where we started */
519 strbuf_read_file(&start_head
, git_path_bisect_start(), 0);
520 strbuf_trim(&start_head
);
522 struct argv_array argv
= ARGV_ARRAY_INIT
;
524 argv_array_pushl(&argv
, "checkout", start_head
.buf
,
526 if (run_command_v_opt(argv
.argv
, RUN_GIT_CMD
)) {
527 retval
= error(_("checking out '%s' failed."
528 " Try 'git bisect start "
535 /* Get the rev from where we start. */
536 if (!get_oid(head
, &head_oid
) &&
537 !starts_with(head
, "refs/heads/")) {
538 strbuf_reset(&start_head
);
539 strbuf_addstr(&start_head
, oid_to_hex(&head_oid
));
540 } else if (!get_oid(head
, &head_oid
) &&
541 skip_prefix(head
, "refs/heads/", &head
)) {
543 * This error message should only be triggered by
544 * cogito usage, and cogito users should understand
545 * it relates to cg-seek.
547 if (!is_empty_or_missing_file(git_path_head_name()))
548 return error(_("won't bisect on cg-seek'ed tree"));
549 strbuf_addstr(&start_head
, head
);
551 return error(_("bad HEAD - strange symbolic ref"));
556 * Get rid of any old bisect state.
558 if (bisect_clean_state())
562 * In case of mistaken revs or checkout error, or signals received,
563 * "bisect_auto_next" below may exit or misbehave.
564 * We have to trap this to be able to clean up using
565 * "bisect_clean_state".
569 * Write new start state
571 write_file(git_path_bisect_start(), "%s\n", start_head
.buf
);
574 if (get_oid(start_head
.buf
, &oid
) < 0) {
575 retval
= error(_("invalid ref: '%s'"), start_head
.buf
);
578 if (update_ref(NULL
, "BISECT_HEAD", &oid
, NULL
, 0,
579 UPDATE_REFS_MSG_ON_ERR
)) {
585 if (pathspec_pos
< argc
- 1)
586 sq_quote_argv(&bisect_names
, argv
+ pathspec_pos
);
587 write_file(git_path_bisect_names(), "%s\n", bisect_names
.buf
);
589 for (i
= 0; i
< states
.nr
; i
++)
590 if (bisect_write(states
.items
[i
].string
,
591 revs
.items
[i
].string
, terms
, 1)) {
596 if (must_write_terms
&& write_terms(terms
->term_bad
,
602 retval
= bisect_append_log_quoted(argv
);
607 string_list_clear(&revs
, 0);
608 string_list_clear(&states
, 0);
609 strbuf_release(&start_head
);
610 strbuf_release(&bisect_names
);
614 int cmd_bisect__helper(int argc
, const char **argv
, const char *prefix
)
628 int no_checkout
= 0, res
= 0, nolog
= 0;
629 struct option options
[] = {
630 OPT_CMDMODE(0, "next-all", &cmdmode
,
631 N_("perform 'git bisect next'"), NEXT_ALL
),
632 OPT_CMDMODE(0, "write-terms", &cmdmode
,
633 N_("write the terms to .git/BISECT_TERMS"), WRITE_TERMS
),
634 OPT_CMDMODE(0, "bisect-clean-state", &cmdmode
,
635 N_("cleanup the bisection state"), BISECT_CLEAN_STATE
),
636 OPT_CMDMODE(0, "check-expected-revs", &cmdmode
,
637 N_("check for expected revs"), CHECK_EXPECTED_REVS
),
638 OPT_CMDMODE(0, "bisect-reset", &cmdmode
,
639 N_("reset the bisection state"), BISECT_RESET
),
640 OPT_CMDMODE(0, "bisect-write", &cmdmode
,
641 N_("write out the bisection state in BISECT_LOG"), BISECT_WRITE
),
642 OPT_CMDMODE(0, "check-and-set-terms", &cmdmode
,
643 N_("check and set terms in a bisection state"), CHECK_AND_SET_TERMS
),
644 OPT_CMDMODE(0, "bisect-next-check", &cmdmode
,
645 N_("check whether bad or good terms exist"), BISECT_NEXT_CHECK
),
646 OPT_CMDMODE(0, "bisect-terms", &cmdmode
,
647 N_("print out the bisect terms"), BISECT_TERMS
),
648 OPT_CMDMODE(0, "bisect-start", &cmdmode
,
649 N_("start the bisect session"), BISECT_START
),
650 OPT_BOOL(0, "no-checkout", &no_checkout
,
651 N_("update BISECT_HEAD instead of checking out the current commit")),
652 OPT_BOOL(0, "no-log", &nolog
,
653 N_("no log for BISECT_WRITE")),
656 struct bisect_terms terms
= { .term_good
= NULL
, .term_bad
= NULL
};
658 argc
= parse_options(argc
, argv
, prefix
, options
,
659 git_bisect_helper_usage
,
660 PARSE_OPT_KEEP_DASHDASH
| PARSE_OPT_KEEP_UNKNOWN
);
663 usage_with_options(git_bisect_helper_usage
, options
);
667 return bisect_next_all(the_repository
, prefix
, no_checkout
);
670 return error(_("--write-terms requires two arguments"));
671 return write_terms(argv
[0], argv
[1]);
672 case BISECT_CLEAN_STATE
:
674 return error(_("--bisect-clean-state requires no arguments"));
675 return bisect_clean_state();
676 case CHECK_EXPECTED_REVS
:
677 check_expected_revs(argv
, argc
);
681 return error(_("--bisect-reset requires either no argument or a commit"));
682 return !!bisect_reset(argc
? argv
[0] : NULL
);
684 if (argc
!= 4 && argc
!= 5)
685 return error(_("--bisect-write requires either 4 or 5 arguments"));
686 set_terms(&terms
, argv
[3], argv
[2]);
687 res
= bisect_write(argv
[0], argv
[1], &terms
, nolog
);
689 case CHECK_AND_SET_TERMS
:
691 return error(_("--check-and-set-terms requires 3 arguments"));
692 set_terms(&terms
, argv
[2], argv
[1]);
693 res
= check_and_set_terms(&terms
, argv
[0]);
695 case BISECT_NEXT_CHECK
:
696 if (argc
!= 2 && argc
!= 3)
697 return error(_("--bisect-next-check requires 2 or 3 arguments"));
698 set_terms(&terms
, argv
[1], argv
[0]);
699 res
= bisect_next_check(&terms
, argc
== 3 ? argv
[2] : NULL
);
703 return error(_("--bisect-terms requires 0 or 1 argument"));
704 res
= bisect_terms(&terms
, argc
== 1 ? argv
[0] : NULL
);
707 set_terms(&terms
, "bad", "good");
708 res
= bisect_start(&terms
, no_checkout
, argv
, argc
);
711 return error("BUG: unknown subcommand '%d'", cmdmode
);