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