bisect--helper: retire `--bisect-clean-state` subcommand
[git] / builtin / bisect--helper.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "parse-options.h"
4 #include "bisect.h"
5 #include "refs.h"
6 #include "dir.h"
7 #include "strvec.h"
8 #include "run-command.h"
9 #include "prompt.h"
10 #include "quote.h"
11 #include "revision.h"
12
13 static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
14 static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
15 static GIT_PATH_FUNC(git_path_bisect_ancestors_ok, "BISECT_ANCESTORS_OK")
16 static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START")
17 static GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG")
18 static GIT_PATH_FUNC(git_path_head_name, "head-name")
19 static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES")
20 static GIT_PATH_FUNC(git_path_bisect_first_parent, "BISECT_FIRST_PARENT")
21
22 static const char * const git_bisect_helper_usage[] = {
23         N_("git bisect--helper --next-all"),
24         N_("git bisect--helper --write-terms <bad_term> <good_term>"),
25         N_("git bisect--helper --bisect-reset [<commit>]"),
26         N_("git bisect--helper --bisect-write [--no-log] <state> <revision> <good_term> <bad_term>"),
27         N_("git bisect--helper --bisect-check-and-set-terms <command> <good_term> <bad_term>"),
28         N_("git bisect--helper --bisect-next-check <good_term> <bad_term> [<term>]"),
29         N_("git bisect--helper --bisect-terms [--term-good | --term-old | --term-bad | --term-new]"),
30         N_("git bisect--helper --bisect-start [--term-{old,good}=<term> --term-{new,bad}=<term>]"
31                                             " [--no-checkout] [--first-parent] [<bad> [<good>...]] [--] [<paths>...]"),
32         N_("git bisect--helper --bisect-next"),
33         N_("git bisect--helper --bisect-auto-next"),
34         N_("git bisect--helper --bisect-autostart"),
35         NULL
36 };
37
38 struct add_bisect_ref_data {
39         struct rev_info *revs;
40         unsigned int object_flags;
41 };
42
43 struct bisect_terms {
44         char *term_good;
45         char *term_bad;
46 };
47
48 static void free_terms(struct bisect_terms *terms)
49 {
50         FREE_AND_NULL(terms->term_good);
51         FREE_AND_NULL(terms->term_bad);
52 }
53
54 static 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
63 static const char vocab_bad[] = "bad|new";
64 static const char vocab_good[] = "good|old";
65
66 static int bisect_autostart(struct bisect_terms *terms);
67
68 /*
69  * Check whether the string `term` belongs to the set of strings
70  * included in the variable arguments.
71  */
72 LAST_ARG_MUST_BE_NULL
73 static 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
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
93 static 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
100 static 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
105         if (strcmp(mode, "w") && strcmp(mode, "a"))
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
122 static int write_to_file(const char *path, const char *format, ...)
123 {
124         int res;
125         va_list args;
126
127         va_start(args, format);
128         res = write_in_file(path, "w", format, args);
129         va_end(args);
130
131         return res;
132 }
133
134 static int append_to_file(const char *path, const char *format, ...)
135 {
136         int res;
137         va_list args;
138
139         va_start(args, format);
140         res = write_in_file(path, "a", format, args);
141         va_end(args);
142
143         return res;
144 }
145
146 static int check_term_format(const char *term, const char *orig_term)
147 {
148         int res;
149         char *new_term = xstrfmt("refs/bisect/%s", term);
150
151         res = check_refname_format(new_term, 0);
152         free(new_term);
153
154         if (res)
155                 return error(_("'%s' is not a valid term"), term);
156
157         if (one_of(term, "help", "start", "skip", "next", "reset",
158                         "visualize", "view", "replay", "log", "run", "terms", NULL))
159                 return error(_("can't use the builtin command '%s' as a term"), term);
160
161         /*
162          * In theory, nothing prevents swapping completely good and bad,
163          * but this situation could be confusing and hasn't been tested
164          * enough. Forbid it for now.
165          */
166
167         if ((strcmp(orig_term, "bad") && one_of(term, "bad", "new", NULL)) ||
168                  (strcmp(orig_term, "good") && one_of(term, "good", "old", NULL)))
169                 return error(_("can't change the meaning of the term '%s'"), term);
170
171         return 0;
172 }
173
174 static int write_terms(const char *bad, const char *good)
175 {
176         int res;
177
178         if (!strcmp(bad, good))
179                 return error(_("please use two different terms"));
180
181         if (check_term_format(bad, "bad") || check_term_format(good, "good"))
182                 return -1;
183
184         res = write_to_file(git_path_bisect_terms(), "%s\n%s\n", bad, good);
185
186         return res;
187 }
188
189 static int is_expected_rev(const char *expected_hex)
190 {
191         struct strbuf actual_hex = STRBUF_INIT;
192         int res = 0;
193         if (strbuf_read_file(&actual_hex, git_path_bisect_expected_rev(), 0) >= 40) {
194                 strbuf_trim(&actual_hex);
195                 res = !strcmp(actual_hex.buf, expected_hex);
196         }
197         strbuf_release(&actual_hex);
198         return res;
199 }
200
201 static void check_expected_revs(const char **revs, int rev_nr)
202 {
203         int i;
204
205         for (i = 0; i < rev_nr; i++) {
206                 if (!is_expected_rev(revs[i])) {
207                         unlink_or_warn(git_path_bisect_ancestors_ok());
208                         unlink_or_warn(git_path_bisect_expected_rev());
209                 }
210         }
211 }
212
213 static int bisect_reset(const char *commit)
214 {
215         struct strbuf branch = STRBUF_INIT;
216
217         if (!commit) {
218                 if (strbuf_read_file(&branch, git_path_bisect_start(), 0) < 1) {
219                         printf(_("We are not bisecting.\n"));
220                         return 0;
221                 }
222                 strbuf_rtrim(&branch);
223         } else {
224                 struct object_id oid;
225
226                 if (get_oid_commit(commit, &oid))
227                         return error(_("'%s' is not a valid commit"), commit);
228                 strbuf_addstr(&branch, commit);
229         }
230
231         if (!ref_exists("BISECT_HEAD")) {
232                 struct strvec argv = STRVEC_INIT;
233
234                 strvec_pushl(&argv, "checkout", branch.buf, "--", NULL);
235                 if (run_command_v_opt(argv.v, RUN_GIT_CMD)) {
236                         error(_("could not check out original"
237                                 " HEAD '%s'. Try 'git bisect"
238                                 " reset <commit>'."), branch.buf);
239                         strbuf_release(&branch);
240                         strvec_clear(&argv);
241                         return -1;
242                 }
243                 strvec_clear(&argv);
244         }
245
246         strbuf_release(&branch);
247         return bisect_clean_state();
248 }
249
250 static void log_commit(FILE *fp, char *fmt, const char *state,
251                        struct commit *commit)
252 {
253         struct pretty_print_context pp = {0};
254         struct strbuf commit_msg = STRBUF_INIT;
255         char *label = xstrfmt(fmt, state);
256
257         format_commit_message(commit, "%s", &commit_msg, &pp);
258
259         fprintf(fp, "# %s: [%s] %s\n", label, oid_to_hex(&commit->object.oid),
260                 commit_msg.buf);
261
262         strbuf_release(&commit_msg);
263         free(label);
264 }
265
266 static int bisect_write(const char *state, const char *rev,
267                         const struct bisect_terms *terms, int nolog)
268 {
269         struct strbuf tag = STRBUF_INIT;
270         struct object_id oid;
271         struct commit *commit;
272         FILE *fp = NULL;
273         int res = 0;
274
275         if (!strcmp(state, terms->term_bad)) {
276                 strbuf_addf(&tag, "refs/bisect/%s", state);
277         } else if (one_of(state, terms->term_good, "skip", NULL)) {
278                 strbuf_addf(&tag, "refs/bisect/%s-%s", state, rev);
279         } else {
280                 res = error(_("Bad bisect_write argument: %s"), state);
281                 goto finish;
282         }
283
284         if (get_oid(rev, &oid)) {
285                 res = error(_("couldn't get the oid of the rev '%s'"), rev);
286                 goto finish;
287         }
288
289         if (update_ref(NULL, tag.buf, &oid, NULL, 0,
290                        UPDATE_REFS_MSG_ON_ERR)) {
291                 res = -1;
292                 goto finish;
293         }
294
295         fp = fopen(git_path_bisect_log(), "a");
296         if (!fp) {
297                 res = error_errno(_("couldn't open the file '%s'"), git_path_bisect_log());
298                 goto finish;
299         }
300
301         commit = lookup_commit_reference(the_repository, &oid);
302         log_commit(fp, "%s", state, commit);
303
304         if (!nolog)
305                 fprintf(fp, "git bisect %s %s\n", state, rev);
306
307 finish:
308         if (fp)
309                 fclose(fp);
310         strbuf_release(&tag);
311         return res;
312 }
313
314 static int check_and_set_terms(struct bisect_terms *terms, const char *cmd)
315 {
316         int has_term_file = !is_empty_or_missing_file(git_path_bisect_terms());
317
318         if (one_of(cmd, "skip", "start", "terms", NULL))
319                 return 0;
320
321         if (has_term_file && strcmp(cmd, terms->term_bad) &&
322             strcmp(cmd, terms->term_good))
323                 return error(_("Invalid command: you're currently in a "
324                                 "%s/%s bisect"), terms->term_bad,
325                                 terms->term_good);
326
327         if (!has_term_file) {
328                 if (one_of(cmd, "bad", "good", NULL)) {
329                         set_terms(terms, "bad", "good");
330                         return write_terms(terms->term_bad, terms->term_good);
331                 }
332                 if (one_of(cmd, "new", "old", NULL)) {
333                         set_terms(terms, "new", "old");
334                         return write_terms(terms->term_bad, terms->term_good);
335                 }
336         }
337
338         return 0;
339 }
340
341 static int mark_good(const char *refname, const struct object_id *oid,
342                      int flag, void *cb_data)
343 {
344         int *m_good = (int *)cb_data;
345         *m_good = 0;
346         return 1;
347 }
348
349 static const char need_bad_and_good_revision_warning[] =
350         N_("You need to give me at least one %s and %s revision.\n"
351            "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
352
353 static const char need_bisect_start_warning[] =
354         N_("You need to start by \"git bisect start\".\n"
355            "You then need to give me at least one %s and %s revision.\n"
356            "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
357
358 static int decide_next(const struct bisect_terms *terms,
359                        const char *current_term, int missing_good,
360                        int missing_bad)
361 {
362         if (!missing_good && !missing_bad)
363                 return 0;
364         if (!current_term)
365                 return -1;
366
367         if (missing_good && !missing_bad &&
368             !strcmp(current_term, terms->term_good)) {
369                 char *yesno;
370                 /*
371                  * have bad (or new) but not good (or old). We could bisect
372                  * although this is less optimum.
373                  */
374                 warning(_("bisecting only with a %s commit"), terms->term_bad);
375                 if (!isatty(0))
376                         return 0;
377                 /*
378                  * TRANSLATORS: Make sure to include [Y] and [n] in your
379                  * translation. The program will only accept English input
380                  * at this point.
381                  */
382                 yesno = git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO);
383                 if (starts_with(yesno, "N") || starts_with(yesno, "n"))
384                         return -1;
385                 return 0;
386         }
387
388         if (!is_empty_or_missing_file(git_path_bisect_start()))
389                 return error(_(need_bad_and_good_revision_warning),
390                              vocab_bad, vocab_good, vocab_bad, vocab_good);
391         else
392                 return error(_(need_bisect_start_warning),
393                              vocab_good, vocab_bad, vocab_good, vocab_bad);
394 }
395
396 static int bisect_next_check(const struct bisect_terms *terms,
397                              const char *current_term)
398 {
399         int missing_good = 1, missing_bad = 1;
400         char *bad_ref = xstrfmt("refs/bisect/%s", terms->term_bad);
401         char *good_glob = xstrfmt("%s-*", terms->term_good);
402
403         if (ref_exists(bad_ref))
404                 missing_bad = 0;
405
406         for_each_glob_ref_in(mark_good, good_glob, "refs/bisect/",
407                              (void *) &missing_good);
408
409         free(good_glob);
410         free(bad_ref);
411
412         return decide_next(terms, current_term, missing_good, missing_bad);
413 }
414
415 static int get_terms(struct bisect_terms *terms)
416 {
417         struct strbuf str = STRBUF_INIT;
418         FILE *fp = NULL;
419         int res = 0;
420
421         fp = fopen(git_path_bisect_terms(), "r");
422         if (!fp) {
423                 res = -1;
424                 goto finish;
425         }
426
427         free_terms(terms);
428         strbuf_getline_lf(&str, fp);
429         terms->term_bad = strbuf_detach(&str, NULL);
430         strbuf_getline_lf(&str, fp);
431         terms->term_good = strbuf_detach(&str, NULL);
432
433 finish:
434         if (fp)
435                 fclose(fp);
436         strbuf_release(&str);
437         return res;
438 }
439
440 static int bisect_terms(struct bisect_terms *terms, const char *option)
441 {
442         if (get_terms(terms))
443                 return error(_("no terms defined"));
444
445         if (option == NULL) {
446                 printf(_("Your current terms are %s for the old state\n"
447                          "and %s for the new state.\n"),
448                        terms->term_good, terms->term_bad);
449                 return 0;
450         }
451         if (one_of(option, "--term-good", "--term-old", NULL))
452                 printf("%s\n", terms->term_good);
453         else if (one_of(option, "--term-bad", "--term-new", NULL))
454                 printf("%s\n", terms->term_bad);
455         else
456                 return error(_("invalid argument %s for 'git bisect terms'.\n"
457                                "Supported options are: "
458                                "--term-good|--term-old and "
459                                "--term-bad|--term-new."), option);
460
461         return 0;
462 }
463
464 static int bisect_append_log_quoted(const char **argv)
465 {
466         int res = 0;
467         FILE *fp = fopen(git_path_bisect_log(), "a");
468         struct strbuf orig_args = STRBUF_INIT;
469
470         if (!fp)
471                 return -1;
472
473         if (fprintf(fp, "git bisect start") < 1) {
474                 res = -1;
475                 goto finish;
476         }
477
478         sq_quote_argv(&orig_args, argv);
479         if (fprintf(fp, "%s\n", orig_args.buf) < 1)
480                 res = -1;
481
482 finish:
483         fclose(fp);
484         strbuf_release(&orig_args);
485         return res;
486 }
487
488 static int add_bisect_ref(const char *refname, const struct object_id *oid,
489                           int flags, void *cb)
490 {
491         struct add_bisect_ref_data *data = cb;
492
493         add_pending_oid(data->revs, refname, oid, data->object_flags);
494
495         return 0;
496 }
497
498 static int prepare_revs(struct bisect_terms *terms, struct rev_info *revs)
499 {
500         int res = 0;
501         struct add_bisect_ref_data cb = { revs };
502         char *good = xstrfmt("%s-*", terms->term_good);
503
504         /*
505          * We cannot use terms->term_bad directly in
506          * for_each_glob_ref_in() and we have to append a '*' to it,
507          * otherwise for_each_glob_ref_in() will append '/' and '*'.
508          */
509         char *bad = xstrfmt("%s*", terms->term_bad);
510
511         /*
512          * It is important to reset the flags used by revision walks
513          * as the previous call to bisect_next_all() in turn
514          * sets up a revision walk.
515          */
516         reset_revision_walk();
517         init_revisions(revs, NULL);
518         setup_revisions(0, NULL, revs, NULL);
519         for_each_glob_ref_in(add_bisect_ref, bad, "refs/bisect/", &cb);
520         cb.object_flags = UNINTERESTING;
521         for_each_glob_ref_in(add_bisect_ref, good, "refs/bisect/", &cb);
522         if (prepare_revision_walk(revs))
523                 res = error(_("revision walk setup failed\n"));
524
525         free(good);
526         free(bad);
527         return res;
528 }
529
530 static int bisect_skipped_commits(struct bisect_terms *terms)
531 {
532         int res;
533         FILE *fp = NULL;
534         struct rev_info revs;
535         struct commit *commit;
536         struct pretty_print_context pp = {0};
537         struct strbuf commit_name = STRBUF_INIT;
538
539         res = prepare_revs(terms, &revs);
540         if (res)
541                 return res;
542
543         fp = fopen(git_path_bisect_log(), "a");
544         if (!fp)
545                 return error_errno(_("could not open '%s' for appending"),
546                                   git_path_bisect_log());
547
548         if (fprintf(fp, "# only skipped commits left to test\n") < 0)
549                 return error_errno(_("failed to write to '%s'"), git_path_bisect_log());
550
551         while ((commit = get_revision(&revs)) != NULL) {
552                 strbuf_reset(&commit_name);
553                 format_commit_message(commit, "%s",
554                                       &commit_name, &pp);
555                 fprintf(fp, "# possible first %s commit: [%s] %s\n",
556                         terms->term_bad, oid_to_hex(&commit->object.oid),
557                         commit_name.buf);
558         }
559
560         /*
561          * Reset the flags used by revision walks in case
562          * there is another revision walk after this one.
563          */
564         reset_revision_walk();
565
566         strbuf_release(&commit_name);
567         fclose(fp);
568         return 0;
569 }
570
571 static int bisect_successful(struct bisect_terms *terms)
572 {
573         struct object_id oid;
574         struct commit *commit;
575         struct pretty_print_context pp = {0};
576         struct strbuf commit_name = STRBUF_INIT;
577         char *bad_ref = xstrfmt("refs/bisect/%s",terms->term_bad);
578         int res;
579
580         read_ref(bad_ref, &oid);
581         commit = lookup_commit_reference_by_name(bad_ref);
582         format_commit_message(commit, "%s", &commit_name, &pp);
583
584         res = append_to_file(git_path_bisect_log(), "# first %s commit: [%s] %s\n",
585                             terms->term_bad, oid_to_hex(&commit->object.oid),
586                             commit_name.buf);
587
588         strbuf_release(&commit_name);
589         free(bad_ref);
590         return res;
591 }
592
593 static enum bisect_error bisect_next(struct bisect_terms *terms, const char *prefix)
594 {
595         enum bisect_error res;
596
597         if (bisect_autostart(terms))
598                 return BISECT_FAILED;
599
600         if (bisect_next_check(terms, terms->term_good))
601                 return BISECT_FAILED;
602
603         /* Perform all bisection computation */
604         res = bisect_next_all(the_repository, prefix);
605
606         if (res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND) {
607                 res = bisect_successful(terms);
608                 return res ? res : BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND;
609         } else if (res == BISECT_ONLY_SKIPPED_LEFT) {
610                 res = bisect_skipped_commits(terms);
611                 return res ? res : BISECT_ONLY_SKIPPED_LEFT;
612         }
613         return res;
614 }
615
616 static enum bisect_error bisect_auto_next(struct bisect_terms *terms, const char *prefix)
617 {
618         if (bisect_next_check(terms, NULL))
619                 return BISECT_OK;
620
621         return bisect_next(terms, prefix);
622 }
623
624 static enum bisect_error bisect_start(struct bisect_terms *terms, const char **argv, int argc)
625 {
626         int no_checkout = 0;
627         int first_parent_only = 0;
628         int i, has_double_dash = 0, must_write_terms = 0, bad_seen = 0;
629         int flags, pathspec_pos;
630         enum bisect_error res = BISECT_OK;
631         struct string_list revs = STRING_LIST_INIT_DUP;
632         struct string_list states = STRING_LIST_INIT_DUP;
633         struct strbuf start_head = STRBUF_INIT;
634         struct strbuf bisect_names = STRBUF_INIT;
635         struct object_id head_oid;
636         struct object_id oid;
637         const char *head;
638
639         if (is_bare_repository())
640                 no_checkout = 1;
641
642         /*
643          * Check for one bad and then some good revisions
644          */
645         for (i = 0; i < argc; i++) {
646                 if (!strcmp(argv[i], "--")) {
647                         has_double_dash = 1;
648                         break;
649                 }
650         }
651
652         for (i = 0; i < argc; i++) {
653                 const char *arg = argv[i];
654                 if (!strcmp(argv[i], "--")) {
655                         break;
656                 } else if (!strcmp(arg, "--no-checkout")) {
657                         no_checkout = 1;
658                 } else if (!strcmp(arg, "--first-parent")) {
659                         first_parent_only = 1;
660                 } else if (!strcmp(arg, "--term-good") ||
661                          !strcmp(arg, "--term-old")) {
662                         i++;
663                         if (argc <= i)
664                                 return error(_("'' is not a valid term"));
665                         must_write_terms = 1;
666                         free((void *) terms->term_good);
667                         terms->term_good = xstrdup(argv[i]);
668                 } else if (skip_prefix(arg, "--term-good=", &arg) ||
669                            skip_prefix(arg, "--term-old=", &arg)) {
670                         must_write_terms = 1;
671                         free((void *) terms->term_good);
672                         terms->term_good = xstrdup(arg);
673                 } else if (!strcmp(arg, "--term-bad") ||
674                          !strcmp(arg, "--term-new")) {
675                         i++;
676                         if (argc <= i)
677                                 return error(_("'' is not a valid term"));
678                         must_write_terms = 1;
679                         free((void *) terms->term_bad);
680                         terms->term_bad = xstrdup(argv[i]);
681                 } else if (skip_prefix(arg, "--term-bad=", &arg) ||
682                            skip_prefix(arg, "--term-new=", &arg)) {
683                         must_write_terms = 1;
684                         free((void *) terms->term_bad);
685                         terms->term_bad = xstrdup(arg);
686                 } else if (starts_with(arg, "--")) {
687                         return error(_("unrecognized option: '%s'"), arg);
688                 } else {
689                         char *commit_id = xstrfmt("%s^{commit}", arg);
690                         if (get_oid(commit_id, &oid) && has_double_dash)
691                                 die(_("'%s' does not appear to be a valid "
692                                       "revision"), arg);
693
694                         string_list_append(&revs, oid_to_hex(&oid));
695                         free(commit_id);
696                 }
697         }
698         pathspec_pos = i;
699
700         /*
701          * The user ran "git bisect start <sha1> <sha1>", hence did not
702          * explicitly specify the terms, but we are already starting to
703          * set references named with the default terms, and won't be able
704          * to change afterwards.
705          */
706         if (revs.nr)
707                 must_write_terms = 1;
708         for (i = 0; i < revs.nr; i++) {
709                 if (bad_seen) {
710                         string_list_append(&states, terms->term_good);
711                 } else {
712                         bad_seen = 1;
713                         string_list_append(&states, terms->term_bad);
714                 }
715         }
716
717         /*
718          * Verify HEAD
719          */
720         head = resolve_ref_unsafe("HEAD", 0, &head_oid, &flags);
721         if (!head)
722                 if (get_oid("HEAD", &head_oid))
723                         return error(_("bad HEAD - I need a HEAD"));
724
725         /*
726          * Check if we are bisecting
727          */
728         if (!is_empty_or_missing_file(git_path_bisect_start())) {
729                 /* Reset to the rev from where we started */
730                 strbuf_read_file(&start_head, git_path_bisect_start(), 0);
731                 strbuf_trim(&start_head);
732                 if (!no_checkout) {
733                         struct strvec argv = STRVEC_INIT;
734
735                         strvec_pushl(&argv, "checkout", start_head.buf,
736                                      "--", NULL);
737                         if (run_command_v_opt(argv.v, RUN_GIT_CMD)) {
738                                 res = error(_("checking out '%s' failed."
739                                                  " Try 'git bisect start "
740                                                  "<valid-branch>'."),
741                                                start_head.buf);
742                                 goto finish;
743                         }
744                 }
745         } else {
746                 /* Get the rev from where we start. */
747                 if (!get_oid(head, &head_oid) &&
748                     !starts_with(head, "refs/heads/")) {
749                         strbuf_reset(&start_head);
750                         strbuf_addstr(&start_head, oid_to_hex(&head_oid));
751                 } else if (!get_oid(head, &head_oid) &&
752                            skip_prefix(head, "refs/heads/", &head)) {
753                         /*
754                          * This error message should only be triggered by
755                          * cogito usage, and cogito users should understand
756                          * it relates to cg-seek.
757                          */
758                         if (!is_empty_or_missing_file(git_path_head_name()))
759                                 return error(_("won't bisect on cg-seek'ed tree"));
760                         strbuf_addstr(&start_head, head);
761                 } else {
762                         return error(_("bad HEAD - strange symbolic ref"));
763                 }
764         }
765
766         /*
767          * Get rid of any old bisect state.
768          */
769         if (bisect_clean_state())
770                 return BISECT_FAILED;
771
772         /*
773          * Write new start state
774          */
775         write_file(git_path_bisect_start(), "%s\n", start_head.buf);
776
777         if (first_parent_only)
778                 write_file(git_path_bisect_first_parent(), "\n");
779
780         if (no_checkout) {
781                 if (get_oid(start_head.buf, &oid) < 0) {
782                         res = error(_("invalid ref: '%s'"), start_head.buf);
783                         goto finish;
784                 }
785                 if (update_ref(NULL, "BISECT_HEAD", &oid, NULL, 0,
786                                UPDATE_REFS_MSG_ON_ERR)) {
787                         res = BISECT_FAILED;
788                         goto finish;
789                 }
790         }
791
792         if (pathspec_pos < argc - 1)
793                 sq_quote_argv(&bisect_names, argv + pathspec_pos);
794         write_file(git_path_bisect_names(), "%s\n", bisect_names.buf);
795
796         for (i = 0; i < states.nr; i++)
797                 if (bisect_write(states.items[i].string,
798                                  revs.items[i].string, terms, 1)) {
799                         res = BISECT_FAILED;
800                         goto finish;
801                 }
802
803         if (must_write_terms && write_terms(terms->term_bad,
804                                             terms->term_good)) {
805                 res = BISECT_FAILED;
806                 goto finish;
807         }
808
809         res = bisect_append_log_quoted(argv);
810         if (res)
811                 res = BISECT_FAILED;
812
813 finish:
814         string_list_clear(&revs, 0);
815         string_list_clear(&states, 0);
816         strbuf_release(&start_head);
817         strbuf_release(&bisect_names);
818         if (res)
819                 return res;
820
821         res = bisect_auto_next(terms, NULL);
822         if (!is_bisect_success(res))
823                 bisect_clean_state();
824         return res;
825 }
826
827 static inline int file_is_not_empty(const char *path)
828 {
829         return !is_empty_or_missing_file(path);
830 }
831
832 static int bisect_autostart(struct bisect_terms *terms)
833 {
834         int res;
835         const char *yesno;
836
837         if (file_is_not_empty(git_path_bisect_start()))
838                 return 0;
839
840         fprintf_ln(stderr, _("You need to start by \"git bisect "
841                           "start\"\n"));
842
843         if (!isatty(STDIN_FILENO))
844                 return -1;
845
846         /*
847          * TRANSLATORS: Make sure to include [Y] and [n] in your
848          * translation. The program will only accept English input
849          * at this point.
850          */
851         yesno = git_prompt(_("Do you want me to do it for you "
852                              "[Y/n]? "), PROMPT_ECHO);
853         res = tolower(*yesno) == 'n' ?
854                 -1 : bisect_start(terms, empty_strvec, 0);
855
856         return res;
857 }
858
859 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
860 {
861         enum {
862                 NEXT_ALL = 1,
863                 WRITE_TERMS,
864                 CHECK_EXPECTED_REVS,
865                 BISECT_RESET,
866                 BISECT_WRITE,
867                 CHECK_AND_SET_TERMS,
868                 BISECT_NEXT_CHECK,
869                 BISECT_TERMS,
870                 BISECT_START,
871                 BISECT_AUTOSTART,
872                 BISECT_NEXT,
873                 BISECT_AUTO_NEXT
874         } cmdmode = 0;
875         int res = 0, nolog = 0;
876         struct option options[] = {
877                 OPT_CMDMODE(0, "next-all", &cmdmode,
878                          N_("perform 'git bisect next'"), NEXT_ALL),
879                 OPT_CMDMODE(0, "write-terms", &cmdmode,
880                          N_("write the terms to .git/BISECT_TERMS"), WRITE_TERMS),
881                 OPT_CMDMODE(0, "check-expected-revs", &cmdmode,
882                          N_("check for expected revs"), CHECK_EXPECTED_REVS),
883                 OPT_CMDMODE(0, "bisect-reset", &cmdmode,
884                          N_("reset the bisection state"), BISECT_RESET),
885                 OPT_CMDMODE(0, "bisect-write", &cmdmode,
886                          N_("write out the bisection state in BISECT_LOG"), BISECT_WRITE),
887                 OPT_CMDMODE(0, "check-and-set-terms", &cmdmode,
888                          N_("check and set terms in a bisection state"), CHECK_AND_SET_TERMS),
889                 OPT_CMDMODE(0, "bisect-next-check", &cmdmode,
890                          N_("check whether bad or good terms exist"), BISECT_NEXT_CHECK),
891                 OPT_CMDMODE(0, "bisect-terms", &cmdmode,
892                          N_("print out the bisect terms"), BISECT_TERMS),
893                 OPT_CMDMODE(0, "bisect-start", &cmdmode,
894                          N_("start the bisect session"), BISECT_START),
895                 OPT_CMDMODE(0, "bisect-next", &cmdmode,
896                          N_("find the next bisection commit"), BISECT_NEXT),
897                 OPT_CMDMODE(0, "bisect-auto-next", &cmdmode,
898                          N_("verify the next bisection state then checkout the next bisection commit"), BISECT_AUTO_NEXT),
899                 OPT_CMDMODE(0, "bisect-autostart", &cmdmode,
900                          N_("start the bisection if it has not yet been started"), BISECT_AUTOSTART),
901                 OPT_BOOL(0, "no-log", &nolog,
902                          N_("no log for BISECT_WRITE")),
903                 OPT_END()
904         };
905         struct bisect_terms terms = { .term_good = NULL, .term_bad = NULL };
906
907         argc = parse_options(argc, argv, prefix, options,
908                              git_bisect_helper_usage,
909                              PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_UNKNOWN);
910
911         if (!cmdmode)
912                 usage_with_options(git_bisect_helper_usage, options);
913
914         switch (cmdmode) {
915         case NEXT_ALL:
916                 res = bisect_next_all(the_repository, prefix);
917                 break;
918         case WRITE_TERMS:
919                 if (argc != 2)
920                         return error(_("--write-terms requires two arguments"));
921                 return write_terms(argv[0], argv[1]);
922         case CHECK_EXPECTED_REVS:
923                 check_expected_revs(argv, argc);
924                 return 0;
925         case BISECT_RESET:
926                 if (argc > 1)
927                         return error(_("--bisect-reset requires either no argument or a commit"));
928                 return !!bisect_reset(argc ? argv[0] : NULL);
929         case BISECT_WRITE:
930                 if (argc != 4 && argc != 5)
931                         return error(_("--bisect-write requires either 4 or 5 arguments"));
932                 set_terms(&terms, argv[3], argv[2]);
933                 res = bisect_write(argv[0], argv[1], &terms, nolog);
934                 break;
935         case CHECK_AND_SET_TERMS:
936                 if (argc != 3)
937                         return error(_("--check-and-set-terms requires 3 arguments"));
938                 set_terms(&terms, argv[2], argv[1]);
939                 res = check_and_set_terms(&terms, argv[0]);
940                 break;
941         case BISECT_NEXT_CHECK:
942                 if (argc != 2 && argc != 3)
943                         return error(_("--bisect-next-check requires 2 or 3 arguments"));
944                 set_terms(&terms, argv[1], argv[0]);
945                 res = bisect_next_check(&terms, argc == 3 ? argv[2] : NULL);
946                 break;
947         case BISECT_TERMS:
948                 if (argc > 1)
949                         return error(_("--bisect-terms requires 0 or 1 argument"));
950                 res = bisect_terms(&terms, argc == 1 ? argv[0] : NULL);
951                 break;
952         case BISECT_START:
953                 set_terms(&terms, "bad", "good");
954                 res = bisect_start(&terms, argv, argc);
955                 break;
956         case BISECT_NEXT:
957                 if (argc)
958                         return error(_("--bisect-next requires 0 arguments"));
959                 get_terms(&terms);
960                 res = bisect_next(&terms, prefix);
961                 break;
962         case BISECT_AUTO_NEXT:
963                 if (argc)
964                         return error(_("--bisect-auto-next requires 0 arguments"));
965                 get_terms(&terms);
966                 res = bisect_auto_next(&terms, prefix);
967                 break;
968         case BISECT_AUTOSTART:
969                 if (argc)
970                         return error(_("--bisect-autostart does not accept arguments"));
971                 set_terms(&terms, "bad", "good");
972                 res = bisect_autostart(&terms);
973                 break;
974         default:
975                 BUG("unknown subcommand %d", cmdmode);
976         }
977         free_terms(&terms);
978
979         /*
980          * Handle early success
981          * From check_merge_bases > check_good_are_ancestors_of_bad > bisect_next_all
982          */
983         if ((res == BISECT_INTERNAL_SUCCESS_MERGE_BASE) || (res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND))
984                 res = BISECT_OK;
985
986         return -res;
987 }