3 #include "environment.h"
6 #include "object-name.h"
7 #include "parse-options.h"
11 #include "run-command.h"
12 #include "oid-array.h"
18 static GIT_PATH_FUNC(git_path_bisect_terms
, "BISECT_TERMS")
19 static GIT_PATH_FUNC(git_path_bisect_ancestors_ok
, "BISECT_ANCESTORS_OK")
20 static GIT_PATH_FUNC(git_path_bisect_start
, "BISECT_START")
21 static GIT_PATH_FUNC(git_path_bisect_log
, "BISECT_LOG")
22 static GIT_PATH_FUNC(git_path_bisect_names
, "BISECT_NAMES")
23 static GIT_PATH_FUNC(git_path_bisect_first_parent
, "BISECT_FIRST_PARENT")
24 static GIT_PATH_FUNC(git_path_bisect_run
, "BISECT_RUN")
26 #define BUILTIN_GIT_BISECT_START_USAGE \
27 N_("git bisect start [--term-(new|bad)=<term> --term-(old|good)=<term>]" \
28 " [--no-checkout] [--first-parent] [<bad> [<good>...]] [--]" \
30 #define BUILTIN_GIT_BISECT_STATE_USAGE \
31 N_("git bisect (good|bad) [<rev>...]")
32 #define BUILTIN_GIT_BISECT_TERMS_USAGE \
33 "git bisect terms [--term-good | --term-bad]"
34 #define BUILTIN_GIT_BISECT_SKIP_USAGE \
35 N_("git bisect skip [(<rev>|<range>)...]")
36 #define BUILTIN_GIT_BISECT_NEXT_USAGE \
38 #define BUILTIN_GIT_BISECT_RESET_USAGE \
39 N_("git bisect reset [<commit>]")
40 #define BUILTIN_GIT_BISECT_VISUALIZE_USAGE \
41 "git bisect visualize"
42 #define BUILTIN_GIT_BISECT_REPLAY_USAGE \
43 N_("git bisect replay <logfile>")
44 #define BUILTIN_GIT_BISECT_LOG_USAGE \
46 #define BUILTIN_GIT_BISECT_RUN_USAGE \
47 N_("git bisect run <cmd> [<arg>...]")
49 static const char * const git_bisect_usage
[] = {
50 BUILTIN_GIT_BISECT_START_USAGE
,
51 BUILTIN_GIT_BISECT_STATE_USAGE
,
52 BUILTIN_GIT_BISECT_TERMS_USAGE
,
53 BUILTIN_GIT_BISECT_SKIP_USAGE
,
54 BUILTIN_GIT_BISECT_NEXT_USAGE
,
55 BUILTIN_GIT_BISECT_RESET_USAGE
,
56 BUILTIN_GIT_BISECT_VISUALIZE_USAGE
,
57 BUILTIN_GIT_BISECT_REPLAY_USAGE
,
58 BUILTIN_GIT_BISECT_LOG_USAGE
,
59 BUILTIN_GIT_BISECT_RUN_USAGE
,
63 struct add_bisect_ref_data
{
64 struct rev_info
*revs
;
65 unsigned int object_flags
;
73 static void free_terms(struct bisect_terms
*terms
)
75 FREE_AND_NULL(terms
->term_good
);
76 FREE_AND_NULL(terms
->term_bad
);
79 static void set_terms(struct bisect_terms
*terms
, const char *bad
,
82 free((void *)terms
->term_good
);
83 terms
->term_good
= xstrdup(good
);
84 free((void *)terms
->term_bad
);
85 terms
->term_bad
= xstrdup(bad
);
88 static const char vocab_bad
[] = "bad|new";
89 static const char vocab_good
[] = "good|old";
91 static int bisect_autostart(struct bisect_terms
*terms
);
94 * Check whether the string `term` belongs to the set of strings
95 * included in the variable arguments.
98 static int one_of(const char *term
, ...)
104 va_start(matches
, term
);
105 while (!res
&& (match
= va_arg(matches
, const char *)))
106 res
= !strcmp(term
, match
);
113 * return code BISECT_INTERNAL_SUCCESS_MERGE_BASE
114 * and BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND are codes
115 * that indicate special success.
118 static int is_bisect_success(enum bisect_error res
)
121 res
== BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND
||
122 res
== BISECT_INTERNAL_SUCCESS_MERGE_BASE
;
125 static int write_in_file(const char *path
, const char *mode
, const char *format
, va_list args
)
130 if (strcmp(mode
, "w") && strcmp(mode
, "a"))
131 BUG("write-in-file does not support '%s' mode", mode
);
132 fp
= fopen(path
, mode
);
134 return error_errno(_("cannot open file '%s' in mode '%s'"), path
, mode
);
135 res
= vfprintf(fp
, format
, args
);
138 int saved_errno
= errno
;
141 return error_errno(_("could not write to file '%s'"), path
);
147 __attribute__((format (printf
, 2, 3)))
148 static int write_to_file(const char *path
, const char *format
, ...)
153 va_start(args
, format
);
154 res
= write_in_file(path
, "w", format
, args
);
160 __attribute__((format (printf
, 2, 3)))
161 static int append_to_file(const char *path
, const char *format
, ...)
166 va_start(args
, format
);
167 res
= write_in_file(path
, "a", format
, args
);
173 static int print_file_to_stdout(const char *path
)
175 int fd
= open(path
, O_RDONLY
);
179 return error_errno(_("cannot open file '%s' for reading"), path
);
180 if (copy_fd(fd
, 1) < 0)
181 ret
= error_errno(_("failed to read '%s'"), path
);
186 static int check_term_format(const char *term
, const char *orig_term
)
189 char *new_term
= xstrfmt("refs/bisect/%s", term
);
191 res
= check_refname_format(new_term
, 0);
195 return error(_("'%s' is not a valid term"), term
);
197 if (one_of(term
, "help", "start", "skip", "next", "reset",
198 "visualize", "view", "replay", "log", "run", "terms", NULL
))
199 return error(_("can't use the builtin command '%s' as a term"), term
);
202 * In theory, nothing prevents swapping completely good and bad,
203 * but this situation could be confusing and hasn't been tested
204 * enough. Forbid it for now.
207 if ((strcmp(orig_term
, "bad") && one_of(term
, "bad", "new", NULL
)) ||
208 (strcmp(orig_term
, "good") && one_of(term
, "good", "old", NULL
)))
209 return error(_("can't change the meaning of the term '%s'"), term
);
214 static int write_terms(const char *bad
, const char *good
)
218 if (!strcmp(bad
, good
))
219 return error(_("please use two different terms"));
221 if (check_term_format(bad
, "bad") || check_term_format(good
, "good"))
224 res
= write_to_file(git_path_bisect_terms(), "%s\n%s\n", bad
, good
);
229 static int bisect_reset(const char *commit
)
231 struct strbuf branch
= STRBUF_INIT
;
234 if (!strbuf_read_file(&branch
, git_path_bisect_start(), 0))
235 printf(_("We are not bisecting.\n"));
237 strbuf_rtrim(&branch
);
239 struct object_id oid
;
241 if (repo_get_oid_commit(the_repository
, commit
, &oid
))
242 return error(_("'%s' is not a valid commit"), commit
);
243 strbuf_addstr(&branch
, commit
);
246 if (branch
.len
&& !ref_exists("BISECT_HEAD")) {
247 struct child_process cmd
= CHILD_PROCESS_INIT
;
250 strvec_pushl(&cmd
.args
, "checkout", "--ignore-other-worktrees",
251 branch
.buf
, "--", NULL
);
252 if (run_command(&cmd
)) {
253 error(_("could not check out original"
254 " HEAD '%s'. Try 'git bisect"
255 " reset <commit>'."), branch
.buf
);
256 strbuf_release(&branch
);
261 strbuf_release(&branch
);
262 return bisect_clean_state();
265 static void log_commit(FILE *fp
, char *fmt
, const char *state
,
266 struct commit
*commit
)
268 struct pretty_print_context pp
= {0};
269 struct strbuf commit_msg
= STRBUF_INIT
;
270 char *label
= xstrfmt(fmt
, state
);
272 repo_format_commit_message(the_repository
, commit
, "%s", &commit_msg
,
275 fprintf(fp
, "# %s: [%s] %s\n", label
, oid_to_hex(&commit
->object
.oid
),
278 strbuf_release(&commit_msg
);
282 static int bisect_write(const char *state
, const char *rev
,
283 const struct bisect_terms
*terms
, int nolog
)
285 struct strbuf tag
= STRBUF_INIT
;
286 struct object_id oid
;
287 struct commit
*commit
;
291 if (!strcmp(state
, terms
->term_bad
)) {
292 strbuf_addf(&tag
, "refs/bisect/%s", state
);
293 } else if (one_of(state
, terms
->term_good
, "skip", NULL
)) {
294 strbuf_addf(&tag
, "refs/bisect/%s-%s", state
, rev
);
296 res
= error(_("Bad bisect_write argument: %s"), state
);
300 if (repo_get_oid(the_repository
, rev
, &oid
)) {
301 res
= error(_("couldn't get the oid of the rev '%s'"), rev
);
305 if (update_ref(NULL
, tag
.buf
, &oid
, NULL
, 0,
306 UPDATE_REFS_MSG_ON_ERR
)) {
311 fp
= fopen(git_path_bisect_log(), "a");
313 res
= error_errno(_("couldn't open the file '%s'"), git_path_bisect_log());
317 commit
= lookup_commit_reference(the_repository
, &oid
);
318 log_commit(fp
, "%s", state
, commit
);
321 fprintf(fp
, "git bisect %s %s\n", state
, rev
);
326 strbuf_release(&tag
);
330 static int check_and_set_terms(struct bisect_terms
*terms
, const char *cmd
)
332 int has_term_file
= !is_empty_or_missing_file(git_path_bisect_terms());
334 if (one_of(cmd
, "skip", "start", "terms", NULL
))
337 if (has_term_file
&& strcmp(cmd
, terms
->term_bad
) &&
338 strcmp(cmd
, terms
->term_good
))
339 return error(_("Invalid command: you're currently in a "
340 "%s/%s bisect"), terms
->term_bad
,
343 if (!has_term_file
) {
344 if (one_of(cmd
, "bad", "good", NULL
)) {
345 set_terms(terms
, "bad", "good");
346 return write_terms(terms
->term_bad
, terms
->term_good
);
348 if (one_of(cmd
, "new", "old", NULL
)) {
349 set_terms(terms
, "new", "old");
350 return write_terms(terms
->term_bad
, terms
->term_good
);
357 static int inc_nr(const char *refname UNUSED
,
358 const struct object_id
*oid UNUSED
,
359 int flag UNUSED
, void *cb_data
)
361 unsigned int *nr
= (unsigned int *)cb_data
;
366 static const char need_bad_and_good_revision_warning
[] =
367 N_("You need to give me at least one %s and %s revision.\n"
368 "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
370 static const char need_bisect_start_warning
[] =
371 N_("You need to start by \"git bisect start\".\n"
372 "You then need to give me at least one %s and %s revision.\n"
373 "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
375 static int decide_next(const struct bisect_terms
*terms
,
376 const char *current_term
, int missing_good
,
379 if (!missing_good
&& !missing_bad
)
384 if (missing_good
&& !missing_bad
&&
385 !strcmp(current_term
, terms
->term_good
)) {
388 * have bad (or new) but not good (or old). We could bisect
389 * although this is less optimum.
391 warning(_("bisecting only with a %s commit"), terms
->term_bad
);
395 * TRANSLATORS: Make sure to include [Y] and [n] in your
396 * translation. The program will only accept English input
399 yesno
= git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO
);
400 if (starts_with(yesno
, "N") || starts_with(yesno
, "n"))
405 if (!is_empty_or_missing_file(git_path_bisect_start()))
406 return error(_(need_bad_and_good_revision_warning
),
407 vocab_bad
, vocab_good
, vocab_bad
, vocab_good
);
409 return error(_(need_bisect_start_warning
),
410 vocab_good
, vocab_bad
, vocab_good
, vocab_bad
);
413 static void bisect_status(struct bisect_state
*state
,
414 const struct bisect_terms
*terms
)
416 char *bad_ref
= xstrfmt("refs/bisect/%s", terms
->term_bad
);
417 char *good_glob
= xstrfmt("%s-*", terms
->term_good
);
419 if (ref_exists(bad_ref
))
422 for_each_glob_ref_in(inc_nr
, good_glob
, "refs/bisect/",
423 (void *) &state
->nr_good
);
429 __attribute__((format (printf
, 1, 2)))
430 static void bisect_log_printf(const char *fmt
, ...)
432 struct strbuf buf
= STRBUF_INIT
;
436 strbuf_vaddf(&buf
, fmt
, ap
);
439 printf("%s", buf
.buf
);
440 append_to_file(git_path_bisect_log(), "# %s", buf
.buf
);
442 strbuf_release(&buf
);
445 static void bisect_print_status(const struct bisect_terms
*terms
)
447 struct bisect_state state
= { 0 };
449 bisect_status(&state
, terms
);
451 /* If we had both, we'd already be started, and shouldn't get here. */
452 if (state
.nr_good
&& state
.nr_bad
)
455 if (!state
.nr_good
&& !state
.nr_bad
)
456 bisect_log_printf(_("status: waiting for both good and bad commits\n"));
457 else if (state
.nr_good
)
458 bisect_log_printf(Q_("status: waiting for bad commit, %d good commit known\n",
459 "status: waiting for bad commit, %d good commits known\n",
460 state
.nr_good
), state
.nr_good
);
462 bisect_log_printf(_("status: waiting for good commit(s), bad commit known\n"));
465 static int bisect_next_check(const struct bisect_terms
*terms
,
466 const char *current_term
)
468 struct bisect_state state
= { 0 };
469 bisect_status(&state
, terms
);
470 return decide_next(terms
, current_term
, !state
.nr_good
, !state
.nr_bad
);
473 static int get_terms(struct bisect_terms
*terms
)
475 struct strbuf str
= STRBUF_INIT
;
479 fp
= fopen(git_path_bisect_terms(), "r");
486 strbuf_getline_lf(&str
, fp
);
487 terms
->term_bad
= strbuf_detach(&str
, NULL
);
488 strbuf_getline_lf(&str
, fp
);
489 terms
->term_good
= strbuf_detach(&str
, NULL
);
494 strbuf_release(&str
);
498 static int bisect_terms(struct bisect_terms
*terms
, const char *option
)
500 if (get_terms(terms
))
501 return error(_("no terms defined"));
504 printf(_("Your current terms are %s for the old state\n"
505 "and %s for the new state.\n"),
506 terms
->term_good
, terms
->term_bad
);
509 if (one_of(option
, "--term-good", "--term-old", NULL
))
510 printf("%s\n", terms
->term_good
);
511 else if (one_of(option
, "--term-bad", "--term-new", NULL
))
512 printf("%s\n", terms
->term_bad
);
514 return error(_("invalid argument %s for 'git bisect terms'.\n"
515 "Supported options are: "
516 "--term-good|--term-old and "
517 "--term-bad|--term-new."), option
);
522 static int bisect_append_log_quoted(const char **argv
)
525 FILE *fp
= fopen(git_path_bisect_log(), "a");
526 struct strbuf orig_args
= STRBUF_INIT
;
531 if (fprintf(fp
, "git bisect start") < 1) {
536 sq_quote_argv(&orig_args
, argv
);
537 if (fprintf(fp
, "%s\n", orig_args
.buf
) < 1)
542 strbuf_release(&orig_args
);
546 static int add_bisect_ref(const char *refname
, const struct object_id
*oid
,
547 int flags UNUSED
, void *cb
)
549 struct add_bisect_ref_data
*data
= cb
;
551 add_pending_oid(data
->revs
, refname
, oid
, data
->object_flags
);
556 static int prepare_revs(struct bisect_terms
*terms
, struct rev_info
*revs
)
559 struct add_bisect_ref_data cb
= { revs
};
560 char *good
= xstrfmt("%s-*", terms
->term_good
);
563 * We cannot use terms->term_bad directly in
564 * for_each_glob_ref_in() and we have to append a '*' to it,
565 * otherwise for_each_glob_ref_in() will append '/' and '*'.
567 char *bad
= xstrfmt("%s*", terms
->term_bad
);
570 * It is important to reset the flags used by revision walks
571 * as the previous call to bisect_next_all() in turn
572 * sets up a revision walk.
574 reset_revision_walk();
575 repo_init_revisions(the_repository
, revs
, NULL
);
576 setup_revisions(0, NULL
, revs
, NULL
);
577 for_each_glob_ref_in(add_bisect_ref
, bad
, "refs/bisect/", &cb
);
578 cb
.object_flags
= UNINTERESTING
;
579 for_each_glob_ref_in(add_bisect_ref
, good
, "refs/bisect/", &cb
);
580 if (prepare_revision_walk(revs
))
581 res
= error(_("revision walk setup failed\n"));
588 static int bisect_skipped_commits(struct bisect_terms
*terms
)
592 struct rev_info revs
;
593 struct commit
*commit
;
594 struct pretty_print_context pp
= {0};
595 struct strbuf commit_name
= STRBUF_INIT
;
597 res
= prepare_revs(terms
, &revs
);
601 fp
= fopen(git_path_bisect_log(), "a");
603 return error_errno(_("could not open '%s' for appending"),
604 git_path_bisect_log());
606 if (fprintf(fp
, "# only skipped commits left to test\n") < 0)
607 return error_errno(_("failed to write to '%s'"), git_path_bisect_log());
609 while ((commit
= get_revision(&revs
)) != NULL
) {
610 strbuf_reset(&commit_name
);
611 repo_format_commit_message(the_repository
, commit
, "%s",
613 fprintf(fp
, "# possible first %s commit: [%s] %s\n",
614 terms
->term_bad
, oid_to_hex(&commit
->object
.oid
),
619 * Reset the flags used by revision walks in case
620 * there is another revision walk after this one.
622 reset_revision_walk();
624 strbuf_release(&commit_name
);
625 release_revisions(&revs
);
630 static int bisect_successful(struct bisect_terms
*terms
)
632 struct object_id oid
;
633 struct commit
*commit
;
634 struct pretty_print_context pp
= {0};
635 struct strbuf commit_name
= STRBUF_INIT
;
636 char *bad_ref
= xstrfmt("refs/bisect/%s",terms
->term_bad
);
639 read_ref(bad_ref
, &oid
);
640 commit
= lookup_commit_reference_by_name(bad_ref
);
641 repo_format_commit_message(the_repository
, commit
, "%s", &commit_name
,
644 res
= append_to_file(git_path_bisect_log(), "# first %s commit: [%s] %s\n",
645 terms
->term_bad
, oid_to_hex(&commit
->object
.oid
),
648 strbuf_release(&commit_name
);
653 static enum bisect_error
bisect_next(struct bisect_terms
*terms
, const char *prefix
)
655 enum bisect_error res
;
657 if (bisect_autostart(terms
))
658 return BISECT_FAILED
;
660 if (bisect_next_check(terms
, terms
->term_good
))
661 return BISECT_FAILED
;
663 /* Perform all bisection computation */
664 res
= bisect_next_all(the_repository
, prefix
);
666 if (res
== BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND
) {
667 res
= bisect_successful(terms
);
668 return res
? res
: BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND
;
669 } else if (res
== BISECT_ONLY_SKIPPED_LEFT
) {
670 res
= bisect_skipped_commits(terms
);
671 return res
? res
: BISECT_ONLY_SKIPPED_LEFT
;
676 static enum bisect_error
bisect_auto_next(struct bisect_terms
*terms
, const char *prefix
)
678 if (bisect_next_check(terms
, NULL
)) {
679 bisect_print_status(terms
);
683 return bisect_next(terms
, prefix
);
686 static enum bisect_error
bisect_start(struct bisect_terms
*terms
, int argc
,
690 int first_parent_only
= 0;
691 int i
, has_double_dash
= 0, must_write_terms
= 0, bad_seen
= 0;
692 int flags
, pathspec_pos
;
693 enum bisect_error res
= BISECT_OK
;
694 struct string_list revs
= STRING_LIST_INIT_DUP
;
695 struct string_list states
= STRING_LIST_INIT_DUP
;
696 struct strbuf start_head
= STRBUF_INIT
;
697 struct strbuf bisect_names
= STRBUF_INIT
;
698 struct object_id head_oid
;
699 struct object_id oid
;
702 if (is_bare_repository())
706 * Check for one bad and then some good revisions
708 for (i
= 0; i
< argc
; i
++) {
709 if (!strcmp(argv
[i
], "--")) {
715 for (i
= 0; i
< argc
; i
++) {
716 const char *arg
= argv
[i
];
717 if (!strcmp(argv
[i
], "--")) {
719 } else if (!strcmp(arg
, "--no-checkout")) {
721 } else if (!strcmp(arg
, "--first-parent")) {
722 first_parent_only
= 1;
723 } else if (!strcmp(arg
, "--term-good") ||
724 !strcmp(arg
, "--term-old")) {
727 return error(_("'' is not a valid term"));
728 must_write_terms
= 1;
729 free((void *) terms
->term_good
);
730 terms
->term_good
= xstrdup(argv
[i
]);
731 } else if (skip_prefix(arg
, "--term-good=", &arg
) ||
732 skip_prefix(arg
, "--term-old=", &arg
)) {
733 must_write_terms
= 1;
734 free((void *) terms
->term_good
);
735 terms
->term_good
= xstrdup(arg
);
736 } else if (!strcmp(arg
, "--term-bad") ||
737 !strcmp(arg
, "--term-new")) {
740 return error(_("'' is not a valid term"));
741 must_write_terms
= 1;
742 free((void *) terms
->term_bad
);
743 terms
->term_bad
= xstrdup(argv
[i
]);
744 } else if (skip_prefix(arg
, "--term-bad=", &arg
) ||
745 skip_prefix(arg
, "--term-new=", &arg
)) {
746 must_write_terms
= 1;
747 free((void *) terms
->term_bad
);
748 terms
->term_bad
= xstrdup(arg
);
749 } else if (starts_with(arg
, "--")) {
750 return error(_("unrecognized option: '%s'"), arg
);
751 } else if (!get_oidf(&oid
, "%s^{commit}", arg
)) {
752 string_list_append(&revs
, oid_to_hex(&oid
));
753 } else if (has_double_dash
) {
754 die(_("'%s' does not appear to be a valid "
763 * The user ran "git bisect start <sha1> <sha1>", hence did not
764 * explicitly specify the terms, but we are already starting to
765 * set references named with the default terms, and won't be able
766 * to change afterwards.
769 must_write_terms
= 1;
770 for (i
= 0; i
< revs
.nr
; i
++) {
772 string_list_append(&states
, terms
->term_good
);
775 string_list_append(&states
, terms
->term_bad
);
782 head
= resolve_ref_unsafe("HEAD", 0, &head_oid
, &flags
);
784 if (repo_get_oid(the_repository
, "HEAD", &head_oid
))
785 return error(_("bad HEAD - I need a HEAD"));
788 * Check if we are bisecting
790 if (!is_empty_or_missing_file(git_path_bisect_start())) {
791 /* Reset to the rev from where we started */
792 strbuf_read_file(&start_head
, git_path_bisect_start(), 0);
793 strbuf_trim(&start_head
);
795 struct child_process cmd
= CHILD_PROCESS_INIT
;
798 strvec_pushl(&cmd
.args
, "checkout", start_head
.buf
,
800 if (run_command(&cmd
)) {
801 res
= error(_("checking out '%s' failed."
802 " Try 'git bisect start "
809 /* Get the rev from where we start. */
810 if (!repo_get_oid(the_repository
, head
, &head_oid
) &&
811 !starts_with(head
, "refs/heads/")) {
812 strbuf_reset(&start_head
);
813 strbuf_addstr(&start_head
, oid_to_hex(&head_oid
));
814 } else if (!repo_get_oid(the_repository
, head
, &head_oid
) &&
815 skip_prefix(head
, "refs/heads/", &head
)) {
816 strbuf_addstr(&start_head
, head
);
818 return error(_("bad HEAD - strange symbolic ref"));
823 * Get rid of any old bisect state.
825 if (bisect_clean_state())
826 return BISECT_FAILED
;
829 * Write new start state
831 write_file(git_path_bisect_start(), "%s\n", start_head
.buf
);
833 if (first_parent_only
)
834 write_file(git_path_bisect_first_parent(), "\n");
837 if (repo_get_oid(the_repository
, start_head
.buf
, &oid
) < 0) {
838 res
= error(_("invalid ref: '%s'"), start_head
.buf
);
841 if (update_ref(NULL
, "BISECT_HEAD", &oid
, NULL
, 0,
842 UPDATE_REFS_MSG_ON_ERR
)) {
848 if (pathspec_pos
< argc
- 1)
849 sq_quote_argv(&bisect_names
, argv
+ pathspec_pos
);
850 write_file(git_path_bisect_names(), "%s\n", bisect_names
.buf
);
852 for (i
= 0; i
< states
.nr
; i
++)
853 if (bisect_write(states
.items
[i
].string
,
854 revs
.items
[i
].string
, terms
, 1)) {
859 if (must_write_terms
&& write_terms(terms
->term_bad
,
865 res
= bisect_append_log_quoted(argv
);
870 string_list_clear(&revs
, 0);
871 string_list_clear(&states
, 0);
872 strbuf_release(&start_head
);
873 strbuf_release(&bisect_names
);
877 res
= bisect_auto_next(terms
, NULL
);
878 if (!is_bisect_success(res
))
879 bisect_clean_state();
883 static inline int file_is_not_empty(const char *path
)
885 return !is_empty_or_missing_file(path
);
888 static int bisect_autostart(struct bisect_terms
*terms
)
893 if (file_is_not_empty(git_path_bisect_start()))
896 fprintf_ln(stderr
, _("You need to start by \"git bisect "
899 if (!isatty(STDIN_FILENO
))
903 * TRANSLATORS: Make sure to include [Y] and [n] in your
904 * translation. The program will only accept English input
907 yesno
= git_prompt(_("Do you want me to do it for you "
908 "[Y/n]? "), PROMPT_ECHO
);
909 res
= tolower(*yesno
) == 'n' ?
910 -1 : bisect_start(terms
, 0, empty_strvec
);
915 static enum bisect_error
bisect_state(struct bisect_terms
*terms
, int argc
,
919 int i
, verify_expected
= 1;
920 struct object_id oid
, expected
;
921 struct oid_array revs
= OID_ARRAY_INIT
;
924 return error(_("Please call `--bisect-state` with at least one argument"));
926 if (bisect_autostart(terms
))
927 return BISECT_FAILED
;
930 if (check_and_set_terms(terms
, state
) ||
931 !one_of(state
, terms
->term_good
, terms
->term_bad
, "skip", NULL
))
932 return BISECT_FAILED
;
936 if (argc
> 1 && !strcmp(state
, terms
->term_bad
))
937 return error(_("'git bisect %s' can take only one argument."), terms
->term_bad
);
940 const char *head
= "BISECT_HEAD";
941 enum get_oid_result res_head
= repo_get_oid(the_repository
,
944 if (res_head
== MISSING_OBJECT
) {
946 res_head
= repo_get_oid(the_repository
, head
, &oid
);
950 error(_("Bad rev input: %s"), head
);
951 oid_array_append(&revs
, &oid
);
955 * All input revs must be checked before executing bisect_write()
956 * to discard junk revs.
959 for (; argc
; argc
--, argv
++) {
960 struct commit
*commit
;
962 if (repo_get_oid(the_repository
, *argv
, &oid
)){
963 error(_("Bad rev input: %s"), *argv
);
964 oid_array_clear(&revs
);
965 return BISECT_FAILED
;
968 commit
= lookup_commit_reference(the_repository
, &oid
);
970 die(_("Bad rev input (not a commit): %s"), *argv
);
972 oid_array_append(&revs
, &commit
->object
.oid
);
975 if (read_ref("BISECT_EXPECTED_REV", &expected
))
976 verify_expected
= 0; /* Ignore invalid file contents */
978 for (i
= 0; i
< revs
.nr
; i
++) {
979 if (bisect_write(state
, oid_to_hex(&revs
.oid
[i
]), terms
, 0)) {
980 oid_array_clear(&revs
);
981 return BISECT_FAILED
;
983 if (verify_expected
&& !oideq(&revs
.oid
[i
], &expected
)) {
984 unlink_or_warn(git_path_bisect_ancestors_ok());
985 delete_ref(NULL
, "BISECT_EXPECTED_REV", NULL
, REF_NO_DEREF
);
990 oid_array_clear(&revs
);
991 return bisect_auto_next(terms
, NULL
);
994 static enum bisect_error
bisect_log(void)
997 const char* filename
= git_path_bisect_log();
999 if (is_empty_or_missing_file(filename
))
1000 return error(_("We are not bisecting."));
1002 fd
= open(filename
, O_RDONLY
);
1004 return BISECT_FAILED
;
1006 status
= copy_fd(fd
, STDOUT_FILENO
);
1008 return status
? BISECT_FAILED
: BISECT_OK
;
1011 static int process_replay_line(struct bisect_terms
*terms
, struct strbuf
*line
)
1013 const char *p
= line
->buf
+ strspn(line
->buf
, " \t");
1014 char *word_end
, *rev
;
1016 if ((!skip_prefix(p
, "git bisect", &p
) &&
1017 !skip_prefix(p
, "git-bisect", &p
)) || !isspace(*p
))
1019 p
+= strspn(p
, " \t");
1021 word_end
= (char *)p
+ strcspn(p
, " \t");
1022 rev
= word_end
+ strspn(word_end
, " \t");
1023 *word_end
= '\0'; /* NUL-terminate the word */
1026 if (check_and_set_terms(terms
, p
))
1029 if (!strcmp(p
, "start")) {
1030 struct strvec argv
= STRVEC_INIT
;
1032 sq_dequote_to_strvec(rev
, &argv
);
1033 res
= bisect_start(terms
, argv
.nr
, argv
.v
);
1034 strvec_clear(&argv
);
1038 if (one_of(p
, terms
->term_good
,
1039 terms
->term_bad
, "skip", NULL
))
1040 return bisect_write(p
, rev
, terms
, 0);
1042 if (!strcmp(p
, "terms")) {
1043 struct strvec argv
= STRVEC_INIT
;
1045 sq_dequote_to_strvec(rev
, &argv
);
1046 res
= bisect_terms(terms
, argv
.nr
== 1 ? argv
.v
[0] : NULL
);
1047 strvec_clear(&argv
);
1050 error(_("'%s'?? what are you talking about?"), p
);
1055 static enum bisect_error
bisect_replay(struct bisect_terms
*terms
, const char *filename
)
1058 enum bisect_error res
= BISECT_OK
;
1059 struct strbuf line
= STRBUF_INIT
;
1061 if (is_empty_or_missing_file(filename
))
1062 return error(_("cannot read file '%s' for replaying"), filename
);
1064 if (bisect_reset(NULL
))
1065 return BISECT_FAILED
;
1067 fp
= fopen(filename
, "r");
1069 return BISECT_FAILED
;
1071 while ((strbuf_getline(&line
, fp
) != EOF
) && !res
)
1072 res
= process_replay_line(terms
, &line
);
1074 strbuf_release(&line
);
1078 return BISECT_FAILED
;
1080 return bisect_auto_next(terms
, NULL
);
1083 static enum bisect_error
bisect_skip(struct bisect_terms
*terms
, int argc
,
1087 enum bisect_error res
;
1088 struct strvec argv_state
= STRVEC_INIT
;
1090 strvec_push(&argv_state
, "skip");
1092 for (i
= 0; i
< argc
; i
++) {
1093 const char *dotdot
= strstr(argv
[i
], "..");
1096 struct rev_info revs
;
1097 struct commit
*commit
;
1099 repo_init_revisions(the_repository
, &revs
, NULL
);
1100 setup_revisions(2, argv
+ i
- 1, &revs
, NULL
);
1102 if (prepare_revision_walk(&revs
))
1103 die(_("revision walk setup failed\n"));
1104 while ((commit
= get_revision(&revs
)) != NULL
)
1105 strvec_push(&argv_state
,
1106 oid_to_hex(&commit
->object
.oid
));
1108 reset_revision_walk();
1109 release_revisions(&revs
);
1111 strvec_push(&argv_state
, argv
[i
]);
1114 res
= bisect_state(terms
, argv_state
.nr
, argv_state
.v
);
1116 strvec_clear(&argv_state
);
1120 static int bisect_visualize(struct bisect_terms
*terms
, int argc
,
1123 struct child_process cmd
= CHILD_PROCESS_INIT
;
1124 struct strbuf sb
= STRBUF_INIT
;
1126 if (bisect_next_check(terms
, NULL
) != 0)
1127 return BISECT_FAILED
;
1131 if ((getenv("DISPLAY") || getenv("SESSIONNAME") || getenv("MSYSTEM") ||
1132 getenv("SECURITYSESSIONID")) && exists_in_PATH("gitk")) {
1133 strvec_push(&cmd
.args
, "gitk");
1135 strvec_push(&cmd
.args
, "log");
1139 if (argv
[0][0] == '-') {
1140 strvec_push(&cmd
.args
, "log");
1142 } else if (strcmp(argv
[0], "tig") && !starts_with(argv
[0], "git"))
1145 strvec_pushv(&cmd
.args
, argv
);
1148 strvec_pushl(&cmd
.args
, "--bisect", "--", NULL
);
1150 strbuf_read_file(&sb
, git_path_bisect_names(), 0);
1151 sq_dequote_to_strvec(sb
.buf
, &cmd
.args
);
1152 strbuf_release(&sb
);
1154 return run_command(&cmd
);
1157 static int get_first_good(const char *refname UNUSED
,
1158 const struct object_id
*oid
,
1159 int flag UNUSED
, void *cb_data
)
1161 oidcpy(cb_data
, oid
);
1165 static int do_bisect_run(const char *command
)
1167 struct child_process cmd
= CHILD_PROCESS_INIT
;
1169 printf(_("running %s\n"), command
);
1171 strvec_push(&cmd
.args
, command
);
1172 return run_command(&cmd
);
1175 static int verify_good(const struct bisect_terms
*terms
, const char *command
)
1178 enum bisect_error res
;
1179 struct object_id good_rev
;
1180 struct object_id current_rev
;
1181 char *good_glob
= xstrfmt("%s-*", terms
->term_good
);
1182 int no_checkout
= ref_exists("BISECT_HEAD");
1184 for_each_glob_ref_in(get_first_good
, good_glob
, "refs/bisect/",
1188 if (read_ref(no_checkout
? "BISECT_HEAD" : "HEAD", ¤t_rev
))
1191 res
= bisect_checkout(&good_rev
, no_checkout
);
1192 if (res
!= BISECT_OK
)
1195 rc
= do_bisect_run(command
);
1197 res
= bisect_checkout(¤t_rev
, no_checkout
);
1198 if (res
!= BISECT_OK
)
1204 static int bisect_run(struct bisect_terms
*terms
, int argc
, const char **argv
)
1206 int res
= BISECT_OK
;
1207 struct strbuf command
= STRBUF_INIT
;
1208 const char *new_state
;
1209 int temporary_stdout_fd
, saved_stdout
;
1210 int is_first_run
= 1;
1212 if (bisect_next_check(terms
, NULL
))
1213 return BISECT_FAILED
;
1216 error(_("bisect run failed: no command provided."));
1217 return BISECT_FAILED
;
1220 sq_quote_argv(&command
, argv
);
1221 strbuf_ltrim(&command
);
1223 res
= do_bisect_run(command
.buf
);
1226 * Exit code 126 and 127 can either come from the shell
1227 * if it was unable to execute or even find the script,
1228 * or from the script itself. Check with a known-good
1229 * revision to avoid trashing the bisect run due to a
1230 * missing or non-executable script.
1232 if (is_first_run
&& (res
== 126 || res
== 127)) {
1233 int rc
= verify_good(terms
, command
.buf
);
1235 if (rc
< 0 || 128 <= rc
) {
1236 error(_("unable to verify %s on good"
1237 " revision"), command
.buf
);
1238 res
= BISECT_FAILED
;
1242 error(_("bogus exit code %d for good revision"),
1244 res
= BISECT_FAILED
;
1249 if (res
< 0 || 128 <= res
) {
1250 error(_("bisect run failed: exit code %d from"
1251 " %s is < 0 or >= 128"), res
, command
.buf
);
1258 new_state
= terms
->term_good
;
1260 new_state
= terms
->term_bad
;
1262 temporary_stdout_fd
= open(git_path_bisect_run(), O_CREAT
| O_WRONLY
| O_TRUNC
, 0666);
1264 if (temporary_stdout_fd
< 0) {
1265 res
= error_errno(_("cannot open file '%s' for writing"), git_path_bisect_run());
1270 saved_stdout
= dup(1);
1271 dup2(temporary_stdout_fd
, 1);
1273 res
= bisect_state(terms
, 1, &new_state
);
1276 dup2(saved_stdout
, 1);
1277 close(saved_stdout
);
1278 close(temporary_stdout_fd
);
1280 print_file_to_stdout(git_path_bisect_run());
1282 if (res
== BISECT_ONLY_SKIPPED_LEFT
)
1283 error(_("bisect run cannot continue any more"));
1284 else if (res
== BISECT_INTERNAL_SUCCESS_MERGE_BASE
) {
1285 puts(_("bisect run success"));
1287 } else if (res
== BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND
) {
1288 puts(_("bisect found first bad commit"));
1291 error(_("bisect run failed: 'git bisect %s'"
1292 " exited with error code %d"), new_state
, res
);
1299 strbuf_release(&command
);
1303 static int cmd_bisect__reset(int argc
, const char **argv
, const char *prefix UNUSED
)
1306 return error(_("'%s' requires either no argument or a commit"),
1307 "git bisect reset");
1308 return bisect_reset(argc
? argv
[0] : NULL
);
1311 static int cmd_bisect__terms(int argc
, const char **argv
, const char *prefix UNUSED
)
1314 struct bisect_terms terms
= { 0 };
1317 return error(_("'%s' requires 0 or 1 argument"),
1318 "git bisect terms");
1319 res
= bisect_terms(&terms
, argc
== 1 ? argv
[0] : NULL
);
1324 static int cmd_bisect__start(int argc
, const char **argv
, const char *prefix UNUSED
)
1327 struct bisect_terms terms
= { 0 };
1329 set_terms(&terms
, "bad", "good");
1330 res
= bisect_start(&terms
, argc
, argv
);
1335 static int cmd_bisect__next(int argc
, const char **argv UNUSED
, const char *prefix
)
1338 struct bisect_terms terms
= { 0 };
1341 return error(_("'%s' requires 0 arguments"),
1344 res
= bisect_next(&terms
, prefix
);
1349 static int cmd_bisect__log(int argc UNUSED
, const char **argv UNUSED
, const char *prefix UNUSED
)
1351 return bisect_log();
1354 static int cmd_bisect__replay(int argc
, const char **argv
, const char *prefix UNUSED
)
1357 struct bisect_terms terms
= { 0 };
1360 return error(_("no logfile given"));
1361 set_terms(&terms
, "bad", "good");
1362 res
= bisect_replay(&terms
, argv
[0]);
1367 static int cmd_bisect__skip(int argc
, const char **argv
, const char *prefix UNUSED
)
1370 struct bisect_terms terms
= { 0 };
1372 set_terms(&terms
, "bad", "good");
1374 res
= bisect_skip(&terms
, argc
, argv
);
1379 static int cmd_bisect__visualize(int argc
, const char **argv
, const char *prefix UNUSED
)
1382 struct bisect_terms terms
= { 0 };
1385 res
= bisect_visualize(&terms
, argc
, argv
);
1390 static int cmd_bisect__run(int argc
, const char **argv
, const char *prefix UNUSED
)
1393 struct bisect_terms terms
= { 0 };
1396 return error(_("'%s' failed: no command provided."), "git bisect run");
1398 res
= bisect_run(&terms
, argc
, argv
);
1403 int cmd_bisect(int argc
, const char **argv
, const char *prefix
)
1406 parse_opt_subcommand_fn
*fn
= NULL
;
1407 struct option options
[] = {
1408 OPT_SUBCOMMAND("reset", &fn
, cmd_bisect__reset
),
1409 OPT_SUBCOMMAND("terms", &fn
, cmd_bisect__terms
),
1410 OPT_SUBCOMMAND("start", &fn
, cmd_bisect__start
),
1411 OPT_SUBCOMMAND("next", &fn
, cmd_bisect__next
),
1412 OPT_SUBCOMMAND("log", &fn
, cmd_bisect__log
),
1413 OPT_SUBCOMMAND("replay", &fn
, cmd_bisect__replay
),
1414 OPT_SUBCOMMAND("skip", &fn
, cmd_bisect__skip
),
1415 OPT_SUBCOMMAND("visualize", &fn
, cmd_bisect__visualize
),
1416 OPT_SUBCOMMAND("view", &fn
, cmd_bisect__visualize
),
1417 OPT_SUBCOMMAND("run", &fn
, cmd_bisect__run
),
1420 argc
= parse_options(argc
, argv
, prefix
, options
, git_bisect_usage
,
1421 PARSE_OPT_SUBCOMMAND_OPTIONAL
);
1424 struct bisect_terms terms
= { 0 };
1427 usage_msg_opt(_("need a command"), git_bisect_usage
, options
);
1429 set_terms(&terms
, "bad", "good");
1431 if (check_and_set_terms(&terms
, argv
[0]))
1432 usage_msg_optf(_("unknown command: '%s'"), git_bisect_usage
,
1434 res
= bisect_state(&terms
, argc
, argv
);
1439 res
= fn(argc
, argv
, prefix
);
1442 return is_bisect_success(res
) ? 0 : -res
;