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")
22 static const char * const git_bisect_helper_usage
[] = {
23 N_("git bisect--helper --bisect-reset [<commit>]"),
24 N_("git bisect--helper --bisect-next-check <good_term> <bad_term> [<term>]"),
25 N_("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 N_("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>)...]"),
36 struct add_bisect_ref_data
{
37 struct rev_info
*revs
;
38 unsigned int object_flags
;
46 static void free_terms(struct bisect_terms
*terms
)
48 FREE_AND_NULL(terms
->term_good
);
49 FREE_AND_NULL(terms
->term_bad
);
52 static void set_terms(struct bisect_terms
*terms
, const char *bad
,
55 free((void *)terms
->term_good
);
56 terms
->term_good
= xstrdup(good
);
57 free((void *)terms
->term_bad
);
58 terms
->term_bad
= xstrdup(bad
);
61 static const char vocab_bad
[] = "bad|new";
62 static const char vocab_good
[] = "good|old";
64 static int bisect_autostart(struct bisect_terms
*terms
);
67 * Check whether the string `term` belongs to the set of strings
68 * included in the variable arguments.
71 static int one_of(const char *term
, ...)
77 va_start(matches
, term
);
78 while (!res
&& (match
= va_arg(matches
, const char *)))
79 res
= !strcmp(term
, match
);
86 * return code BISECT_INTERNAL_SUCCESS_MERGE_BASE
87 * and BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND are codes
88 * that indicate special success.
91 static int is_bisect_success(enum bisect_error res
)
94 res
== BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND
||
95 res
== BISECT_INTERNAL_SUCCESS_MERGE_BASE
;
98 static int write_in_file(const char *path
, const char *mode
, const char *format
, va_list args
)
103 if (strcmp(mode
, "w") && strcmp(mode
, "a"))
104 BUG("write-in-file does not support '%s' mode", mode
);
105 fp
= fopen(path
, mode
);
107 return error_errno(_("cannot open file '%s' in mode '%s'"), path
, mode
);
108 res
= vfprintf(fp
, format
, args
);
111 int saved_errno
= errno
;
114 return error_errno(_("could not write to file '%s'"), path
);
120 __attribute__((format (printf
, 2, 3)))
121 static int write_to_file(const char *path
, const char *format
, ...)
126 va_start(args
, format
);
127 res
= write_in_file(path
, "w", format
, args
);
133 __attribute__((format (printf
, 2, 3)))
134 static int append_to_file(const char *path
, const char *format
, ...)
139 va_start(args
, format
);
140 res
= write_in_file(path
, "a", format
, args
);
146 static int check_term_format(const char *term
, const char *orig_term
)
149 char *new_term
= xstrfmt("refs/bisect/%s", term
);
151 res
= check_refname_format(new_term
, 0);
155 return error(_("'%s' is not a valid term"), term
);
157 if (one_of(term
, "help", "start", "skip", "next", "reset",
158 "visualize", "view", "replay", "log", "run", "terms", NULL
))
159 return error(_("can't use the builtin command '%s' as a term"), term
);
162 * In theory, nothing prevents swapping completely good and bad,
163 * but this situation could be confusing and hasn't been tested
164 * enough. Forbid it for now.
167 if ((strcmp(orig_term
, "bad") && one_of(term
, "bad", "new", NULL
)) ||
168 (strcmp(orig_term
, "good") && one_of(term
, "good", "old", NULL
)))
169 return error(_("can't change the meaning of the term '%s'"), term
);
174 static int write_terms(const char *bad
, const char *good
)
178 if (!strcmp(bad
, good
))
179 return error(_("please use two different terms"));
181 if (check_term_format(bad
, "bad") || check_term_format(good
, "good"))
184 res
= write_to_file(git_path_bisect_terms(), "%s\n%s\n", bad
, good
);
189 static int bisect_reset(const char *commit
)
191 struct strbuf branch
= STRBUF_INIT
;
194 if (strbuf_read_file(&branch
, git_path_bisect_start(), 0) < 1) {
195 printf(_("We are not bisecting.\n"));
198 strbuf_rtrim(&branch
);
200 struct object_id oid
;
202 if (get_oid_commit(commit
, &oid
))
203 return error(_("'%s' is not a valid commit"), commit
);
204 strbuf_addstr(&branch
, commit
);
207 if (!ref_exists("BISECT_HEAD")) {
208 struct strvec argv
= STRVEC_INIT
;
210 strvec_pushl(&argv
, "checkout", branch
.buf
, "--", NULL
);
211 if (run_command_v_opt(argv
.v
, RUN_GIT_CMD
)) {
212 error(_("could not check out original"
213 " HEAD '%s'. Try 'git bisect"
214 " reset <commit>'."), branch
.buf
);
215 strbuf_release(&branch
);
222 strbuf_release(&branch
);
223 return bisect_clean_state();
226 static void log_commit(FILE *fp
, char *fmt
, const char *state
,
227 struct commit
*commit
)
229 struct pretty_print_context pp
= {0};
230 struct strbuf commit_msg
= STRBUF_INIT
;
231 char *label
= xstrfmt(fmt
, state
);
233 format_commit_message(commit
, "%s", &commit_msg
, &pp
);
235 fprintf(fp
, "# %s: [%s] %s\n", label
, oid_to_hex(&commit
->object
.oid
),
238 strbuf_release(&commit_msg
);
242 static int bisect_write(const char *state
, const char *rev
,
243 const struct bisect_terms
*terms
, int nolog
)
245 struct strbuf tag
= STRBUF_INIT
;
246 struct object_id oid
;
247 struct commit
*commit
;
251 if (!strcmp(state
, terms
->term_bad
)) {
252 strbuf_addf(&tag
, "refs/bisect/%s", state
);
253 } else if (one_of(state
, terms
->term_good
, "skip", NULL
)) {
254 strbuf_addf(&tag
, "refs/bisect/%s-%s", state
, rev
);
256 res
= error(_("Bad bisect_write argument: %s"), state
);
260 if (get_oid(rev
, &oid
)) {
261 res
= error(_("couldn't get the oid of the rev '%s'"), rev
);
265 if (update_ref(NULL
, tag
.buf
, &oid
, NULL
, 0,
266 UPDATE_REFS_MSG_ON_ERR
)) {
271 fp
= fopen(git_path_bisect_log(), "a");
273 res
= error_errno(_("couldn't open the file '%s'"), git_path_bisect_log());
277 commit
= lookup_commit_reference(the_repository
, &oid
);
278 log_commit(fp
, "%s", state
, commit
);
281 fprintf(fp
, "git bisect %s %s\n", state
, rev
);
286 strbuf_release(&tag
);
290 static int check_and_set_terms(struct bisect_terms
*terms
, const char *cmd
)
292 int has_term_file
= !is_empty_or_missing_file(git_path_bisect_terms());
294 if (one_of(cmd
, "skip", "start", "terms", NULL
))
297 if (has_term_file
&& strcmp(cmd
, terms
->term_bad
) &&
298 strcmp(cmd
, terms
->term_good
))
299 return error(_("Invalid command: you're currently in a "
300 "%s/%s bisect"), terms
->term_bad
,
303 if (!has_term_file
) {
304 if (one_of(cmd
, "bad", "good", NULL
)) {
305 set_terms(terms
, "bad", "good");
306 return write_terms(terms
->term_bad
, terms
->term_good
);
308 if (one_of(cmd
, "new", "old", NULL
)) {
309 set_terms(terms
, "new", "old");
310 return write_terms(terms
->term_bad
, terms
->term_good
);
317 static int mark_good(const char *refname
, const struct object_id
*oid
,
318 int flag
, void *cb_data
)
320 int *m_good
= (int *)cb_data
;
325 static const char need_bad_and_good_revision_warning
[] =
326 N_("You need to give me at least one %s and %s revision.\n"
327 "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
329 static const char need_bisect_start_warning
[] =
330 N_("You need to start by \"git bisect start\".\n"
331 "You then need to give me at least one %s and %s revision.\n"
332 "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
334 static int decide_next(const struct bisect_terms
*terms
,
335 const char *current_term
, int missing_good
,
338 if (!missing_good
&& !missing_bad
)
343 if (missing_good
&& !missing_bad
&&
344 !strcmp(current_term
, terms
->term_good
)) {
347 * have bad (or new) but not good (or old). We could bisect
348 * although this is less optimum.
350 warning(_("bisecting only with a %s commit"), terms
->term_bad
);
354 * TRANSLATORS: Make sure to include [Y] and [n] in your
355 * translation. The program will only accept English input
358 yesno
= git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO
);
359 if (starts_with(yesno
, "N") || starts_with(yesno
, "n"))
364 if (!is_empty_or_missing_file(git_path_bisect_start()))
365 return error(_(need_bad_and_good_revision_warning
),
366 vocab_bad
, vocab_good
, vocab_bad
, vocab_good
);
368 return error(_(need_bisect_start_warning
),
369 vocab_good
, vocab_bad
, vocab_good
, vocab_bad
);
372 static int bisect_next_check(const struct bisect_terms
*terms
,
373 const char *current_term
)
375 int missing_good
= 1, missing_bad
= 1;
376 char *bad_ref
= xstrfmt("refs/bisect/%s", terms
->term_bad
);
377 char *good_glob
= xstrfmt("%s-*", terms
->term_good
);
379 if (ref_exists(bad_ref
))
382 for_each_glob_ref_in(mark_good
, good_glob
, "refs/bisect/",
383 (void *) &missing_good
);
388 return decide_next(terms
, current_term
, missing_good
, missing_bad
);
391 static int get_terms(struct bisect_terms
*terms
)
393 struct strbuf str
= STRBUF_INIT
;
397 fp
= fopen(git_path_bisect_terms(), "r");
404 strbuf_getline_lf(&str
, fp
);
405 terms
->term_bad
= strbuf_detach(&str
, NULL
);
406 strbuf_getline_lf(&str
, fp
);
407 terms
->term_good
= strbuf_detach(&str
, NULL
);
412 strbuf_release(&str
);
416 static int bisect_terms(struct bisect_terms
*terms
, const char *option
)
418 if (get_terms(terms
))
419 return error(_("no terms defined"));
421 if (option
== NULL
) {
422 printf(_("Your current terms are %s for the old state\n"
423 "and %s for the new state.\n"),
424 terms
->term_good
, terms
->term_bad
);
427 if (one_of(option
, "--term-good", "--term-old", NULL
))
428 printf("%s\n", terms
->term_good
);
429 else if (one_of(option
, "--term-bad", "--term-new", NULL
))
430 printf("%s\n", terms
->term_bad
);
432 return error(_("invalid argument %s for 'git bisect terms'.\n"
433 "Supported options are: "
434 "--term-good|--term-old and "
435 "--term-bad|--term-new."), option
);
440 static int bisect_append_log_quoted(const char **argv
)
443 FILE *fp
= fopen(git_path_bisect_log(), "a");
444 struct strbuf orig_args
= STRBUF_INIT
;
449 if (fprintf(fp
, "git bisect start") < 1) {
454 sq_quote_argv(&orig_args
, argv
);
455 if (fprintf(fp
, "%s\n", orig_args
.buf
) < 1)
460 strbuf_release(&orig_args
);
464 static int add_bisect_ref(const char *refname
, const struct object_id
*oid
,
467 struct add_bisect_ref_data
*data
= cb
;
469 add_pending_oid(data
->revs
, refname
, oid
, data
->object_flags
);
474 static int prepare_revs(struct bisect_terms
*terms
, struct rev_info
*revs
)
477 struct add_bisect_ref_data cb
= { revs
};
478 char *good
= xstrfmt("%s-*", terms
->term_good
);
481 * We cannot use terms->term_bad directly in
482 * for_each_glob_ref_in() and we have to append a '*' to it,
483 * otherwise for_each_glob_ref_in() will append '/' and '*'.
485 char *bad
= xstrfmt("%s*", terms
->term_bad
);
488 * It is important to reset the flags used by revision walks
489 * as the previous call to bisect_next_all() in turn
490 * sets up a revision walk.
492 reset_revision_walk();
493 init_revisions(revs
, NULL
);
494 setup_revisions(0, NULL
, revs
, NULL
);
495 for_each_glob_ref_in(add_bisect_ref
, bad
, "refs/bisect/", &cb
);
496 cb
.object_flags
= UNINTERESTING
;
497 for_each_glob_ref_in(add_bisect_ref
, good
, "refs/bisect/", &cb
);
498 if (prepare_revision_walk(revs
))
499 res
= error(_("revision walk setup failed\n"));
506 static int bisect_skipped_commits(struct bisect_terms
*terms
)
510 struct rev_info revs
;
511 struct commit
*commit
;
512 struct pretty_print_context pp
= {0};
513 struct strbuf commit_name
= STRBUF_INIT
;
515 res
= prepare_revs(terms
, &revs
);
519 fp
= fopen(git_path_bisect_log(), "a");
521 return error_errno(_("could not open '%s' for appending"),
522 git_path_bisect_log());
524 if (fprintf(fp
, "# only skipped commits left to test\n") < 0)
525 return error_errno(_("failed to write to '%s'"), git_path_bisect_log());
527 while ((commit
= get_revision(&revs
)) != NULL
) {
528 strbuf_reset(&commit_name
);
529 format_commit_message(commit
, "%s",
531 fprintf(fp
, "# possible first %s commit: [%s] %s\n",
532 terms
->term_bad
, oid_to_hex(&commit
->object
.oid
),
537 * Reset the flags used by revision walks in case
538 * there is another revision walk after this one.
540 reset_revision_walk();
542 strbuf_release(&commit_name
);
547 static int bisect_successful(struct bisect_terms
*terms
)
549 struct object_id oid
;
550 struct commit
*commit
;
551 struct pretty_print_context pp
= {0};
552 struct strbuf commit_name
= STRBUF_INIT
;
553 char *bad_ref
= xstrfmt("refs/bisect/%s",terms
->term_bad
);
556 read_ref(bad_ref
, &oid
);
557 commit
= lookup_commit_reference_by_name(bad_ref
);
558 format_commit_message(commit
, "%s", &commit_name
, &pp
);
560 res
= append_to_file(git_path_bisect_log(), "# first %s commit: [%s] %s\n",
561 terms
->term_bad
, oid_to_hex(&commit
->object
.oid
),
564 strbuf_release(&commit_name
);
569 static enum bisect_error
bisect_next(struct bisect_terms
*terms
, const char *prefix
)
571 enum bisect_error res
;
573 if (bisect_autostart(terms
))
574 return BISECT_FAILED
;
576 if (bisect_next_check(terms
, terms
->term_good
))
577 return BISECT_FAILED
;
579 /* Perform all bisection computation */
580 res
= bisect_next_all(the_repository
, prefix
);
582 if (res
== BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND
) {
583 res
= bisect_successful(terms
);
584 return res
? res
: BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND
;
585 } else if (res
== BISECT_ONLY_SKIPPED_LEFT
) {
586 res
= bisect_skipped_commits(terms
);
587 return res
? res
: BISECT_ONLY_SKIPPED_LEFT
;
592 static enum bisect_error
bisect_auto_next(struct bisect_terms
*terms
, const char *prefix
)
594 if (bisect_next_check(terms
, NULL
))
597 return bisect_next(terms
, prefix
);
600 static enum bisect_error
bisect_start(struct bisect_terms
*terms
, const char **argv
, int argc
)
603 int first_parent_only
= 0;
604 int i
, has_double_dash
= 0, must_write_terms
= 0, bad_seen
= 0;
605 int flags
, pathspec_pos
;
606 enum bisect_error res
= BISECT_OK
;
607 struct string_list revs
= STRING_LIST_INIT_DUP
;
608 struct string_list states
= STRING_LIST_INIT_DUP
;
609 struct strbuf start_head
= STRBUF_INIT
;
610 struct strbuf bisect_names
= STRBUF_INIT
;
611 struct object_id head_oid
;
612 struct object_id oid
;
615 if (is_bare_repository())
619 * Check for one bad and then some good revisions
621 for (i
= 0; i
< argc
; i
++) {
622 if (!strcmp(argv
[i
], "--")) {
628 for (i
= 0; i
< argc
; i
++) {
629 const char *arg
= argv
[i
];
630 if (!strcmp(argv
[i
], "--")) {
632 } else if (!strcmp(arg
, "--no-checkout")) {
634 } else if (!strcmp(arg
, "--first-parent")) {
635 first_parent_only
= 1;
636 } else if (!strcmp(arg
, "--term-good") ||
637 !strcmp(arg
, "--term-old")) {
640 return error(_("'' is not a valid term"));
641 must_write_terms
= 1;
642 free((void *) terms
->term_good
);
643 terms
->term_good
= xstrdup(argv
[i
]);
644 } else if (skip_prefix(arg
, "--term-good=", &arg
) ||
645 skip_prefix(arg
, "--term-old=", &arg
)) {
646 must_write_terms
= 1;
647 free((void *) terms
->term_good
);
648 terms
->term_good
= xstrdup(arg
);
649 } else if (!strcmp(arg
, "--term-bad") ||
650 !strcmp(arg
, "--term-new")) {
653 return error(_("'' is not a valid term"));
654 must_write_terms
= 1;
655 free((void *) terms
->term_bad
);
656 terms
->term_bad
= xstrdup(argv
[i
]);
657 } else if (skip_prefix(arg
, "--term-bad=", &arg
) ||
658 skip_prefix(arg
, "--term-new=", &arg
)) {
659 must_write_terms
= 1;
660 free((void *) terms
->term_bad
);
661 terms
->term_bad
= xstrdup(arg
);
662 } else if (starts_with(arg
, "--")) {
663 return error(_("unrecognized option: '%s'"), arg
);
664 } else if (!get_oidf(&oid
, "%s^{commit}", arg
)) {
665 string_list_append(&revs
, oid_to_hex(&oid
));
666 } else if (has_double_dash
) {
667 die(_("'%s' does not appear to be a valid "
676 * The user ran "git bisect start <sha1> <sha1>", hence did not
677 * explicitly specify the terms, but we are already starting to
678 * set references named with the default terms, and won't be able
679 * to change afterwards.
682 must_write_terms
= 1;
683 for (i
= 0; i
< revs
.nr
; i
++) {
685 string_list_append(&states
, terms
->term_good
);
688 string_list_append(&states
, terms
->term_bad
);
695 head
= resolve_ref_unsafe("HEAD", 0, &head_oid
, &flags
);
697 if (get_oid("HEAD", &head_oid
))
698 return error(_("bad HEAD - I need a HEAD"));
701 * Check if we are bisecting
703 if (!is_empty_or_missing_file(git_path_bisect_start())) {
704 /* Reset to the rev from where we started */
705 strbuf_read_file(&start_head
, git_path_bisect_start(), 0);
706 strbuf_trim(&start_head
);
708 struct strvec argv
= STRVEC_INIT
;
710 strvec_pushl(&argv
, "checkout", start_head
.buf
,
712 if (run_command_v_opt(argv
.v
, RUN_GIT_CMD
)) {
713 res
= error(_("checking out '%s' failed."
714 " Try 'git bisect start "
721 /* Get the rev from where we start. */
722 if (!get_oid(head
, &head_oid
) &&
723 !starts_with(head
, "refs/heads/")) {
724 strbuf_reset(&start_head
);
725 strbuf_addstr(&start_head
, oid_to_hex(&head_oid
));
726 } else if (!get_oid(head
, &head_oid
) &&
727 skip_prefix(head
, "refs/heads/", &head
)) {
729 * This error message should only be triggered by
730 * cogito usage, and cogito users should understand
731 * it relates to cg-seek.
733 if (!is_empty_or_missing_file(git_path_head_name()))
734 return error(_("won't bisect on cg-seek'ed tree"));
735 strbuf_addstr(&start_head
, head
);
737 return error(_("bad HEAD - strange symbolic ref"));
742 * Get rid of any old bisect state.
744 if (bisect_clean_state())
745 return BISECT_FAILED
;
748 * Write new start state
750 write_file(git_path_bisect_start(), "%s\n", start_head
.buf
);
752 if (first_parent_only
)
753 write_file(git_path_bisect_first_parent(), "\n");
756 if (get_oid(start_head
.buf
, &oid
) < 0) {
757 res
= error(_("invalid ref: '%s'"), start_head
.buf
);
760 if (update_ref(NULL
, "BISECT_HEAD", &oid
, NULL
, 0,
761 UPDATE_REFS_MSG_ON_ERR
)) {
767 if (pathspec_pos
< argc
- 1)
768 sq_quote_argv(&bisect_names
, argv
+ pathspec_pos
);
769 write_file(git_path_bisect_names(), "%s\n", bisect_names
.buf
);
771 for (i
= 0; i
< states
.nr
; i
++)
772 if (bisect_write(states
.items
[i
].string
,
773 revs
.items
[i
].string
, terms
, 1)) {
778 if (must_write_terms
&& write_terms(terms
->term_bad
,
784 res
= bisect_append_log_quoted(argv
);
789 string_list_clear(&revs
, 0);
790 string_list_clear(&states
, 0);
791 strbuf_release(&start_head
);
792 strbuf_release(&bisect_names
);
796 res
= bisect_auto_next(terms
, NULL
);
797 if (!is_bisect_success(res
))
798 bisect_clean_state();
802 static inline int file_is_not_empty(const char *path
)
804 return !is_empty_or_missing_file(path
);
807 static int bisect_autostart(struct bisect_terms
*terms
)
812 if (file_is_not_empty(git_path_bisect_start()))
815 fprintf_ln(stderr
, _("You need to start by \"git bisect "
818 if (!isatty(STDIN_FILENO
))
822 * TRANSLATORS: Make sure to include [Y] and [n] in your
823 * translation. The program will only accept English input
826 yesno
= git_prompt(_("Do you want me to do it for you "
827 "[Y/n]? "), PROMPT_ECHO
);
828 res
= tolower(*yesno
) == 'n' ?
829 -1 : bisect_start(terms
, empty_strvec
, 0);
834 static enum bisect_error
bisect_state(struct bisect_terms
*terms
, const char **argv
,
838 int i
, verify_expected
= 1;
839 struct object_id oid
, expected
;
840 struct strbuf buf
= STRBUF_INIT
;
841 struct oid_array revs
= OID_ARRAY_INIT
;
844 return error(_("Please call `--bisect-state` with at least one argument"));
846 if (bisect_autostart(terms
))
847 return BISECT_FAILED
;
850 if (check_and_set_terms(terms
, state
) ||
851 !one_of(state
, terms
->term_good
, terms
->term_bad
, "skip", NULL
))
852 return BISECT_FAILED
;
856 if (argc
> 1 && !strcmp(state
, terms
->term_bad
))
857 return error(_("'git bisect %s' can take only one argument."), terms
->term_bad
);
860 const char *head
= "BISECT_HEAD";
861 enum get_oid_result res_head
= get_oid(head
, &oid
);
863 if (res_head
== MISSING_OBJECT
) {
865 res_head
= get_oid(head
, &oid
);
869 error(_("Bad rev input: %s"), head
);
870 oid_array_append(&revs
, &oid
);
874 * All input revs must be checked before executing bisect_write()
875 * to discard junk revs.
878 for (; argc
; argc
--, argv
++) {
879 struct commit
*commit
;
881 if (get_oid(*argv
, &oid
)){
882 error(_("Bad rev input: %s"), *argv
);
883 oid_array_clear(&revs
);
884 return BISECT_FAILED
;
887 commit
= lookup_commit_reference(the_repository
, &oid
);
889 die(_("Bad rev input (not a commit): %s"), *argv
);
891 oid_array_append(&revs
, &commit
->object
.oid
);
894 if (strbuf_read_file(&buf
, git_path_bisect_expected_rev(), 0) < the_hash_algo
->hexsz
||
895 get_oid_hex(buf
.buf
, &expected
) < 0)
896 verify_expected
= 0; /* Ignore invalid file contents */
897 strbuf_release(&buf
);
899 for (i
= 0; i
< revs
.nr
; i
++) {
900 if (bisect_write(state
, oid_to_hex(&revs
.oid
[i
]), terms
, 0)) {
901 oid_array_clear(&revs
);
902 return BISECT_FAILED
;
904 if (verify_expected
&& !oideq(&revs
.oid
[i
], &expected
)) {
905 unlink_or_warn(git_path_bisect_ancestors_ok());
906 unlink_or_warn(git_path_bisect_expected_rev());
911 oid_array_clear(&revs
);
912 return bisect_auto_next(terms
, NULL
);
915 static enum bisect_error
bisect_log(void)
918 const char* filename
= git_path_bisect_log();
920 if (is_empty_or_missing_file(filename
))
921 return error(_("We are not bisecting."));
923 fd
= open(filename
, O_RDONLY
);
925 return BISECT_FAILED
;
927 status
= copy_fd(fd
, STDOUT_FILENO
);
929 return status
? BISECT_FAILED
: BISECT_OK
;
932 static int process_replay_line(struct bisect_terms
*terms
, struct strbuf
*line
)
934 const char *p
= line
->buf
+ strspn(line
->buf
, " \t");
935 char *word_end
, *rev
;
937 if ((!skip_prefix(p
, "git bisect", &p
) &&
938 !skip_prefix(p
, "git-bisect", &p
)) || !isspace(*p
))
940 p
+= strspn(p
, " \t");
942 word_end
= (char *)p
+ strcspn(p
, " \t");
943 rev
= word_end
+ strspn(word_end
, " \t");
944 *word_end
= '\0'; /* NUL-terminate the word */
947 if (check_and_set_terms(terms
, p
))
950 if (!strcmp(p
, "start")) {
951 struct strvec argv
= STRVEC_INIT
;
953 sq_dequote_to_strvec(rev
, &argv
);
954 res
= bisect_start(terms
, argv
.v
, argv
.nr
);
959 if (one_of(p
, terms
->term_good
,
960 terms
->term_bad
, "skip", NULL
))
961 return bisect_write(p
, rev
, terms
, 0);
963 if (!strcmp(p
, "terms")) {
964 struct strvec argv
= STRVEC_INIT
;
966 sq_dequote_to_strvec(rev
, &argv
);
967 res
= bisect_terms(terms
, argv
.nr
== 1 ? argv
.v
[0] : NULL
);
971 error(_("'%s'?? what are you talking about?"), p
);
976 static enum bisect_error
bisect_replay(struct bisect_terms
*terms
, const char *filename
)
979 enum bisect_error res
= BISECT_OK
;
980 struct strbuf line
= STRBUF_INIT
;
982 if (is_empty_or_missing_file(filename
))
983 return error(_("cannot read file '%s' for replaying"), filename
);
985 if (bisect_reset(NULL
))
986 return BISECT_FAILED
;
988 fp
= fopen(filename
, "r");
990 return BISECT_FAILED
;
992 while ((strbuf_getline(&line
, fp
) != EOF
) && !res
)
993 res
= process_replay_line(terms
, &line
);
995 strbuf_release(&line
);
999 return BISECT_FAILED
;
1001 return bisect_auto_next(terms
, NULL
);
1004 static enum bisect_error
bisect_skip(struct bisect_terms
*terms
, const char **argv
, int argc
)
1007 enum bisect_error res
;
1008 struct strvec argv_state
= STRVEC_INIT
;
1010 strvec_push(&argv_state
, "skip");
1012 for (i
= 0; i
< argc
; i
++) {
1013 const char *dotdot
= strstr(argv
[i
], "..");
1016 struct rev_info revs
;
1017 struct commit
*commit
;
1019 init_revisions(&revs
, NULL
);
1020 setup_revisions(2, argv
+ i
- 1, &revs
, NULL
);
1022 if (prepare_revision_walk(&revs
))
1023 die(_("revision walk setup failed\n"));
1024 while ((commit
= get_revision(&revs
)) != NULL
)
1025 strvec_push(&argv_state
,
1026 oid_to_hex(&commit
->object
.oid
));
1028 reset_revision_walk();
1030 strvec_push(&argv_state
, argv
[i
]);
1033 res
= bisect_state(terms
, argv_state
.v
, argv_state
.nr
);
1035 strvec_clear(&argv_state
);
1039 int cmd_bisect__helper(int argc
, const char **argv
, const char *prefix
)
1053 int res
= 0, nolog
= 0;
1054 struct option options
[] = {
1055 OPT_CMDMODE(0, "bisect-reset", &cmdmode
,
1056 N_("reset the bisection state"), BISECT_RESET
),
1057 OPT_CMDMODE(0, "bisect-next-check", &cmdmode
,
1058 N_("check whether bad or good terms exist"), BISECT_NEXT_CHECK
),
1059 OPT_CMDMODE(0, "bisect-terms", &cmdmode
,
1060 N_("print out the bisect terms"), BISECT_TERMS
),
1061 OPT_CMDMODE(0, "bisect-start", &cmdmode
,
1062 N_("start the bisect session"), BISECT_START
),
1063 OPT_CMDMODE(0, "bisect-next", &cmdmode
,
1064 N_("find the next bisection commit"), BISECT_NEXT
),
1065 OPT_CMDMODE(0, "bisect-state", &cmdmode
,
1066 N_("mark the state of ref (or refs)"), BISECT_STATE
),
1067 OPT_CMDMODE(0, "bisect-log", &cmdmode
,
1068 N_("list the bisection steps so far"), BISECT_LOG
),
1069 OPT_CMDMODE(0, "bisect-replay", &cmdmode
,
1070 N_("replay the bisection process from the given file"), BISECT_REPLAY
),
1071 OPT_CMDMODE(0, "bisect-skip", &cmdmode
,
1072 N_("skip some commits for checkout"), BISECT_SKIP
),
1073 OPT_BOOL(0, "no-log", &nolog
,
1074 N_("no log for BISECT_WRITE")),
1077 struct bisect_terms terms
= { .term_good
= NULL
, .term_bad
= NULL
};
1079 argc
= parse_options(argc
, argv
, prefix
, options
,
1080 git_bisect_helper_usage
,
1081 PARSE_OPT_KEEP_DASHDASH
| PARSE_OPT_KEEP_UNKNOWN
);
1084 usage_with_options(git_bisect_helper_usage
, options
);
1089 return error(_("--bisect-reset requires either no argument or a commit"));
1090 res
= bisect_reset(argc
? argv
[0] : NULL
);
1092 case BISECT_NEXT_CHECK
:
1093 if (argc
!= 2 && argc
!= 3)
1094 return error(_("--bisect-next-check requires 2 or 3 arguments"));
1095 set_terms(&terms
, argv
[1], argv
[0]);
1096 res
= bisect_next_check(&terms
, argc
== 3 ? argv
[2] : NULL
);
1100 return error(_("--bisect-terms requires 0 or 1 argument"));
1101 res
= bisect_terms(&terms
, argc
== 1 ? argv
[0] : NULL
);
1104 set_terms(&terms
, "bad", "good");
1105 res
= bisect_start(&terms
, argv
, argc
);
1109 return error(_("--bisect-next requires 0 arguments"));
1111 res
= bisect_next(&terms
, prefix
);
1114 set_terms(&terms
, "bad", "good");
1116 res
= bisect_state(&terms
, argv
, argc
);
1120 return error(_("--bisect-log requires 0 arguments"));
1125 return error(_("no logfile given"));
1126 set_terms(&terms
, "bad", "good");
1127 res
= bisect_replay(&terms
, argv
[0]);
1130 set_terms(&terms
, "bad", "good");
1132 res
= bisect_skip(&terms
, argv
, argc
);
1135 BUG("unknown subcommand %d", cmdmode
);
1140 * Handle early success
1141 * From check_merge_bases > check_good_are_ancestors_of_bad > bisect_next_all
1143 if ((res
== BISECT_INTERNAL_SUCCESS_MERGE_BASE
) || (res
== BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND
))