debian: Release git 1:2.9.3-1
[git/debian.git] / builtin / revert.c
blob4e693808b197780c1029aaf886478795b3a6d0a1
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"
11 * This implements the builtins revert and cherry-pick.
13 * Copyright (c) 2007 Johannes E. Schindelin
15 * Based on git-revert.sh, which is
17 * Copyright (c) 2005 Linus Torvalds
18 * Copyright (c) 2005 Junio C Hamano
21 static const char * const revert_usage[] = {
22 N_("git revert [<options>] <commit-ish>..."),
23 N_("git revert <subcommand>"),
24 NULL
27 static const char * const cherry_pick_usage[] = {
28 N_("git cherry-pick [<options>] <commit-ish>..."),
29 N_("git cherry-pick <subcommand>"),
30 NULL
33 static const char *action_name(const struct replay_opts *opts)
35 return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick";
38 static const char * const *revert_or_cherry_pick_usage(struct replay_opts *opts)
40 return opts->action == REPLAY_REVERT ? revert_usage : cherry_pick_usage;
43 static int option_parse_x(const struct option *opt,
44 const char *arg, int unset)
46 struct replay_opts **opts_ptr = opt->value;
47 struct replay_opts *opts = *opts_ptr;
49 if (unset)
50 return 0;
52 ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc);
53 opts->xopts[opts->xopts_nr++] = xstrdup(arg);
54 return 0;
57 LAST_ARG_MUST_BE_NULL
58 static void verify_opt_compatible(const char *me, const char *base_opt, ...)
60 const char *this_opt;
61 va_list ap;
63 va_start(ap, base_opt);
64 while ((this_opt = va_arg(ap, const char *))) {
65 if (va_arg(ap, int))
66 break;
68 va_end(ap);
70 if (this_opt)
71 die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt);
74 static void parse_args(int argc, const char **argv, struct replay_opts *opts)
76 const char * const * usage_str = revert_or_cherry_pick_usage(opts);
77 const char *me = action_name(opts);
78 int cmd = 0;
79 struct option base_options[] = {
80 OPT_CMDMODE(0, "quit", &cmd, N_("end revert or cherry-pick sequence"), 'q'),
81 OPT_CMDMODE(0, "continue", &cmd, N_("resume revert or cherry-pick sequence"), 'c'),
82 OPT_CMDMODE(0, "abort", &cmd, N_("cancel revert or cherry-pick sequence"), 'a'),
83 OPT_BOOL('n', "no-commit", &opts->no_commit, N_("don't automatically commit")),
84 OPT_BOOL('e', "edit", &opts->edit, N_("edit the commit message")),
85 OPT_NOOP_NOARG('r', NULL),
86 OPT_BOOL('s', "signoff", &opts->signoff, N_("add Signed-off-by:")),
87 OPT_INTEGER('m', "mainline", &opts->mainline, N_("parent number")),
88 OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto),
89 OPT_STRING(0, "strategy", &opts->strategy, N_("strategy"), N_("merge strategy")),
90 OPT_CALLBACK('X', "strategy-option", &opts, N_("option"),
91 N_("option for merge strategy"), option_parse_x),
92 { OPTION_STRING, 'S', "gpg-sign", &opts->gpg_sign, N_("key-id"),
93 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
94 OPT_END()
96 struct option *options = base_options;
98 if (opts->action == REPLAY_PICK) {
99 struct option cp_extra[] = {
100 OPT_BOOL('x', NULL, &opts->record_origin, N_("append commit name")),
101 OPT_BOOL(0, "ff", &opts->allow_ff, N_("allow fast-forward")),
102 OPT_BOOL(0, "allow-empty", &opts->allow_empty, N_("preserve initially empty commits")),
103 OPT_BOOL(0, "allow-empty-message", &opts->allow_empty_message, N_("allow commits with empty messages")),
104 OPT_BOOL(0, "keep-redundant-commits", &opts->keep_redundant_commits, N_("keep redundant, empty commits")),
105 OPT_END(),
107 options = parse_options_concat(options, cp_extra);
110 argc = parse_options(argc, argv, NULL, options, usage_str,
111 PARSE_OPT_KEEP_ARGV0 |
112 PARSE_OPT_KEEP_UNKNOWN);
114 /* implies allow_empty */
115 if (opts->keep_redundant_commits)
116 opts->allow_empty = 1;
118 /* Set the subcommand */
119 if (cmd == 'q')
120 opts->subcommand = REPLAY_REMOVE_STATE;
121 else if (cmd == 'c')
122 opts->subcommand = REPLAY_CONTINUE;
123 else if (cmd == 'a')
124 opts->subcommand = REPLAY_ROLLBACK;
125 else
126 opts->subcommand = REPLAY_NONE;
128 /* Check for incompatible command line arguments */
129 if (opts->subcommand != REPLAY_NONE) {
130 char *this_operation;
131 if (opts->subcommand == REPLAY_REMOVE_STATE)
132 this_operation = "--quit";
133 else if (opts->subcommand == REPLAY_CONTINUE)
134 this_operation = "--continue";
135 else {
136 assert(opts->subcommand == REPLAY_ROLLBACK);
137 this_operation = "--abort";
140 verify_opt_compatible(me, this_operation,
141 "--no-commit", opts->no_commit,
142 "--signoff", opts->signoff,
143 "--mainline", opts->mainline,
144 "--strategy", opts->strategy ? 1 : 0,
145 "--strategy-option", opts->xopts ? 1 : 0,
146 "-x", opts->record_origin,
147 "--ff", opts->allow_ff,
148 NULL);
151 if (opts->allow_ff)
152 verify_opt_compatible(me, "--ff",
153 "--signoff", opts->signoff,
154 "--no-commit", opts->no_commit,
155 "-x", opts->record_origin,
156 "--edit", opts->edit,
157 NULL);
159 if (opts->subcommand != REPLAY_NONE) {
160 opts->revs = NULL;
161 } else {
162 struct setup_revision_opt s_r_opt;
163 opts->revs = xmalloc(sizeof(*opts->revs));
164 init_revisions(opts->revs, NULL);
165 opts->revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
166 if (argc < 2)
167 usage_with_options(usage_str, options);
168 if (!strcmp(argv[1], "-"))
169 argv[1] = "@{-1}";
170 memset(&s_r_opt, 0, sizeof(s_r_opt));
171 s_r_opt.assume_dashdash = 1;
172 argc = setup_revisions(argc, argv, opts->revs, &s_r_opt);
175 if (argc > 1)
176 usage_with_options(usage_str, options);
179 int cmd_revert(int argc, const char **argv, const char *prefix)
181 struct replay_opts opts;
182 int res;
184 memset(&opts, 0, sizeof(opts));
185 if (isatty(0))
186 opts.edit = 1;
187 opts.action = REPLAY_REVERT;
188 git_config(git_default_config, NULL);
189 parse_args(argc, argv, &opts);
190 res = sequencer_pick_revisions(&opts);
191 if (res < 0)
192 die(_("revert failed"));
193 return res;
196 int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
198 struct replay_opts opts;
199 int res;
201 memset(&opts, 0, sizeof(opts));
202 opts.action = REPLAY_PICK;
203 git_config(git_default_config, NULL);
204 parse_args(argc, argv, &opts);
205 res = sequencer_pick_revisions(&opts);
206 if (res < 0)
207 die(_("cherry-pick failed"));
208 return res;