builtin / bisect--helper.con commit bisect--helper: `bisect_next_check` shell function in C (129a6cf)
   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
  11static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
  12static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
  13static GIT_PATH_FUNC(git_path_bisect_ancestors_ok, "BISECT_ANCESTORS_OK")
  14static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START")
  15static GIT_PATH_FUNC(git_path_bisect_head, "BISECT_HEAD")
  16static GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG")
  17
  18static const char * const git_bisect_helper_usage[] = {
  19        N_("git bisect--helper --next-all [--no-checkout]"),
  20        N_("git bisect--helper --write-terms <bad_term> <good_term>"),
  21        N_("git bisect--helper --bisect-clean-state"),
  22        N_("git bisect--helper --bisect-reset [<commit>]"),
  23        N_("git bisect--helper --bisect-write [--no-log] <state> <revision> <good_term> <bad_term>"),
  24        N_("git bisect--helper --bisect-check-and-set-terms <command> <good_term> <bad_term>"),
  25        N_("git bisect--helper --bisect-next-check <good_term> <bad_term> [<term>]"),
  26        NULL
  27};
  28
  29struct bisect_terms {
  30        char *term_good;
  31        char *term_bad;
  32};
  33
  34static void free_terms(struct bisect_terms *terms)
  35{
  36        FREE_AND_NULL(terms->term_good);
  37        FREE_AND_NULL(terms->term_bad);
  38}
  39
  40static void set_terms(struct bisect_terms *terms, const char *bad,
  41                      const char *good)
  42{
  43        free((void *)terms->term_good);
  44        terms->term_good = xstrdup(good);
  45        free((void *)terms->term_bad);
  46        terms->term_bad = xstrdup(bad);
  47}
  48
  49static const char *vocab_bad = "bad|new";
  50static const char *vocab_good = "good|old";
  51
  52/*
  53 * Check whether the string `term` belongs to the set of strings
  54 * included in the variable arguments.
  55 */
  56LAST_ARG_MUST_BE_NULL
  57static int one_of(const char *term, ...)
  58{
  59        int res = 0;
  60        va_list matches;
  61        const char *match;
  62
  63        va_start(matches, term);
  64        while (!res && (match = va_arg(matches, const char *)))
  65                res = !strcmp(term, match);
  66        va_end(matches);
  67
  68        return res;
  69}
  70
  71static int check_term_format(const char *term, const char *orig_term)
  72{
  73        int res;
  74        char *new_term = xstrfmt("refs/bisect/%s", term);
  75
  76        res = check_refname_format(new_term, 0);
  77        free(new_term);
  78
  79        if (res)
  80                return error(_("'%s' is not a valid term"), term);
  81
  82        if (one_of(term, "help", "start", "skip", "next", "reset",
  83                        "visualize", "view", "replay", "log", "run", "terms", NULL))
  84                return error(_("can't use the builtin command '%s' as a term"), term);
  85
  86        /*
  87         * In theory, nothing prevents swapping completely good and bad,
  88         * but this situation could be confusing and hasn't been tested
  89         * enough. Forbid it for now.
  90         */
  91
  92        if ((strcmp(orig_term, "bad") && one_of(term, "bad", "new", NULL)) ||
  93                 (strcmp(orig_term, "good") && one_of(term, "good", "old", NULL)))
  94                return error(_("can't change the meaning of the term '%s'"), term);
  95
  96        return 0;
  97}
  98
  99static int write_terms(const char *bad, const char *good)
 100{
 101        FILE *fp = NULL;
 102        int res;
 103
 104        if (!strcmp(bad, good))
 105                return error(_("please use two different terms"));
 106
 107        if (check_term_format(bad, "bad") || check_term_format(good, "good"))
 108                return -1;
 109
 110        fp = fopen(git_path_bisect_terms(), "w");
 111        if (!fp)
 112                return error_errno(_("could not open the file BISECT_TERMS"));
 113
 114        res = fprintf(fp, "%s\n%s\n", bad, good);
 115        res |= fclose(fp);
 116        return (res < 0) ? -1 : 0;
 117}
 118
 119static int is_expected_rev(const char *expected_hex)
 120{
 121        struct strbuf actual_hex = STRBUF_INIT;
 122        int res = 0;
 123        if (strbuf_read_file(&actual_hex, git_path_bisect_expected_rev(), 0) >= 40) {
 124                strbuf_trim(&actual_hex);
 125                res = !strcmp(actual_hex.buf, expected_hex);
 126        }
 127        strbuf_release(&actual_hex);
 128        return res;
 129}
 130
 131static void check_expected_revs(const char **revs, int rev_nr)
 132{
 133        int i;
 134
 135        for (i = 0; i < rev_nr; i++) {
 136                if (!is_expected_rev(revs[i])) {
 137                        unlink_or_warn(git_path_bisect_ancestors_ok());
 138                        unlink_or_warn(git_path_bisect_expected_rev());
 139                }
 140        }
 141}
 142
 143static int bisect_reset(const char *commit)
 144{
 145        struct strbuf branch = STRBUF_INIT;
 146
 147        if (!commit) {
 148                if (strbuf_read_file(&branch, git_path_bisect_start(), 0) < 1) {
 149                        printf(_("We are not bisecting.\n"));
 150                        return 0;
 151                }
 152                strbuf_rtrim(&branch);
 153        } else {
 154                struct object_id oid;
 155
 156                if (get_oid_commit(commit, &oid))
 157                        return error(_("'%s' is not a valid commit"), commit);
 158                strbuf_addstr(&branch, commit);
 159        }
 160
 161        if (!file_exists(git_path_bisect_head())) {
 162                struct argv_array argv = ARGV_ARRAY_INIT;
 163
 164                argv_array_pushl(&argv, "checkout", branch.buf, "--", NULL);
 165                if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
 166                        strbuf_release(&branch);
 167                        argv_array_clear(&argv);
 168                        return error(_("could not check out original"
 169                                       " HEAD '%s'. Try 'git bisect"
 170                                       "reset <commit>'."), branch.buf);
 171                }
 172                argv_array_clear(&argv);
 173        }
 174
 175        strbuf_release(&branch);
 176        return bisect_clean_state();
 177}
 178
 179static void log_commit(FILE *fp, char *fmt, const char *state,
 180                       struct commit *commit)
 181{
 182        struct pretty_print_context pp = {0};
 183        struct strbuf commit_msg = STRBUF_INIT;
 184        char *label = xstrfmt(fmt, state);
 185
 186        format_commit_message(commit, "%s", &commit_msg, &pp);
 187
 188        fprintf(fp, "# %s: [%s] %s\n", label, oid_to_hex(&commit->object.oid),
 189                commit_msg.buf);
 190
 191        strbuf_release(&commit_msg);
 192        free(label);
 193}
 194
 195static int bisect_write(const char *state, const char *rev,
 196                        const struct bisect_terms *terms, int nolog)
 197{
 198        struct strbuf tag = STRBUF_INIT;
 199        struct object_id oid;
 200        struct commit *commit;
 201        FILE *fp = NULL;
 202        int retval = 0;
 203
 204        if (!strcmp(state, terms->term_bad)) {
 205                strbuf_addf(&tag, "refs/bisect/%s", state);
 206        } else if (one_of(state, terms->term_good, "skip", NULL)) {
 207                strbuf_addf(&tag, "refs/bisect/%s-%s", state, rev);
 208        } else {
 209                retval = error(_("Bad bisect_write argument: %s"), state);
 210                goto finish;
 211        }
 212
 213        if (get_oid(rev, &oid)) {
 214                retval = error(_("couldn't get the oid of the rev '%s'"), rev);
 215                goto finish;
 216        }
 217
 218        if (update_ref(NULL, tag.buf, &oid, NULL, 0,
 219                       UPDATE_REFS_MSG_ON_ERR)) {
 220                retval = -1;
 221                goto finish;
 222        }
 223
 224        fp = fopen(git_path_bisect_log(), "a");
 225        if (!fp) {
 226                retval = error_errno(_("couldn't open the file '%s'"), git_path_bisect_log());
 227                goto finish;
 228        }
 229
 230        commit = lookup_commit_reference(the_repository, &oid);
 231        log_commit(fp, "%s", state, commit);
 232
 233        if (!nolog)
 234                fprintf(fp, "git bisect %s %s\n", state, rev);
 235
 236finish:
 237        if (fp)
 238                fclose(fp);
 239        strbuf_release(&tag);
 240        return retval;
 241}
 242
 243static int check_and_set_terms(struct bisect_terms *terms, const char *cmd)
 244{
 245        int has_term_file = !is_empty_or_missing_file(git_path_bisect_terms());
 246
 247        if (one_of(cmd, "skip", "start", "terms", NULL))
 248                return 0;
 249
 250        if (has_term_file && strcmp(cmd, terms->term_bad) &&
 251            strcmp(cmd, terms->term_good))
 252                return error(_("Invalid command: you're currently in a "
 253                                "%s/%s bisect"), terms->term_bad,
 254                                terms->term_good);
 255
 256        if (!has_term_file) {
 257                if (one_of(cmd, "bad", "good", NULL)) {
 258                        set_terms(terms, "bad", "good");
 259                        return write_terms(terms->term_bad, terms->term_good);
 260                }
 261                if (one_of(cmd, "new", "old", NULL)) {
 262                        set_terms(terms, "new", "old");
 263                        return write_terms(terms->term_bad, terms->term_good);
 264                }
 265        }
 266
 267        return 0;
 268}
 269
 270static int mark_good(const char *refname, const struct object_id *oid,
 271                     int flag, void *cb_data)
 272{
 273        int *m_good = (int *)cb_data;
 274        *m_good = 0;
 275        return 1;
 276}
 277
 278static const char *need_bad_and_good_revision_warning =
 279        N_("You need to give me at least one %s and %s revision.\n"
 280           "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
 281
 282static const char *need_bisect_start_warning =
 283        N_("You need to start by \"git bisect start\".\n"
 284           "You then need to give me at least one %s and %s revision.\n"
 285           "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
 286
 287static int bisect_next_check(const struct bisect_terms *terms,
 288                             const char *current_term)
 289{
 290        int missing_good = 1, missing_bad = 1, retval = 0;
 291        const char *bad_ref = xstrfmt("refs/bisect/%s", terms->term_bad);
 292        const char *good_glob = xstrfmt("%s-*", terms->term_good);
 293
 294        if (ref_exists(bad_ref))
 295                missing_bad = 0;
 296
 297        for_each_glob_ref_in(mark_good, good_glob, "refs/bisect/",
 298                             (void *) &missing_good);
 299
 300        if (!missing_good && !missing_bad)
 301                goto finish;
 302
 303        if (!current_term) {
 304                retval = -1;
 305                goto finish;
 306        }
 307
 308        if (missing_good && !missing_bad &&
 309            !strcmp(current_term, terms->term_good)) {
 310                char *yesno;
 311                /*
 312                 * have bad (or new) but not good (or old). We could bisect
 313                 * although this is less optimum.
 314                 */
 315                warning(_("bisecting only with a %s commit"), terms->term_bad);
 316                if (!isatty(0))
 317                        goto finish;
 318                /*
 319                 * TRANSLATORS: Make sure to include [Y] and [n] in your
 320                 * translation. The program will only accept English input
 321                 * at this point.
 322                 */
 323                yesno = git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO);
 324                if (starts_with(yesno, "N") || starts_with(yesno, "n"))
 325                        retval = -1;
 326                goto finish;
 327        }
 328        if (!is_empty_or_missing_file(git_path_bisect_start())) {
 329                retval = error(_(need_bad_and_good_revision_warning),
 330                               vocab_bad, vocab_good, vocab_bad, vocab_good);
 331        } else {
 332                retval = error(_(need_bisect_start_warning),
 333                               vocab_good, vocab_bad, vocab_good, vocab_bad);
 334        }
 335
 336finish:
 337        free((void *) good_glob);
 338        free((void *) bad_ref);
 339        return retval;
 340}
 341
 342int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 343{
 344        enum {
 345                NEXT_ALL = 1,
 346                WRITE_TERMS,
 347                BISECT_CLEAN_STATE,
 348                CHECK_EXPECTED_REVS,
 349                BISECT_RESET,
 350                BISECT_WRITE,
 351                CHECK_AND_SET_TERMS,
 352                BISECT_NEXT_CHECK
 353        } cmdmode = 0;
 354        int no_checkout = 0, res = 0, nolog = 0;
 355        struct option options[] = {
 356                OPT_CMDMODE(0, "next-all", &cmdmode,
 357                         N_("perform 'git bisect next'"), NEXT_ALL),
 358                OPT_CMDMODE(0, "write-terms", &cmdmode,
 359                         N_("write the terms to .git/BISECT_TERMS"), WRITE_TERMS),
 360                OPT_CMDMODE(0, "bisect-clean-state", &cmdmode,
 361                         N_("cleanup the bisection state"), BISECT_CLEAN_STATE),
 362                OPT_CMDMODE(0, "check-expected-revs", &cmdmode,
 363                         N_("check for expected revs"), CHECK_EXPECTED_REVS),
 364                OPT_CMDMODE(0, "bisect-reset", &cmdmode,
 365                         N_("reset the bisection state"), BISECT_RESET),
 366                OPT_CMDMODE(0, "bisect-write", &cmdmode,
 367                         N_("write out the bisection state in BISECT_LOG"), BISECT_WRITE),
 368                OPT_CMDMODE(0, "check-and-set-terms", &cmdmode,
 369                         N_("check and set terms in a bisection state"), CHECK_AND_SET_TERMS),
 370                OPT_CMDMODE(0, "bisect-next-check", &cmdmode,
 371                         N_("check whether bad or good terms exist"), BISECT_NEXT_CHECK),
 372                OPT_BOOL(0, "no-checkout", &no_checkout,
 373                         N_("update BISECT_HEAD instead of checking out the current commit")),
 374                OPT_BOOL(0, "no-log", &nolog,
 375                         N_("no log for BISECT_WRITE ")),
 376                OPT_END()
 377        };
 378        struct bisect_terms terms = { .term_good = NULL, .term_bad = NULL };
 379
 380        argc = parse_options(argc, argv, prefix, options,
 381                             git_bisect_helper_usage, 0);
 382
 383        if (!cmdmode)
 384                usage_with_options(git_bisect_helper_usage, options);
 385
 386        switch (cmdmode) {
 387        case NEXT_ALL:
 388                return bisect_next_all(prefix, no_checkout);
 389        case WRITE_TERMS:
 390                if (argc != 2)
 391                        return error(_("--write-terms requires two arguments"));
 392                return write_terms(argv[0], argv[1]);
 393        case BISECT_CLEAN_STATE:
 394                if (argc != 0)
 395                        return error(_("--bisect-clean-state requires no arguments"));
 396                return bisect_clean_state();
 397        case CHECK_EXPECTED_REVS:
 398                check_expected_revs(argv, argc);
 399                return 0;
 400        case BISECT_RESET:
 401                if (argc > 1)
 402                        return error(_("--bisect-reset requires either no argument or a commit"));
 403                return !!bisect_reset(argc ? argv[0] : NULL);
 404        case BISECT_WRITE:
 405                if (argc != 4 && argc != 5)
 406                        return error(_("--bisect-write requires either 4 or 5 arguments"));
 407                set_terms(&terms, argv[3], argv[2]);
 408                res = bisect_write(argv[0], argv[1], &terms, nolog);
 409                break;
 410        case CHECK_AND_SET_TERMS:
 411                if (argc != 3)
 412                        return error(_("--check-and-set-terms requires 3 arguments"));
 413                set_terms(&terms, argv[2], argv[1]);
 414                res = check_and_set_terms(&terms, argv[0]);
 415                break;
 416        case BISECT_NEXT_CHECK:
 417                if (argc != 2 && argc != 3)
 418                        return error(_("--bisect-next-check requires 2 or 3 arguments"));
 419                set_terms(&terms, argv[1], argv[0]);
 420                res = bisect_next_check(&terms, argc == 3 ? argv[2] : NULL);
 421                break;
 422        default:
 423                return error("BUG: unknown subcommand '%d'", cmdmode);
 424        }
 425        free_terms(&terms);
 426        return !!res;
 427}