3
#include "environment.h"
6
#include "object-name.h"
7
#include "parse-options.h"
11
#include "run-command.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);
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);
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);
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 && !refs_ref_exists(get_main_ref_store(the_repository), "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,
266
const char *fmt, const char *state,
267
struct commit *commit)
269
struct pretty_print_context pp = {0};
270
struct strbuf commit_msg = STRBUF_INIT;
271
char *label = xstrfmt(fmt, state);
273
repo_format_commit_message(the_repository, commit, "%s", &commit_msg,
276
fprintf(fp, "# %s: [%s] %s\n", label, oid_to_hex(&commit->object.oid),
279
strbuf_release(&commit_msg);
283
static int bisect_write(const char *state, const char *rev,
284
const struct bisect_terms *terms, int nolog)
286
struct strbuf tag = STRBUF_INIT;
287
struct object_id oid;
288
struct commit *commit;
292
if (!strcmp(state, terms->term_bad)) {
293
strbuf_addf(&tag, "refs/bisect/%s", state);
294
} else if (one_of(state, terms->term_good, "skip", NULL)) {
295
strbuf_addf(&tag, "refs/bisect/%s-%s", state, rev);
297
res = error(_("Bad bisect_write argument: %s"), state);
301
if (repo_get_oid(the_repository, rev, &oid)) {
302
res = error(_("couldn't get the oid of the rev '%s'"), rev);
306
if (refs_update_ref(get_main_ref_store(the_repository), NULL, tag.buf, &oid, NULL, 0,
307
UPDATE_REFS_MSG_ON_ERR)) {
312
fp = fopen(git_path_bisect_log(), "a");
314
res = error_errno(_("couldn't open the file '%s'"), git_path_bisect_log());
318
commit = lookup_commit_reference(the_repository, &oid);
319
log_commit(fp, "%s", state, commit);
322
fprintf(fp, "git bisect %s %s\n", state, rev);
327
strbuf_release(&tag);
331
static int check_and_set_terms(struct bisect_terms *terms, const char *cmd)
333
int has_term_file = !is_empty_or_missing_file(git_path_bisect_terms());
335
if (one_of(cmd, "skip", "start", "terms", NULL))
338
if (has_term_file && strcmp(cmd, terms->term_bad) &&
339
strcmp(cmd, terms->term_good))
340
return error(_("Invalid command: you're currently in a "
341
"%s/%s bisect"), terms->term_bad,
344
if (!has_term_file) {
345
if (one_of(cmd, "bad", "good", NULL)) {
346
set_terms(terms, "bad", "good");
347
return write_terms(terms->term_bad, terms->term_good);
349
if (one_of(cmd, "new", "old", NULL)) {
350
set_terms(terms, "new", "old");
351
return write_terms(terms->term_bad, terms->term_good);
358
static int inc_nr(const char *refname UNUSED,
359
const char *referent UNUSED,
360
const struct object_id *oid UNUSED,
361
int flag UNUSED, void *cb_data)
363
unsigned int *nr = (unsigned int *)cb_data;
368
static const char need_bad_and_good_revision_warning[] =
369
N_("You 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 const char need_bisect_start_warning[] =
373
N_("You need to start by \"git bisect start\".\n"
374
"You then need to give me at least one %s and %s revision.\n"
375
"You can use \"git bisect %s\" and \"git bisect %s\" for that.");
377
static int decide_next(const struct bisect_terms *terms,
378
const char *current_term, int missing_good,
381
if (!missing_good && !missing_bad)
386
if (missing_good && !missing_bad &&
387
!strcmp(current_term, terms->term_good)) {
393
warning(_("bisecting only with a %s commit"), terms->term_bad);
401
yesno = git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO);
402
if (starts_with(yesno, "N") || starts_with(yesno, "n"))
407
if (!is_empty_or_missing_file(git_path_bisect_start()))
408
return error(_(need_bad_and_good_revision_warning),
409
vocab_bad, vocab_good, vocab_bad, vocab_good);
411
return error(_(need_bisect_start_warning),
412
vocab_good, vocab_bad, vocab_good, vocab_bad);
415
static void bisect_status(struct bisect_state *state,
416
const struct bisect_terms *terms)
418
char *bad_ref = xstrfmt("refs/bisect/%s", terms->term_bad);
419
char *good_glob = xstrfmt("%s-*", terms->term_good);
421
if (refs_ref_exists(get_main_ref_store(the_repository), bad_ref))
424
refs_for_each_glob_ref_in(get_main_ref_store(the_repository), inc_nr,
425
good_glob, "refs/bisect/",
426
(void *) &state->nr_good);
432
__attribute__((format (printf, 1, 2)))
433
static void bisect_log_printf(const char *fmt, ...)
435
struct strbuf buf = STRBUF_INIT;
439
strbuf_vaddf(&buf, fmt, ap);
442
printf("%s", buf.buf);
443
append_to_file(git_path_bisect_log(), "# %s", buf.buf);
445
strbuf_release(&buf);
448
static void bisect_print_status(const struct bisect_terms *terms)
450
struct bisect_state state = { 0 };
452
bisect_status(&state, terms);
455
if (state.nr_good && state.nr_bad)
458
if (!state.nr_good && !state.nr_bad)
459
bisect_log_printf(_("status: waiting for both good and bad commits\n"));
460
else if (state.nr_good)
461
bisect_log_printf(Q_("status: waiting for bad commit, %d good commit known\n",
462
"status: waiting for bad commit, %d good commits known\n",
463
state.nr_good), state.nr_good);
465
bisect_log_printf(_("status: waiting for good commit(s), bad commit known\n"));
468
static int bisect_next_check(const struct bisect_terms *terms,
469
const char *current_term)
471
struct bisect_state state = { 0 };
472
bisect_status(&state, terms);
473
return decide_next(terms, current_term, !state.nr_good, !state.nr_bad);
476
static int get_terms(struct bisect_terms *terms)
478
struct strbuf str = STRBUF_INIT;
482
fp = fopen(git_path_bisect_terms(), "r");
489
strbuf_getline_lf(&str, fp);
490
terms->term_bad = strbuf_detach(&str, NULL);
491
strbuf_getline_lf(&str, fp);
492
terms->term_good = strbuf_detach(&str, NULL);
497
strbuf_release(&str);
501
static int bisect_terms(struct bisect_terms *terms, const char *option)
503
if (get_terms(terms))
504
return error(_("no terms defined"));
507
printf(_("Your current terms are %s for the old state\n"
508
"and %s for the new state.\n"),
509
terms->term_good, terms->term_bad);
512
if (one_of(option, "--term-good", "--term-old", NULL))
513
printf("%s\n", terms->term_good);
514
else if (one_of(option, "--term-bad", "--term-new", NULL))
515
printf("%s\n", terms->term_bad);
517
return error(_("invalid argument %s for 'git bisect terms'.\n"
518
"Supported options are: "
519
"--term-good|--term-old and "
520
"--term-bad|--term-new."), option);
525
static int bisect_append_log_quoted(const char **argv)
528
FILE *fp = fopen(git_path_bisect_log(), "a");
529
struct strbuf orig_args = STRBUF_INIT;
534
if (fprintf(fp, "git bisect start") < 1) {
539
sq_quote_argv(&orig_args, argv);
540
if (fprintf(fp, "%s\n", orig_args.buf) < 1)
545
strbuf_release(&orig_args);
549
static int add_bisect_ref(const char *refname, const char *referent UNUSED, const struct object_id *oid,
550
int flags UNUSED, void *cb)
552
struct add_bisect_ref_data *data = cb;
554
add_pending_oid(data->revs, refname, oid, data->object_flags);
559
static int prepare_revs(struct bisect_terms *terms, struct rev_info *revs)
562
struct add_bisect_ref_data cb = { revs };
563
char *good = xstrfmt("%s-*", terms->term_good);
570
char *bad = xstrfmt("%s*", terms->term_bad);
577
reset_revision_walk();
578
repo_init_revisions(the_repository, revs, NULL);
579
setup_revisions(0, NULL, revs, NULL);
580
refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
581
add_bisect_ref, bad, "refs/bisect/", &cb);
582
cb.object_flags = UNINTERESTING;
583
refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
584
add_bisect_ref, good, "refs/bisect/", &cb);
585
if (prepare_revision_walk(revs))
586
res = error(_("revision walk setup failed\n"));
593
static int bisect_skipped_commits(struct bisect_terms *terms)
597
struct rev_info revs;
598
struct commit *commit;
599
struct pretty_print_context pp = {0};
600
struct strbuf commit_name = STRBUF_INIT;
602
res = prepare_revs(terms, &revs);
606
fp = fopen(git_path_bisect_log(), "a");
608
return error_errno(_("could not open '%s' for appending"),
609
git_path_bisect_log());
611
if (fprintf(fp, "# only skipped commits left to test\n") < 0)
612
return error_errno(_("failed to write to '%s'"), git_path_bisect_log());
614
while ((commit = get_revision(&revs)) != NULL) {
615
strbuf_reset(&commit_name);
616
repo_format_commit_message(the_repository, commit, "%s",
618
fprintf(fp, "# possible first %s commit: [%s] %s\n",
619
terms->term_bad, oid_to_hex(&commit->object.oid),
627
reset_revision_walk();
629
strbuf_release(&commit_name);
630
release_revisions(&revs);
635
static int bisect_successful(struct bisect_terms *terms)
637
struct object_id oid;
638
struct commit *commit;
639
struct pretty_print_context pp = {0};
640
struct strbuf commit_name = STRBUF_INIT;
641
char *bad_ref = xstrfmt("refs/bisect/%s",terms->term_bad);
644
refs_read_ref(get_main_ref_store(the_repository), bad_ref, &oid);
645
commit = lookup_commit_reference_by_name(bad_ref);
646
repo_format_commit_message(the_repository, commit, "%s", &commit_name,
649
res = append_to_file(git_path_bisect_log(), "# first %s commit: [%s] %s\n",
650
terms->term_bad, oid_to_hex(&commit->object.oid),
653
strbuf_release(&commit_name);
658
static enum bisect_error bisect_next(struct bisect_terms *terms, const char *prefix)
660
enum bisect_error res;
662
if (bisect_autostart(terms))
663
return BISECT_FAILED;
665
if (bisect_next_check(terms, terms->term_good))
666
return BISECT_FAILED;
669
res = bisect_next_all(the_repository, prefix);
671
if (res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND) {
672
res = bisect_successful(terms);
673
return res ? res : BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND;
674
} else if (res == BISECT_ONLY_SKIPPED_LEFT) {
675
res = bisect_skipped_commits(terms);
676
return res ? res : BISECT_ONLY_SKIPPED_LEFT;
681
static enum bisect_error bisect_auto_next(struct bisect_terms *terms, const char *prefix)
683
if (bisect_next_check(terms, NULL)) {
684
bisect_print_status(terms);
688
return bisect_next(terms, prefix);
691
static enum bisect_error bisect_start(struct bisect_terms *terms, int argc,
695
int first_parent_only = 0;
696
int i, has_double_dash = 0, must_write_terms = 0, bad_seen = 0;
697
int flags, pathspec_pos;
698
enum bisect_error res = BISECT_OK;
699
struct string_list revs = STRING_LIST_INIT_DUP;
700
struct string_list states = STRING_LIST_INIT_DUP;
701
struct strbuf start_head = STRBUF_INIT;
702
struct strbuf bisect_names = STRBUF_INIT;
703
struct object_id head_oid;
704
struct object_id oid;
707
if (is_bare_repository())
713
for (i = 0; i < argc; i++) {
714
if (!strcmp(argv[i], "--")) {
720
for (i = 0; i < argc; i++) {
721
const char *arg = argv[i];
722
if (!strcmp(argv[i], "--")) {
724
} else if (!strcmp(arg, "--no-checkout")) {
726
} else if (!strcmp(arg, "--first-parent")) {
727
first_parent_only = 1;
728
} else if (!strcmp(arg, "--term-good") ||
729
!strcmp(arg, "--term-old")) {
732
return error(_("'' is not a valid term"));
733
must_write_terms = 1;
734
free((void *) terms->term_good);
735
terms->term_good = xstrdup(argv[i]);
736
} else if (skip_prefix(arg, "--term-good=", &arg) ||
737
skip_prefix(arg, "--term-old=", &arg)) {
738
must_write_terms = 1;
739
free((void *) terms->term_good);
740
terms->term_good = xstrdup(arg);
741
} else if (!strcmp(arg, "--term-bad") ||
742
!strcmp(arg, "--term-new")) {
745
return error(_("'' is not a valid term"));
746
must_write_terms = 1;
747
free((void *) terms->term_bad);
748
terms->term_bad = xstrdup(argv[i]);
749
} else if (skip_prefix(arg, "--term-bad=", &arg) ||
750
skip_prefix(arg, "--term-new=", &arg)) {
751
must_write_terms = 1;
752
free((void *) terms->term_bad);
753
terms->term_bad = xstrdup(arg);
754
} else if (starts_with(arg, "--")) {
755
return error(_("unrecognized option: '%s'"), arg);
756
} else if (!get_oidf(&oid, "%s^{commit}", arg)) {
757
string_list_append(&revs, oid_to_hex(&oid));
758
} else if (has_double_dash) {
759
die(_("'%s' does not appear to be a valid "
774
must_write_terms = 1;
775
for (i = 0; i < revs.nr; i++) {
777
string_list_append(&states, terms->term_good);
780
string_list_append(&states, terms->term_bad);
787
head = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
788
"HEAD", 0, &head_oid, &flags);
790
if (repo_get_oid(the_repository, "HEAD", &head_oid))
791
return error(_("bad HEAD - I need a HEAD"));
796
if (!is_empty_or_missing_file(git_path_bisect_start())) {
798
strbuf_read_file(&start_head, git_path_bisect_start(), 0);
799
strbuf_trim(&start_head);
801
struct child_process cmd = CHILD_PROCESS_INIT;
804
strvec_pushl(&cmd.args, "checkout", start_head.buf,
806
if (run_command(&cmd)) {
807
res = error(_("checking out '%s' failed."
808
" Try 'git bisect start "
816
if (!repo_get_oid(the_repository, head, &head_oid) &&
817
!starts_with(head, "refs/heads/")) {
818
strbuf_reset(&start_head);
819
strbuf_addstr(&start_head, oid_to_hex(&head_oid));
820
} else if (!repo_get_oid(the_repository, head, &head_oid) &&
821
skip_prefix(head, "refs/heads/", &head)) {
822
strbuf_addstr(&start_head, head);
824
return error(_("bad HEAD - strange symbolic ref"));
831
if (bisect_clean_state())
832
return BISECT_FAILED;
837
write_file(git_path_bisect_start(), "%s\n", start_head.buf);
839
if (first_parent_only)
840
write_file(git_path_bisect_first_parent(), "\n");
843
if (repo_get_oid(the_repository, start_head.buf, &oid) < 0) {
844
res = error(_("invalid ref: '%s'"), start_head.buf);
847
if (refs_update_ref(get_main_ref_store(the_repository), NULL, "BISECT_HEAD", &oid, NULL, 0,
848
UPDATE_REFS_MSG_ON_ERR)) {
854
if (pathspec_pos < argc - 1)
855
sq_quote_argv(&bisect_names, argv + pathspec_pos);
856
write_file(git_path_bisect_names(), "%s\n", bisect_names.buf);
858
for (i = 0; i < states.nr; i++)
859
if (bisect_write(states.items[i].string,
860
revs.items[i].string, terms, 1)) {
865
if (must_write_terms && write_terms(terms->term_bad,
871
res = bisect_append_log_quoted(argv);
876
string_list_clear(&revs, 0);
877
string_list_clear(&states, 0);
878
strbuf_release(&start_head);
879
strbuf_release(&bisect_names);
883
res = bisect_auto_next(terms, NULL);
884
if (!is_bisect_success(res))
885
bisect_clean_state();
889
static inline int file_is_not_empty(const char *path)
891
return !is_empty_or_missing_file(path);
894
static int bisect_autostart(struct bisect_terms *terms)
899
if (file_is_not_empty(git_path_bisect_start()))
902
fprintf_ln(stderr, _("You need to start by \"git bisect "
905
if (!isatty(STDIN_FILENO))
913
yesno = git_prompt(_("Do you want me to do it for you "
914
"[Y/n]? "), PROMPT_ECHO);
915
res = tolower(*yesno) == 'n' ?
916
-1 : bisect_start(terms, 0, empty_strvec);
921
static enum bisect_error bisect_state(struct bisect_terms *terms, int argc,
925
int i, verify_expected = 1;
926
struct object_id oid, expected;
927
struct oid_array revs = OID_ARRAY_INIT;
930
return error(_("Please call `--bisect-state` with at least one argument"));
932
if (bisect_autostart(terms))
933
return BISECT_FAILED;
936
if (check_and_set_terms(terms, state) ||
937
!one_of(state, terms->term_good, terms->term_bad, "skip", NULL))
938
return BISECT_FAILED;
942
if (argc > 1 && !strcmp(state, terms->term_bad))
943
return error(_("'git bisect %s' can take only one argument."), terms->term_bad);
946
const char *head = "BISECT_HEAD";
947
enum get_oid_result res_head = repo_get_oid(the_repository,
950
if (res_head == MISSING_OBJECT) {
952
res_head = repo_get_oid(the_repository, head, &oid);
956
error(_("Bad rev input: %s"), head);
957
oid_array_append(&revs, &oid);
965
for (; argc; argc--, argv++) {
966
struct commit *commit;
968
if (repo_get_oid(the_repository, *argv, &oid)){
969
error(_("Bad rev input: %s"), *argv);
970
oid_array_clear(&revs);
971
return BISECT_FAILED;
974
commit = lookup_commit_reference(the_repository, &oid);
976
die(_("Bad rev input (not a commit): %s"), *argv);
978
oid_array_append(&revs, &commit->object.oid);
981
if (refs_read_ref(get_main_ref_store(the_repository), "BISECT_EXPECTED_REV", &expected))
984
for (i = 0; i < revs.nr; i++) {
985
if (bisect_write(state, oid_to_hex(&revs.oid[i]), terms, 0)) {
986
oid_array_clear(&revs);
987
return BISECT_FAILED;
989
if (verify_expected && !oideq(&revs.oid[i], &expected)) {
990
unlink_or_warn(git_path_bisect_ancestors_ok());
991
refs_delete_ref(get_main_ref_store(the_repository),
992
NULL, "BISECT_EXPECTED_REV", NULL,
998
oid_array_clear(&revs);
999
return bisect_auto_next(terms, NULL);
1002
static enum bisect_error bisect_log(void)
1005
const char* filename = git_path_bisect_log();
1007
if (is_empty_or_missing_file(filename))
1008
return error(_("We are not bisecting."));
1010
fd = open(filename, O_RDONLY);
1012
return BISECT_FAILED;
1014
status = copy_fd(fd, STDOUT_FILENO);
1016
return status ? BISECT_FAILED : BISECT_OK;
1019
static int process_replay_line(struct bisect_terms *terms, struct strbuf *line)
1021
const char *p = line->buf + strspn(line->buf, " \t");
1022
char *word_end, *rev;
1024
if ((!skip_prefix(p, "git bisect", &p) &&
1025
!skip_prefix(p, "git-bisect", &p)) || !isspace(*p))
1027
p += strspn(p, " \t");
1029
word_end = (char *)p + strcspn(p, " \t");
1030
rev = word_end + strspn(word_end, " \t");
1034
if (check_and_set_terms(terms, p))
1037
if (!strcmp(p, "start")) {
1038
struct strvec argv = STRVEC_INIT;
1040
sq_dequote_to_strvec(rev, &argv);
1041
res = bisect_start(terms, argv.nr, argv.v);
1042
strvec_clear(&argv);
1046
if (one_of(p, terms->term_good,
1047
terms->term_bad, "skip", NULL))
1048
return bisect_write(p, rev, terms, 0);
1050
if (!strcmp(p, "terms")) {
1051
struct strvec argv = STRVEC_INIT;
1053
sq_dequote_to_strvec(rev, &argv);
1054
res = bisect_terms(terms, argv.nr == 1 ? argv.v[0] : NULL);
1055
strvec_clear(&argv);
1058
error(_("'%s'?? what are you talking about?"), p);
1063
static enum bisect_error bisect_replay(struct bisect_terms *terms, const char *filename)
1066
enum bisect_error res = BISECT_OK;
1067
struct strbuf line = STRBUF_INIT;
1069
if (is_empty_or_missing_file(filename))
1070
return error(_("cannot read file '%s' for replaying"), filename);
1072
if (bisect_reset(NULL))
1073
return BISECT_FAILED;
1075
fp = fopen(filename, "r");
1077
return BISECT_FAILED;
1079
while ((strbuf_getline(&line, fp) != EOF) && !res)
1080
res = process_replay_line(terms, &line);
1082
strbuf_release(&line);
1086
return BISECT_FAILED;
1088
return bisect_auto_next(terms, NULL);
1091
static enum bisect_error bisect_skip(struct bisect_terms *terms, int argc,
1095
enum bisect_error res;
1096
struct strvec argv_state = STRVEC_INIT;
1098
strvec_push(&argv_state, "skip");
1100
for (i = 0; i < argc; i++) {
1101
const char *dotdot = strstr(argv[i], "..");
1104
struct rev_info revs;
1105
struct commit *commit;
1107
repo_init_revisions(the_repository, &revs, NULL);
1108
setup_revisions(2, argv + i - 1, &revs, NULL);
1110
if (prepare_revision_walk(&revs))
1111
die(_("revision walk setup failed\n"));
1112
while ((commit = get_revision(&revs)) != NULL)
1113
strvec_push(&argv_state,
1114
oid_to_hex(&commit->object.oid));
1116
reset_revision_walk();
1117
release_revisions(&revs);
1119
strvec_push(&argv_state, argv[i]);
1122
res = bisect_state(terms, argv_state.nr, argv_state.v);
1124
strvec_clear(&argv_state);
1128
static int bisect_visualize(struct bisect_terms *terms, int argc,
1131
struct child_process cmd = CHILD_PROCESS_INIT;
1132
struct strbuf sb = STRBUF_INIT;
1134
if (bisect_next_check(terms, NULL) != 0)
1135
return BISECT_FAILED;
1139
if ((getenv("DISPLAY") || getenv("SESSIONNAME") || getenv("MSYSTEM") ||
1140
getenv("SECURITYSESSIONID")) && exists_in_PATH("gitk")) {
1141
strvec_push(&cmd.args, "gitk");
1143
strvec_push(&cmd.args, "log");
1147
if (argv[0][0] == '-') {
1148
strvec_push(&cmd.args, "log");
1150
} else if (strcmp(argv[0], "tig") && !starts_with(argv[0], "git"))
1153
strvec_pushv(&cmd.args, argv);
1156
strvec_pushl(&cmd.args, "--bisect", "--", NULL);
1158
strbuf_read_file(&sb, git_path_bisect_names(), 0);
1159
sq_dequote_to_strvec(sb.buf, &cmd.args);
1160
strbuf_release(&sb);
1162
return run_command(&cmd);
1165
static int get_first_good(const char *refname UNUSED,
1166
const char *referent UNUSED,
1167
const struct object_id *oid,
1168
int flag UNUSED, void *cb_data)
1170
oidcpy(cb_data, oid);
1174
static int do_bisect_run(const char *command)
1176
struct child_process cmd = CHILD_PROCESS_INIT;
1178
printf(_("running %s\n"), command);
1180
strvec_push(&cmd.args, command);
1181
return run_command(&cmd);
1184
static int verify_good(const struct bisect_terms *terms, const char *command)
1187
enum bisect_error res;
1188
struct object_id good_rev;
1189
struct object_id current_rev;
1190
char *good_glob = xstrfmt("%s-*", terms->term_good);
1191
int no_checkout = refs_ref_exists(get_main_ref_store(the_repository),
1194
refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
1195
get_first_good, good_glob, "refs/bisect/",
1199
if (refs_read_ref(get_main_ref_store(the_repository), no_checkout ? "BISECT_HEAD" : "HEAD", ¤t_rev))
1202
res = bisect_checkout(&good_rev, no_checkout);
1203
if (res != BISECT_OK)
1206
rc = do_bisect_run(command);
1208
res = bisect_checkout(¤t_rev, no_checkout);
1209
if (res != BISECT_OK)
1215
static int bisect_run(struct bisect_terms *terms, int argc, const char **argv)
1217
int res = BISECT_OK;
1218
struct strbuf command = STRBUF_INIT;
1219
const char *new_state;
1220
int temporary_stdout_fd, saved_stdout;
1221
int is_first_run = 1;
1223
if (bisect_next_check(terms, NULL))
1224
return BISECT_FAILED;
1227
error(_("bisect run failed: no command provided."));
1228
return BISECT_FAILED;
1231
sq_quote_argv(&command, argv);
1232
strbuf_ltrim(&command);
1234
res = do_bisect_run(command.buf);
1243
if (is_first_run && (res == 126 || res == 127)) {
1244
int rc = verify_good(terms, command.buf);
1246
if (rc < 0 || 128 <= rc) {
1247
error(_("unable to verify %s on good"
1248
" revision"), command.buf);
1249
res = BISECT_FAILED;
1253
error(_("bogus exit code %d for good revision"),
1255
res = BISECT_FAILED;
1260
if (res < 0 || 128 <= res) {
1261
error(_("bisect run failed: exit code %d from"
1262
" %s is < 0 or >= 128"), res, command.buf);
1269
new_state = terms->term_good;
1271
new_state = terms->term_bad;
1273
temporary_stdout_fd = open(git_path_bisect_run(), O_CREAT | O_WRONLY | O_TRUNC, 0666);
1275
if (temporary_stdout_fd < 0) {
1276
res = error_errno(_("cannot open file '%s' for writing"), git_path_bisect_run());
1281
saved_stdout = dup(1);
1282
dup2(temporary_stdout_fd, 1);
1284
res = bisect_state(terms, 1, &new_state);
1287
dup2(saved_stdout, 1);
1288
close(saved_stdout);
1289
close(temporary_stdout_fd);
1291
print_file_to_stdout(git_path_bisect_run());
1293
if (res == BISECT_ONLY_SKIPPED_LEFT)
1294
error(_("bisect run cannot continue any more"));
1295
else if (res == BISECT_INTERNAL_SUCCESS_MERGE_BASE) {
1296
puts(_("bisect run success"));
1298
} else if (res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND) {
1299
puts(_("bisect found first bad commit"));
1302
error(_("bisect run failed: 'git bisect %s'"
1303
" exited with error code %d"), new_state, res);
1310
strbuf_release(&command);
1314
static int cmd_bisect__reset(int argc, const char **argv, const char *prefix UNUSED)
1317
return error(_("'%s' requires either no argument or a commit"),
1318
"git bisect reset");
1319
return bisect_reset(argc ? argv[0] : NULL);
1322
static int cmd_bisect__terms(int argc, const char **argv, const char *prefix UNUSED)
1325
struct bisect_terms terms = { 0 };
1328
return error(_("'%s' requires 0 or 1 argument"),
1329
"git bisect terms");
1330
res = bisect_terms(&terms, argc == 1 ? argv[0] : NULL);
1335
static int cmd_bisect__start(int argc, const char **argv, const char *prefix UNUSED)
1338
struct bisect_terms terms = { 0 };
1340
set_terms(&terms, "bad", "good");
1341
res = bisect_start(&terms, argc, argv);
1346
static int cmd_bisect__next(int argc, const char **argv UNUSED, const char *prefix)
1349
struct bisect_terms terms = { 0 };
1352
return error(_("'%s' requires 0 arguments"),
1355
res = bisect_next(&terms, prefix);
1360
static int cmd_bisect__log(int argc UNUSED, const char **argv UNUSED, const char *prefix UNUSED)
1362
return bisect_log();
1365
static int cmd_bisect__replay(int argc, const char **argv, const char *prefix UNUSED)
1368
struct bisect_terms terms = { 0 };
1371
return error(_("no logfile given"));
1372
set_terms(&terms, "bad", "good");
1373
res = bisect_replay(&terms, argv[0]);
1378
static int cmd_bisect__skip(int argc, const char **argv, const char *prefix UNUSED)
1381
struct bisect_terms terms = { 0 };
1383
set_terms(&terms, "bad", "good");
1385
res = bisect_skip(&terms, argc, argv);
1390
static int cmd_bisect__visualize(int argc, const char **argv, const char *prefix UNUSED)
1393
struct bisect_terms terms = { 0 };
1396
res = bisect_visualize(&terms, argc, argv);
1401
static int cmd_bisect__run(int argc, const char **argv, const char *prefix UNUSED)
1404
struct bisect_terms terms = { 0 };
1407
return error(_("'%s' failed: no command provided."), "git bisect run");
1409
res = bisect_run(&terms, argc, argv);
1414
int cmd_bisect(int argc, const char **argv, const char *prefix)
1417
parse_opt_subcommand_fn *fn = NULL;
1418
struct option options[] = {
1419
OPT_SUBCOMMAND("reset", &fn, cmd_bisect__reset),
1420
OPT_SUBCOMMAND("terms", &fn, cmd_bisect__terms),
1421
OPT_SUBCOMMAND("start", &fn, cmd_bisect__start),
1422
OPT_SUBCOMMAND("next", &fn, cmd_bisect__next),
1423
OPT_SUBCOMMAND("log", &fn, cmd_bisect__log),
1424
OPT_SUBCOMMAND("replay", &fn, cmd_bisect__replay),
1425
OPT_SUBCOMMAND("skip", &fn, cmd_bisect__skip),
1426
OPT_SUBCOMMAND("visualize", &fn, cmd_bisect__visualize),
1427
OPT_SUBCOMMAND("view", &fn, cmd_bisect__visualize),
1428
OPT_SUBCOMMAND("run", &fn, cmd_bisect__run),
1431
argc = parse_options(argc, argv, prefix, options, git_bisect_usage,
1432
PARSE_OPT_SUBCOMMAND_OPTIONAL);
1435
struct bisect_terms terms = { 0 };
1438
usage_msg_opt(_("need a command"), git_bisect_usage, options);
1440
set_terms(&terms, "bad", "good");
1442
if (check_and_set_terms(&terms, argv[0]))
1443
usage_msg_optf(_("unknown command: '%s'"), git_bisect_usage,
1445
res = bisect_state(&terms, argc, argv);
1450
res = fn(argc, argv, prefix);
1453
return is_bisect_success(res) ? 0 : -res;