3 #include "parse-options.h"
8 #include "run-command.h"
13 static GIT_PATH_FUNC(git_path_bisect_terms
, "BISECT_TERMS")
14 static GIT_PATH_FUNC(git_path_bisect_expected_rev
, "BISECT_EXPECTED_REV")
15 static GIT_PATH_FUNC(git_path_bisect_ancestors_ok
, "BISECT_ANCESTORS_OK")
16 static GIT_PATH_FUNC(git_path_bisect_start
, "BISECT_START")
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")
20 static GIT_PATH_FUNC(git_path_bisect_first_parent
, "BISECT_FIRST_PARENT")
21 static GIT_PATH_FUNC(git_path_bisect_run
, "BISECT_RUN")
23 static const char * const git_bisect_helper_usage
[] = {
24 N_("git bisect--helper --bisect-reset [<commit>]"),
25 "git bisect--helper --bisect-terms [--term-good | --term-old | --term-bad | --term-new]",
26 N_("git bisect--helper --bisect-start [--term-{new,bad}=<term> --term-{old,good}=<term>]"
27 " [--no-checkout] [--first-parent] [<bad> [<good>...]] [--] [<paths>...]"),
28 "git bisect--helper --bisect-next",
29 N_("git bisect--helper --bisect-state (bad|new) [<rev>]"),
30 N_("git bisect--helper --bisect-state (good|old) [<rev>...]"),
31 N_("git bisect--helper --bisect-replay <filename>"),
32 N_("git bisect--helper --bisect-skip [(<rev>|<range>)...]"),
33 "git bisect--helper --bisect-visualize",
34 N_("git bisect--helper --bisect-run <cmd>..."),
38 struct add_bisect_ref_data
{
39 struct rev_info
*revs
;
40 unsigned int object_flags
;
48 static void free_terms(struct bisect_terms
*terms
)
50 FREE_AND_NULL(terms
->term_good
);
51 FREE_AND_NULL(terms
->term_bad
);
54 static void set_terms(struct bisect_terms
*terms
, const char *bad
,
57 free((void *)terms
->term_good
);
58 terms
->term_good
= xstrdup(good
);
59 free((void *)terms
->term_bad
);
60 terms
->term_bad
= xstrdup(bad
);
63 static const char vocab_bad
[] = "bad|new";
64 static const char vocab_good
[] = "good|old";
66 static int bisect_autostart(struct bisect_terms
*terms
);
69 * Check whether the string `term` belongs to the set of strings
70 * included in the variable arguments.
73 static int one_of(const char *term
, ...)
79 va_start(matches
, term
);
80 while (!res
&& (match
= va_arg(matches
, const char *)))
81 res
= !strcmp(term
, match
);
88 * return code BISECT_INTERNAL_SUCCESS_MERGE_BASE
89 * and BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND are codes
90 * that indicate special success.
93 static int is_bisect_success(enum bisect_error res
)
96 res
== BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND
||
97 res
== BISECT_INTERNAL_SUCCESS_MERGE_BASE
;
100 static int write_in_file(const char *path
, const char *mode
, const char *format
, va_list args
)
105 if (strcmp(mode
, "w") && strcmp(mode
, "a"))
106 BUG("write-in-file does not support '%s' mode", mode
);
107 fp
= fopen(path
, mode
);
109 return error_errno(_("cannot open file '%s' in mode '%s'"), path
, mode
);
110 res
= vfprintf(fp
, format
, args
);
113 int saved_errno
= errno
;
116 return error_errno(_("could not write to file '%s'"), path
);
122 __attribute__((format (printf
, 2, 3)))
123 static int write_to_file(const char *path
, const char *format
, ...)
128 va_start(args
, format
);
129 res
= write_in_file(path
, "w", format
, args
);
135 __attribute__((format (printf
, 2, 3)))
136 static int append_to_file(const char *path
, const char *format
, ...)
141 va_start(args
, format
);
142 res
= write_in_file(path
, "a", format
, args
);
148 static int print_file_to_stdout(const char *path
)
150 int fd
= open(path
, O_RDONLY
);
154 return error_errno(_("cannot open file '%s' for reading"), path
);
155 if (copy_fd(fd
, 1) < 0)
156 ret
= error_errno(_("failed to read '%s'"), path
);
161 static int check_term_format(const char *term
, const char *orig_term
)
164 char *new_term
= xstrfmt("refs/bisect/%s", term
);
166 res
= check_refname_format(new_term
, 0);
170 return error(_("'%s' is not a valid term"), term
);
172 if (one_of(term
, "help", "start", "skip", "next", "reset",
173 "visualize", "view", "replay", "log", "run", "terms", NULL
))
174 return error(_("can't use the builtin command '%s' as a term"), term
);
177 * In theory, nothing prevents swapping completely good and bad,
178 * but this situation could be confusing and hasn't been tested
179 * enough. Forbid it for now.
182 if ((strcmp(orig_term
, "bad") && one_of(term
, "bad", "new", NULL
)) ||
183 (strcmp(orig_term
, "good") && one_of(term
, "good", "old", NULL
)))
184 return error(_("can't change the meaning of the term '%s'"), term
);
189 static int write_terms(const char *bad
, const char *good
)
193 if (!strcmp(bad
, good
))
194 return error(_("please use two different terms"));
196 if (check_term_format(bad
, "bad") || check_term_format(good
, "good"))
199 res
= write_to_file(git_path_bisect_terms(), "%s\n%s\n", bad
, good
);
204 static int bisect_reset(const char *commit
)
206 struct strbuf branch
= STRBUF_INIT
;
209 if (strbuf_read_file(&branch
, git_path_bisect_start(), 0) < 1) {
210 printf(_("We are not bisecting.\n"));
213 strbuf_rtrim(&branch
);
215 struct object_id oid
;
217 if (get_oid_commit(commit
, &oid
))
218 return error(_("'%s' is not a valid commit"), commit
);
219 strbuf_addstr(&branch
, commit
);
222 if (!ref_exists("BISECT_HEAD")) {
223 struct strvec argv
= STRVEC_INIT
;
225 strvec_pushl(&argv
, "checkout", branch
.buf
, "--", NULL
);
226 if (run_command_v_opt(argv
.v
, RUN_GIT_CMD
)) {
227 error(_("could not check out original"
228 " HEAD '%s'. Try 'git bisect"
229 " reset <commit>'."), branch
.buf
);
230 strbuf_release(&branch
);
237 strbuf_release(&branch
);
238 return bisect_clean_state();
241 static void log_commit(FILE *fp
, char *fmt
, const char *state
,
242 struct commit
*commit
)
244 struct pretty_print_context pp
= {0};
245 struct strbuf commit_msg
= STRBUF_INIT
;
246 char *label
= xstrfmt(fmt
, state
);
248 format_commit_message(commit
, "%s", &commit_msg
, &pp
);
250 fprintf(fp
, "# %s: [%s] %s\n", label
, oid_to_hex(&commit
->object
.oid
),
253 strbuf_release(&commit_msg
);
257 static int bisect_write(const char *state
, const char *rev
,
258 const struct bisect_terms
*terms
, int nolog
)
260 struct strbuf tag
= STRBUF_INIT
;
261 struct object_id oid
;
262 struct commit
*commit
;
266 if (!strcmp(state
, terms
->term_bad
)) {
267 strbuf_addf(&tag
, "refs/bisect/%s", state
);
268 } else if (one_of(state
, terms
->term_good
, "skip", NULL
)) {
269 strbuf_addf(&tag
, "refs/bisect/%s-%s", state
, rev
);
271 res
= error(_("Bad bisect_write argument: %s"), state
);
275 if (get_oid(rev
, &oid
)) {
276 res
= error(_("couldn't get the oid of the rev '%s'"), rev
);
280 if (update_ref(NULL
, tag
.buf
, &oid
, NULL
, 0,
281 UPDATE_REFS_MSG_ON_ERR
)) {
286 fp
= fopen(git_path_bisect_log(), "a");
288 res
= error_errno(_("couldn't open the file '%s'"), git_path_bisect_log());
292 commit
= lookup_commit_reference(the_repository
, &oid
);
293 log_commit(fp
, "%s", state
, commit
);
296 fprintf(fp
, "git bisect %s %s\n", state
, rev
);
301 strbuf_release(&tag
);
305 static int check_and_set_terms(struct bisect_terms
*terms
, const char *cmd
)
307 int has_term_file
= !is_empty_or_missing_file(git_path_bisect_terms());
309 if (one_of(cmd
, "skip", "start", "terms", NULL
))
312 if (has_term_file
&& strcmp(cmd
, terms
->term_bad
) &&
313 strcmp(cmd
, terms
->term_good
))
314 return error(_("Invalid command: you're currently in a "
315 "%s/%s bisect"), terms
->term_bad
,
318 if (!has_term_file
) {
319 if (one_of(cmd
, "bad", "good", NULL
)) {
320 set_terms(terms
, "bad", "good");
321 return write_terms(terms
->term_bad
, terms
->term_good
);
323 if (one_of(cmd
, "new", "old", NULL
)) {
324 set_terms(terms
, "new", "old");
325 return write_terms(terms
->term_bad
, terms
->term_good
);
332 static int inc_nr(const char *refname
, const struct object_id
*oid
,
333 int flag
, void *cb_data
)
335 unsigned int *nr
= (unsigned int *)cb_data
;
340 static const char need_bad_and_good_revision_warning
[] =
341 N_("You need to give me at least one %s and %s revision.\n"
342 "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
344 static const char need_bisect_start_warning
[] =
345 N_("You need to start by \"git bisect start\".\n"
346 "You then need to give me at least one %s and %s revision.\n"
347 "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
349 static int decide_next(const struct bisect_terms
*terms
,
350 const char *current_term
, int missing_good
,
353 if (!missing_good
&& !missing_bad
)
358 if (missing_good
&& !missing_bad
&&
359 !strcmp(current_term
, terms
->term_good
)) {
362 * have bad (or new) but not good (or old). We could bisect
363 * although this is less optimum.
365 warning(_("bisecting only with a %s commit"), terms
->term_bad
);
369 * TRANSLATORS: Make sure to include [Y] and [n] in your
370 * translation. The program will only accept English input
373 yesno
= git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO
);
374 if (starts_with(yesno
, "N") || starts_with(yesno
, "n"))
379 if (!is_empty_or_missing_file(git_path_bisect_start()))
380 return error(_(need_bad_and_good_revision_warning
),
381 vocab_bad
, vocab_good
, vocab_bad
, vocab_good
);
383 return error(_(need_bisect_start_warning
),
384 vocab_good
, vocab_bad
, vocab_good
, vocab_bad
);
387 static void bisect_status(struct bisect_state
*state
,
388 const struct bisect_terms
*terms
)
390 char *bad_ref
= xstrfmt("refs/bisect/%s", terms
->term_bad
);
391 char *good_glob
= xstrfmt("%s-*", terms
->term_good
);
393 if (ref_exists(bad_ref
))
396 for_each_glob_ref_in(inc_nr
, good_glob
, "refs/bisect/",
397 (void *) &state
->nr_good
);
403 __attribute__((format (printf
, 1, 2)))
404 static void bisect_log_printf(const char *fmt
, ...)
406 struct strbuf buf
= STRBUF_INIT
;
410 strbuf_vaddf(&buf
, fmt
, ap
);
413 printf("%s", buf
.buf
);
414 append_to_file(git_path_bisect_log(), "# %s", buf
.buf
);
416 strbuf_release(&buf
);
419 static void bisect_print_status(const struct bisect_terms
*terms
)
421 struct bisect_state state
= { 0 };
423 bisect_status(&state
, terms
);
425 /* If we had both, we'd already be started, and shouldn't get here. */
426 if (state
.nr_good
&& state
.nr_bad
)
429 if (!state
.nr_good
&& !state
.nr_bad
)
430 bisect_log_printf(_("status: waiting for both good and bad commits\n"));
431 else if (state
.nr_good
)
432 bisect_log_printf(Q_("status: waiting for bad commit, %d good commit known\n",
433 "status: waiting for bad commit, %d good commits known\n",
434 state
.nr_good
), state
.nr_good
);
436 bisect_log_printf(_("status: waiting for good commit(s), bad commit known\n"));
439 static int bisect_next_check(const struct bisect_terms
*terms
,
440 const char *current_term
)
442 struct bisect_state state
= { 0 };
443 bisect_status(&state
, terms
);
444 return decide_next(terms
, current_term
, !state
.nr_good
, !state
.nr_bad
);
447 static int get_terms(struct bisect_terms
*terms
)
449 struct strbuf str
= STRBUF_INIT
;
453 fp
= fopen(git_path_bisect_terms(), "r");
460 strbuf_getline_lf(&str
, fp
);
461 terms
->term_bad
= strbuf_detach(&str
, NULL
);
462 strbuf_getline_lf(&str
, fp
);
463 terms
->term_good
= strbuf_detach(&str
, NULL
);
468 strbuf_release(&str
);
472 static int bisect_terms(struct bisect_terms
*terms
, const char *option
)
474 if (get_terms(terms
))
475 return error(_("no terms defined"));
478 printf(_("Your current terms are %s for the old state\n"
479 "and %s for the new state.\n"),
480 terms
->term_good
, terms
->term_bad
);
483 if (one_of(option
, "--term-good", "--term-old", NULL
))
484 printf("%s\n", terms
->term_good
);
485 else if (one_of(option
, "--term-bad", "--term-new", NULL
))
486 printf("%s\n", terms
->term_bad
);
488 return error(_("invalid argument %s for 'git bisect terms'.\n"
489 "Supported options are: "
490 "--term-good|--term-old and "
491 "--term-bad|--term-new."), option
);
496 static int bisect_append_log_quoted(const char **argv
)
499 FILE *fp
= fopen(git_path_bisect_log(), "a");
500 struct strbuf orig_args
= STRBUF_INIT
;
505 if (fprintf(fp
, "git bisect start") < 1) {
510 sq_quote_argv(&orig_args
, argv
);
511 if (fprintf(fp
, "%s\n", orig_args
.buf
) < 1)
516 strbuf_release(&orig_args
);
520 static int add_bisect_ref(const char *refname
, const struct object_id
*oid
,
523 struct add_bisect_ref_data
*data
= cb
;
525 add_pending_oid(data
->revs
, refname
, oid
, data
->object_flags
);
530 static int prepare_revs(struct bisect_terms
*terms
, struct rev_info
*revs
)
533 struct add_bisect_ref_data cb
= { revs
};
534 char *good
= xstrfmt("%s-*", terms
->term_good
);
537 * We cannot use terms->term_bad directly in
538 * for_each_glob_ref_in() and we have to append a '*' to it,
539 * otherwise for_each_glob_ref_in() will append '/' and '*'.
541 char *bad
= xstrfmt("%s*", terms
->term_bad
);
544 * It is important to reset the flags used by revision walks
545 * as the previous call to bisect_next_all() in turn
546 * sets up a revision walk.
548 reset_revision_walk();
549 init_revisions(revs
, NULL
);
550 setup_revisions(0, NULL
, revs
, NULL
);
551 for_each_glob_ref_in(add_bisect_ref
, bad
, "refs/bisect/", &cb
);
552 cb
.object_flags
= UNINTERESTING
;
553 for_each_glob_ref_in(add_bisect_ref
, good
, "refs/bisect/", &cb
);
554 if (prepare_revision_walk(revs
))
555 res
= error(_("revision walk setup failed\n"));
562 static int bisect_skipped_commits(struct bisect_terms
*terms
)
566 struct rev_info revs
;
567 struct commit
*commit
;
568 struct pretty_print_context pp
= {0};
569 struct strbuf commit_name
= STRBUF_INIT
;
571 res
= prepare_revs(terms
, &revs
);
575 fp
= fopen(git_path_bisect_log(), "a");
577 return error_errno(_("could not open '%s' for appending"),
578 git_path_bisect_log());
580 if (fprintf(fp
, "# only skipped commits left to test\n") < 0)
581 return error_errno(_("failed to write to '%s'"), git_path_bisect_log());
583 while ((commit
= get_revision(&revs
)) != NULL
) {
584 strbuf_reset(&commit_name
);
585 format_commit_message(commit
, "%s",
587 fprintf(fp
, "# possible first %s commit: [%s] %s\n",
588 terms
->term_bad
, oid_to_hex(&commit
->object
.oid
),
593 * Reset the flags used by revision walks in case
594 * there is another revision walk after this one.
596 reset_revision_walk();
598 strbuf_release(&commit_name
);
599 release_revisions(&revs
);
604 static int bisect_successful(struct bisect_terms
*terms
)
606 struct object_id oid
;
607 struct commit
*commit
;
608 struct pretty_print_context pp
= {0};
609 struct strbuf commit_name
= STRBUF_INIT
;
610 char *bad_ref
= xstrfmt("refs/bisect/%s",terms
->term_bad
);
613 read_ref(bad_ref
, &oid
);
614 commit
= lookup_commit_reference_by_name(bad_ref
);
615 format_commit_message(commit
, "%s", &commit_name
, &pp
);
617 res
= append_to_file(git_path_bisect_log(), "# first %s commit: [%s] %s\n",
618 terms
->term_bad
, oid_to_hex(&commit
->object
.oid
),
621 strbuf_release(&commit_name
);
626 static enum bisect_error
bisect_next(struct bisect_terms
*terms
, const char *prefix
)
628 enum bisect_error res
;
630 if (bisect_autostart(terms
))
631 return BISECT_FAILED
;
633 if (bisect_next_check(terms
, terms
->term_good
))
634 return BISECT_FAILED
;
636 /* Perform all bisection computation */
637 res
= bisect_next_all(the_repository
, prefix
);
639 if (res
== BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND
) {
640 res
= bisect_successful(terms
);
641 return res
? res
: BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND
;
642 } else if (res
== BISECT_ONLY_SKIPPED_LEFT
) {
643 res
= bisect_skipped_commits(terms
);
644 return res
? res
: BISECT_ONLY_SKIPPED_LEFT
;
649 static enum bisect_error
bisect_auto_next(struct bisect_terms
*terms
, const char *prefix
)
651 if (bisect_next_check(terms
, NULL
)) {
652 bisect_print_status(terms
);
656 return bisect_next(terms
, prefix
);
659 static enum bisect_error
bisect_start(struct bisect_terms
*terms
, const char **argv
, int argc
)
662 int first_parent_only
= 0;
663 int i
, has_double_dash
= 0, must_write_terms
= 0, bad_seen
= 0;
664 int flags
, pathspec_pos
;
665 enum bisect_error res
= BISECT_OK
;
666 struct string_list revs
= STRING_LIST_INIT_DUP
;
667 struct string_list states
= STRING_LIST_INIT_DUP
;
668 struct strbuf start_head
= STRBUF_INIT
;
669 struct strbuf bisect_names
= STRBUF_INIT
;
670 struct object_id head_oid
;
671 struct object_id oid
;
674 if (is_bare_repository())
678 * Check for one bad and then some good revisions
680 for (i
= 0; i
< argc
; i
++) {
681 if (!strcmp(argv
[i
], "--")) {
687 for (i
= 0; i
< argc
; i
++) {
688 const char *arg
= argv
[i
];
689 if (!strcmp(argv
[i
], "--")) {
691 } else if (!strcmp(arg
, "--no-checkout")) {
693 } else if (!strcmp(arg
, "--first-parent")) {
694 first_parent_only
= 1;
695 } else if (!strcmp(arg
, "--term-good") ||
696 !strcmp(arg
, "--term-old")) {
699 return error(_("'' is not a valid term"));
700 must_write_terms
= 1;
701 free((void *) terms
->term_good
);
702 terms
->term_good
= xstrdup(argv
[i
]);
703 } else if (skip_prefix(arg
, "--term-good=", &arg
) ||
704 skip_prefix(arg
, "--term-old=", &arg
)) {
705 must_write_terms
= 1;
706 free((void *) terms
->term_good
);
707 terms
->term_good
= xstrdup(arg
);
708 } else if (!strcmp(arg
, "--term-bad") ||
709 !strcmp(arg
, "--term-new")) {
712 return error(_("'' is not a valid term"));
713 must_write_terms
= 1;
714 free((void *) terms
->term_bad
);
715 terms
->term_bad
= xstrdup(argv
[i
]);
716 } else if (skip_prefix(arg
, "--term-bad=", &arg
) ||
717 skip_prefix(arg
, "--term-new=", &arg
)) {
718 must_write_terms
= 1;
719 free((void *) terms
->term_bad
);
720 terms
->term_bad
= xstrdup(arg
);
721 } else if (starts_with(arg
, "--")) {
722 return error(_("unrecognized option: '%s'"), arg
);
723 } else if (!get_oidf(&oid
, "%s^{commit}", arg
)) {
724 string_list_append(&revs
, oid_to_hex(&oid
));
725 } else if (has_double_dash
) {
726 die(_("'%s' does not appear to be a valid "
735 * The user ran "git bisect start <sha1> <sha1>", hence did not
736 * explicitly specify the terms, but we are already starting to
737 * set references named with the default terms, and won't be able
738 * to change afterwards.
741 must_write_terms
= 1;
742 for (i
= 0; i
< revs
.nr
; i
++) {
744 string_list_append(&states
, terms
->term_good
);
747 string_list_append(&states
, terms
->term_bad
);
754 head
= resolve_ref_unsafe("HEAD", 0, &head_oid
, &flags
);
756 if (get_oid("HEAD", &head_oid
))
757 return error(_("bad HEAD - I need a HEAD"));
760 * Check if we are bisecting
762 if (!is_empty_or_missing_file(git_path_bisect_start())) {
763 /* Reset to the rev from where we started */
764 strbuf_read_file(&start_head
, git_path_bisect_start(), 0);
765 strbuf_trim(&start_head
);
767 struct strvec argv
= STRVEC_INIT
;
769 strvec_pushl(&argv
, "checkout", start_head
.buf
,
771 if (run_command_v_opt(argv
.v
, RUN_GIT_CMD
)) {
772 res
= error(_("checking out '%s' failed."
773 " Try 'git bisect start "
780 /* Get the rev from where we start. */
781 if (!get_oid(head
, &head_oid
) &&
782 !starts_with(head
, "refs/heads/")) {
783 strbuf_reset(&start_head
);
784 strbuf_addstr(&start_head
, oid_to_hex(&head_oid
));
785 } else if (!get_oid(head
, &head_oid
) &&
786 skip_prefix(head
, "refs/heads/", &head
)) {
788 * This error message should only be triggered by
789 * cogito usage, and cogito users should understand
790 * it relates to cg-seek.
792 if (!is_empty_or_missing_file(git_path_head_name()))
793 return error(_("won't bisect on cg-seek'ed tree"));
794 strbuf_addstr(&start_head
, head
);
796 return error(_("bad HEAD - strange symbolic ref"));
801 * Get rid of any old bisect state.
803 if (bisect_clean_state())
804 return BISECT_FAILED
;
807 * Write new start state
809 write_file(git_path_bisect_start(), "%s\n", start_head
.buf
);
811 if (first_parent_only
)
812 write_file(git_path_bisect_first_parent(), "\n");
815 if (get_oid(start_head
.buf
, &oid
) < 0) {
816 res
= error(_("invalid ref: '%s'"), start_head
.buf
);
819 if (update_ref(NULL
, "BISECT_HEAD", &oid
, NULL
, 0,
820 UPDATE_REFS_MSG_ON_ERR
)) {
826 if (pathspec_pos
< argc
- 1)
827 sq_quote_argv(&bisect_names
, argv
+ pathspec_pos
);
828 write_file(git_path_bisect_names(), "%s\n", bisect_names
.buf
);
830 for (i
= 0; i
< states
.nr
; i
++)
831 if (bisect_write(states
.items
[i
].string
,
832 revs
.items
[i
].string
, terms
, 1)) {
837 if (must_write_terms
&& write_terms(terms
->term_bad
,
843 res
= bisect_append_log_quoted(argv
);
848 string_list_clear(&revs
, 0);
849 string_list_clear(&states
, 0);
850 strbuf_release(&start_head
);
851 strbuf_release(&bisect_names
);
855 res
= bisect_auto_next(terms
, NULL
);
856 if (!is_bisect_success(res
))
857 bisect_clean_state();
861 static inline int file_is_not_empty(const char *path
)
863 return !is_empty_or_missing_file(path
);
866 static int bisect_autostart(struct bisect_terms
*terms
)
871 if (file_is_not_empty(git_path_bisect_start()))
874 fprintf_ln(stderr
, _("You need to start by \"git bisect "
877 if (!isatty(STDIN_FILENO
))
881 * TRANSLATORS: Make sure to include [Y] and [n] in your
882 * translation. The program will only accept English input
885 yesno
= git_prompt(_("Do you want me to do it for you "
886 "[Y/n]? "), PROMPT_ECHO
);
887 res
= tolower(*yesno
) == 'n' ?
888 -1 : bisect_start(terms
, empty_strvec
, 0);
893 static enum bisect_error
bisect_state(struct bisect_terms
*terms
, const char **argv
,
897 int i
, verify_expected
= 1;
898 struct object_id oid
, expected
;
899 struct strbuf buf
= STRBUF_INIT
;
900 struct oid_array revs
= OID_ARRAY_INIT
;
903 return error(_("Please call `--bisect-state` with at least one argument"));
905 if (bisect_autostart(terms
))
906 return BISECT_FAILED
;
909 if (check_and_set_terms(terms
, state
) ||
910 !one_of(state
, terms
->term_good
, terms
->term_bad
, "skip", NULL
))
911 return BISECT_FAILED
;
915 if (argc
> 1 && !strcmp(state
, terms
->term_bad
))
916 return error(_("'git bisect %s' can take only one argument."), terms
->term_bad
);
919 const char *head
= "BISECT_HEAD";
920 enum get_oid_result res_head
= get_oid(head
, &oid
);
922 if (res_head
== MISSING_OBJECT
) {
924 res_head
= get_oid(head
, &oid
);
928 error(_("Bad rev input: %s"), head
);
929 oid_array_append(&revs
, &oid
);
933 * All input revs must be checked before executing bisect_write()
934 * to discard junk revs.
937 for (; argc
; argc
--, argv
++) {
938 struct commit
*commit
;
940 if (get_oid(*argv
, &oid
)){
941 error(_("Bad rev input: %s"), *argv
);
942 oid_array_clear(&revs
);
943 return BISECT_FAILED
;
946 commit
= lookup_commit_reference(the_repository
, &oid
);
948 die(_("Bad rev input (not a commit): %s"), *argv
);
950 oid_array_append(&revs
, &commit
->object
.oid
);
953 if (strbuf_read_file(&buf
, git_path_bisect_expected_rev(), 0) < the_hash_algo
->hexsz
||
954 get_oid_hex(buf
.buf
, &expected
) < 0)
955 verify_expected
= 0; /* Ignore invalid file contents */
956 strbuf_release(&buf
);
958 for (i
= 0; i
< revs
.nr
; i
++) {
959 if (bisect_write(state
, oid_to_hex(&revs
.oid
[i
]), terms
, 0)) {
960 oid_array_clear(&revs
);
961 return BISECT_FAILED
;
963 if (verify_expected
&& !oideq(&revs
.oid
[i
], &expected
)) {
964 unlink_or_warn(git_path_bisect_ancestors_ok());
965 unlink_or_warn(git_path_bisect_expected_rev());
970 oid_array_clear(&revs
);
971 return bisect_auto_next(terms
, NULL
);
974 static enum bisect_error
bisect_log(void)
977 const char* filename
= git_path_bisect_log();
979 if (is_empty_or_missing_file(filename
))
980 return error(_("We are not bisecting."));
982 fd
= open(filename
, O_RDONLY
);
984 return BISECT_FAILED
;
986 status
= copy_fd(fd
, STDOUT_FILENO
);
988 return status
? BISECT_FAILED
: BISECT_OK
;
991 static int process_replay_line(struct bisect_terms
*terms
, struct strbuf
*line
)
993 const char *p
= line
->buf
+ strspn(line
->buf
, " \t");
994 char *word_end
, *rev
;
996 if ((!skip_prefix(p
, "git bisect", &p
) &&
997 !skip_prefix(p
, "git-bisect", &p
)) || !isspace(*p
))
999 p
+= strspn(p
, " \t");
1001 word_end
= (char *)p
+ strcspn(p
, " \t");
1002 rev
= word_end
+ strspn(word_end
, " \t");
1003 *word_end
= '\0'; /* NUL-terminate the word */
1006 if (check_and_set_terms(terms
, p
))
1009 if (!strcmp(p
, "start")) {
1010 struct strvec argv
= STRVEC_INIT
;
1012 sq_dequote_to_strvec(rev
, &argv
);
1013 res
= bisect_start(terms
, argv
.v
, argv
.nr
);
1014 strvec_clear(&argv
);
1018 if (one_of(p
, terms
->term_good
,
1019 terms
->term_bad
, "skip", NULL
))
1020 return bisect_write(p
, rev
, terms
, 0);
1022 if (!strcmp(p
, "terms")) {
1023 struct strvec argv
= STRVEC_INIT
;
1025 sq_dequote_to_strvec(rev
, &argv
);
1026 res
= bisect_terms(terms
, argv
.nr
== 1 ? argv
.v
[0] : NULL
);
1027 strvec_clear(&argv
);
1030 error(_("'%s'?? what are you talking about?"), p
);
1035 static enum bisect_error
bisect_replay(struct bisect_terms
*terms
, const char *filename
)
1038 enum bisect_error res
= BISECT_OK
;
1039 struct strbuf line
= STRBUF_INIT
;
1041 if (is_empty_or_missing_file(filename
))
1042 return error(_("cannot read file '%s' for replaying"), filename
);
1044 if (bisect_reset(NULL
))
1045 return BISECT_FAILED
;
1047 fp
= fopen(filename
, "r");
1049 return BISECT_FAILED
;
1051 while ((strbuf_getline(&line
, fp
) != EOF
) && !res
)
1052 res
= process_replay_line(terms
, &line
);
1054 strbuf_release(&line
);
1058 return BISECT_FAILED
;
1060 return bisect_auto_next(terms
, NULL
);
1063 static enum bisect_error
bisect_skip(struct bisect_terms
*terms
, const char **argv
, int argc
)
1066 enum bisect_error res
;
1067 struct strvec argv_state
= STRVEC_INIT
;
1069 strvec_push(&argv_state
, "skip");
1071 for (i
= 0; i
< argc
; i
++) {
1072 const char *dotdot
= strstr(argv
[i
], "..");
1075 struct rev_info revs
;
1076 struct commit
*commit
;
1078 init_revisions(&revs
, NULL
);
1079 setup_revisions(2, argv
+ i
- 1, &revs
, NULL
);
1081 if (prepare_revision_walk(&revs
))
1082 die(_("revision walk setup failed\n"));
1083 while ((commit
= get_revision(&revs
)) != NULL
)
1084 strvec_push(&argv_state
,
1085 oid_to_hex(&commit
->object
.oid
));
1087 reset_revision_walk();
1088 release_revisions(&revs
);
1090 strvec_push(&argv_state
, argv
[i
]);
1093 res
= bisect_state(terms
, argv_state
.v
, argv_state
.nr
);
1095 strvec_clear(&argv_state
);
1099 static int bisect_visualize(struct bisect_terms
*terms
, const char **argv
, int argc
)
1101 struct strvec args
= STRVEC_INIT
;
1102 int flags
= RUN_COMMAND_NO_STDIN
, res
= 0;
1103 struct strbuf sb
= STRBUF_INIT
;
1105 if (bisect_next_check(terms
, NULL
) != 0)
1106 return BISECT_FAILED
;
1109 if ((getenv("DISPLAY") || getenv("SESSIONNAME") || getenv("MSYSTEM") ||
1110 getenv("SECURITYSESSIONID")) && exists_in_PATH("gitk")) {
1111 strvec_push(&args
, "gitk");
1113 strvec_push(&args
, "log");
1114 flags
|= RUN_GIT_CMD
;
1117 if (argv
[0][0] == '-') {
1118 strvec_push(&args
, "log");
1119 flags
|= RUN_GIT_CMD
;
1120 } else if (strcmp(argv
[0], "tig") && !starts_with(argv
[0], "git"))
1121 flags
|= RUN_GIT_CMD
;
1123 strvec_pushv(&args
, argv
);
1126 strvec_pushl(&args
, "--bisect", "--", NULL
);
1128 strbuf_read_file(&sb
, git_path_bisect_names(), 0);
1129 sq_dequote_to_strvec(sb
.buf
, &args
);
1130 strbuf_release(&sb
);
1132 res
= run_command_v_opt(args
.v
, flags
);
1133 strvec_clear(&args
);
1137 static int get_first_good(const char *refname
, const struct object_id
*oid
,
1138 int flag
, void *cb_data
)
1140 oidcpy(cb_data
, oid
);
1144 static int verify_good(const struct bisect_terms
*terms
,
1145 const char **quoted_argv
)
1148 enum bisect_error res
;
1149 struct object_id good_rev
;
1150 struct object_id current_rev
;
1151 char *good_glob
= xstrfmt("%s-*", terms
->term_good
);
1152 int no_checkout
= ref_exists("BISECT_HEAD");
1154 for_each_glob_ref_in(get_first_good
, good_glob
, "refs/bisect/",
1158 if (read_ref(no_checkout
? "BISECT_HEAD" : "HEAD", ¤t_rev
))
1161 res
= bisect_checkout(&good_rev
, no_checkout
);
1162 if (res
!= BISECT_OK
)
1165 printf(_("running %s\n"), quoted_argv
[0]);
1166 rc
= run_command_v_opt(quoted_argv
, RUN_USING_SHELL
);
1168 res
= bisect_checkout(¤t_rev
, no_checkout
);
1169 if (res
!= BISECT_OK
)
1175 static int bisect_run(struct bisect_terms
*terms
, const char **argv
, int argc
)
1177 int res
= BISECT_OK
;
1178 struct strbuf command
= STRBUF_INIT
;
1179 struct strvec run_args
= STRVEC_INIT
;
1180 const char *new_state
;
1181 int temporary_stdout_fd
, saved_stdout
;
1182 int is_first_run
= 1;
1184 if (bisect_next_check(terms
, NULL
))
1185 return BISECT_FAILED
;
1188 sq_quote_argv(&command
, argv
);
1190 error(_("bisect run failed: no command provided."));
1191 return BISECT_FAILED
;
1194 strvec_push(&run_args
, command
.buf
);
1197 printf(_("running %s\n"), command
.buf
);
1198 res
= run_command_v_opt(run_args
.v
, RUN_USING_SHELL
);
1201 * Exit code 126 and 127 can either come from the shell
1202 * if it was unable to execute or even find the script,
1203 * or from the script itself. Check with a known-good
1204 * revision to avoid trashing the bisect run due to a
1205 * missing or non-executable script.
1207 if (is_first_run
&& (res
== 126 || res
== 127)) {
1208 int rc
= verify_good(terms
, run_args
.v
);
1211 error(_("unable to verify '%s' on good"
1212 " revision"), command
.buf
);
1213 res
= BISECT_FAILED
;
1217 error(_("bogus exit code %d for good revision"),
1219 res
= BISECT_FAILED
;
1224 if (res
< 0 || 128 <= res
) {
1225 error(_("bisect run failed: exit code %d from"
1226 " '%s' is < 0 or >= 128"), res
, command
.buf
);
1233 new_state
= terms
->term_good
;
1235 new_state
= terms
->term_bad
;
1237 temporary_stdout_fd
= open(git_path_bisect_run(), O_CREAT
| O_WRONLY
| O_TRUNC
, 0666);
1239 if (temporary_stdout_fd
< 0) {
1240 res
= error_errno(_("cannot open file '%s' for writing"), git_path_bisect_run());
1245 saved_stdout
= dup(1);
1246 dup2(temporary_stdout_fd
, 1);
1248 res
= bisect_state(terms
, &new_state
, 1);
1251 dup2(saved_stdout
, 1);
1252 close(saved_stdout
);
1253 close(temporary_stdout_fd
);
1255 print_file_to_stdout(git_path_bisect_run());
1257 if (res
== BISECT_ONLY_SKIPPED_LEFT
)
1258 error(_("bisect run cannot continue any more"));
1259 else if (res
== BISECT_INTERNAL_SUCCESS_MERGE_BASE
) {
1260 printf(_("bisect run success"));
1262 } else if (res
== BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND
) {
1263 printf(_("bisect found first bad commit"));
1266 error(_("bisect run failed: 'git bisect--helper --bisect-state"
1267 " %s' exited with error code %d"), new_state
, res
);
1274 strbuf_release(&command
);
1275 strvec_clear(&run_args
);
1279 int cmd_bisect__helper(int argc
, const char **argv
, const char *prefix
)
1295 int res
= 0, nolog
= 0;
1296 struct option options
[] = {
1297 OPT_CMDMODE(0, "bisect-reset", &cmdmode
,
1298 N_("reset the bisection state"), BISECT_RESET
),
1299 OPT_CMDMODE(0, "bisect-next-check", &cmdmode
,
1300 N_("check whether bad or good terms exist"), BISECT_NEXT_CHECK
),
1301 OPT_CMDMODE(0, "bisect-terms", &cmdmode
,
1302 N_("print out the bisect terms"), BISECT_TERMS
),
1303 OPT_CMDMODE(0, "bisect-start", &cmdmode
,
1304 N_("start the bisect session"), BISECT_START
),
1305 OPT_CMDMODE(0, "bisect-next", &cmdmode
,
1306 N_("find the next bisection commit"), BISECT_NEXT
),
1307 OPT_CMDMODE(0, "bisect-state", &cmdmode
,
1308 N_("mark the state of ref (or refs)"), BISECT_STATE
),
1309 OPT_CMDMODE(0, "bisect-log", &cmdmode
,
1310 N_("list the bisection steps so far"), BISECT_LOG
),
1311 OPT_CMDMODE(0, "bisect-replay", &cmdmode
,
1312 N_("replay the bisection process from the given file"), BISECT_REPLAY
),
1313 OPT_CMDMODE(0, "bisect-skip", &cmdmode
,
1314 N_("skip some commits for checkout"), BISECT_SKIP
),
1315 OPT_CMDMODE(0, "bisect-visualize", &cmdmode
,
1316 N_("visualize the bisection"), BISECT_VISUALIZE
),
1317 OPT_CMDMODE(0, "bisect-run", &cmdmode
,
1318 N_("use <cmd>... to automatically bisect"), BISECT_RUN
),
1319 OPT_BOOL(0, "no-log", &nolog
,
1320 N_("no log for BISECT_WRITE")),
1323 struct bisect_terms terms
= { .term_good
= NULL
, .term_bad
= NULL
};
1325 argc
= parse_options(argc
, argv
, prefix
, options
,
1326 git_bisect_helper_usage
,
1327 PARSE_OPT_KEEP_DASHDASH
| PARSE_OPT_KEEP_UNKNOWN
);
1330 usage_with_options(git_bisect_helper_usage
, options
);
1335 return error(_("--bisect-reset requires either no argument or a commit"));
1336 res
= bisect_reset(argc
? argv
[0] : NULL
);
1340 return error(_("--bisect-terms requires 0 or 1 argument"));
1341 res
= bisect_terms(&terms
, argc
== 1 ? argv
[0] : NULL
);
1344 set_terms(&terms
, "bad", "good");
1345 res
= bisect_start(&terms
, argv
, argc
);
1349 return error(_("--bisect-next requires 0 arguments"));
1351 res
= bisect_next(&terms
, prefix
);
1354 set_terms(&terms
, "bad", "good");
1356 res
= bisect_state(&terms
, argv
, argc
);
1360 return error(_("--bisect-log requires 0 arguments"));
1365 return error(_("no logfile given"));
1366 set_terms(&terms
, "bad", "good");
1367 res
= bisect_replay(&terms
, argv
[0]);
1370 set_terms(&terms
, "bad", "good");
1372 res
= bisect_skip(&terms
, argv
, argc
);
1374 case BISECT_VISUALIZE
:
1376 res
= bisect_visualize(&terms
, argv
, argc
);
1380 return error(_("bisect run failed: no command provided."));
1382 res
= bisect_run(&terms
, argv
, argc
);
1385 BUG("unknown subcommand %d", cmdmode
);
1390 * Handle early success
1391 * From check_merge_bases > check_good_are_ancestors_of_bad > bisect_next_all
1393 if ((res
== BISECT_INTERNAL_SUCCESS_MERGE_BASE
) || (res
== BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND
))