2bd2d396e335df9fe063af0ffcf69c32a798c9ef
   1#include "builtin.h"
   2#include "cache.h"
   3#include "parse-options.h"
   4#include "bisect.h"
   5#include "refs.h"
   6
   7static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
   8
   9static const char * const git_bisect_helper_usage[] = {
  10        N_("git bisect--helper --next-all [--no-checkout]"),
  11        N_("git bisect--helper --write-terms <bad_term> <good_term>"),
  12        NULL
  13};
  14
  15/*
  16 * Check whether the string `term` belongs to the set of strings
  17 * included in the variable arguments.
  18 */
  19LAST_ARG_MUST_BE_NULL
  20static int one_of(const char *term, ...)
  21{
  22        int res = 0;
  23        va_list matches;
  24        const char *match;
  25
  26        va_start(matches, term);
  27        while (!res && (match = va_arg(matches, const char *)))
  28                res = !strcmp(term, match);
  29        va_end(matches);
  30
  31        return res;
  32}
  33
  34static int check_term_format(const char *term, const char *orig_term)
  35{
  36        int res;
  37        char *new_term = xstrfmt("refs/bisect/%s", term);
  38
  39        res = check_refname_format(new_term, 0);
  40        free(new_term);
  41
  42        if (res)
  43                return error(_("'%s' is not a valid term"), term);
  44
  45        if (one_of(term, "help", "start", "skip", "next", "reset",
  46                        "visualize", "replay", "log", "run", "terms", NULL))
  47                return error(_("can't use the builtin command '%s' as a term"), term);
  48
  49        /*
  50         * In theory, nothing prevents swapping completely good and bad,
  51         * but this situation could be confusing and hasn't been tested
  52         * enough. Forbid it for now.
  53         */
  54
  55        if ((strcmp(orig_term, "bad") && one_of(term, "bad", "new", NULL)) ||
  56                 (strcmp(orig_term, "good") && one_of(term, "good", "old", NULL)))
  57                return error(_("can't change the meaning of the term '%s'"), term);
  58
  59        return 0;
  60}
  61
  62static int write_terms(const char *bad, const char *good)
  63{
  64        FILE *fp = NULL;
  65        int res;
  66
  67        if (!strcmp(bad, good))
  68                return error(_("please use two different terms"));
  69
  70        if (check_term_format(bad, "bad") || check_term_format(good, "good"))
  71                return -1;
  72
  73        fp = fopen(git_path_bisect_terms(), "w");
  74        if (!fp)
  75                return error_errno(_("could not open the file BISECT_TERMS"));
  76
  77        res = fprintf(fp, "%s\n%s\n", bad, good);
  78        res |= fclose(fp);
  79        return (res < 0) ? -1 : 0;
  80}
  81
  82int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
  83{
  84        enum {
  85                NEXT_ALL = 1,
  86                WRITE_TERMS
  87        } cmdmode = 0;
  88        int no_checkout = 0;
  89        struct option options[] = {
  90                OPT_CMDMODE(0, "next-all", &cmdmode,
  91                         N_("perform 'git bisect next'"), NEXT_ALL),
  92                OPT_CMDMODE(0, "write-terms", &cmdmode,
  93                         N_("write the terms to .git/BISECT_TERMS"), WRITE_TERMS),
  94                OPT_BOOL(0, "no-checkout", &no_checkout,
  95                         N_("update BISECT_HEAD instead of checking out the current commit")),
  96                OPT_END()
  97        };
  98
  99        argc = parse_options(argc, argv, prefix, options,
 100                             git_bisect_helper_usage, 0);
 101
 102        if (!cmdmode)
 103                usage_with_options(git_bisect_helper_usage, options);
 104
 105        switch (cmdmode) {
 106        case NEXT_ALL:
 107                return bisect_next_all(prefix, no_checkout);
 108        case WRITE_TERMS:
 109                if (argc != 2)
 110                        return error(_("--write-terms requires two arguments"));
 111                return write_terms(argv[0], argv[1]);
 112        default:
 113                return error("BUG: unknown subcommand '%d'", cmdmode);
 114        }
 115        return 0;
 116}