builtin / revert.con commit travis-ci: fix "skip_branch_tip_with_tag()" string comparison (8376eb4)
   1#include "cache.h"
   2#include "config.h"
   3#include "builtin.h"
   4#include "parse-options.h"
   5#include "diff.h"
   6#include "revision.h"
   7#include "rerere.h"
   8#include "dir.h"
   9#include "sequencer.h"
  10
  11/*
  12 * This implements the builtins revert and cherry-pick.
  13 *
  14 * Copyright (c) 2007 Johannes E. Schindelin
  15 *
  16 * Based on git-revert.sh, which is
  17 *
  18 * Copyright (c) 2005 Linus Torvalds
  19 * Copyright (c) 2005 Junio C Hamano
  20 */
  21
  22static const char * const revert_usage[] = {
  23        N_("git revert [<options>] <commit-ish>..."),
  24        N_("git revert <subcommand>"),
  25        NULL
  26};
  27
  28static const char * const cherry_pick_usage[] = {
  29        N_("git cherry-pick [<options>] <commit-ish>..."),
  30        N_("git cherry-pick <subcommand>"),
  31        NULL
  32};
  33
  34static const char *action_name(const struct replay_opts *opts)
  35{
  36        return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick";
  37}
  38
  39static const char * const *revert_or_cherry_pick_usage(struct replay_opts *opts)
  40{
  41        return opts->action == REPLAY_REVERT ? revert_usage : cherry_pick_usage;
  42}
  43
  44static int option_parse_x(const struct option *opt,
  45                          const char *arg, int unset)
  46{
  47        struct replay_opts **opts_ptr = opt->value;
  48        struct replay_opts *opts = *opts_ptr;
  49
  50        if (unset)
  51                return 0;
  52
  53        ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc);
  54        opts->xopts[opts->xopts_nr++] = xstrdup(arg);
  55        return 0;
  56}
  57
  58static int option_parse_m(const struct option *opt,
  59                          const char *arg, int unset)
  60{
  61        struct replay_opts *replay = opt->value;
  62        char *end;
  63
  64        if (unset) {
  65                replay->mainline = 0;
  66                return 0;
  67        }
  68
  69        replay->mainline = strtol(arg, &end, 10);
  70        if (*end || replay->mainline <= 0)
  71                return opterror(opt, "expects a number greater than zero", 0);
  72
  73        return 0;
  74}
  75
  76LAST_ARG_MUST_BE_NULL
  77static void verify_opt_compatible(const char *me, const char *base_opt, ...)
  78{
  79        const char *this_opt;
  80        va_list ap;
  81
  82        va_start(ap, base_opt);
  83        while ((this_opt = va_arg(ap, const char *))) {
  84                if (va_arg(ap, int))
  85                        break;
  86        }
  87        va_end(ap);
  88
  89        if (this_opt)
  90                die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt);
  91}
  92
  93static int run_sequencer(int argc, const char **argv, struct replay_opts *opts)
  94{
  95        const char * const * usage_str = revert_or_cherry_pick_usage(opts);
  96        const char *me = action_name(opts);
  97        int cmd = 0;
  98        struct option base_options[] = {
  99                OPT_CMDMODE(0, "quit", &cmd, N_("end revert or cherry-pick sequence"), 'q'),
 100                OPT_CMDMODE(0, "continue", &cmd, N_("resume revert or cherry-pick sequence"), 'c'),
 101                OPT_CMDMODE(0, "abort", &cmd, N_("cancel revert or cherry-pick sequence"), 'a'),
 102                OPT_BOOL('n', "no-commit", &opts->no_commit, N_("don't automatically commit")),
 103                OPT_BOOL('e', "edit", &opts->edit, N_("edit the commit message")),
 104                OPT_NOOP_NOARG('r', NULL),
 105                OPT_BOOL('s', "signoff", &opts->signoff, N_("add Signed-off-by:")),
 106                OPT_CALLBACK('m', "mainline", opts, N_("parent-number"),
 107                             N_("select mainline parent"), option_parse_m),
 108                OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto),
 109                OPT_STRING(0, "strategy", &opts->strategy, N_("strategy"), N_("merge strategy")),
 110                OPT_CALLBACK('X', "strategy-option", &opts, N_("option"),
 111                        N_("option for merge strategy"), option_parse_x),
 112                { OPTION_STRING, 'S', "gpg-sign", &opts->gpg_sign, N_("key-id"),
 113                  N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
 114                OPT_END()
 115        };
 116        struct option *options = base_options;
 117
 118        if (opts->action == REPLAY_PICK) {
 119                struct option cp_extra[] = {
 120                        OPT_BOOL('x', NULL, &opts->record_origin, N_("append commit name")),
 121                        OPT_BOOL(0, "ff", &opts->allow_ff, N_("allow fast-forward")),
 122                        OPT_BOOL(0, "allow-empty", &opts->allow_empty, N_("preserve initially empty commits")),
 123                        OPT_BOOL(0, "allow-empty-message", &opts->allow_empty_message, N_("allow commits with empty messages")),
 124                        OPT_BOOL(0, "keep-redundant-commits", &opts->keep_redundant_commits, N_("keep redundant, empty commits")),
 125                        OPT_END(),
 126                };
 127                options = parse_options_concat(options, cp_extra);
 128        }
 129
 130        argc = parse_options(argc, argv, NULL, options, usage_str,
 131                        PARSE_OPT_KEEP_ARGV0 |
 132                        PARSE_OPT_KEEP_UNKNOWN);
 133
 134        /* implies allow_empty */
 135        if (opts->keep_redundant_commits)
 136                opts->allow_empty = 1;
 137
 138        /* Check for incompatible command line arguments */
 139        if (cmd) {
 140                char *this_operation;
 141                if (cmd == 'q')
 142                        this_operation = "--quit";
 143                else if (cmd == 'c')
 144                        this_operation = "--continue";
 145                else {
 146                        assert(cmd == 'a');
 147                        this_operation = "--abort";
 148                }
 149
 150                verify_opt_compatible(me, this_operation,
 151                                "--no-commit", opts->no_commit,
 152                                "--signoff", opts->signoff,
 153                                "--mainline", opts->mainline,
 154                                "--strategy", opts->strategy ? 1 : 0,
 155                                "--strategy-option", opts->xopts ? 1 : 0,
 156                                "-x", opts->record_origin,
 157                                "--ff", opts->allow_ff,
 158                                NULL);
 159        }
 160
 161        if (opts->allow_ff)
 162                verify_opt_compatible(me, "--ff",
 163                                "--signoff", opts->signoff,
 164                                "--no-commit", opts->no_commit,
 165                                "-x", opts->record_origin,
 166                                "--edit", opts->edit,
 167                                NULL);
 168
 169        if (cmd) {
 170                opts->revs = NULL;
 171        } else {
 172                struct setup_revision_opt s_r_opt;
 173                opts->revs = xmalloc(sizeof(*opts->revs));
 174                init_revisions(opts->revs, NULL);
 175                opts->revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
 176                if (argc < 2)
 177                        usage_with_options(usage_str, options);
 178                if (!strcmp(argv[1], "-"))
 179                        argv[1] = "@{-1}";
 180                memset(&s_r_opt, 0, sizeof(s_r_opt));
 181                s_r_opt.assume_dashdash = 1;
 182                argc = setup_revisions(argc, argv, opts->revs, &s_r_opt);
 183        }
 184
 185        if (argc > 1)
 186                usage_with_options(usage_str, options);
 187
 188        /* These option values will be free()d */
 189        opts->gpg_sign = xstrdup_or_null(opts->gpg_sign);
 190        opts->strategy = xstrdup_or_null(opts->strategy);
 191
 192        if (cmd == 'q')
 193                return sequencer_remove_state(opts);
 194        if (cmd == 'c')
 195                return sequencer_continue(opts);
 196        if (cmd == 'a')
 197                return sequencer_rollback(opts);
 198        return sequencer_pick_revisions(opts);
 199}
 200
 201int cmd_revert(int argc, const char **argv, const char *prefix)
 202{
 203        struct replay_opts opts = REPLAY_OPTS_INIT;
 204        int res;
 205
 206        if (isatty(0))
 207                opts.edit = 1;
 208        opts.action = REPLAY_REVERT;
 209        git_config(git_default_config, NULL);
 210        res = run_sequencer(argc, argv, &opts);
 211        if (res < 0)
 212                die(_("revert failed"));
 213        return res;
 214}
 215
 216int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
 217{
 218        struct replay_opts opts = REPLAY_OPTS_INIT;
 219        int res;
 220
 221        opts.action = REPLAY_PICK;
 222        git_config(git_default_config, NULL);
 223        res = run_sequencer(argc, argv, &opts);
 224        if (res < 0)
 225                die(_("cherry-pick failed"));
 226        return res;
 227}