4 #include "parse-options.h"
9 #include "run-command.h"
14 static GIT_PATH_FUNC(git_path_bisect_terms
, "BISECT_TERMS")
15 static GIT_PATH_FUNC(git_path_bisect_expected_rev
, "BISECT_EXPECTED_REV")
16 static GIT_PATH_FUNC(git_path_bisect_ancestors_ok
, "BISECT_ANCESTORS_OK")
17 static GIT_PATH_FUNC(git_path_bisect_start
, "BISECT_START")
18 static GIT_PATH_FUNC(git_path_bisect_log
, "BISECT_LOG")
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 #define BUILTIN_GIT_BISECT_START_USAGE \
24 N_("git bisect start [--term-{new,bad}=<term> --term-{old,good}=<term>]" \
25 " [--no-checkout] [--first-parent] [<bad> [<good>...]] [--]" \
27 #define BUILTIN_GIT_BISECT_STATE_USAGE \
28 N_("git bisect (good|bad) [<rev>...]")
29 #define BUILTIN_GIT_BISECT_TERMS_USAGE \
30 "git bisect terms [--term-good | --term-bad]"
31 #define BUILTIN_GIT_BISECT_SKIP_USAGE \
32 N_("git bisect skip [(<rev>|<range>)...]")
33 #define BUILTIN_GIT_BISECT_NEXT_USAGE \
35 #define BUILTIN_GIT_BISECT_RESET_USAGE \
36 N_("git bisect reset [<commit>]")
37 #define BUILTIN_GIT_BISECT_VISUALIZE_USAGE \
38 "git bisect visualize"
39 #define BUILTIN_GIT_BISECT_REPLAY_USAGE \
40 N_("git bisect replay <logfile>")
41 #define BUILTIN_GIT_BISECT_LOG_USAGE \
43 #define BUILTIN_GIT_BISECT_RUN_USAGE \
44 N_("git bisect run <cmd>...")
46 static const char * const git_bisect_usage
[] = {
47 BUILTIN_GIT_BISECT_START_USAGE
,
48 BUILTIN_GIT_BISECT_STATE_USAGE
,
49 BUILTIN_GIT_BISECT_TERMS_USAGE
,
50 BUILTIN_GIT_BISECT_SKIP_USAGE
,
51 BUILTIN_GIT_BISECT_NEXT_USAGE
,
52 BUILTIN_GIT_BISECT_RESET_USAGE
,
53 BUILTIN_GIT_BISECT_VISUALIZE_USAGE
,
54 BUILTIN_GIT_BISECT_REPLAY_USAGE
,
55 BUILTIN_GIT_BISECT_LOG_USAGE
,
56 BUILTIN_GIT_BISECT_RUN_USAGE
,
60 struct add_bisect_ref_data
{
61 struct rev_info
*revs
;
62 unsigned int object_flags
;
70 static void free_terms(struct bisect_terms
*terms
)
72 FREE_AND_NULL(terms
->term_good
);
73 FREE_AND_NULL(terms
->term_bad
);
76 static void set_terms(struct bisect_terms
*terms
, const char *bad
,
79 free((void *)terms
->term_good
);
80 terms
->term_good
= xstrdup(good
);
81 free((void *)terms
->term_bad
);
82 terms
->term_bad
= xstrdup(bad
);
85 static const char vocab_bad
[] = "bad|new";
86 static const char vocab_good
[] = "good|old";
88 static int bisect_autostart(struct bisect_terms
*terms
);
91 * Check whether the string `term` belongs to the set of strings
92 * included in the variable arguments.
95 static int one_of(const char *term
, ...)
101 va_start(matches
, term
);
102 while (!res
&& (match
= va_arg(matches
, const char *)))
103 res
= !strcmp(term
, match
);
110 * return code BISECT_INTERNAL_SUCCESS_MERGE_BASE
111 * and BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND are codes
112 * that indicate special success.
115 static int is_bisect_success(enum bisect_error res
)
118 res
== BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND
||
119 res
== BISECT_INTERNAL_SUCCESS_MERGE_BASE
;
122 static int write_in_file(const char *path
, const char *mode
, const char *format
, va_list args
)
127 if (strcmp(mode
, "w") && strcmp(mode
, "a"))
128 BUG("write-in-file does not support '%s' mode", mode
);
129 fp
= fopen(path
, mode
);
131 return error_errno(_("cannot open file '%s' in mode '%s'"), path
, mode
);
132 res
= vfprintf(fp
, format
, args
);
135 int saved_errno
= errno
;
138 return error_errno(_("could not write to file '%s'"), path
);
144 __attribute__((format (printf
, 2, 3)))
145 static int write_to_file(const char *path
, const char *format
, ...)
150 va_start(args
, format
);
151 res
= write_in_file(path
, "w", format
, args
);
157 __attribute__((format (printf
, 2, 3)))
158 static int append_to_file(const char *path
, const char *format
, ...)
163 va_start(args
, format
);
164 res
= write_in_file(path
, "a", format
, args
);
170 static int print_file_to_stdout(const char *path
)
172 int fd
= open(path
, O_RDONLY
);
176 return error_errno(_("cannot open file '%s' for reading"), path
);
177 if (copy_fd(fd
, 1) < 0)
178 ret
= error_errno(_("failed to read '%s'"), path
);
183 static int check_term_format(const char *term
, const char *orig_term
)
186 char *new_term
= xstrfmt("refs/bisect/%s", term
);
188 res
= check_refname_format(new_term
, 0);
192 return error(_("'%s' is not a valid term"), term
);
194 if (one_of(term
, "help", "start", "skip", "next", "reset",
195 "visualize", "view", "replay", "log", "run", "terms", NULL
))
196 return error(_("can't use the builtin command '%s' as a term"), term
);
199 * In theory, nothing prevents swapping completely good and bad,
200 * but this situation could be confusing and hasn't been tested
201 * enough. Forbid it for now.
204 if ((strcmp(orig_term
, "bad") && one_of(term
, "bad", "new", NULL
)) ||
205 (strcmp(orig_term
, "good") && one_of(term
, "good", "old", NULL
)))
206 return error(_("can't change the meaning of the term '%s'"), term
);
211 static int write_terms(const char *bad
, const char *good
)
215 if (!strcmp(bad
, good
))
216 return error(_("please use two different terms"));
218 if (check_term_format(bad
, "bad") || check_term_format(good
, "good"))
221 res
= write_to_file(git_path_bisect_terms(), "%s\n%s\n", bad
, good
);
226 static int bisect_reset(const char *commit
)
228 struct strbuf branch
= STRBUF_INIT
;
231 if (strbuf_read_file(&branch
, git_path_bisect_start(), 0) < 1) {
232 printf(_("We are not bisecting.\n"));
235 strbuf_rtrim(&branch
);
237 struct object_id oid
;
239 if (get_oid_commit(commit
, &oid
))
240 return error(_("'%s' is not a valid commit"), commit
);
241 strbuf_addstr(&branch
, commit
);
244 if (!ref_exists("BISECT_HEAD")) {
245 struct child_process cmd
= CHILD_PROCESS_INIT
;
248 strvec_pushl(&cmd
.args
, "checkout", "--ignore-other-worktrees",
249 branch
.buf
, "--", NULL
);
250 if (run_command(&cmd
)) {
251 error(_("could not check out original"
252 " HEAD '%s'. Try 'git bisect"
253 " reset <commit>'."), branch
.buf
);
254 strbuf_release(&branch
);
259 strbuf_release(&branch
);
260 return bisect_clean_state();
263 static void log_commit(FILE *fp
, char *fmt
, const char *state
,
264 struct commit
*commit
)
266 struct pretty_print_context pp
= {0};
267 struct strbuf commit_msg
= STRBUF_INIT
;
268 char *label
= xstrfmt(fmt
, state
);
270 format_commit_message(commit
, "%s", &commit_msg
, &pp
);
272 fprintf(fp
, "# %s: [%s] %s\n", label
, oid_to_hex(&commit
->object
.oid
),
275 strbuf_release(&commit_msg
);
279 static int bisect_write(const char *state
, const char *rev
,
280 const struct bisect_terms
*terms
, int nolog
)
282 struct strbuf tag
= STRBUF_INIT
;
283 struct object_id oid
;
284 struct commit
*commit
;
288 if (!strcmp(state
, terms
->term_bad
)) {
289 strbuf_addf(&tag
, "refs/bisect/%s", state
);
290 } else if (one_of(state
, terms
->term_good
, "skip", NULL
)) {
291 strbuf_addf(&tag
, "refs/bisect/%s-%s", state
, rev
);
293 res
= error(_("Bad bisect_write argument: %s"), state
);
297 if (get_oid(rev
, &oid
)) {
298 res
= error(_("couldn't get the oid of the rev '%s'"), rev
);
302 if (update_ref(NULL
, tag
.buf
, &oid
, NULL
, 0,
303 UPDATE_REFS_MSG_ON_ERR
)) {
308 fp
= fopen(git_path_bisect_log(), "a");
310 res
= error_errno(_("couldn't open the file '%s'"), git_path_bisect_log());
314 commit
= lookup_commit_reference(the_repository
, &oid
);
315 log_commit(fp
, "%s", state
, commit
);
318 fprintf(fp
, "git bisect %s %s\n", state
, rev
);
323 strbuf_release(&tag
);
327 static int check_and_set_terms(struct bisect_terms
*terms
, const char *cmd
)
329 int has_term_file
= !is_empty_or_missing_file(git_path_bisect_terms());
331 if (one_of(cmd
, "skip", "start", "terms", NULL
))
334 if (has_term_file
&& strcmp(cmd
, terms
->term_bad
) &&
335 strcmp(cmd
, terms
->term_good
))
336 return error(_("Invalid command: you're currently in a "
337 "%s/%s bisect"), terms
->term_bad
,
340 if (!has_term_file
) {
341 if (one_of(cmd
, "bad", "good", NULL
)) {
342 set_terms(terms
, "bad", "good");
343 return write_terms(terms
->term_bad
, terms
->term_good
);
345 if (one_of(cmd
, "new", "old", NULL
)) {
346 set_terms(terms
, "new", "old");
347 return write_terms(terms
->term_bad
, terms
->term_good
);
354 static int inc_nr(const char *refname UNUSED
,
355 const struct object_id
*oid UNUSED
,
356 int flag UNUSED
, void *cb_data
)
358 unsigned int *nr
= (unsigned int *)cb_data
;
363 static const char need_bad_and_good_revision_warning
[] =
364 N_("You need to give me at least one %s and %s revision.\n"
365 "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
367 static const char need_bisect_start_warning
[] =
368 N_("You need to start by \"git bisect start\".\n"
369 "You then need to give me at least one %s and %s revision.\n"
370 "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
372 static int decide_next(const struct bisect_terms
*terms
,
373 const char *current_term
, int missing_good
,
376 if (!missing_good
&& !missing_bad
)
381 if (missing_good
&& !missing_bad
&&
382 !strcmp(current_term
, terms
->term_good
)) {
385 * have bad (or new) but not good (or old). We could bisect
386 * although this is less optimum.
388 warning(_("bisecting only with a %s commit"), terms
->term_bad
);
392 * TRANSLATORS: Make sure to include [Y] and [n] in your
393 * translation. The program will only accept English input
396 yesno
= git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO
);
397 if (starts_with(yesno
, "N") || starts_with(yesno
, "n"))
402 if (!is_empty_or_missing_file(git_path_bisect_start()))
403 return error(_(need_bad_and_good_revision_warning
),
404 vocab_bad
, vocab_good
, vocab_bad
, vocab_good
);
406 return error(_(need_bisect_start_warning
),
407 vocab_good
, vocab_bad
, vocab_good
, vocab_bad
);
410 static void bisect_status(struct bisect_state
*state
,
411 const struct bisect_terms
*terms
)
413 char *bad_ref
= xstrfmt("refs/bisect/%s", terms
->term_bad
);
414 char *good_glob
= xstrfmt("%s-*", terms
->term_good
);
416 if (ref_exists(bad_ref
))
419 for_each_glob_ref_in(inc_nr
, good_glob
, "refs/bisect/",
420 (void *) &state
->nr_good
);
426 __attribute__((format (printf
, 1, 2)))
427 static void bisect_log_printf(const char *fmt
, ...)
429 struct strbuf buf
= STRBUF_INIT
;
433 strbuf_vaddf(&buf
, fmt
, ap
);
436 printf("%s", buf
.buf
);
437 append_to_file(git_path_bisect_log(), "# %s", buf
.buf
);
439 strbuf_release(&buf
);
442 static void bisect_print_status(const struct bisect_terms
*terms
)
444 struct bisect_state state
= { 0 };
446 bisect_status(&state
, terms
);
448 /* If we had both, we'd already be started, and shouldn't get here. */
449 if (state
.nr_good
&& state
.nr_bad
)
452 if (!state
.nr_good
&& !state
.nr_bad
)
453 bisect_log_printf(_("status: waiting for both good and bad commits\n"));
454 else if (state
.nr_good
)
455 bisect_log_printf(Q_("status: waiting for bad commit, %d good commit known\n",
456 "status: waiting for bad commit, %d good commits known\n",
457 state
.nr_good
), state
.nr_good
);
459 bisect_log_printf(_("status: waiting for good commit(s), bad commit known\n"));
462 static int bisect_next_check(const struct bisect_terms
*terms
,
463 const char *current_term
)
465 struct bisect_state state
= { 0 };
466 bisect_status(&state
, terms
);
467 return decide_next(terms
, current_term
, !state
.nr_good
, !state
.nr_bad
);
470 static int get_terms(struct bisect_terms
*terms
)
472 struct strbuf str
= STRBUF_INIT
;
476 fp
= fopen(git_path_bisect_terms(), "r");
483 strbuf_getline_lf(&str
, fp
);
484 terms
->term_bad
= strbuf_detach(&str
, NULL
);
485 strbuf_getline_lf(&str
, fp
);
486 terms
->term_good
= strbuf_detach(&str
, NULL
);
491 strbuf_release(&str
);
495 static int bisect_terms(struct bisect_terms
*terms
, const char *option
)
497 if (get_terms(terms
))
498 return error(_("no terms defined"));
501 printf(_("Your current terms are %s for the old state\n"
502 "and %s for the new state.\n"),
503 terms
->term_good
, terms
->term_bad
);
506 if (one_of(option
, "--term-good", "--term-old", NULL
))
507 printf("%s\n", terms
->term_good
);
508 else if (one_of(option
, "--term-bad", "--term-new", NULL
))
509 printf("%s\n", terms
->term_bad
);
511 return error(_("invalid argument %s for 'git bisect terms'.\n"
512 "Supported options are: "
513 "--term-good|--term-old and "
514 "--term-bad|--term-new."), option
);
519 static int bisect_append_log_quoted(const char **argv
)
522 FILE *fp
= fopen(git_path_bisect_log(), "a");
523 struct strbuf orig_args
= STRBUF_INIT
;
528 if (fprintf(fp
, "git bisect start") < 1) {
533 sq_quote_argv(&orig_args
, argv
);
534 if (fprintf(fp
, "%s\n", orig_args
.buf
) < 1)
539 strbuf_release(&orig_args
);
543 static int add_bisect_ref(const char *refname
, const struct object_id
*oid
,
544 int flags UNUSED
, void *cb
)
546 struct add_bisect_ref_data
*data
= cb
;
548 add_pending_oid(data
->revs
, refname
, oid
, data
->object_flags
);
553 static int prepare_revs(struct bisect_terms
*terms
, struct rev_info
*revs
)
556 struct add_bisect_ref_data cb
= { revs
};
557 char *good
= xstrfmt("%s-*", terms
->term_good
);
560 * We cannot use terms->term_bad directly in
561 * for_each_glob_ref_in() and we have to append a '*' to it,
562 * otherwise for_each_glob_ref_in() will append '/' and '*'.
564 char *bad
= xstrfmt("%s*", terms
->term_bad
);
567 * It is important to reset the flags used by revision walks
568 * as the previous call to bisect_next_all() in turn
569 * sets up a revision walk.
571 reset_revision_walk();
572 init_revisions(revs
, NULL
);
573 setup_revisions(0, NULL
, revs
, NULL
);
574 for_each_glob_ref_in(add_bisect_ref
, bad
, "refs/bisect/", &cb
);
575 cb
.object_flags
= UNINTERESTING
;
576 for_each_glob_ref_in(add_bisect_ref
, good
, "refs/bisect/", &cb
);
577 if (prepare_revision_walk(revs
))
578 res
= error(_("revision walk setup failed\n"));
585 static int bisect_skipped_commits(struct bisect_terms
*terms
)
589 struct rev_info revs
;
590 struct commit
*commit
;
591 struct pretty_print_context pp
= {0};
592 struct strbuf commit_name
= STRBUF_INIT
;
594 res
= prepare_revs(terms
, &revs
);
598 fp
= fopen(git_path_bisect_log(), "a");
600 return error_errno(_("could not open '%s' for appending"),
601 git_path_bisect_log());
603 if (fprintf(fp
, "# only skipped commits left to test\n") < 0)
604 return error_errno(_("failed to write to '%s'"), git_path_bisect_log());
606 while ((commit
= get_revision(&revs
)) != NULL
) {
607 strbuf_reset(&commit_name
);
608 format_commit_message(commit
, "%s",
610 fprintf(fp
, "# possible first %s commit: [%s] %s\n",
611 terms
->term_bad
, oid_to_hex(&commit
->object
.oid
),
616 * Reset the flags used by revision walks in case
617 * there is another revision walk after this one.
619 reset_revision_walk();
621 strbuf_release(&commit_name
);
622 release_revisions(&revs
);
627 static int bisect_successful(struct bisect_terms
*terms
)
629 struct object_id oid
;
630 struct commit
*commit
;
631 struct pretty_print_context pp
= {0};
632 struct strbuf commit_name
= STRBUF_INIT
;
633 char *bad_ref
= xstrfmt("refs/bisect/%s",terms
->term_bad
);
636 read_ref(bad_ref
, &oid
);
637 commit
= lookup_commit_reference_by_name(bad_ref
);
638 format_commit_message(commit
, "%s", &commit_name
, &pp
);
640 res
= append_to_file(git_path_bisect_log(), "# first %s commit: [%s] %s\n",
641 terms
->term_bad
, oid_to_hex(&commit
->object
.oid
),
644 strbuf_release(&commit_name
);
649 static enum bisect_error
bisect_next(struct bisect_terms
*terms
, const char *prefix
)
651 enum bisect_error res
;
653 if (bisect_autostart(terms
))
654 return BISECT_FAILED
;
656 if (bisect_next_check(terms
, terms
->term_good
))
657 return BISECT_FAILED
;
659 /* Perform all bisection computation */
660 res
= bisect_next_all(the_repository
, prefix
);
662 if (res
== BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND
) {
663 res
= bisect_successful(terms
);
664 return res
? res
: BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND
;
665 } else if (res
== BISECT_ONLY_SKIPPED_LEFT
) {
666 res
= bisect_skipped_commits(terms
);
667 return res
? res
: BISECT_ONLY_SKIPPED_LEFT
;
672 static enum bisect_error
bisect_auto_next(struct bisect_terms
*terms
, const char *prefix
)
674 if (bisect_next_check(terms
, NULL
)) {
675 bisect_print_status(terms
);
679 return bisect_next(terms
, prefix
);
682 static enum bisect_error
bisect_start(struct bisect_terms
*terms
, int argc
,
686 int first_parent_only
= 0;
687 int i
, has_double_dash
= 0, must_write_terms
= 0, bad_seen
= 0;
688 int flags
, pathspec_pos
;
689 enum bisect_error res
= BISECT_OK
;
690 struct string_list revs
= STRING_LIST_INIT_DUP
;
691 struct string_list states
= STRING_LIST_INIT_DUP
;
692 struct strbuf start_head
= STRBUF_INIT
;
693 struct strbuf bisect_names
= STRBUF_INIT
;
694 struct object_id head_oid
;
695 struct object_id oid
;
698 if (is_bare_repository())
702 * Check for one bad and then some good revisions
704 for (i
= 0; i
< argc
; i
++) {
705 if (!strcmp(argv
[i
], "--")) {
711 for (i
= 0; i
< argc
; i
++) {
712 const char *arg
= argv
[i
];
713 if (!strcmp(argv
[i
], "--")) {
715 } else if (!strcmp(arg
, "--no-checkout")) {
717 } else if (!strcmp(arg
, "--first-parent")) {
718 first_parent_only
= 1;
719 } else if (!strcmp(arg
, "--term-good") ||
720 !strcmp(arg
, "--term-old")) {
723 return error(_("'' is not a valid term"));
724 must_write_terms
= 1;
725 free((void *) terms
->term_good
);
726 terms
->term_good
= xstrdup(argv
[i
]);
727 } else if (skip_prefix(arg
, "--term-good=", &arg
) ||
728 skip_prefix(arg
, "--term-old=", &arg
)) {
729 must_write_terms
= 1;
730 free((void *) terms
->term_good
);
731 terms
->term_good
= xstrdup(arg
);
732 } else if (!strcmp(arg
, "--term-bad") ||
733 !strcmp(arg
, "--term-new")) {
736 return error(_("'' is not a valid term"));
737 must_write_terms
= 1;
738 free((void *) terms
->term_bad
);
739 terms
->term_bad
= xstrdup(argv
[i
]);
740 } else if (skip_prefix(arg
, "--term-bad=", &arg
) ||
741 skip_prefix(arg
, "--term-new=", &arg
)) {
742 must_write_terms
= 1;
743 free((void *) terms
->term_bad
);
744 terms
->term_bad
= xstrdup(arg
);
745 } else if (starts_with(arg
, "--")) {
746 return error(_("unrecognized option: '%s'"), arg
);
747 } else if (!get_oidf(&oid
, "%s^{commit}", arg
)) {
748 string_list_append(&revs
, oid_to_hex(&oid
));
749 } else if (has_double_dash
) {
750 die(_("'%s' does not appear to be a valid "
759 * The user ran "git bisect start <sha1> <sha1>", hence did not
760 * explicitly specify the terms, but we are already starting to
761 * set references named with the default terms, and won't be able
762 * to change afterwards.
765 must_write_terms
= 1;
766 for (i
= 0; i
< revs
.nr
; i
++) {
768 string_list_append(&states
, terms
->term_good
);
771 string_list_append(&states
, terms
->term_bad
);
778 head
= resolve_ref_unsafe("HEAD", 0, &head_oid
, &flags
);
780 if (get_oid("HEAD", &head_oid
))
781 return error(_("bad HEAD - I need a HEAD"));
784 * Check if we are bisecting
786 if (!is_empty_or_missing_file(git_path_bisect_start())) {
787 /* Reset to the rev from where we started */
788 strbuf_read_file(&start_head
, git_path_bisect_start(), 0);
789 strbuf_trim(&start_head
);
791 struct child_process cmd
= CHILD_PROCESS_INIT
;
794 strvec_pushl(&cmd
.args
, "checkout", start_head
.buf
,
796 if (run_command(&cmd
)) {
797 res
= error(_("checking out '%s' failed."
798 " Try 'git bisect start "
805 /* Get the rev from where we start. */
806 if (!get_oid(head
, &head_oid
) &&
807 !starts_with(head
, "refs/heads/")) {
808 strbuf_reset(&start_head
);
809 strbuf_addstr(&start_head
, oid_to_hex(&head_oid
));
810 } else if (!get_oid(head
, &head_oid
) &&
811 skip_prefix(head
, "refs/heads/", &head
)) {
812 strbuf_addstr(&start_head
, head
);
814 return error(_("bad HEAD - strange symbolic ref"));
819 * Get rid of any old bisect state.
821 if (bisect_clean_state())
822 return BISECT_FAILED
;
825 * Write new start state
827 write_file(git_path_bisect_start(), "%s\n", start_head
.buf
);
829 if (first_parent_only
)
830 write_file(git_path_bisect_first_parent(), "\n");
833 if (get_oid(start_head
.buf
, &oid
) < 0) {
834 res
= error(_("invalid ref: '%s'"), start_head
.buf
);
837 if (update_ref(NULL
, "BISECT_HEAD", &oid
, NULL
, 0,
838 UPDATE_REFS_MSG_ON_ERR
)) {
844 if (pathspec_pos
< argc
- 1)
845 sq_quote_argv(&bisect_names
, argv
+ pathspec_pos
);
846 write_file(git_path_bisect_names(), "%s\n", bisect_names
.buf
);
848 for (i
= 0; i
< states
.nr
; i
++)
849 if (bisect_write(states
.items
[i
].string
,
850 revs
.items
[i
].string
, terms
, 1)) {
855 if (must_write_terms
&& write_terms(terms
->term_bad
,
861 res
= bisect_append_log_quoted(argv
);
866 string_list_clear(&revs
, 0);
867 string_list_clear(&states
, 0);
868 strbuf_release(&start_head
);
869 strbuf_release(&bisect_names
);
873 res
= bisect_auto_next(terms
, NULL
);
874 if (!is_bisect_success(res
))
875 bisect_clean_state();
879 static inline int file_is_not_empty(const char *path
)
881 return !is_empty_or_missing_file(path
);
884 static int bisect_autostart(struct bisect_terms
*terms
)
889 if (file_is_not_empty(git_path_bisect_start()))
892 fprintf_ln(stderr
, _("You need to start by \"git bisect "
895 if (!isatty(STDIN_FILENO
))
899 * TRANSLATORS: Make sure to include [Y] and [n] in your
900 * translation. The program will only accept English input
903 yesno
= git_prompt(_("Do you want me to do it for you "
904 "[Y/n]? "), PROMPT_ECHO
);
905 res
= tolower(*yesno
) == 'n' ?
906 -1 : bisect_start(terms
, 0, empty_strvec
);
911 static enum bisect_error
bisect_state(struct bisect_terms
*terms
, int argc
,
915 int i
, verify_expected
= 1;
916 struct object_id oid
, expected
;
917 struct strbuf buf
= STRBUF_INIT
;
918 struct oid_array revs
= OID_ARRAY_INIT
;
921 return error(_("Please call `--bisect-state` with at least one argument"));
923 if (bisect_autostart(terms
))
924 return BISECT_FAILED
;
927 if (check_and_set_terms(terms
, state
) ||
928 !one_of(state
, terms
->term_good
, terms
->term_bad
, "skip", NULL
))
929 return BISECT_FAILED
;
933 if (argc
> 1 && !strcmp(state
, terms
->term_bad
))
934 return error(_("'git bisect %s' can take only one argument."), terms
->term_bad
);
937 const char *head
= "BISECT_HEAD";
938 enum get_oid_result res_head
= get_oid(head
, &oid
);
940 if (res_head
== MISSING_OBJECT
) {
942 res_head
= get_oid(head
, &oid
);
946 error(_("Bad rev input: %s"), head
);
947 oid_array_append(&revs
, &oid
);
951 * All input revs must be checked before executing bisect_write()
952 * to discard junk revs.
955 for (; argc
; argc
--, argv
++) {
956 struct commit
*commit
;
958 if (get_oid(*argv
, &oid
)){
959 error(_("Bad rev input: %s"), *argv
);
960 oid_array_clear(&revs
);
961 return BISECT_FAILED
;
964 commit
= lookup_commit_reference(the_repository
, &oid
);
966 die(_("Bad rev input (not a commit): %s"), *argv
);
968 oid_array_append(&revs
, &commit
->object
.oid
);
971 if (strbuf_read_file(&buf
, git_path_bisect_expected_rev(), 0) < the_hash_algo
->hexsz
||
972 get_oid_hex(buf
.buf
, &expected
) < 0)
973 verify_expected
= 0; /* Ignore invalid file contents */
974 strbuf_release(&buf
);
976 for (i
= 0; i
< revs
.nr
; i
++) {
977 if (bisect_write(state
, oid_to_hex(&revs
.oid
[i
]), terms
, 0)) {
978 oid_array_clear(&revs
);
979 return BISECT_FAILED
;
981 if (verify_expected
&& !oideq(&revs
.oid
[i
], &expected
)) {
982 unlink_or_warn(git_path_bisect_ancestors_ok());
983 unlink_or_warn(git_path_bisect_expected_rev());
988 oid_array_clear(&revs
);
989 return bisect_auto_next(terms
, NULL
);
992 static enum bisect_error
bisect_log(void)
995 const char* filename
= git_path_bisect_log();
997 if (is_empty_or_missing_file(filename
))
998 return error(_("We are not bisecting."));
1000 fd
= open(filename
, O_RDONLY
);
1002 return BISECT_FAILED
;
1004 status
= copy_fd(fd
, STDOUT_FILENO
);
1006 return status
? BISECT_FAILED
: BISECT_OK
;
1009 static int process_replay_line(struct bisect_terms
*terms
, struct strbuf
*line
)
1011 const char *p
= line
->buf
+ strspn(line
->buf
, " \t");
1012 char *word_end
, *rev
;
1014 if ((!skip_prefix(p
, "git bisect", &p
) &&
1015 !skip_prefix(p
, "git-bisect", &p
)) || !isspace(*p
))
1017 p
+= strspn(p
, " \t");
1019 word_end
= (char *)p
+ strcspn(p
, " \t");
1020 rev
= word_end
+ strspn(word_end
, " \t");
1021 *word_end
= '\0'; /* NUL-terminate the word */
1024 if (check_and_set_terms(terms
, p
))
1027 if (!strcmp(p
, "start")) {
1028 struct strvec argv
= STRVEC_INIT
;
1030 sq_dequote_to_strvec(rev
, &argv
);
1031 res
= bisect_start(terms
, argv
.nr
, argv
.v
);
1032 strvec_clear(&argv
);
1036 if (one_of(p
, terms
->term_good
,
1037 terms
->term_bad
, "skip", NULL
))
1038 return bisect_write(p
, rev
, terms
, 0);
1040 if (!strcmp(p
, "terms")) {
1041 struct strvec argv
= STRVEC_INIT
;
1043 sq_dequote_to_strvec(rev
, &argv
);
1044 res
= bisect_terms(terms
, argv
.nr
== 1 ? argv
.v
[0] : NULL
);
1045 strvec_clear(&argv
);
1048 error(_("'%s'?? what are you talking about?"), p
);
1053 static enum bisect_error
bisect_replay(struct bisect_terms
*terms
, const char *filename
)
1056 enum bisect_error res
= BISECT_OK
;
1057 struct strbuf line
= STRBUF_INIT
;
1059 if (is_empty_or_missing_file(filename
))
1060 return error(_("cannot read file '%s' for replaying"), filename
);
1062 if (bisect_reset(NULL
))
1063 return BISECT_FAILED
;
1065 fp
= fopen(filename
, "r");
1067 return BISECT_FAILED
;
1069 while ((strbuf_getline(&line
, fp
) != EOF
) && !res
)
1070 res
= process_replay_line(terms
, &line
);
1072 strbuf_release(&line
);
1076 return BISECT_FAILED
;
1078 return bisect_auto_next(terms
, NULL
);
1081 static enum bisect_error
bisect_skip(struct bisect_terms
*terms
, int argc
,
1085 enum bisect_error res
;
1086 struct strvec argv_state
= STRVEC_INIT
;
1088 strvec_push(&argv_state
, "skip");
1090 for (i
= 0; i
< argc
; i
++) {
1091 const char *dotdot
= strstr(argv
[i
], "..");
1094 struct rev_info revs
;
1095 struct commit
*commit
;
1097 init_revisions(&revs
, NULL
);
1098 setup_revisions(2, argv
+ i
- 1, &revs
, NULL
);
1100 if (prepare_revision_walk(&revs
))
1101 die(_("revision walk setup failed\n"));
1102 while ((commit
= get_revision(&revs
)) != NULL
)
1103 strvec_push(&argv_state
,
1104 oid_to_hex(&commit
->object
.oid
));
1106 reset_revision_walk();
1107 release_revisions(&revs
);
1109 strvec_push(&argv_state
, argv
[i
]);
1112 res
= bisect_state(terms
, argv_state
.nr
, argv_state
.v
);
1114 strvec_clear(&argv_state
);
1118 static int bisect_visualize(struct bisect_terms
*terms
, int argc
,
1121 struct child_process cmd
= CHILD_PROCESS_INIT
;
1122 struct strbuf sb
= STRBUF_INIT
;
1124 if (bisect_next_check(terms
, NULL
) != 0)
1125 return BISECT_FAILED
;
1129 if ((getenv("DISPLAY") || getenv("SESSIONNAME") || getenv("MSYSTEM") ||
1130 getenv("SECURITYSESSIONID")) && exists_in_PATH("gitk")) {
1131 strvec_push(&cmd
.args
, "gitk");
1133 strvec_push(&cmd
.args
, "log");
1137 if (argv
[0][0] == '-') {
1138 strvec_push(&cmd
.args
, "log");
1140 } else if (strcmp(argv
[0], "tig") && !starts_with(argv
[0], "git"))
1143 strvec_pushv(&cmd
.args
, argv
);
1146 strvec_pushl(&cmd
.args
, "--bisect", "--", NULL
);
1148 strbuf_read_file(&sb
, git_path_bisect_names(), 0);
1149 sq_dequote_to_strvec(sb
.buf
, &cmd
.args
);
1150 strbuf_release(&sb
);
1152 return run_command(&cmd
);
1155 static int get_first_good(const char *refname UNUSED
,
1156 const struct object_id
*oid
,
1157 int flag UNUSED
, void *cb_data
)
1159 oidcpy(cb_data
, oid
);
1163 static int do_bisect_run(const char *command
)
1165 struct child_process cmd
= CHILD_PROCESS_INIT
;
1167 printf(_("running %s\n"), command
);
1169 strvec_push(&cmd
.args
, command
);
1170 return run_command(&cmd
);
1173 static int verify_good(const struct bisect_terms
*terms
, const char *command
)
1176 enum bisect_error res
;
1177 struct object_id good_rev
;
1178 struct object_id current_rev
;
1179 char *good_glob
= xstrfmt("%s-*", terms
->term_good
);
1180 int no_checkout
= ref_exists("BISECT_HEAD");
1182 for_each_glob_ref_in(get_first_good
, good_glob
, "refs/bisect/",
1186 if (read_ref(no_checkout
? "BISECT_HEAD" : "HEAD", ¤t_rev
))
1189 res
= bisect_checkout(&good_rev
, no_checkout
);
1190 if (res
!= BISECT_OK
)
1193 rc
= do_bisect_run(command
);
1195 res
= bisect_checkout(¤t_rev
, no_checkout
);
1196 if (res
!= BISECT_OK
)
1202 static int bisect_run(struct bisect_terms
*terms
, int argc
, const char **argv
)
1204 int res
= BISECT_OK
;
1205 struct strbuf command
= STRBUF_INIT
;
1206 const char *new_state
;
1207 int temporary_stdout_fd
, saved_stdout
;
1208 int is_first_run
= 1;
1210 if (bisect_next_check(terms
, NULL
))
1211 return BISECT_FAILED
;
1214 error(_("bisect run failed: no command provided."));
1215 return BISECT_FAILED
;
1218 sq_quote_argv(&command
, argv
);
1219 strbuf_ltrim(&command
);
1221 res
= do_bisect_run(command
.buf
);
1224 * Exit code 126 and 127 can either come from the shell
1225 * if it was unable to execute or even find the script,
1226 * or from the script itself. Check with a known-good
1227 * revision to avoid trashing the bisect run due to a
1228 * missing or non-executable script.
1230 if (is_first_run
&& (res
== 126 || res
== 127)) {
1231 int rc
= verify_good(terms
, command
.buf
);
1233 if (rc
< 0 || 128 <= rc
) {
1234 error(_("unable to verify %s on good"
1235 " revision"), command
.buf
);
1236 res
= BISECT_FAILED
;
1240 error(_("bogus exit code %d for good revision"),
1242 res
= BISECT_FAILED
;
1247 if (res
< 0 || 128 <= res
) {
1248 error(_("bisect run failed: exit code %d from"
1249 " %s is < 0 or >= 128"), res
, command
.buf
);
1256 new_state
= terms
->term_good
;
1258 new_state
= terms
->term_bad
;
1260 temporary_stdout_fd
= open(git_path_bisect_run(), O_CREAT
| O_WRONLY
| O_TRUNC
, 0666);
1262 if (temporary_stdout_fd
< 0) {
1263 res
= error_errno(_("cannot open file '%s' for writing"), git_path_bisect_run());
1268 saved_stdout
= dup(1);
1269 dup2(temporary_stdout_fd
, 1);
1271 res
= bisect_state(terms
, 1, &new_state
);
1274 dup2(saved_stdout
, 1);
1275 close(saved_stdout
);
1276 close(temporary_stdout_fd
);
1278 print_file_to_stdout(git_path_bisect_run());
1280 if (res
== BISECT_ONLY_SKIPPED_LEFT
)
1281 error(_("bisect run cannot continue any more"));
1282 else if (res
== BISECT_INTERNAL_SUCCESS_MERGE_BASE
) {
1283 puts(_("bisect run success"));
1285 } else if (res
== BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND
) {
1286 puts(_("bisect found first bad commit"));
1289 error(_("bisect run failed: 'git bisect %s'"
1290 " exited with error code %d"), new_state
, res
);
1297 strbuf_release(&command
);
1301 static int cmd_bisect__reset(int argc
, const char **argv
, const char *prefix UNUSED
)
1304 return error(_("'%s' requires either no argument or a commit"),
1305 "git bisect reset");
1306 return bisect_reset(argc
? argv
[0] : NULL
);
1309 static int cmd_bisect__terms(int argc
, const char **argv
, const char *prefix UNUSED
)
1312 struct bisect_terms terms
= { 0 };
1315 return error(_("'%s' requires 0 or 1 argument"),
1316 "git bisect terms");
1317 res
= bisect_terms(&terms
, argc
== 1 ? argv
[0] : NULL
);
1322 static int cmd_bisect__start(int argc
, const char **argv
, const char *prefix UNUSED
)
1325 struct bisect_terms terms
= { 0 };
1327 set_terms(&terms
, "bad", "good");
1328 res
= bisect_start(&terms
, argc
, argv
);
1333 static int cmd_bisect__next(int argc
, const char **argv UNUSED
, const char *prefix
)
1336 struct bisect_terms terms
= { 0 };
1339 return error(_("'%s' requires 0 arguments"),
1342 res
= bisect_next(&terms
, prefix
);
1347 static int cmd_bisect__log(int argc UNUSED
, const char **argv UNUSED
, const char *prefix UNUSED
)
1349 return bisect_log();
1352 static int cmd_bisect__replay(int argc
, const char **argv
, const char *prefix UNUSED
)
1355 struct bisect_terms terms
= { 0 };
1358 return error(_("no logfile given"));
1359 set_terms(&terms
, "bad", "good");
1360 res
= bisect_replay(&terms
, argv
[0]);
1365 static int cmd_bisect__skip(int argc
, const char **argv
, const char *prefix UNUSED
)
1368 struct bisect_terms terms
= { 0 };
1370 set_terms(&terms
, "bad", "good");
1372 res
= bisect_skip(&terms
, argc
, argv
);
1377 static int cmd_bisect__visualize(int argc
, const char **argv
, const char *prefix UNUSED
)
1380 struct bisect_terms terms
= { 0 };
1383 res
= bisect_visualize(&terms
, argc
, argv
);
1388 static int cmd_bisect__run(int argc
, const char **argv
, const char *prefix UNUSED
)
1391 struct bisect_terms terms
= { 0 };
1394 return error(_("'%s' failed: no command provided."), "git bisect run");
1396 res
= bisect_run(&terms
, argc
, argv
);
1401 int cmd_bisect(int argc
, const char **argv
, const char *prefix
)
1404 parse_opt_subcommand_fn
*fn
= NULL
;
1405 struct option options
[] = {
1406 OPT_SUBCOMMAND("reset", &fn
, cmd_bisect__reset
),
1407 OPT_SUBCOMMAND("terms", &fn
, cmd_bisect__terms
),
1408 OPT_SUBCOMMAND("start", &fn
, cmd_bisect__start
),
1409 OPT_SUBCOMMAND("next", &fn
, cmd_bisect__next
),
1410 OPT_SUBCOMMAND("log", &fn
, cmd_bisect__log
),
1411 OPT_SUBCOMMAND("replay", &fn
, cmd_bisect__replay
),
1412 OPT_SUBCOMMAND("skip", &fn
, cmd_bisect__skip
),
1413 OPT_SUBCOMMAND("visualize", &fn
, cmd_bisect__visualize
),
1414 OPT_SUBCOMMAND("view", &fn
, cmd_bisect__visualize
),
1415 OPT_SUBCOMMAND("run", &fn
, cmd_bisect__run
),
1418 argc
= parse_options(argc
, argv
, prefix
, options
, git_bisect_usage
,
1419 PARSE_OPT_SUBCOMMAND_OPTIONAL
);
1422 struct bisect_terms terms
= { 0 };
1425 usage_msg_opt(_("need a command"), git_bisect_usage
, options
);
1427 set_terms(&terms
, "bad", "good");
1429 if (check_and_set_terms(&terms
, argv
[0]))
1430 usage_msg_optf(_("unknown command: '%s'"), git_bisect_usage
,
1432 res
= bisect_state(&terms
, argc
, argv
);
1437 res
= fn(argc
, argv
, prefix
);
1440 return is_bisect_success(res
) ? 0 : -res
;