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