Merge branch 'bc/push-cas-cquoted-refname' into master
[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 "argv-array.h"
8 #include "run-command.h"
9 #include "prompt.h"
10 #include "quote.h"
11
12 static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
13 static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
14 static GIT_PATH_FUNC(git_path_bisect_ancestors_ok, "BISECT_ANCESTORS_OK")
15 static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START")
16 static GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG")
17 static GIT_PATH_FUNC(git_path_head_name, "head-name")
18 static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES")
19
20 static const char * const git_bisect_helper_usage[] = {
21         N_("git bisect--helper --next-all [--no-checkout]"),
22         N_("git bisect--helper --write-terms <bad_term> <good_term>"),
23         N_("git bisect--helper --bisect-clean-state"),
24         N_("git bisect--helper --bisect-reset [<commit>]"),
25         N_("git bisect--helper --bisect-write [--no-log] <state> <revision> <good_term> <bad_term>"),
26         N_("git bisect--helper --bisect-check-and-set-terms <command> <good_term> <bad_term>"),
27         N_("git bisect--helper --bisect-next-check <good_term> <bad_term> [<term>]"),
28         N_("git bisect--helper --bisect-terms [--term-good | --term-old | --term-bad | --term-new]"),
29         N_("git bisect--helper --bisect-start [--term-{old,good}=<term> --term-{new,bad}=<term>]"
30                                              "[--no-checkout] [<bad> [<good>...]] [--] [<paths>...]"),
31         NULL
32 };
33
34 struct bisect_terms {
35         char *term_good;
36         char *term_bad;
37 };
38
39 static void free_terms(struct bisect_terms *terms)
40 {
41         FREE_AND_NULL(terms->term_good);
42         FREE_AND_NULL(terms->term_bad);
43 }
44
45 static void set_terms(struct bisect_terms *terms, const char *bad,
46                       const char *good)
47 {
48         free((void *)terms->term_good);
49         terms->term_good = xstrdup(good);
50         free((void *)terms->term_bad);
51         terms->term_bad = xstrdup(bad);
52 }
53
54 static const char vocab_bad[] = "bad|new";
55 static const char vocab_good[] = "good|old";
56
57 /*
58  * Check whether the string `term` belongs to the set of strings
59  * included in the variable arguments.
60  */
61 LAST_ARG_MUST_BE_NULL
62 static int one_of(const char *term, ...)
63 {
64         int res = 0;
65         va_list matches;
66         const char *match;
67
68         va_start(matches, term);
69         while (!res && (match = va_arg(matches, const char *)))
70                 res = !strcmp(term, match);
71         va_end(matches);
72
73         return res;
74 }
75
76 static int check_term_format(const char *term, const char *orig_term)
77 {
78         int res;
79         char *new_term = xstrfmt("refs/bisect/%s", term);
80
81         res = check_refname_format(new_term, 0);
82         free(new_term);
83
84         if (res)
85                 return error(_("'%s' is not a valid term"), term);
86
87         if (one_of(term, "help", "start", "skip", "next", "reset",
88                         "visualize", "view", "replay", "log", "run", "terms", NULL))
89                 return error(_("can't use the builtin command '%s' as a term"), term);
90
91         /*
92          * In theory, nothing prevents swapping completely good and bad,
93          * but this situation could be confusing and hasn't been tested
94          * enough. Forbid it for now.
95          */
96
97         if ((strcmp(orig_term, "bad") && one_of(term, "bad", "new", NULL)) ||
98                  (strcmp(orig_term, "good") && one_of(term, "good", "old", NULL)))
99                 return error(_("can't change the meaning of the term '%s'"), term);
100
101         return 0;
102 }
103
104 static int write_terms(const char *bad, const char *good)
105 {
106         FILE *fp = NULL;
107         int res;
108
109         if (!strcmp(bad, good))
110                 return error(_("please use two different terms"));
111
112         if (check_term_format(bad, "bad") || check_term_format(good, "good"))
113                 return -1;
114
115         fp = fopen(git_path_bisect_terms(), "w");
116         if (!fp)
117                 return error_errno(_("could not open the file BISECT_TERMS"));
118
119         res = fprintf(fp, "%s\n%s\n", bad, good);
120         res |= fclose(fp);
121         return (res < 0) ? -1 : 0;
122 }
123
124 static int is_expected_rev(const char *expected_hex)
125 {
126         struct strbuf actual_hex = STRBUF_INIT;
127         int res = 0;
128         if (strbuf_read_file(&actual_hex, git_path_bisect_expected_rev(), 0) >= 40) {
129                 strbuf_trim(&actual_hex);
130                 res = !strcmp(actual_hex.buf, expected_hex);
131         }
132         strbuf_release(&actual_hex);
133         return res;
134 }
135
136 static void check_expected_revs(const char **revs, int rev_nr)
137 {
138         int i;
139
140         for (i = 0; i < rev_nr; i++) {
141                 if (!is_expected_rev(revs[i])) {
142                         unlink_or_warn(git_path_bisect_ancestors_ok());
143                         unlink_or_warn(git_path_bisect_expected_rev());
144                 }
145         }
146 }
147
148 static int bisect_reset(const char *commit)
149 {
150         struct strbuf branch = STRBUF_INIT;
151
152         if (!commit) {
153                 if (strbuf_read_file(&branch, git_path_bisect_start(), 0) < 1) {
154                         printf(_("We are not bisecting.\n"));
155                         return 0;
156                 }
157                 strbuf_rtrim(&branch);
158         } else {
159                 struct object_id oid;
160
161                 if (get_oid_commit(commit, &oid))
162                         return error(_("'%s' is not a valid commit"), commit);
163                 strbuf_addstr(&branch, commit);
164         }
165
166         if (!ref_exists("BISECT_HEAD")) {
167                 struct argv_array argv = ARGV_ARRAY_INIT;
168
169                 argv_array_pushl(&argv, "checkout", branch.buf, "--", NULL);
170                 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
171                         error(_("could not check out original"
172                                 " HEAD '%s'. Try 'git bisect"
173                                 " reset <commit>'."), branch.buf);
174                         strbuf_release(&branch);
175                         argv_array_clear(&argv);
176                         return -1;
177                 }
178                 argv_array_clear(&argv);
179         }
180
181         strbuf_release(&branch);
182         return bisect_clean_state();
183 }
184
185 static void log_commit(FILE *fp, char *fmt, const char *state,
186                        struct commit *commit)
187 {
188         struct pretty_print_context pp = {0};
189         struct strbuf commit_msg = STRBUF_INIT;
190         char *label = xstrfmt(fmt, state);
191
192         format_commit_message(commit, "%s", &commit_msg, &pp);
193
194         fprintf(fp, "# %s: [%s] %s\n", label, oid_to_hex(&commit->object.oid),
195                 commit_msg.buf);
196
197         strbuf_release(&commit_msg);
198         free(label);
199 }
200
201 static int bisect_write(const char *state, const char *rev,
202                         const struct bisect_terms *terms, int nolog)
203 {
204         struct strbuf tag = STRBUF_INIT;
205         struct object_id oid;
206         struct commit *commit;
207         FILE *fp = NULL;
208         int res = 0;
209
210         if (!strcmp(state, terms->term_bad)) {
211                 strbuf_addf(&tag, "refs/bisect/%s", state);
212         } else if (one_of(state, terms->term_good, "skip", NULL)) {
213                 strbuf_addf(&tag, "refs/bisect/%s-%s", state, rev);
214         } else {
215                 res = error(_("Bad bisect_write argument: %s"), state);
216                 goto finish;
217         }
218
219         if (get_oid(rev, &oid)) {
220                 res = error(_("couldn't get the oid of the rev '%s'"), rev);
221                 goto finish;
222         }
223
224         if (update_ref(NULL, tag.buf, &oid, NULL, 0,
225                        UPDATE_REFS_MSG_ON_ERR)) {
226                 res = -1;
227                 goto finish;
228         }
229
230         fp = fopen(git_path_bisect_log(), "a");
231         if (!fp) {
232                 res = error_errno(_("couldn't open the file '%s'"), git_path_bisect_log());
233                 goto finish;
234         }
235
236         commit = lookup_commit_reference(the_repository, &oid);
237         log_commit(fp, "%s", state, commit);
238
239         if (!nolog)
240                 fprintf(fp, "git bisect %s %s\n", state, rev);
241
242 finish:
243         if (fp)
244                 fclose(fp);
245         strbuf_release(&tag);
246         return res;
247 }
248
249 static int check_and_set_terms(struct bisect_terms *terms, const char *cmd)
250 {
251         int has_term_file = !is_empty_or_missing_file(git_path_bisect_terms());
252
253         if (one_of(cmd, "skip", "start", "terms", NULL))
254                 return 0;
255
256         if (has_term_file && strcmp(cmd, terms->term_bad) &&
257             strcmp(cmd, terms->term_good))
258                 return error(_("Invalid command: you're currently in a "
259                                 "%s/%s bisect"), terms->term_bad,
260                                 terms->term_good);
261
262         if (!has_term_file) {
263                 if (one_of(cmd, "bad", "good", NULL)) {
264                         set_terms(terms, "bad", "good");
265                         return write_terms(terms->term_bad, terms->term_good);
266                 }
267                 if (one_of(cmd, "new", "old", NULL)) {
268                         set_terms(terms, "new", "old");
269                         return write_terms(terms->term_bad, terms->term_good);
270                 }
271         }
272
273         return 0;
274 }
275
276 static int mark_good(const char *refname, const struct object_id *oid,
277                      int flag, void *cb_data)
278 {
279         int *m_good = (int *)cb_data;
280         *m_good = 0;
281         return 1;
282 }
283
284 static const char need_bad_and_good_revision_warning[] =
285         N_("You need to give me at least one %s and %s revision.\n"
286            "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
287
288 static const char need_bisect_start_warning[] =
289         N_("You need to start by \"git bisect start\".\n"
290            "You then need to give me at least one %s and %s revision.\n"
291            "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
292
293 static int decide_next(const struct bisect_terms *terms,
294                        const char *current_term, int missing_good,
295                        int missing_bad)
296 {
297         if (!missing_good && !missing_bad)
298                 return 0;
299         if (!current_term)
300                 return -1;
301
302         if (missing_good && !missing_bad &&
303             !strcmp(current_term, terms->term_good)) {
304                 char *yesno;
305                 /*
306                  * have bad (or new) but not good (or old). We could bisect
307                  * although this is less optimum.
308                  */
309                 warning(_("bisecting only with a %s commit"), terms->term_bad);
310                 if (!isatty(0))
311                         return 0;
312                 /*
313                  * TRANSLATORS: Make sure to include [Y] and [n] in your
314                  * translation. The program will only accept English input
315                  * at this point.
316                  */
317                 yesno = git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO);
318                 if (starts_with(yesno, "N") || starts_with(yesno, "n"))
319                         return -1;
320                 return 0;
321         }
322
323         if (!is_empty_or_missing_file(git_path_bisect_start()))
324                 return error(_(need_bad_and_good_revision_warning),
325                              vocab_bad, vocab_good, vocab_bad, vocab_good);
326         else
327                 return error(_(need_bisect_start_warning),
328                              vocab_good, vocab_bad, vocab_good, vocab_bad);
329 }
330
331 static int bisect_next_check(const struct bisect_terms *terms,
332                              const char *current_term)
333 {
334         int missing_good = 1, missing_bad = 1;
335         char *bad_ref = xstrfmt("refs/bisect/%s", terms->term_bad);
336         char *good_glob = xstrfmt("%s-*", terms->term_good);
337
338         if (ref_exists(bad_ref))
339                 missing_bad = 0;
340
341         for_each_glob_ref_in(mark_good, good_glob, "refs/bisect/",
342                              (void *) &missing_good);
343
344         free(good_glob);
345         free(bad_ref);
346
347         return decide_next(terms, current_term, missing_good, missing_bad);
348 }
349
350 static int get_terms(struct bisect_terms *terms)
351 {
352         struct strbuf str = STRBUF_INIT;
353         FILE *fp = NULL;
354         int res = 0;
355
356         fp = fopen(git_path_bisect_terms(), "r");
357         if (!fp) {
358                 res = -1;
359                 goto finish;
360         }
361
362         free_terms(terms);
363         strbuf_getline_lf(&str, fp);
364         terms->term_bad = strbuf_detach(&str, NULL);
365         strbuf_getline_lf(&str, fp);
366         terms->term_good = strbuf_detach(&str, NULL);
367
368 finish:
369         if (fp)
370                 fclose(fp);
371         strbuf_release(&str);
372         return res;
373 }
374
375 static int bisect_terms(struct bisect_terms *terms, const char *option)
376 {
377         if (get_terms(terms))
378                 return error(_("no terms defined"));
379
380         if (option == NULL) {
381                 printf(_("Your current terms are %s for the old state\n"
382                          "and %s for the new state.\n"),
383                        terms->term_good, terms->term_bad);
384                 return 0;
385         }
386         if (one_of(option, "--term-good", "--term-old", NULL))
387                 printf("%s\n", terms->term_good);
388         else if (one_of(option, "--term-bad", "--term-new", NULL))
389                 printf("%s\n", terms->term_bad);
390         else
391                 return error(_("invalid argument %s for 'git bisect terms'.\n"
392                                "Supported options are: "
393                                "--term-good|--term-old and "
394                                "--term-bad|--term-new."), option);
395
396         return 0;
397 }
398
399 static int bisect_append_log_quoted(const char **argv)
400 {
401         int res = 0;
402         FILE *fp = fopen(git_path_bisect_log(), "a");
403         struct strbuf orig_args = STRBUF_INIT;
404
405         if (!fp)
406                 return -1;
407
408         if (fprintf(fp, "git bisect start") < 1) {
409                 res = -1;
410                 goto finish;
411         }
412
413         sq_quote_argv(&orig_args, argv);
414         if (fprintf(fp, "%s\n", orig_args.buf) < 1)
415                 res = -1;
416
417 finish:
418         fclose(fp);
419         strbuf_release(&orig_args);
420         return res;
421 }
422
423 static int bisect_start(struct bisect_terms *terms, int no_checkout,
424                         const char **argv, int argc)
425 {
426         int i, has_double_dash = 0, must_write_terms = 0, bad_seen = 0;
427         int flags, pathspec_pos, res = 0;
428         struct string_list revs = STRING_LIST_INIT_DUP;
429         struct string_list states = STRING_LIST_INIT_DUP;
430         struct strbuf start_head = STRBUF_INIT;
431         struct strbuf bisect_names = STRBUF_INIT;
432         struct object_id head_oid;
433         struct object_id oid;
434         const char *head;
435
436         if (is_bare_repository())
437                 no_checkout = 1;
438
439         /*
440          * Check for one bad and then some good revisions
441          */
442         for (i = 0; i < argc; i++) {
443                 if (!strcmp(argv[i], "--")) {
444                         has_double_dash = 1;
445                         break;
446                 }
447         }
448
449         for (i = 0; i < argc; i++) {
450                 const char *arg = argv[i];
451                 if (!strcmp(argv[i], "--")) {
452                         break;
453                 } else if (!strcmp(arg, "--no-checkout")) {
454                         no_checkout = 1;
455                 } else if (!strcmp(arg, "--term-good") ||
456                          !strcmp(arg, "--term-old")) {
457                         i++;
458                         if (argc <= i)
459                                 return error(_("'' is not a valid term"));
460                         must_write_terms = 1;
461                         free((void *) terms->term_good);
462                         terms->term_good = xstrdup(argv[i]);
463                 } else if (skip_prefix(arg, "--term-good=", &arg) ||
464                            skip_prefix(arg, "--term-old=", &arg)) {
465                         must_write_terms = 1;
466                         free((void *) terms->term_good);
467                         terms->term_good = xstrdup(arg);
468                 } else if (!strcmp(arg, "--term-bad") ||
469                          !strcmp(arg, "--term-new")) {
470                         i++;
471                         if (argc <= i)
472                                 return error(_("'' is not a valid term"));
473                         must_write_terms = 1;
474                         free((void *) terms->term_bad);
475                         terms->term_bad = xstrdup(argv[i]);
476                 } else if (skip_prefix(arg, "--term-bad=", &arg) ||
477                            skip_prefix(arg, "--term-new=", &arg)) {
478                         must_write_terms = 1;
479                         free((void *) terms->term_bad);
480                         terms->term_bad = xstrdup(arg);
481                 } else if (starts_with(arg, "--")) {
482                         return error(_("unrecognized option: '%s'"), arg);
483                 } else {
484                         char *commit_id = xstrfmt("%s^{commit}", arg);
485                         if (get_oid(commit_id, &oid) && has_double_dash)
486                                 die(_("'%s' does not appear to be a valid "
487                                       "revision"), arg);
488
489                         string_list_append(&revs, oid_to_hex(&oid));
490                         free(commit_id);
491                 }
492         }
493         pathspec_pos = i;
494
495         /*
496          * The user ran "git bisect start <sha1> <sha1>", hence did not
497          * explicitly specify the terms, but we are already starting to
498          * set references named with the default terms, and won't be able
499          * to change afterwards.
500          */
501         if (revs.nr)
502                 must_write_terms = 1;
503         for (i = 0; i < revs.nr; i++) {
504                 if (bad_seen) {
505                         string_list_append(&states, terms->term_good);
506                 } else {
507                         bad_seen = 1;
508                         string_list_append(&states, terms->term_bad);
509                 }
510         }
511
512         /*
513          * Verify HEAD
514          */
515         head = resolve_ref_unsafe("HEAD", 0, &head_oid, &flags);
516         if (!head)
517                 if (get_oid("HEAD", &head_oid))
518                         return error(_("bad HEAD - I need a HEAD"));
519
520         /*
521          * Check if we are bisecting
522          */
523         if (!is_empty_or_missing_file(git_path_bisect_start())) {
524                 /* Reset to the rev from where we started */
525                 strbuf_read_file(&start_head, git_path_bisect_start(), 0);
526                 strbuf_trim(&start_head);
527                 if (!no_checkout) {
528                         struct argv_array argv = ARGV_ARRAY_INIT;
529
530                         argv_array_pushl(&argv, "checkout", start_head.buf,
531                                          "--", NULL);
532                         if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
533                                 res = error(_("checking out '%s' failed."
534                                                  " Try 'git bisect start "
535                                                  "<valid-branch>'."),
536                                                start_head.buf);
537                                 goto finish;
538                         }
539                 }
540         } else {
541                 /* Get the rev from where we start. */
542                 if (!get_oid(head, &head_oid) &&
543                     !starts_with(head, "refs/heads/")) {
544                         strbuf_reset(&start_head);
545                         strbuf_addstr(&start_head, oid_to_hex(&head_oid));
546                 } else if (!get_oid(head, &head_oid) &&
547                            skip_prefix(head, "refs/heads/", &head)) {
548                         /*
549                          * This error message should only be triggered by
550                          * cogito usage, and cogito users should understand
551                          * it relates to cg-seek.
552                          */
553                         if (!is_empty_or_missing_file(git_path_head_name()))
554                                 return error(_("won't bisect on cg-seek'ed tree"));
555                         strbuf_addstr(&start_head, head);
556                 } else {
557                         return error(_("bad HEAD - strange symbolic ref"));
558                 }
559         }
560
561         /*
562          * Get rid of any old bisect state.
563          */
564         if (bisect_clean_state())
565                 return -1;
566
567         /*
568          * In case of mistaken revs or checkout error, or signals received,
569          * "bisect_auto_next" below may exit or misbehave.
570          * We have to trap this to be able to clean up using
571          * "bisect_clean_state".
572          */
573
574         /*
575          * Write new start state
576          */
577         write_file(git_path_bisect_start(), "%s\n", start_head.buf);
578
579         if (no_checkout) {
580                 if (get_oid(start_head.buf, &oid) < 0) {
581                         res = error(_("invalid ref: '%s'"), start_head.buf);
582                         goto finish;
583                 }
584                 if (update_ref(NULL, "BISECT_HEAD", &oid, NULL, 0,
585                                UPDATE_REFS_MSG_ON_ERR)) {
586                         res = -1;
587                         goto finish;
588                 }
589         }
590
591         if (pathspec_pos < argc - 1)
592                 sq_quote_argv(&bisect_names, argv + pathspec_pos);
593         write_file(git_path_bisect_names(), "%s\n", bisect_names.buf);
594
595         for (i = 0; i < states.nr; i++)
596                 if (bisect_write(states.items[i].string,
597                                  revs.items[i].string, terms, 1)) {
598                         res = -1;
599                         goto finish;
600                 }
601
602         if (must_write_terms && write_terms(terms->term_bad,
603                                             terms->term_good)) {
604                 res = -1;
605                 goto finish;
606         }
607
608         res = bisect_append_log_quoted(argv);
609         if (res)
610                 res = -1;
611
612 finish:
613         string_list_clear(&revs, 0);
614         string_list_clear(&states, 0);
615         strbuf_release(&start_head);
616         strbuf_release(&bisect_names);
617         return res;
618 }
619
620 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
621 {
622         enum {
623                 NEXT_ALL = 1,
624                 WRITE_TERMS,
625                 BISECT_CLEAN_STATE,
626                 CHECK_EXPECTED_REVS,
627                 BISECT_RESET,
628                 BISECT_WRITE,
629                 CHECK_AND_SET_TERMS,
630                 BISECT_NEXT_CHECK,
631                 BISECT_TERMS,
632                 BISECT_START
633         } cmdmode = 0;
634         int no_checkout = 0, res = 0, nolog = 0;
635         struct option options[] = {
636                 OPT_CMDMODE(0, "next-all", &cmdmode,
637                          N_("perform 'git bisect next'"), NEXT_ALL),
638                 OPT_CMDMODE(0, "write-terms", &cmdmode,
639                          N_("write the terms to .git/BISECT_TERMS"), WRITE_TERMS),
640                 OPT_CMDMODE(0, "bisect-clean-state", &cmdmode,
641                          N_("cleanup the bisection state"), BISECT_CLEAN_STATE),
642                 OPT_CMDMODE(0, "check-expected-revs", &cmdmode,
643                          N_("check for expected revs"), CHECK_EXPECTED_REVS),
644                 OPT_CMDMODE(0, "bisect-reset", &cmdmode,
645                          N_("reset the bisection state"), BISECT_RESET),
646                 OPT_CMDMODE(0, "bisect-write", &cmdmode,
647                          N_("write out the bisection state in BISECT_LOG"), BISECT_WRITE),
648                 OPT_CMDMODE(0, "check-and-set-terms", &cmdmode,
649                          N_("check and set terms in a bisection state"), CHECK_AND_SET_TERMS),
650                 OPT_CMDMODE(0, "bisect-next-check", &cmdmode,
651                          N_("check whether bad or good terms exist"), BISECT_NEXT_CHECK),
652                 OPT_CMDMODE(0, "bisect-terms", &cmdmode,
653                          N_("print out the bisect terms"), BISECT_TERMS),
654                 OPT_CMDMODE(0, "bisect-start", &cmdmode,
655                          N_("start the bisect session"), BISECT_START),
656                 OPT_BOOL(0, "no-checkout", &no_checkout,
657                          N_("update BISECT_HEAD instead of checking out the current commit")),
658                 OPT_BOOL(0, "no-log", &nolog,
659                          N_("no log for BISECT_WRITE")),
660                 OPT_END()
661         };
662         struct bisect_terms terms = { .term_good = NULL, .term_bad = NULL };
663
664         argc = parse_options(argc, argv, prefix, options,
665                              git_bisect_helper_usage,
666                              PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_UNKNOWN);
667
668         if (!cmdmode)
669                 usage_with_options(git_bisect_helper_usage, options);
670
671         switch (cmdmode) {
672         case NEXT_ALL:
673                 res = bisect_next_all(the_repository, prefix, no_checkout);
674                 break;
675         case WRITE_TERMS:
676                 if (argc != 2)
677                         return error(_("--write-terms requires two arguments"));
678                 return write_terms(argv[0], argv[1]);
679         case BISECT_CLEAN_STATE:
680                 if (argc != 0)
681                         return error(_("--bisect-clean-state requires no arguments"));
682                 return bisect_clean_state();
683         case CHECK_EXPECTED_REVS:
684                 check_expected_revs(argv, argc);
685                 return 0;
686         case BISECT_RESET:
687                 if (argc > 1)
688                         return error(_("--bisect-reset requires either no argument or a commit"));
689                 return !!bisect_reset(argc ? argv[0] : NULL);
690         case BISECT_WRITE:
691                 if (argc != 4 && argc != 5)
692                         return error(_("--bisect-write requires either 4 or 5 arguments"));
693                 set_terms(&terms, argv[3], argv[2]);
694                 res = bisect_write(argv[0], argv[1], &terms, nolog);
695                 break;
696         case CHECK_AND_SET_TERMS:
697                 if (argc != 3)
698                         return error(_("--check-and-set-terms requires 3 arguments"));
699                 set_terms(&terms, argv[2], argv[1]);
700                 res = check_and_set_terms(&terms, argv[0]);
701                 break;
702         case BISECT_NEXT_CHECK:
703                 if (argc != 2 && argc != 3)
704                         return error(_("--bisect-next-check requires 2 or 3 arguments"));
705                 set_terms(&terms, argv[1], argv[0]);
706                 res = bisect_next_check(&terms, argc == 3 ? argv[2] : NULL);
707                 break;
708         case BISECT_TERMS:
709                 if (argc > 1)
710                         return error(_("--bisect-terms requires 0 or 1 argument"));
711                 res = bisect_terms(&terms, argc == 1 ? argv[0] : NULL);
712                 break;
713         case BISECT_START:
714                 set_terms(&terms, "bad", "good");
715                 res = bisect_start(&terms, no_checkout, argv, argc);
716                 break;
717         default:
718                 return error("BUG: unknown subcommand '%d'", cmdmode);
719         }
720         free_terms(&terms);
721
722         /*
723          * Handle early success
724          * From check_merge_bases > check_good_are_ancestors_of_bad > bisect_next_all
725          */
726         if (res == BISECT_INTERNAL_SUCCESS_MERGE_BASE)
727                 res = BISECT_OK;
728
729         return abs(res);
730 }