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