]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/bisect.c
Merge branch 'js/empty-index-fixes'
[thirdparty/git.git] / builtin / bisect.c
CommitLineData
1bf072e3 1#include "builtin.h"
d5fff46f 2#include "copy.h"
32a8f510 3#include "environment.h"
f394e093 4#include "gettext.h"
41771fa4 5#include "hex.h"
dabab1d6 6#include "object-name.h"
1bf072e3
CC
7#include "parse-options.h"
8#include "bisect.h"
4ba1e5c4 9#include "refs.h"
5e82c3dd 10#include "dir.h"
dbbcd44f 11#include "strvec.h"
5e82c3dd 12#include "run-command.h"
6f2d7430 13#include "oid-array.h"
c339932b 14#include "path.h"
129a6cf3 15#include "prompt.h"
06f5608c 16#include "quote.h"
517ecb31 17#include "revision.h"
d5ebb50d 18#include "wrapper.h"
1bf072e3 19
ecb3f373 20static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
b903674b
PB
21static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
22static GIT_PATH_FUNC(git_path_bisect_ancestors_ok, "BISECT_ANCESTORS_OK")
5e82c3dd 23static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START")
0f30233a 24static GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG")
06f5608c 25static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES")
e8861ffc 26static GIT_PATH_FUNC(git_path_bisect_first_parent, "BISECT_FIRST_PARENT")
d1bbbe45 27static GIT_PATH_FUNC(git_path_bisect_run, "BISECT_RUN")
ecb3f373 28
5512376a
ÆAB
29#define BUILTIN_GIT_BISECT_START_USAGE \
30 N_("git bisect start [--term-{new,bad}=<term> --term-{old,good}=<term>]" \
31 " [--no-checkout] [--first-parent] [<bad> [<good>...]] [--]" \
32 " [<pathspec>...]")
33#define BUILTIN_GIT_BISECT_STATE_USAGE \
34 N_("git bisect (good|bad) [<rev>...]")
35#define BUILTIN_GIT_BISECT_TERMS_USAGE \
36 "git bisect terms [--term-good | --term-bad]"
37#define BUILTIN_GIT_BISECT_SKIP_USAGE \
38 N_("git bisect skip [(<rev>|<range>)...]")
39#define BUILTIN_GIT_BISECT_NEXT_USAGE \
40 "git bisect next"
41#define BUILTIN_GIT_BISECT_RESET_USAGE \
42 N_("git bisect reset [<commit>]")
43#define BUILTIN_GIT_BISECT_VISUALIZE_USAGE \
44 "git bisect visualize"
45#define BUILTIN_GIT_BISECT_REPLAY_USAGE \
46 N_("git bisect replay <logfile>")
47#define BUILTIN_GIT_BISECT_LOG_USAGE \
48 "git bisect log"
49#define BUILTIN_GIT_BISECT_RUN_USAGE \
50 N_("git bisect run <cmd>...")
51
52static const char * const git_bisect_usage[] = {
53 BUILTIN_GIT_BISECT_START_USAGE,
54 BUILTIN_GIT_BISECT_STATE_USAGE,
55 BUILTIN_GIT_BISECT_TERMS_USAGE,
56 BUILTIN_GIT_BISECT_SKIP_USAGE,
57 BUILTIN_GIT_BISECT_NEXT_USAGE,
58 BUILTIN_GIT_BISECT_RESET_USAGE,
59 BUILTIN_GIT_BISECT_VISUALIZE_USAGE,
60 BUILTIN_GIT_BISECT_REPLAY_USAGE,
61 BUILTIN_GIT_BISECT_LOG_USAGE,
62 BUILTIN_GIT_BISECT_RUN_USAGE,
1bf072e3
CC
63 NULL
64};
65
517ecb31
PB
66struct add_bisect_ref_data {
67 struct rev_info *revs;
68 unsigned int object_flags;
69};
70
0f30233a
PB
71struct bisect_terms {
72 char *term_good;
73 char *term_bad;
74};
75
76static void free_terms(struct bisect_terms *terms)
77{
78 FREE_AND_NULL(terms->term_good);
79 FREE_AND_NULL(terms->term_bad);
80}
81
82static void set_terms(struct bisect_terms *terms, const char *bad,
83 const char *good)
84{
85 free((void *)terms->term_good);
86 terms->term_good = xstrdup(good);
87 free((void *)terms->term_bad);
88 terms->term_bad = xstrdup(bad);
89}
90
16538bfd
MR
91static const char vocab_bad[] = "bad|new";
92static const char vocab_good[] = "good|old";
129a6cf3 93
517ecb31
PB
94static int bisect_autostart(struct bisect_terms *terms);
95
4ba1e5c4
PB
96/*
97 * Check whether the string `term` belongs to the set of strings
98 * included in the variable arguments.
99 */
100LAST_ARG_MUST_BE_NULL
101static int one_of(const char *term, ...)
102{
103 int res = 0;
104 va_list matches;
105 const char *match;
106
107 va_start(matches, term);
108 while (!res && (match = va_arg(matches, const char *)))
109 res = !strcmp(term, match);
110 va_end(matches);
111
112 return res;
113}
114
88ad372f
PB
115/*
116 * return code BISECT_INTERNAL_SUCCESS_MERGE_BASE
117 * and BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND are codes
118 * that indicate special success.
119 */
120
121static int is_bisect_success(enum bisect_error res)
122{
123 return !res ||
124 res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND ||
125 res == BISECT_INTERNAL_SUCCESS_MERGE_BASE;
126}
127
7b4de74b
MR
128static int write_in_file(const char *path, const char *mode, const char *format, va_list args)
129{
130 FILE *fp = NULL;
131 int res = 0;
132
517ecb31 133 if (strcmp(mode, "w") && strcmp(mode, "a"))
7b4de74b
MR
134 BUG("write-in-file does not support '%s' mode", mode);
135 fp = fopen(path, mode);
136 if (!fp)
137 return error_errno(_("cannot open file '%s' in mode '%s'"), path, mode);
138 res = vfprintf(fp, format, args);
139
140 if (res < 0) {
141 int saved_errno = errno;
142 fclose(fp);
143 errno = saved_errno;
144 return error_errno(_("could not write to file '%s'"), path);
145 }
146
147 return fclose(fp);
148}
149
48ca53ca 150__attribute__((format (printf, 2, 3)))
7b4de74b
MR
151static int write_to_file(const char *path, const char *format, ...)
152{
153 int res;
154 va_list args;
155
156 va_start(args, format);
157 res = write_in_file(path, "w", format, args);
158 va_end(args);
159
160 return res;
161}
162
48ca53ca 163__attribute__((format (printf, 2, 3)))
517ecb31
PB
164static int append_to_file(const char *path, const char *format, ...)
165{
166 int res;
167 va_list args;
168
169 va_start(args, format);
170 res = write_in_file(path, "a", format, args);
171 va_end(args);
172
173 return res;
174}
175
d1bbbe45
TT
176static int print_file_to_stdout(const char *path)
177{
178 int fd = open(path, O_RDONLY);
179 int ret = 0;
180
181 if (fd < 0)
182 return error_errno(_("cannot open file '%s' for reading"), path);
183 if (copy_fd(fd, 1) < 0)
184 ret = error_errno(_("failed to read '%s'"), path);
185 close(fd);
186 return ret;
187}
188
4ba1e5c4
PB
189static int check_term_format(const char *term, const char *orig_term)
190{
191 int res;
192 char *new_term = xstrfmt("refs/bisect/%s", term);
193
194 res = check_refname_format(new_term, 0);
195 free(new_term);
196
197 if (res)
198 return error(_("'%s' is not a valid term"), term);
199
200 if (one_of(term, "help", "start", "skip", "next", "reset",
dbc349bb 201 "visualize", "view", "replay", "log", "run", "terms", NULL))
4ba1e5c4
PB
202 return error(_("can't use the builtin command '%s' as a term"), term);
203
204 /*
205 * In theory, nothing prevents swapping completely good and bad,
206 * but this situation could be confusing and hasn't been tested
207 * enough. Forbid it for now.
208 */
209
210 if ((strcmp(orig_term, "bad") && one_of(term, "bad", "new", NULL)) ||
211 (strcmp(orig_term, "good") && one_of(term, "good", "old", NULL)))
212 return error(_("can't change the meaning of the term '%s'"), term);
213
214 return 0;
215}
216
ecb3f373
PB
217static int write_terms(const char *bad, const char *good)
218{
ecb3f373
PB
219 int res;
220
221 if (!strcmp(bad, good))
222 return error(_("please use two different terms"));
223
224 if (check_term_format(bad, "bad") || check_term_format(good, "good"))
225 return -1;
226
7b4de74b 227 res = write_to_file(git_path_bisect_terms(), "%s\n%s\n", bad, good);
ecb3f373 228
7b4de74b 229 return res;
ecb3f373
PB
230}
231
5e82c3dd
PB
232static int bisect_reset(const char *commit)
233{
234 struct strbuf branch = STRBUF_INIT;
235
236 if (!commit) {
237 if (strbuf_read_file(&branch, git_path_bisect_start(), 0) < 1) {
238 printf(_("We are not bisecting.\n"));
239 return 0;
240 }
241 strbuf_rtrim(&branch);
242 } else {
243 struct object_id oid;
244
d850b7a5 245 if (repo_get_oid_commit(the_repository, commit, &oid))
5e82c3dd
PB
246 return error(_("'%s' is not a valid commit"), commit);
247 strbuf_addstr(&branch, commit);
248 }
249
de966e39 250 if (!ref_exists("BISECT_HEAD")) {
0e906739 251 struct child_process cmd = CHILD_PROCESS_INIT;
5e82c3dd 252
0e906739 253 cmd.git_cmd = 1;
7fb89047
RJ
254 strvec_pushl(&cmd.args, "checkout", "--ignore-other-worktrees",
255 branch.buf, "--", NULL);
0e906739 256 if (run_command(&cmd)) {
51a0a4ed
TT
257 error(_("could not check out original"
258 " HEAD '%s'. Try 'git bisect"
259 " reset <commit>'."), branch.buf);
5e82c3dd 260 strbuf_release(&branch);
51a0a4ed 261 return -1;
5e82c3dd 262 }
5e82c3dd
PB
263 }
264
265 strbuf_release(&branch);
266 return bisect_clean_state();
267}
268
0f30233a
PB
269static void log_commit(FILE *fp, char *fmt, const char *state,
270 struct commit *commit)
271{
272 struct pretty_print_context pp = {0};
273 struct strbuf commit_msg = STRBUF_INIT;
274 char *label = xstrfmt(fmt, state);
275
bab82164
ÆAB
276 repo_format_commit_message(the_repository, commit, "%s", &commit_msg,
277 &pp);
0f30233a
PB
278
279 fprintf(fp, "# %s: [%s] %s\n", label, oid_to_hex(&commit->object.oid),
280 commit_msg.buf);
281
282 strbuf_release(&commit_msg);
283 free(label);
284}
285
286static int bisect_write(const char *state, const char *rev,
287 const struct bisect_terms *terms, int nolog)
288{
289 struct strbuf tag = STRBUF_INIT;
290 struct object_id oid;
291 struct commit *commit;
292 FILE *fp = NULL;
292731c4 293 int res = 0;
0f30233a
PB
294
295 if (!strcmp(state, terms->term_bad)) {
296 strbuf_addf(&tag, "refs/bisect/%s", state);
297 } else if (one_of(state, terms->term_good, "skip", NULL)) {
298 strbuf_addf(&tag, "refs/bisect/%s-%s", state, rev);
299 } else {
292731c4 300 res = error(_("Bad bisect_write argument: %s"), state);
0f30233a
PB
301 goto finish;
302 }
303
d850b7a5 304 if (repo_get_oid(the_repository, rev, &oid)) {
292731c4 305 res = error(_("couldn't get the oid of the rev '%s'"), rev);
0f30233a
PB
306 goto finish;
307 }
308
309 if (update_ref(NULL, tag.buf, &oid, NULL, 0,
310 UPDATE_REFS_MSG_ON_ERR)) {
292731c4 311 res = -1;
0f30233a
PB
312 goto finish;
313 }
314
315 fp = fopen(git_path_bisect_log(), "a");
316 if (!fp) {
292731c4 317 res = error_errno(_("couldn't open the file '%s'"), git_path_bisect_log());
0f30233a
PB
318 goto finish;
319 }
320
321 commit = lookup_commit_reference(the_repository, &oid);
322 log_commit(fp, "%s", state, commit);
323
324 if (!nolog)
325 fprintf(fp, "git bisect %s %s\n", state, rev);
326
327finish:
328 if (fp)
329 fclose(fp);
330 strbuf_release(&tag);
292731c4 331 return res;
0f30233a
PB
332}
333
4fbdbd5b
PB
334static int check_and_set_terms(struct bisect_terms *terms, const char *cmd)
335{
336 int has_term_file = !is_empty_or_missing_file(git_path_bisect_terms());
337
338 if (one_of(cmd, "skip", "start", "terms", NULL))
339 return 0;
340
341 if (has_term_file && strcmp(cmd, terms->term_bad) &&
342 strcmp(cmd, terms->term_good))
343 return error(_("Invalid command: you're currently in a "
344 "%s/%s bisect"), terms->term_bad,
345 terms->term_good);
346
347 if (!has_term_file) {
348 if (one_of(cmd, "bad", "good", NULL)) {
349 set_terms(terms, "bad", "good");
350 return write_terms(terms->term_bad, terms->term_good);
351 }
352 if (one_of(cmd, "new", "old", NULL)) {
353 set_terms(terms, "new", "old");
354 return write_terms(terms->term_bad, terms->term_good);
355 }
356 }
357
358 return 0;
359}
360
5cf88fd8
ÆAB
361static int inc_nr(const char *refname UNUSED,
362 const struct object_id *oid UNUSED,
363 int flag UNUSED, void *cb_data)
129a6cf3 364{
0cf1defa
CD
365 unsigned int *nr = (unsigned int *)cb_data;
366 (*nr)++;
367 return 0;
129a6cf3
PB
368}
369
7c5cea72 370static const char need_bad_and_good_revision_warning[] =
129a6cf3
PB
371 N_("You need to give me at least one %s and %s revision.\n"
372 "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
373
7c5cea72 374static const char need_bisect_start_warning[] =
129a6cf3
PB
375 N_("You need to start by \"git bisect start\".\n"
376 "You then need to give me at least one %s and %s revision.\n"
377 "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
378
bfacfce7
TT
379static int decide_next(const struct bisect_terms *terms,
380 const char *current_term, int missing_good,
381 int missing_bad)
129a6cf3 382{
129a6cf3 383 if (!missing_good && !missing_bad)
bfacfce7
TT
384 return 0;
385 if (!current_term)
386 return -1;
129a6cf3
PB
387
388 if (missing_good && !missing_bad &&
389 !strcmp(current_term, terms->term_good)) {
390 char *yesno;
391 /*
392 * have bad (or new) but not good (or old). We could bisect
393 * although this is less optimum.
394 */
395 warning(_("bisecting only with a %s commit"), terms->term_bad);
396 if (!isatty(0))
bfacfce7 397 return 0;
129a6cf3
PB
398 /*
399 * TRANSLATORS: Make sure to include [Y] and [n] in your
400 * translation. The program will only accept English input
401 * at this point.
402 */
403 yesno = git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO);
404 if (starts_with(yesno, "N") || starts_with(yesno, "n"))
bfacfce7
TT
405 return -1;
406 return 0;
129a6cf3
PB
407 }
408
bfacfce7
TT
409 if (!is_empty_or_missing_file(git_path_bisect_start()))
410 return error(_(need_bad_and_good_revision_warning),
411 vocab_bad, vocab_good, vocab_bad, vocab_good);
412 else
413 return error(_(need_bisect_start_warning),
414 vocab_good, vocab_bad, vocab_good, vocab_bad);
415}
416
0cf1defa
CD
417static void bisect_status(struct bisect_state *state,
418 const struct bisect_terms *terms)
bfacfce7 419{
bfacfce7
TT
420 char *bad_ref = xstrfmt("refs/bisect/%s", terms->term_bad);
421 char *good_glob = xstrfmt("%s-*", terms->term_good);
422
423 if (ref_exists(bad_ref))
0cf1defa 424 state->nr_bad = 1;
bfacfce7 425
0cf1defa
CD
426 for_each_glob_ref_in(inc_nr, good_glob, "refs/bisect/",
427 (void *) &state->nr_good);
bfacfce7
TT
428
429 free(good_glob);
430 free(bad_ref);
0cf1defa
CD
431}
432
f11046e6
CD
433__attribute__((format (printf, 1, 2)))
434static void bisect_log_printf(const char *fmt, ...)
435{
436 struct strbuf buf = STRBUF_INIT;
437 va_list ap;
438
439 va_start(ap, fmt);
440 strbuf_vaddf(&buf, fmt, ap);
441 va_end(ap);
442
443 printf("%s", buf.buf);
444 append_to_file(git_path_bisect_log(), "# %s", buf.buf);
445
446 strbuf_release(&buf);
447}
448
0cf1defa
CD
449static void bisect_print_status(const struct bisect_terms *terms)
450{
451 struct bisect_state state = { 0 };
452
453 bisect_status(&state, terms);
454
455 /* If we had both, we'd already be started, and shouldn't get here. */
456 if (state.nr_good && state.nr_bad)
457 return;
bfacfce7 458
0cf1defa 459 if (!state.nr_good && !state.nr_bad)
f11046e6 460 bisect_log_printf(_("status: waiting for both good and bad commits\n"));
0cf1defa 461 else if (state.nr_good)
f11046e6
CD
462 bisect_log_printf(Q_("status: waiting for bad commit, %d good commit known\n",
463 "status: waiting for bad commit, %d good commits known\n",
464 state.nr_good), state.nr_good);
0cf1defa 465 else
f11046e6 466 bisect_log_printf(_("status: waiting for good commit(s), bad commit known\n"));
0cf1defa
CD
467}
468
469static int bisect_next_check(const struct bisect_terms *terms,
470 const char *current_term)
471{
472 struct bisect_state state = { 0 };
473 bisect_status(&state, terms);
474 return decide_next(terms, current_term, !state.nr_good, !state.nr_bad);
129a6cf3
PB
475}
476
450ebb73
PB
477static int get_terms(struct bisect_terms *terms)
478{
479 struct strbuf str = STRBUF_INIT;
480 FILE *fp = NULL;
481 int res = 0;
482
483 fp = fopen(git_path_bisect_terms(), "r");
484 if (!fp) {
485 res = -1;
486 goto finish;
487 }
488
489 free_terms(terms);
490 strbuf_getline_lf(&str, fp);
491 terms->term_bad = strbuf_detach(&str, NULL);
492 strbuf_getline_lf(&str, fp);
493 terms->term_good = strbuf_detach(&str, NULL);
494
495finish:
496 if (fp)
497 fclose(fp);
498 strbuf_release(&str);
499 return res;
500}
501
502static int bisect_terms(struct bisect_terms *terms, const char *option)
503{
504 if (get_terms(terms))
505 return error(_("no terms defined"));
506
afe8a907 507 if (!option) {
450ebb73
PB
508 printf(_("Your current terms are %s for the old state\n"
509 "and %s for the new state.\n"),
510 terms->term_good, terms->term_bad);
511 return 0;
512 }
513 if (one_of(option, "--term-good", "--term-old", NULL))
514 printf("%s\n", terms->term_good);
515 else if (one_of(option, "--term-bad", "--term-new", NULL))
516 printf("%s\n", terms->term_bad);
517 else
518 return error(_("invalid argument %s for 'git bisect terms'.\n"
519 "Supported options are: "
520 "--term-good|--term-old and "
521 "--term-bad|--term-new."), option);
522
523 return 0;
524}
525
06f5608c
PB
526static int bisect_append_log_quoted(const char **argv)
527{
292731c4 528 int res = 0;
06f5608c
PB
529 FILE *fp = fopen(git_path_bisect_log(), "a");
530 struct strbuf orig_args = STRBUF_INIT;
531
532 if (!fp)
533 return -1;
534
535 if (fprintf(fp, "git bisect start") < 1) {
292731c4 536 res = -1;
06f5608c
PB
537 goto finish;
538 }
539
540 sq_quote_argv(&orig_args, argv);
541 if (fprintf(fp, "%s\n", orig_args.buf) < 1)
292731c4 542 res = -1;
06f5608c
PB
543
544finish:
545 fclose(fp);
546 strbuf_release(&orig_args);
292731c4 547 return res;
06f5608c
PB
548}
549
517ecb31 550static int add_bisect_ref(const char *refname, const struct object_id *oid,
5cf88fd8 551 int flags UNUSED, void *cb)
517ecb31
PB
552{
553 struct add_bisect_ref_data *data = cb;
554
555 add_pending_oid(data->revs, refname, oid, data->object_flags);
556
557 return 0;
558}
559
560static int prepare_revs(struct bisect_terms *terms, struct rev_info *revs)
561{
562 int res = 0;
563 struct add_bisect_ref_data cb = { revs };
564 char *good = xstrfmt("%s-*", terms->term_good);
565
566 /*
567 * We cannot use terms->term_bad directly in
568 * for_each_glob_ref_in() and we have to append a '*' to it,
569 * otherwise for_each_glob_ref_in() will append '/' and '*'.
570 */
571 char *bad = xstrfmt("%s*", terms->term_bad);
572
573 /*
574 * It is important to reset the flags used by revision walks
575 * as the previous call to bisect_next_all() in turn
576 * sets up a revision walk.
577 */
578 reset_revision_walk();
035c7de9 579 repo_init_revisions(the_repository, revs, NULL);
517ecb31
PB
580 setup_revisions(0, NULL, revs, NULL);
581 for_each_glob_ref_in(add_bisect_ref, bad, "refs/bisect/", &cb);
582 cb.object_flags = UNINTERESTING;
583 for_each_glob_ref_in(add_bisect_ref, good, "refs/bisect/", &cb);
584 if (prepare_revision_walk(revs))
585 res = error(_("revision walk setup failed\n"));
586
587 free(good);
588 free(bad);
589 return res;
590}
591
592static int bisect_skipped_commits(struct bisect_terms *terms)
593{
594 int res;
595 FILE *fp = NULL;
596 struct rev_info revs;
597 struct commit *commit;
598 struct pretty_print_context pp = {0};
599 struct strbuf commit_name = STRBUF_INIT;
600
601 res = prepare_revs(terms, &revs);
602 if (res)
603 return res;
604
605 fp = fopen(git_path_bisect_log(), "a");
606 if (!fp)
607 return error_errno(_("could not open '%s' for appending"),
608 git_path_bisect_log());
609
610 if (fprintf(fp, "# only skipped commits left to test\n") < 0)
611 return error_errno(_("failed to write to '%s'"), git_path_bisect_log());
612
613 while ((commit = get_revision(&revs)) != NULL) {
614 strbuf_reset(&commit_name);
bab82164
ÆAB
615 repo_format_commit_message(the_repository, commit, "%s",
616 &commit_name, &pp);
517ecb31
PB
617 fprintf(fp, "# possible first %s commit: [%s] %s\n",
618 terms->term_bad, oid_to_hex(&commit->object.oid),
619 commit_name.buf);
620 }
621
622 /*
623 * Reset the flags used by revision walks in case
624 * there is another revision walk after this one.
625 */
626 reset_revision_walk();
627
628 strbuf_release(&commit_name);
2108fe4a 629 release_revisions(&revs);
517ecb31
PB
630 fclose(fp);
631 return 0;
632}
633
634static int bisect_successful(struct bisect_terms *terms)
635{
636 struct object_id oid;
637 struct commit *commit;
638 struct pretty_print_context pp = {0};
639 struct strbuf commit_name = STRBUF_INIT;
640 char *bad_ref = xstrfmt("refs/bisect/%s",terms->term_bad);
641 int res;
642
643 read_ref(bad_ref, &oid);
644 commit = lookup_commit_reference_by_name(bad_ref);
bab82164
ÆAB
645 repo_format_commit_message(the_repository, commit, "%s", &commit_name,
646 &pp);
517ecb31
PB
647
648 res = append_to_file(git_path_bisect_log(), "# first %s commit: [%s] %s\n",
649 terms->term_bad, oid_to_hex(&commit->object.oid),
650 commit_name.buf);
651
652 strbuf_release(&commit_name);
653 free(bad_ref);
654 return res;
655}
656
657static enum bisect_error bisect_next(struct bisect_terms *terms, const char *prefix)
658{
659 enum bisect_error res;
660
661 if (bisect_autostart(terms))
662 return BISECT_FAILED;
663
664 if (bisect_next_check(terms, terms->term_good))
665 return BISECT_FAILED;
666
667 /* Perform all bisection computation */
668 res = bisect_next_all(the_repository, prefix);
669
670 if (res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND) {
671 res = bisect_successful(terms);
672 return res ? res : BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND;
673 } else if (res == BISECT_ONLY_SKIPPED_LEFT) {
674 res = bisect_skipped_commits(terms);
675 return res ? res : BISECT_ONLY_SKIPPED_LEFT;
676 }
677 return res;
678}
679
680static enum bisect_error bisect_auto_next(struct bisect_terms *terms, const char *prefix)
681{
0cf1defa
CD
682 if (bisect_next_check(terms, NULL)) {
683 bisect_print_status(terms);
517ecb31 684 return BISECT_OK;
0cf1defa 685 }
517ecb31
PB
686
687 return bisect_next(terms, prefix);
688}
689
6f977922
ÆAB
690static enum bisect_error bisect_start(struct bisect_terms *terms, int argc,
691 const char **argv)
06f5608c 692{
be5fe200 693 int no_checkout = 0;
e8861ffc 694 int first_parent_only = 0;
06f5608c 695 int i, has_double_dash = 0, must_write_terms = 0, bad_seen = 0;
88ad372f
PB
696 int flags, pathspec_pos;
697 enum bisect_error res = BISECT_OK;
06f5608c
PB
698 struct string_list revs = STRING_LIST_INIT_DUP;
699 struct string_list states = STRING_LIST_INIT_DUP;
700 struct strbuf start_head = STRBUF_INIT;
701 struct strbuf bisect_names = STRBUF_INIT;
702 struct object_id head_oid;
703 struct object_id oid;
704 const char *head;
705
706 if (is_bare_repository())
707 no_checkout = 1;
708
709 /*
710 * Check for one bad and then some good revisions
711 */
712 for (i = 0; i < argc; i++) {
713 if (!strcmp(argv[i], "--")) {
714 has_double_dash = 1;
715 break;
716 }
717 }
718
719 for (i = 0; i < argc; i++) {
720 const char *arg = argv[i];
721 if (!strcmp(argv[i], "--")) {
722 break;
723 } else if (!strcmp(arg, "--no-checkout")) {
724 no_checkout = 1;
e8861ffc
AL
725 } else if (!strcmp(arg, "--first-parent")) {
726 first_parent_only = 1;
06f5608c
PB
727 } else if (!strcmp(arg, "--term-good") ||
728 !strcmp(arg, "--term-old")) {
4d9005ff
CMAB
729 i++;
730 if (argc <= i)
731 return error(_("'' is not a valid term"));
06f5608c
PB
732 must_write_terms = 1;
733 free((void *) terms->term_good);
4d9005ff 734 terms->term_good = xstrdup(argv[i]);
06f5608c
PB
735 } else if (skip_prefix(arg, "--term-good=", &arg) ||
736 skip_prefix(arg, "--term-old=", &arg)) {
737 must_write_terms = 1;
738 free((void *) terms->term_good);
739 terms->term_good = xstrdup(arg);
740 } else if (!strcmp(arg, "--term-bad") ||
741 !strcmp(arg, "--term-new")) {
4d9005ff
CMAB
742 i++;
743 if (argc <= i)
744 return error(_("'' is not a valid term"));
06f5608c
PB
745 must_write_terms = 1;
746 free((void *) terms->term_bad);
4d9005ff 747 terms->term_bad = xstrdup(argv[i]);
06f5608c
PB
748 } else if (skip_prefix(arg, "--term-bad=", &arg) ||
749 skip_prefix(arg, "--term-new=", &arg)) {
750 must_write_terms = 1;
751 free((void *) terms->term_bad);
752 terms->term_bad = xstrdup(arg);
4d9005ff 753 } else if (starts_with(arg, "--")) {
06f5608c 754 return error(_("unrecognized option: '%s'"), arg);
73c6de06 755 } else if (!get_oidf(&oid, "%s^{commit}", arg)) {
06f5608c 756 string_list_append(&revs, oid_to_hex(&oid));
73c6de06
CC
757 } else if (has_double_dash) {
758 die(_("'%s' does not appear to be a valid "
759 "revision"), arg);
760 } else {
761 break;
06f5608c
PB
762 }
763 }
764 pathspec_pos = i;
765
766 /*
767 * The user ran "git bisect start <sha1> <sha1>", hence did not
768 * explicitly specify the terms, but we are already starting to
769 * set references named with the default terms, and won't be able
770 * to change afterwards.
771 */
772 if (revs.nr)
773 must_write_terms = 1;
774 for (i = 0; i < revs.nr; i++) {
775 if (bad_seen) {
776 string_list_append(&states, terms->term_good);
777 } else {
778 bad_seen = 1;
779 string_list_append(&states, terms->term_bad);
780 }
781 }
782
783 /*
784 * Verify HEAD
785 */
786 head = resolve_ref_unsafe("HEAD", 0, &head_oid, &flags);
787 if (!head)
d850b7a5 788 if (repo_get_oid(the_repository, "HEAD", &head_oid))
06f5608c
PB
789 return error(_("bad HEAD - I need a HEAD"));
790
791 /*
792 * Check if we are bisecting
793 */
794 if (!is_empty_or_missing_file(git_path_bisect_start())) {
795 /* Reset to the rev from where we started */
796 strbuf_read_file(&start_head, git_path_bisect_start(), 0);
797 strbuf_trim(&start_head);
798 if (!no_checkout) {
ddbb47fd 799 struct child_process cmd = CHILD_PROCESS_INIT;
06f5608c 800
ddbb47fd
RS
801 cmd.git_cmd = 1;
802 strvec_pushl(&cmd.args, "checkout", start_head.buf,
803 "--", NULL);
804 if (run_command(&cmd)) {
292731c4 805 res = error(_("checking out '%s' failed."
06f5608c
PB
806 " Try 'git bisect start "
807 "<valid-branch>'."),
808 start_head.buf);
809 goto finish;
810 }
811 }
812 } else {
813 /* Get the rev from where we start. */
d850b7a5 814 if (!repo_get_oid(the_repository, head, &head_oid) &&
06f5608c
PB
815 !starts_with(head, "refs/heads/")) {
816 strbuf_reset(&start_head);
817 strbuf_addstr(&start_head, oid_to_hex(&head_oid));
d850b7a5 818 } else if (!repo_get_oid(the_repository, head, &head_oid) &&
06f5608c 819 skip_prefix(head, "refs/heads/", &head)) {
06f5608c
PB
820 strbuf_addstr(&start_head, head);
821 } else {
822 return error(_("bad HEAD - strange symbolic ref"));
823 }
824 }
825
826 /*
827 * Get rid of any old bisect state.
828 */
829 if (bisect_clean_state())
88ad372f 830 return BISECT_FAILED;
06f5608c
PB
831
832 /*
833 * Write new start state
834 */
835 write_file(git_path_bisect_start(), "%s\n", start_head.buf);
836
e8861ffc
AL
837 if (first_parent_only)
838 write_file(git_path_bisect_first_parent(), "\n");
839
06f5608c 840 if (no_checkout) {
d850b7a5 841 if (repo_get_oid(the_repository, start_head.buf, &oid) < 0) {
292731c4 842 res = error(_("invalid ref: '%s'"), start_head.buf);
7877ac3d
JS
843 goto finish;
844 }
06f5608c
PB
845 if (update_ref(NULL, "BISECT_HEAD", &oid, NULL, 0,
846 UPDATE_REFS_MSG_ON_ERR)) {
88ad372f 847 res = BISECT_FAILED;
06f5608c
PB
848 goto finish;
849 }
850 }
851
852 if (pathspec_pos < argc - 1)
853 sq_quote_argv(&bisect_names, argv + pathspec_pos);
854 write_file(git_path_bisect_names(), "%s\n", bisect_names.buf);
855
856 for (i = 0; i < states.nr; i++)
857 if (bisect_write(states.items[i].string,
858 revs.items[i].string, terms, 1)) {
88ad372f 859 res = BISECT_FAILED;
06f5608c
PB
860 goto finish;
861 }
862
863 if (must_write_terms && write_terms(terms->term_bad,
864 terms->term_good)) {
88ad372f 865 res = BISECT_FAILED;
06f5608c
PB
866 goto finish;
867 }
868
292731c4
TT
869 res = bisect_append_log_quoted(argv);
870 if (res)
88ad372f 871 res = BISECT_FAILED;
06f5608c
PB
872
873finish:
874 string_list_clear(&revs, 0);
875 string_list_clear(&states, 0);
876 strbuf_release(&start_head);
877 strbuf_release(&bisect_names);
88ad372f
PB
878 if (res)
879 return res;
880
881 res = bisect_auto_next(terms, NULL);
882 if (!is_bisect_success(res))
883 bisect_clean_state();
292731c4 884 return res;
06f5608c
PB
885}
886
09535f05
PB
887static inline int file_is_not_empty(const char *path)
888{
889 return !is_empty_or_missing_file(path);
890}
891
892static int bisect_autostart(struct bisect_terms *terms)
893{
894 int res;
895 const char *yesno;
896
897 if (file_is_not_empty(git_path_bisect_start()))
898 return 0;
899
900 fprintf_ln(stderr, _("You need to start by \"git bisect "
901 "start\"\n"));
902
903 if (!isatty(STDIN_FILENO))
904 return -1;
905
906 /*
907 * TRANSLATORS: Make sure to include [Y] and [n] in your
908 * translation. The program will only accept English input
909 * at this point.
910 */
911 yesno = git_prompt(_("Do you want me to do it for you "
912 "[Y/n]? "), PROMPT_ECHO);
913 res = tolower(*yesno) == 'n' ?
6f977922 914 -1 : bisect_start(terms, 0, empty_strvec);
09535f05
PB
915
916 return res;
917}
918
6f977922
ÆAB
919static enum bisect_error bisect_state(struct bisect_terms *terms, int argc,
920 const char **argv)
27257bc4
PB
921{
922 const char *state;
923 int i, verify_expected = 1;
924 struct object_id oid, expected;
925 struct strbuf buf = STRBUF_INIT;
926 struct oid_array revs = OID_ARRAY_INIT;
927
928 if (!argc)
929 return error(_("Please call `--bisect-state` with at least one argument"));
930
931 if (bisect_autostart(terms))
932 return BISECT_FAILED;
933
934 state = argv[0];
935 if (check_and_set_terms(terms, state) ||
936 !one_of(state, terms->term_good, terms->term_bad, "skip", NULL))
937 return BISECT_FAILED;
938
939 argv++;
940 argc--;
941 if (argc > 1 && !strcmp(state, terms->term_bad))
942 return error(_("'git bisect %s' can take only one argument."), terms->term_bad);
943
944 if (argc == 0) {
945 const char *head = "BISECT_HEAD";
d850b7a5
ÆAB
946 enum get_oid_result res_head = repo_get_oid(the_repository,
947 head, &oid);
27257bc4
PB
948
949 if (res_head == MISSING_OBJECT) {
950 head = "HEAD";
d850b7a5 951 res_head = repo_get_oid(the_repository, head, &oid);
27257bc4
PB
952 }
953
954 if (res_head)
955 error(_("Bad rev input: %s"), head);
956 oid_array_append(&revs, &oid);
957 }
958
959 /*
960 * All input revs must be checked before executing bisect_write()
961 * to discard junk revs.
962 */
963
964 for (; argc; argc--, argv++) {
7730f855
JK
965 struct commit *commit;
966
d850b7a5 967 if (repo_get_oid(the_repository, *argv, &oid)){
27257bc4
PB
968 error(_("Bad rev input: %s"), *argv);
969 oid_array_clear(&revs);
970 return BISECT_FAILED;
971 }
7730f855
JK
972
973 commit = lookup_commit_reference(the_repository, &oid);
974 if (!commit)
975 die(_("Bad rev input (not a commit): %s"), *argv);
976
977 oid_array_append(&revs, &commit->object.oid);
27257bc4
PB
978 }
979
980 if (strbuf_read_file(&buf, git_path_bisect_expected_rev(), 0) < the_hash_algo->hexsz ||
981 get_oid_hex(buf.buf, &expected) < 0)
982 verify_expected = 0; /* Ignore invalid file contents */
983 strbuf_release(&buf);
984
985 for (i = 0; i < revs.nr; i++) {
986 if (bisect_write(state, oid_to_hex(&revs.oid[i]), terms, 0)) {
987 oid_array_clear(&revs);
988 return BISECT_FAILED;
989 }
990 if (verify_expected && !oideq(&revs.oid[i], &expected)) {
991 unlink_or_warn(git_path_bisect_ancestors_ok());
992 unlink_or_warn(git_path_bisect_expected_rev());
993 verify_expected = 0;
994 }
995 }
996
997 oid_array_clear(&revs);
998 return bisect_auto_next(terms, NULL);
999}
1000
97d5ba6a
PB
1001static enum bisect_error bisect_log(void)
1002{
1003 int fd, status;
1004 const char* filename = git_path_bisect_log();
1005
1006 if (is_empty_or_missing_file(filename))
1007 return error(_("We are not bisecting."));
1008
1009 fd = open(filename, O_RDONLY);
1010 if (fd < 0)
1011 return BISECT_FAILED;
1012
1013 status = copy_fd(fd, STDOUT_FILENO);
1014 close(fd);
1015 return status ? BISECT_FAILED : BISECT_OK;
1016}
1017
2b1fd947
PB
1018static int process_replay_line(struct bisect_terms *terms, struct strbuf *line)
1019{
1020 const char *p = line->buf + strspn(line->buf, " \t");
1021 char *word_end, *rev;
1022
1023 if ((!skip_prefix(p, "git bisect", &p) &&
1024 !skip_prefix(p, "git-bisect", &p)) || !isspace(*p))
1025 return 0;
1026 p += strspn(p, " \t");
1027
1028 word_end = (char *)p + strcspn(p, " \t");
1029 rev = word_end + strspn(word_end, " \t");
1030 *word_end = '\0'; /* NUL-terminate the word */
1031
1032 get_terms(terms);
1033 if (check_and_set_terms(terms, p))
1034 return -1;
1035
1036 if (!strcmp(p, "start")) {
1037 struct strvec argv = STRVEC_INIT;
1038 int res;
1039 sq_dequote_to_strvec(rev, &argv);
6f977922 1040 res = bisect_start(terms, argv.nr, argv.v);
2b1fd947
PB
1041 strvec_clear(&argv);
1042 return res;
1043 }
1044
1045 if (one_of(p, terms->term_good,
1046 terms->term_bad, "skip", NULL))
1047 return bisect_write(p, rev, terms, 0);
1048
1049 if (!strcmp(p, "terms")) {
1050 struct strvec argv = STRVEC_INIT;
1051 int res;
1052 sq_dequote_to_strvec(rev, &argv);
1053 res = bisect_terms(terms, argv.nr == 1 ? argv.v[0] : NULL);
1054 strvec_clear(&argv);
1055 return res;
1056 }
1057 error(_("'%s'?? what are you talking about?"), p);
1058
1059 return -1;
1060}
1061
1062static enum bisect_error bisect_replay(struct bisect_terms *terms, const char *filename)
1063{
1064 FILE *fp = NULL;
1065 enum bisect_error res = BISECT_OK;
1066 struct strbuf line = STRBUF_INIT;
1067
1068 if (is_empty_or_missing_file(filename))
1069 return error(_("cannot read file '%s' for replaying"), filename);
1070
1071 if (bisect_reset(NULL))
1072 return BISECT_FAILED;
1073
1074 fp = fopen(filename, "r");
1075 if (!fp)
1076 return BISECT_FAILED;
1077
1078 while ((strbuf_getline(&line, fp) != EOF) && !res)
1079 res = process_replay_line(terms, &line);
1080
1081 strbuf_release(&line);
1082 fclose(fp);
1083
1084 if (res)
1085 return BISECT_FAILED;
1086
1087 return bisect_auto_next(terms, NULL);
1088}
1089
6f977922
ÆAB
1090static enum bisect_error bisect_skip(struct bisect_terms *terms, int argc,
1091 const char **argv)
e4c7b337
PB
1092{
1093 int i;
1094 enum bisect_error res;
1095 struct strvec argv_state = STRVEC_INIT;
1096
1097 strvec_push(&argv_state, "skip");
1098
1099 for (i = 0; i < argc; i++) {
1100 const char *dotdot = strstr(argv[i], "..");
1101
1102 if (dotdot) {
1103 struct rev_info revs;
1104 struct commit *commit;
1105
035c7de9 1106 repo_init_revisions(the_repository, &revs, NULL);
e4c7b337
PB
1107 setup_revisions(2, argv + i - 1, &revs, NULL);
1108
1109 if (prepare_revision_walk(&revs))
1110 die(_("revision walk setup failed\n"));
1111 while ((commit = get_revision(&revs)) != NULL)
1112 strvec_push(&argv_state,
1113 oid_to_hex(&commit->object.oid));
1114
1115 reset_revision_walk();
2108fe4a 1116 release_revisions(&revs);
e4c7b337
PB
1117 } else {
1118 strvec_push(&argv_state, argv[i]);
1119 }
1120 }
6f977922 1121 res = bisect_state(terms, argv_state.nr, argv_state.v);
e4c7b337
PB
1122
1123 strvec_clear(&argv_state);
1124 return res;
1125}
1126
6f977922
ÆAB
1127static int bisect_visualize(struct bisect_terms *terms, int argc,
1128 const char **argv)
5e1f28d2 1129{
0e906739 1130 struct child_process cmd = CHILD_PROCESS_INIT;
5e1f28d2
PB
1131 struct strbuf sb = STRBUF_INIT;
1132
1133 if (bisect_next_check(terms, NULL) != 0)
1134 return BISECT_FAILED;
1135
0e906739 1136 cmd.no_stdin = 1;
5e1f28d2
PB
1137 if (!argc) {
1138 if ((getenv("DISPLAY") || getenv("SESSIONNAME") || getenv("MSYSTEM") ||
1139 getenv("SECURITYSESSIONID")) && exists_in_PATH("gitk")) {
0e906739 1140 strvec_push(&cmd.args, "gitk");
5e1f28d2 1141 } else {
0e906739
RS
1142 strvec_push(&cmd.args, "log");
1143 cmd.git_cmd = 1;
5e1f28d2
PB
1144 }
1145 } else {
1146 if (argv[0][0] == '-') {
0e906739
RS
1147 strvec_push(&cmd.args, "log");
1148 cmd.git_cmd = 1;
5e1f28d2 1149 } else if (strcmp(argv[0], "tig") && !starts_with(argv[0], "git"))
0e906739 1150 cmd.git_cmd = 1;
5e1f28d2 1151
0e906739 1152 strvec_pushv(&cmd.args, argv);
5e1f28d2
PB
1153 }
1154
0e906739 1155 strvec_pushl(&cmd.args, "--bisect", "--", NULL);
5e1f28d2
PB
1156
1157 strbuf_read_file(&sb, git_path_bisect_names(), 0);
0e906739 1158 sq_dequote_to_strvec(sb.buf, &cmd.args);
5e1f28d2
PB
1159 strbuf_release(&sb);
1160
0e906739 1161 return run_command(&cmd);
5e1f28d2
PB
1162}
1163
5cf88fd8 1164static int get_first_good(const char *refname UNUSED,
63e14ee2 1165 const struct object_id *oid,
5cf88fd8 1166 int flag UNUSED, void *cb_data)
48af1fde
RS
1167{
1168 oidcpy(cb_data, oid);
1169 return 1;
1170}
1171
461fec41 1172static int do_bisect_run(const char *command)
eede29aa 1173{
ddbb47fd
RS
1174 struct child_process cmd = CHILD_PROCESS_INIT;
1175
461fec41 1176 printf(_("running %s\n"), command);
ddbb47fd
RS
1177 cmd.use_shell = 1;
1178 strvec_push(&cmd.args, command);
1179 return run_command(&cmd);
eede29aa
RS
1180}
1181
461fec41 1182static int verify_good(const struct bisect_terms *terms, const char *command)
48af1fde
RS
1183{
1184 int rc;
1185 enum bisect_error res;
1186 struct object_id good_rev;
1187 struct object_id current_rev;
1188 char *good_glob = xstrfmt("%s-*", terms->term_good);
1189 int no_checkout = ref_exists("BISECT_HEAD");
1190
1191 for_each_glob_ref_in(get_first_good, good_glob, "refs/bisect/",
1192 &good_rev);
1193 free(good_glob);
1194
1195 if (read_ref(no_checkout ? "BISECT_HEAD" : "HEAD", &current_rev))
1196 return -1;
1197
1198 res = bisect_checkout(&good_rev, no_checkout);
1199 if (res != BISECT_OK)
1200 return -1;
1201
461fec41 1202 rc = do_bisect_run(command);
48af1fde
RS
1203
1204 res = bisect_checkout(&current_rev, no_checkout);
1205 if (res != BISECT_OK)
1206 return -1;
1207
1208 return rc;
1209}
1210
6f977922 1211static int bisect_run(struct bisect_terms *terms, int argc, const char **argv)
d1bbbe45
TT
1212{
1213 int res = BISECT_OK;
1214 struct strbuf command = STRBUF_INIT;
d1bbbe45
TT
1215 const char *new_state;
1216 int temporary_stdout_fd, saved_stdout;
48af1fde 1217 int is_first_run = 1;
d1bbbe45
TT
1218
1219 if (bisect_next_check(terms, NULL))
1220 return BISECT_FAILED;
1221
bdd2aa8a 1222 if (!argc) {
d1bbbe45
TT
1223 error(_("bisect run failed: no command provided."));
1224 return BISECT_FAILED;
1225 }
1226
bdd2aa8a 1227 sq_quote_argv(&command, argv);
461fec41 1228 strbuf_ltrim(&command);
d1bbbe45 1229 while (1) {
461fec41 1230 res = do_bisect_run(command.buf);
d1bbbe45 1231
48af1fde
RS
1232 /*
1233 * Exit code 126 and 127 can either come from the shell
1234 * if it was unable to execute or even find the script,
1235 * or from the script itself. Check with a known-good
1236 * revision to avoid trashing the bisect run due to a
1237 * missing or non-executable script.
1238 */
1239 if (is_first_run && (res == 126 || res == 127)) {
461fec41 1240 int rc = verify_good(terms, command.buf);
48af1fde 1241 is_first_run = 0;
8962f8f8 1242 if (rc < 0 || 128 <= rc) {
461fec41
ĐTCD
1243 error(_("unable to verify %s on good"
1244 " revision"), command.buf);
48af1fde
RS
1245 res = BISECT_FAILED;
1246 break;
1247 }
1248 if (rc == res) {
1249 error(_("bogus exit code %d for good revision"),
1250 rc);
1251 res = BISECT_FAILED;
1252 break;
1253 }
1254 }
1255
d1bbbe45
TT
1256 if (res < 0 || 128 <= res) {
1257 error(_("bisect run failed: exit code %d from"
461fec41 1258 " %s is < 0 or >= 128"), res, command.buf);
8efa2acc 1259 break;
d1bbbe45
TT
1260 }
1261
1262 if (res == 125)
1263 new_state = "skip";
1264 else if (!res)
1265 new_state = terms->term_good;
1266 else
1267 new_state = terms->term_bad;
1268
1269 temporary_stdout_fd = open(git_path_bisect_run(), O_CREAT | O_WRONLY | O_TRUNC, 0666);
1270
8efa2acc
RS
1271 if (temporary_stdout_fd < 0) {
1272 res = error_errno(_("cannot open file '%s' for writing"), git_path_bisect_run());
1273 break;
1274 }
d1bbbe45
TT
1275
1276 fflush(stdout);
1277 saved_stdout = dup(1);
1278 dup2(temporary_stdout_fd, 1);
1279
6f977922 1280 res = bisect_state(terms, 1, &new_state);
d1bbbe45
TT
1281
1282 fflush(stdout);
1283 dup2(saved_stdout, 1);
1284 close(saved_stdout);
1285 close(temporary_stdout_fd);
1286
1287 print_file_to_stdout(git_path_bisect_run());
1288
1289 if (res == BISECT_ONLY_SKIPPED_LEFT)
1290 error(_("bisect run cannot continue any more"));
1291 else if (res == BISECT_INTERNAL_SUCCESS_MERGE_BASE) {
f37d0bdd 1292 puts(_("bisect run success"));
d1bbbe45
TT
1293 res = BISECT_OK;
1294 } else if (res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND) {
461fec41 1295 puts(_("bisect found first bad commit"));
d1bbbe45
TT
1296 res = BISECT_OK;
1297 } else if (res) {
4de06fbd 1298 error(_("bisect run failed: 'git bisect %s'"
f37d0bdd 1299 " exited with error code %d"), new_state, res);
d1bbbe45
TT
1300 } else {
1301 continue;
1302 }
8efa2acc 1303 break;
d1bbbe45 1304 }
8efa2acc
RS
1305
1306 strbuf_release(&command);
8efa2acc 1307 return res;
d1bbbe45
TT
1308}
1309
464ce0ab
ĐTCD
1310static int cmd_bisect__reset(int argc, const char **argv, const char *prefix UNUSED)
1311{
1312 if (argc > 1)
252060be
ĐTCD
1313 return error(_("'%s' requires either no argument or a commit"),
1314 "git bisect reset");
464ce0ab
ĐTCD
1315 return bisect_reset(argc ? argv[0] : NULL);
1316}
1317
1318static int cmd_bisect__terms(int argc, const char **argv, const char *prefix UNUSED)
1319{
1320 int res;
1321 struct bisect_terms terms = { 0 };
1322
1323 if (argc > 1)
252060be
ĐTCD
1324 return error(_("'%s' requires 0 or 1 argument"),
1325 "git bisect terms");
464ce0ab
ĐTCD
1326 res = bisect_terms(&terms, argc == 1 ? argv[0] : NULL);
1327 free_terms(&terms);
1328 return res;
1329}
1330
1331static int cmd_bisect__start(int argc, const char **argv, const char *prefix UNUSED)
1332{
1333 int res;
1334 struct bisect_terms terms = { 0 };
1335
1336 set_terms(&terms, "bad", "good");
6f977922 1337 res = bisect_start(&terms, argc, argv);
464ce0ab
ĐTCD
1338 free_terms(&terms);
1339 return res;
1340}
1341
1342static int cmd_bisect__next(int argc, const char **argv UNUSED, const char *prefix)
1343{
1344 int res;
1345 struct bisect_terms terms = { 0 };
1346
1347 if (argc)
252060be
ĐTCD
1348 return error(_("'%s' requires 0 arguments"),
1349 "git bisect next");
464ce0ab
ĐTCD
1350 get_terms(&terms);
1351 res = bisect_next(&terms, prefix);
1352 free_terms(&terms);
1353 return res;
1354}
1355
0da4b538 1356static int cmd_bisect__log(int argc UNUSED, const char **argv UNUSED, const char *prefix UNUSED)
464ce0ab 1357{
464ce0ab
ĐTCD
1358 return bisect_log();
1359}
1360
1361static int cmd_bisect__replay(int argc, const char **argv, const char *prefix UNUSED)
1362{
1363 int res;
1364 struct bisect_terms terms = { 0 };
1365
1366 if (argc != 1)
1367 return error(_("no logfile given"));
1368 set_terms(&terms, "bad", "good");
1369 res = bisect_replay(&terms, argv[0]);
1370 free_terms(&terms);
1371 return res;
1372}
1373
1374static int cmd_bisect__skip(int argc, const char **argv, const char *prefix UNUSED)
1375{
1376 int res;
1377 struct bisect_terms terms = { 0 };
1378
1379 set_terms(&terms, "bad", "good");
1380 get_terms(&terms);
6f977922 1381 res = bisect_skip(&terms, argc, argv);
464ce0ab
ĐTCD
1382 free_terms(&terms);
1383 return res;
1384}
1385
1386static int cmd_bisect__visualize(int argc, const char **argv, const char *prefix UNUSED)
1387{
1388 int res;
1389 struct bisect_terms terms = { 0 };
1390
1391 get_terms(&terms);
6f977922 1392 res = bisect_visualize(&terms, argc, argv);
464ce0ab
ĐTCD
1393 free_terms(&terms);
1394 return res;
1395}
1396
1397static int cmd_bisect__run(int argc, const char **argv, const char *prefix UNUSED)
1398{
1399 int res;
1400 struct bisect_terms terms = { 0 };
1401
1402 if (!argc)
252060be 1403 return error(_("'%s' failed: no command provided."), "git bisect run");
464ce0ab 1404 get_terms(&terms);
6f977922 1405 res = bisect_run(&terms, argc, argv);
464ce0ab
ĐTCD
1406 free_terms(&terms);
1407 return res;
1408}
1409
73fce294 1410int cmd_bisect(int argc, const char **argv, const char *prefix)
1bf072e3 1411{
58786d73 1412 int res = 0;
e9011b60 1413 parse_opt_subcommand_fn *fn = NULL;
1bf072e3 1414 struct option options[] = {
e9011b60
ĐTCD
1415 OPT_SUBCOMMAND("reset", &fn, cmd_bisect__reset),
1416 OPT_SUBCOMMAND("terms", &fn, cmd_bisect__terms),
1417 OPT_SUBCOMMAND("start", &fn, cmd_bisect__start),
1418 OPT_SUBCOMMAND("next", &fn, cmd_bisect__next),
e9011b60
ĐTCD
1419 OPT_SUBCOMMAND("log", &fn, cmd_bisect__log),
1420 OPT_SUBCOMMAND("replay", &fn, cmd_bisect__replay),
1421 OPT_SUBCOMMAND("skip", &fn, cmd_bisect__skip),
1422 OPT_SUBCOMMAND("visualize", &fn, cmd_bisect__visualize),
1423 OPT_SUBCOMMAND("view", &fn, cmd_bisect__visualize),
1424 OPT_SUBCOMMAND("run", &fn, cmd_bisect__run),
1bf072e3
CC
1425 OPT_END()
1426 };
df63421b
JS
1427 argc = parse_options(argc, argv, prefix, options, git_bisect_usage,
1428 PARSE_OPT_SUBCOMMAND_OPTIONAL);
1429
1430 if (!fn) {
1431 struct bisect_terms terms = { 0 };
1432
1433 if (!argc)
1434 usage_msg_opt(_("need a command"), git_bisect_usage, options);
1435
1436 set_terms(&terms, "bad", "good");
1437 get_terms(&terms);
1438 if (check_and_set_terms(&terms, argv[0]))
1439 usage_msg_optf(_("unknown command: '%s'"), git_bisect_usage,
1440 options, argv[0]);
6f977922 1441 res = bisect_state(&terms, argc, argv);
df63421b
JS
1442 free_terms(&terms);
1443 } else {
1444 argc--;
1445 argv++;
1446 res = fn(argc, argv, prefix);
1447 }
45b63708 1448
7a8d7aaa 1449 return is_bisect_success(res) ? 0 : -res;
1bf072e3 1450}