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 static int write_to_file(const char *path
, const char *format
, ...)
125 va_start(args
, format
);
126 res
= write_in_file(path
, "w", format
, args
);
132 static int append_to_file(const char *path
, const char *format
, ...)
137 va_start(args
, format
);
138 res
= write_in_file(path
, "a", format
, args
);
144 static int check_term_format(const char *term
, const char *orig_term
)
147 char *new_term
= xstrfmt("refs/bisect/%s", term
);
149 res
= check_refname_format(new_term
, 0);
153 return error(_("'%s' is not a valid term"), term
);
155 if (one_of(term
, "help", "start", "skip", "next", "reset",
156 "visualize", "view", "replay", "log", "run", "terms", NULL
))
157 return error(_("can't use the builtin command '%s' as a term"), term
);
160 * In theory, nothing prevents swapping completely good and bad,
161 * but this situation could be confusing and hasn't been tested
162 * enough. Forbid it for now.
165 if ((strcmp(orig_term
, "bad") && one_of(term
, "bad", "new", NULL
)) ||
166 (strcmp(orig_term
, "good") && one_of(term
, "good", "old", NULL
)))
167 return error(_("can't change the meaning of the term '%s'"), term
);
172 static int write_terms(const char *bad
, const char *good
)
176 if (!strcmp(bad
, good
))
177 return error(_("please use two different terms"));
179 if (check_term_format(bad
, "bad") || check_term_format(good
, "good"))
182 res
= write_to_file(git_path_bisect_terms(), "%s\n%s\n", bad
, good
);
187 static int bisect_reset(const char *commit
)
189 struct strbuf branch
= STRBUF_INIT
;
192 if (strbuf_read_file(&branch
, git_path_bisect_start(), 0) < 1) {
193 printf(_("We are not bisecting.\n"));
196 strbuf_rtrim(&branch
);
198 struct object_id oid
;
200 if (get_oid_commit(commit
, &oid
))
201 return error(_("'%s' is not a valid commit"), commit
);
202 strbuf_addstr(&branch
, commit
);
205 if (!ref_exists("BISECT_HEAD")) {
206 struct strvec argv
= STRVEC_INIT
;
208 strvec_pushl(&argv
, "checkout", branch
.buf
, "--", NULL
);
209 if (run_command_v_opt(argv
.v
, RUN_GIT_CMD
)) {
210 error(_("could not check out original"
211 " HEAD '%s'. Try 'git bisect"
212 " reset <commit>'."), branch
.buf
);
213 strbuf_release(&branch
);
220 strbuf_release(&branch
);
221 return bisect_clean_state();
224 static void log_commit(FILE *fp
, char *fmt
, const char *state
,
225 struct commit
*commit
)
227 struct pretty_print_context pp
= {0};
228 struct strbuf commit_msg
= STRBUF_INIT
;
229 char *label
= xstrfmt(fmt
, state
);
231 format_commit_message(commit
, "%s", &commit_msg
, &pp
);
233 fprintf(fp
, "# %s: [%s] %s\n", label
, oid_to_hex(&commit
->object
.oid
),
236 strbuf_release(&commit_msg
);
240 static int bisect_write(const char *state
, const char *rev
,
241 const struct bisect_terms
*terms
, int nolog
)
243 struct strbuf tag
= STRBUF_INIT
;
244 struct object_id oid
;
245 struct commit
*commit
;
249 if (!strcmp(state
, terms
->term_bad
)) {
250 strbuf_addf(&tag
, "refs/bisect/%s", state
);
251 } else if (one_of(state
, terms
->term_good
, "skip", NULL
)) {
252 strbuf_addf(&tag
, "refs/bisect/%s-%s", state
, rev
);
254 res
= error(_("Bad bisect_write argument: %s"), state
);
258 if (get_oid(rev
, &oid
)) {
259 res
= error(_("couldn't get the oid of the rev '%s'"), rev
);
263 if (update_ref(NULL
, tag
.buf
, &oid
, NULL
, 0,
264 UPDATE_REFS_MSG_ON_ERR
)) {
269 fp
= fopen(git_path_bisect_log(), "a");
271 res
= error_errno(_("couldn't open the file '%s'"), git_path_bisect_log());
275 commit
= lookup_commit_reference(the_repository
, &oid
);
276 log_commit(fp
, "%s", state
, commit
);
279 fprintf(fp
, "git bisect %s %s\n", state
, rev
);
284 strbuf_release(&tag
);
288 static int check_and_set_terms(struct bisect_terms
*terms
, const char *cmd
)
290 int has_term_file
= !is_empty_or_missing_file(git_path_bisect_terms());
292 if (one_of(cmd
, "skip", "start", "terms", NULL
))
295 if (has_term_file
&& strcmp(cmd
, terms
->term_bad
) &&
296 strcmp(cmd
, terms
->term_good
))
297 return error(_("Invalid command: you're currently in a "
298 "%s/%s bisect"), terms
->term_bad
,
301 if (!has_term_file
) {
302 if (one_of(cmd
, "bad", "good", NULL
)) {
303 set_terms(terms
, "bad", "good");
304 return write_terms(terms
->term_bad
, terms
->term_good
);
306 if (one_of(cmd
, "new", "old", NULL
)) {
307 set_terms(terms
, "new", "old");
308 return write_terms(terms
->term_bad
, terms
->term_good
);
315 static int mark_good(const char *refname
, const struct object_id
*oid
,
316 int flag
, void *cb_data
)
318 int *m_good
= (int *)cb_data
;
323 static const char need_bad_and_good_revision_warning
[] =
324 N_("You need to give me at least one %s and %s revision.\n"
325 "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
327 static const char need_bisect_start_warning
[] =
328 N_("You need to start by \"git bisect start\".\n"
329 "You then need to give me at least one %s and %s revision.\n"
330 "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
332 static int decide_next(const struct bisect_terms
*terms
,
333 const char *current_term
, int missing_good
,
336 if (!missing_good
&& !missing_bad
)
341 if (missing_good
&& !missing_bad
&&
342 !strcmp(current_term
, terms
->term_good
)) {
345 * have bad (or new) but not good (or old). We could bisect
346 * although this is less optimum.
348 warning(_("bisecting only with a %s commit"), terms
->term_bad
);
352 * TRANSLATORS: Make sure to include [Y] and [n] in your
353 * translation. The program will only accept English input
356 yesno
= git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO
);
357 if (starts_with(yesno
, "N") || starts_with(yesno
, "n"))
362 if (!is_empty_or_missing_file(git_path_bisect_start()))
363 return error(_(need_bad_and_good_revision_warning
),
364 vocab_bad
, vocab_good
, vocab_bad
, vocab_good
);
366 return error(_(need_bisect_start_warning
),
367 vocab_good
, vocab_bad
, vocab_good
, vocab_bad
);
370 static int bisect_next_check(const struct bisect_terms
*terms
,
371 const char *current_term
)
373 int missing_good
= 1, missing_bad
= 1;
374 char *bad_ref
= xstrfmt("refs/bisect/%s", terms
->term_bad
);
375 char *good_glob
= xstrfmt("%s-*", terms
->term_good
);
377 if (ref_exists(bad_ref
))
380 for_each_glob_ref_in(mark_good
, good_glob
, "refs/bisect/",
381 (void *) &missing_good
);
386 return decide_next(terms
, current_term
, missing_good
, missing_bad
);
389 static int get_terms(struct bisect_terms
*terms
)
391 struct strbuf str
= STRBUF_INIT
;
395 fp
= fopen(git_path_bisect_terms(), "r");
402 strbuf_getline_lf(&str
, fp
);
403 terms
->term_bad
= strbuf_detach(&str
, NULL
);
404 strbuf_getline_lf(&str
, fp
);
405 terms
->term_good
= strbuf_detach(&str
, NULL
);
410 strbuf_release(&str
);
414 static int bisect_terms(struct bisect_terms
*terms
, const char *option
)
416 if (get_terms(terms
))
417 return error(_("no terms defined"));
419 if (option
== NULL
) {
420 printf(_("Your current terms are %s for the old state\n"
421 "and %s for the new state.\n"),
422 terms
->term_good
, terms
->term_bad
);
425 if (one_of(option
, "--term-good", "--term-old", NULL
))
426 printf("%s\n", terms
->term_good
);
427 else if (one_of(option
, "--term-bad", "--term-new", NULL
))
428 printf("%s\n", terms
->term_bad
);
430 return error(_("invalid argument %s for 'git bisect terms'.\n"
431 "Supported options are: "
432 "--term-good|--term-old and "
433 "--term-bad|--term-new."), option
);
438 static int bisect_append_log_quoted(const char **argv
)
441 FILE *fp
= fopen(git_path_bisect_log(), "a");
442 struct strbuf orig_args
= STRBUF_INIT
;
447 if (fprintf(fp
, "git bisect start") < 1) {
452 sq_quote_argv(&orig_args
, argv
);
453 if (fprintf(fp
, "%s\n", orig_args
.buf
) < 1)
458 strbuf_release(&orig_args
);
462 static int add_bisect_ref(const char *refname
, const struct object_id
*oid
,
465 struct add_bisect_ref_data
*data
= cb
;
467 add_pending_oid(data
->revs
, refname
, oid
, data
->object_flags
);
472 static int prepare_revs(struct bisect_terms
*terms
, struct rev_info
*revs
)
475 struct add_bisect_ref_data cb
= { revs
};
476 char *good
= xstrfmt("%s-*", terms
->term_good
);
479 * We cannot use terms->term_bad directly in
480 * for_each_glob_ref_in() and we have to append a '*' to it,
481 * otherwise for_each_glob_ref_in() will append '/' and '*'.
483 char *bad
= xstrfmt("%s*", terms
->term_bad
);
486 * It is important to reset the flags used by revision walks
487 * as the previous call to bisect_next_all() in turn
488 * sets up a revision walk.
490 reset_revision_walk();
491 init_revisions(revs
, NULL
);
492 setup_revisions(0, NULL
, revs
, NULL
);
493 for_each_glob_ref_in(add_bisect_ref
, bad
, "refs/bisect/", &cb
);
494 cb
.object_flags
= UNINTERESTING
;
495 for_each_glob_ref_in(add_bisect_ref
, good
, "refs/bisect/", &cb
);
496 if (prepare_revision_walk(revs
))
497 res
= error(_("revision walk setup failed\n"));
504 static int bisect_skipped_commits(struct bisect_terms
*terms
)
508 struct rev_info revs
;
509 struct commit
*commit
;
510 struct pretty_print_context pp
= {0};
511 struct strbuf commit_name
= STRBUF_INIT
;
513 res
= prepare_revs(terms
, &revs
);
517 fp
= fopen(git_path_bisect_log(), "a");
519 return error_errno(_("could not open '%s' for appending"),
520 git_path_bisect_log());
522 if (fprintf(fp
, "# only skipped commits left to test\n") < 0)
523 return error_errno(_("failed to write to '%s'"), git_path_bisect_log());
525 while ((commit
= get_revision(&revs
)) != NULL
) {
526 strbuf_reset(&commit_name
);
527 format_commit_message(commit
, "%s",
529 fprintf(fp
, "# possible first %s commit: [%s] %s\n",
530 terms
->term_bad
, oid_to_hex(&commit
->object
.oid
),
535 * Reset the flags used by revision walks in case
536 * there is another revision walk after this one.
538 reset_revision_walk();
540 strbuf_release(&commit_name
);
545 static int bisect_successful(struct bisect_terms
*terms
)
547 struct object_id oid
;
548 struct commit
*commit
;
549 struct pretty_print_context pp
= {0};
550 struct strbuf commit_name
= STRBUF_INIT
;
551 char *bad_ref
= xstrfmt("refs/bisect/%s",terms
->term_bad
);
554 read_ref(bad_ref
, &oid
);
555 commit
= lookup_commit_reference_by_name(bad_ref
);
556 format_commit_message(commit
, "%s", &commit_name
, &pp
);
558 res
= append_to_file(git_path_bisect_log(), "# first %s commit: [%s] %s\n",
559 terms
->term_bad
, oid_to_hex(&commit
->object
.oid
),
562 strbuf_release(&commit_name
);
567 static enum bisect_error
bisect_next(struct bisect_terms
*terms
, const char *prefix
)
569 enum bisect_error res
;
571 if (bisect_autostart(terms
))
572 return BISECT_FAILED
;
574 if (bisect_next_check(terms
, terms
->term_good
))
575 return BISECT_FAILED
;
577 /* Perform all bisection computation */
578 res
= bisect_next_all(the_repository
, prefix
);
580 if (res
== BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND
) {
581 res
= bisect_successful(terms
);
582 return res
? res
: BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND
;
583 } else if (res
== BISECT_ONLY_SKIPPED_LEFT
) {
584 res
= bisect_skipped_commits(terms
);
585 return res
? res
: BISECT_ONLY_SKIPPED_LEFT
;
590 static enum bisect_error
bisect_auto_next(struct bisect_terms
*terms
, const char *prefix
)
592 if (bisect_next_check(terms
, NULL
))
595 return bisect_next(terms
, prefix
);
598 static enum bisect_error
bisect_start(struct bisect_terms
*terms
, const char **argv
, int argc
)
601 int first_parent_only
= 0;
602 int i
, has_double_dash
= 0, must_write_terms
= 0, bad_seen
= 0;
603 int flags
, pathspec_pos
;
604 enum bisect_error res
= BISECT_OK
;
605 struct string_list revs
= STRING_LIST_INIT_DUP
;
606 struct string_list states
= STRING_LIST_INIT_DUP
;
607 struct strbuf start_head
= STRBUF_INIT
;
608 struct strbuf bisect_names
= STRBUF_INIT
;
609 struct object_id head_oid
;
610 struct object_id oid
;
613 if (is_bare_repository())
617 * Check for one bad and then some good revisions
619 for (i
= 0; i
< argc
; i
++) {
620 if (!strcmp(argv
[i
], "--")) {
626 for (i
= 0; i
< argc
; i
++) {
627 const char *arg
= argv
[i
];
628 if (!strcmp(argv
[i
], "--")) {
630 } else if (!strcmp(arg
, "--no-checkout")) {
632 } else if (!strcmp(arg
, "--first-parent")) {
633 first_parent_only
= 1;
634 } else if (!strcmp(arg
, "--term-good") ||
635 !strcmp(arg
, "--term-old")) {
638 return error(_("'' is not a valid term"));
639 must_write_terms
= 1;
640 free((void *) terms
->term_good
);
641 terms
->term_good
= xstrdup(argv
[i
]);
642 } else if (skip_prefix(arg
, "--term-good=", &arg
) ||
643 skip_prefix(arg
, "--term-old=", &arg
)) {
644 must_write_terms
= 1;
645 free((void *) terms
->term_good
);
646 terms
->term_good
= xstrdup(arg
);
647 } else if (!strcmp(arg
, "--term-bad") ||
648 !strcmp(arg
, "--term-new")) {
651 return error(_("'' is not a valid term"));
652 must_write_terms
= 1;
653 free((void *) terms
->term_bad
);
654 terms
->term_bad
= xstrdup(argv
[i
]);
655 } else if (skip_prefix(arg
, "--term-bad=", &arg
) ||
656 skip_prefix(arg
, "--term-new=", &arg
)) {
657 must_write_terms
= 1;
658 free((void *) terms
->term_bad
);
659 terms
->term_bad
= xstrdup(arg
);
660 } else if (starts_with(arg
, "--")) {
661 return error(_("unrecognized option: '%s'"), arg
);
662 } else if (!get_oidf(&oid
, "%s^{commit}", arg
)) {
663 string_list_append(&revs
, oid_to_hex(&oid
));
664 } else if (has_double_dash
) {
665 die(_("'%s' does not appear to be a valid "
674 * The user ran "git bisect start <sha1> <sha1>", hence did not
675 * explicitly specify the terms, but we are already starting to
676 * set references named with the default terms, and won't be able
677 * to change afterwards.
680 must_write_terms
= 1;
681 for (i
= 0; i
< revs
.nr
; i
++) {
683 string_list_append(&states
, terms
->term_good
);
686 string_list_append(&states
, terms
->term_bad
);
693 head
= resolve_ref_unsafe("HEAD", 0, &head_oid
, &flags
);
695 if (get_oid("HEAD", &head_oid
))
696 return error(_("bad HEAD - I need a HEAD"));
699 * Check if we are bisecting
701 if (!is_empty_or_missing_file(git_path_bisect_start())) {
702 /* Reset to the rev from where we started */
703 strbuf_read_file(&start_head
, git_path_bisect_start(), 0);
704 strbuf_trim(&start_head
);
706 struct strvec argv
= STRVEC_INIT
;
708 strvec_pushl(&argv
, "checkout", start_head
.buf
,
710 if (run_command_v_opt(argv
.v
, RUN_GIT_CMD
)) {
711 res
= error(_("checking out '%s' failed."
712 " Try 'git bisect start "
719 /* Get the rev from where we start. */
720 if (!get_oid(head
, &head_oid
) &&
721 !starts_with(head
, "refs/heads/")) {
722 strbuf_reset(&start_head
);
723 strbuf_addstr(&start_head
, oid_to_hex(&head_oid
));
724 } else if (!get_oid(head
, &head_oid
) &&
725 skip_prefix(head
, "refs/heads/", &head
)) {
727 * This error message should only be triggered by
728 * cogito usage, and cogito users should understand
729 * it relates to cg-seek.
731 if (!is_empty_or_missing_file(git_path_head_name()))
732 return error(_("won't bisect on cg-seek'ed tree"));
733 strbuf_addstr(&start_head
, head
);
735 return error(_("bad HEAD - strange symbolic ref"));
740 * Get rid of any old bisect state.
742 if (bisect_clean_state())
743 return BISECT_FAILED
;
746 * Write new start state
748 write_file(git_path_bisect_start(), "%s\n", start_head
.buf
);
750 if (first_parent_only
)
751 write_file(git_path_bisect_first_parent(), "\n");
754 if (get_oid(start_head
.buf
, &oid
) < 0) {
755 res
= error(_("invalid ref: '%s'"), start_head
.buf
);
758 if (update_ref(NULL
, "BISECT_HEAD", &oid
, NULL
, 0,
759 UPDATE_REFS_MSG_ON_ERR
)) {
765 if (pathspec_pos
< argc
- 1)
766 sq_quote_argv(&bisect_names
, argv
+ pathspec_pos
);
767 write_file(git_path_bisect_names(), "%s\n", bisect_names
.buf
);
769 for (i
= 0; i
< states
.nr
; i
++)
770 if (bisect_write(states
.items
[i
].string
,
771 revs
.items
[i
].string
, terms
, 1)) {
776 if (must_write_terms
&& write_terms(terms
->term_bad
,
782 res
= bisect_append_log_quoted(argv
);
787 string_list_clear(&revs
, 0);
788 string_list_clear(&states
, 0);
789 strbuf_release(&start_head
);
790 strbuf_release(&bisect_names
);
794 res
= bisect_auto_next(terms
, NULL
);
795 if (!is_bisect_success(res
))
796 bisect_clean_state();
800 static inline int file_is_not_empty(const char *path
)
802 return !is_empty_or_missing_file(path
);
805 static int bisect_autostart(struct bisect_terms
*terms
)
810 if (file_is_not_empty(git_path_bisect_start()))
813 fprintf_ln(stderr
, _("You need to start by \"git bisect "
816 if (!isatty(STDIN_FILENO
))
820 * TRANSLATORS: Make sure to include [Y] and [n] in your
821 * translation. The program will only accept English input
824 yesno
= git_prompt(_("Do you want me to do it for you "
825 "[Y/n]? "), PROMPT_ECHO
);
826 res
= tolower(*yesno
) == 'n' ?
827 -1 : bisect_start(terms
, empty_strvec
, 0);
832 static enum bisect_error
bisect_state(struct bisect_terms
*terms
, const char **argv
,
836 int i
, verify_expected
= 1;
837 struct object_id oid
, expected
;
838 struct strbuf buf
= STRBUF_INIT
;
839 struct oid_array revs
= OID_ARRAY_INIT
;
842 return error(_("Please call `--bisect-state` with at least one argument"));
844 if (bisect_autostart(terms
))
845 return BISECT_FAILED
;
848 if (check_and_set_terms(terms
, state
) ||
849 !one_of(state
, terms
->term_good
, terms
->term_bad
, "skip", NULL
))
850 return BISECT_FAILED
;
854 if (argc
> 1 && !strcmp(state
, terms
->term_bad
))
855 return error(_("'git bisect %s' can take only one argument."), terms
->term_bad
);
858 const char *head
= "BISECT_HEAD";
859 enum get_oid_result res_head
= get_oid(head
, &oid
);
861 if (res_head
== MISSING_OBJECT
) {
863 res_head
= get_oid(head
, &oid
);
867 error(_("Bad rev input: %s"), head
);
868 oid_array_append(&revs
, &oid
);
872 * All input revs must be checked before executing bisect_write()
873 * to discard junk revs.
876 for (; argc
; argc
--, argv
++) {
877 if (get_oid(*argv
, &oid
)){
878 error(_("Bad rev input: %s"), *argv
);
879 oid_array_clear(&revs
);
880 return BISECT_FAILED
;
882 oid_array_append(&revs
, &oid
);
885 if (strbuf_read_file(&buf
, git_path_bisect_expected_rev(), 0) < the_hash_algo
->hexsz
||
886 get_oid_hex(buf
.buf
, &expected
) < 0)
887 verify_expected
= 0; /* Ignore invalid file contents */
888 strbuf_release(&buf
);
890 for (i
= 0; i
< revs
.nr
; i
++) {
891 if (bisect_write(state
, oid_to_hex(&revs
.oid
[i
]), terms
, 0)) {
892 oid_array_clear(&revs
);
893 return BISECT_FAILED
;
895 if (verify_expected
&& !oideq(&revs
.oid
[i
], &expected
)) {
896 unlink_or_warn(git_path_bisect_ancestors_ok());
897 unlink_or_warn(git_path_bisect_expected_rev());
902 oid_array_clear(&revs
);
903 return bisect_auto_next(terms
, NULL
);
906 static enum bisect_error
bisect_log(void)
909 const char* filename
= git_path_bisect_log();
911 if (is_empty_or_missing_file(filename
))
912 return error(_("We are not bisecting."));
914 fd
= open(filename
, O_RDONLY
);
916 return BISECT_FAILED
;
918 status
= copy_fd(fd
, STDOUT_FILENO
);
920 return status
? BISECT_FAILED
: BISECT_OK
;
923 static int process_replay_line(struct bisect_terms
*terms
, struct strbuf
*line
)
925 const char *p
= line
->buf
+ strspn(line
->buf
, " \t");
926 char *word_end
, *rev
;
928 if ((!skip_prefix(p
, "git bisect", &p
) &&
929 !skip_prefix(p
, "git-bisect", &p
)) || !isspace(*p
))
931 p
+= strspn(p
, " \t");
933 word_end
= (char *)p
+ strcspn(p
, " \t");
934 rev
= word_end
+ strspn(word_end
, " \t");
935 *word_end
= '\0'; /* NUL-terminate the word */
938 if (check_and_set_terms(terms
, p
))
941 if (!strcmp(p
, "start")) {
942 struct strvec argv
= STRVEC_INIT
;
944 sq_dequote_to_strvec(rev
, &argv
);
945 res
= bisect_start(terms
, argv
.v
, argv
.nr
);
950 if (one_of(p
, terms
->term_good
,
951 terms
->term_bad
, "skip", NULL
))
952 return bisect_write(p
, rev
, terms
, 0);
954 if (!strcmp(p
, "terms")) {
955 struct strvec argv
= STRVEC_INIT
;
957 sq_dequote_to_strvec(rev
, &argv
);
958 res
= bisect_terms(terms
, argv
.nr
== 1 ? argv
.v
[0] : NULL
);
962 error(_("'%s'?? what are you talking about?"), p
);
967 static enum bisect_error
bisect_replay(struct bisect_terms
*terms
, const char *filename
)
970 enum bisect_error res
= BISECT_OK
;
971 struct strbuf line
= STRBUF_INIT
;
973 if (is_empty_or_missing_file(filename
))
974 return error(_("cannot read file '%s' for replaying"), filename
);
976 if (bisect_reset(NULL
))
977 return BISECT_FAILED
;
979 fp
= fopen(filename
, "r");
981 return BISECT_FAILED
;
983 while ((strbuf_getline(&line
, fp
) != EOF
) && !res
)
984 res
= process_replay_line(terms
, &line
);
986 strbuf_release(&line
);
990 return BISECT_FAILED
;
992 return bisect_auto_next(terms
, NULL
);
995 static enum bisect_error
bisect_skip(struct bisect_terms
*terms
, const char **argv
, int argc
)
998 enum bisect_error res
;
999 struct strvec argv_state
= STRVEC_INIT
;
1001 strvec_push(&argv_state
, "skip");
1003 for (i
= 0; i
< argc
; i
++) {
1004 const char *dotdot
= strstr(argv
[i
], "..");
1007 struct rev_info revs
;
1008 struct commit
*commit
;
1010 init_revisions(&revs
, NULL
);
1011 setup_revisions(2, argv
+ i
- 1, &revs
, NULL
);
1013 if (prepare_revision_walk(&revs
))
1014 die(_("revision walk setup failed\n"));
1015 while ((commit
= get_revision(&revs
)) != NULL
)
1016 strvec_push(&argv_state
,
1017 oid_to_hex(&commit
->object
.oid
));
1019 reset_revision_walk();
1021 strvec_push(&argv_state
, argv
[i
]);
1024 res
= bisect_state(terms
, argv_state
.v
, argv_state
.nr
);
1026 strvec_clear(&argv_state
);
1030 int cmd_bisect__helper(int argc
, const char **argv
, const char *prefix
)
1044 int res
= 0, nolog
= 0;
1045 struct option options
[] = {
1046 OPT_CMDMODE(0, "bisect-reset", &cmdmode
,
1047 N_("reset the bisection state"), BISECT_RESET
),
1048 OPT_CMDMODE(0, "bisect-next-check", &cmdmode
,
1049 N_("check whether bad or good terms exist"), BISECT_NEXT_CHECK
),
1050 OPT_CMDMODE(0, "bisect-terms", &cmdmode
,
1051 N_("print out the bisect terms"), BISECT_TERMS
),
1052 OPT_CMDMODE(0, "bisect-start", &cmdmode
,
1053 N_("start the bisect session"), BISECT_START
),
1054 OPT_CMDMODE(0, "bisect-next", &cmdmode
,
1055 N_("find the next bisection commit"), BISECT_NEXT
),
1056 OPT_CMDMODE(0, "bisect-state", &cmdmode
,
1057 N_("mark the state of ref (or refs)"), BISECT_STATE
),
1058 OPT_CMDMODE(0, "bisect-log", &cmdmode
,
1059 N_("list the bisection steps so far"), BISECT_LOG
),
1060 OPT_CMDMODE(0, "bisect-replay", &cmdmode
,
1061 N_("replay the bisection process from the given file"), BISECT_REPLAY
),
1062 OPT_CMDMODE(0, "bisect-skip", &cmdmode
,
1063 N_("skip some commits for checkout"), BISECT_SKIP
),
1064 OPT_BOOL(0, "no-log", &nolog
,
1065 N_("no log for BISECT_WRITE")),
1068 struct bisect_terms terms
= { .term_good
= NULL
, .term_bad
= NULL
};
1070 argc
= parse_options(argc
, argv
, prefix
, options
,
1071 git_bisect_helper_usage
,
1072 PARSE_OPT_KEEP_DASHDASH
| PARSE_OPT_KEEP_UNKNOWN
);
1075 usage_with_options(git_bisect_helper_usage
, options
);
1080 return error(_("--bisect-reset requires either no argument or a commit"));
1081 res
= bisect_reset(argc
? argv
[0] : NULL
);
1083 case BISECT_NEXT_CHECK
:
1084 if (argc
!= 2 && argc
!= 3)
1085 return error(_("--bisect-next-check requires 2 or 3 arguments"));
1086 set_terms(&terms
, argv
[1], argv
[0]);
1087 res
= bisect_next_check(&terms
, argc
== 3 ? argv
[2] : NULL
);
1091 return error(_("--bisect-terms requires 0 or 1 argument"));
1092 res
= bisect_terms(&terms
, argc
== 1 ? argv
[0] : NULL
);
1095 set_terms(&terms
, "bad", "good");
1096 res
= bisect_start(&terms
, argv
, argc
);
1100 return error(_("--bisect-next requires 0 arguments"));
1102 res
= bisect_next(&terms
, prefix
);
1105 set_terms(&terms
, "bad", "good");
1107 res
= bisect_state(&terms
, argv
, argc
);
1111 return error(_("--bisect-log requires 0 arguments"));
1116 return error(_("no logfile given"));
1117 set_terms(&terms
, "bad", "good");
1118 res
= bisect_replay(&terms
, argv
[0]);
1121 set_terms(&terms
, "bad", "good");
1122 res
= bisect_skip(&terms
, argv
, argc
);
1125 BUG("unknown subcommand %d", cmdmode
);
1130 * Handle early success
1131 * From check_merge_bases > check_good_are_ancestors_of_bad > bisect_next_all
1133 if ((res
== BISECT_INTERNAL_SUCCESS_MERGE_BASE
) || (res
== BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND
))