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