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