Coverage Report

Created: 2024-09-08 06:23

/src/git/builtin/revert.c
Line
Count
Source (jump to first uncovered line)
1
#include "git-compat-util.h"
2
#include "builtin.h"
3
#include "parse-options.h"
4
#include "diff.h"
5
#include "gettext.h"
6
#include "repository.h"
7
#include "revision.h"
8
#include "rerere.h"
9
#include "sequencer.h"
10
#include "branch.h"
11
12
/*
13
 * This implements the builtins revert and cherry-pick.
14
 *
15
 * Copyright (c) 2007 Johannes E. Schindelin
16
 *
17
 * Based on git-revert.sh, which is
18
 *
19
 * Copyright (c) 2005 Linus Torvalds
20
 * Copyright (c) 2005 Junio C Hamano
21
 */
22
23
static const char * const revert_usage[] = {
24
  N_("git revert [--[no-]edit] [-n] [-m <parent-number>] [-s] [-S[<keyid>]] <commit>..."),
25
  N_("git revert (--continue | --skip | --abort | --quit)"),
26
  NULL
27
};
28
29
static const char * const cherry_pick_usage[] = {
30
  N_("git cherry-pick [--edit] [-n] [-m <parent-number>] [-s] [-x] [--ff]\n"
31
     "                [-S[<keyid>]] <commit>..."),
32
  N_("git cherry-pick (--continue | --skip | --abort | --quit)"),
33
  NULL
34
};
35
36
static const char *action_name(const struct replay_opts *opts)
37
0
{
38
0
  return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick";
39
0
}
40
41
static const char * const *revert_or_cherry_pick_usage(struct replay_opts *opts)
42
0
{
43
0
  return opts->action == REPLAY_REVERT ? revert_usage : cherry_pick_usage;
44
0
}
45
46
enum empty_action {
47
  EMPTY_COMMIT_UNSPECIFIED = -1,
48
  STOP_ON_EMPTY_COMMIT,      /* output errors and stop in the middle of a cherry-pick */
49
  DROP_EMPTY_COMMIT,         /* skip with a notice message */
50
  KEEP_EMPTY_COMMIT,         /* keep recording as empty commits */
51
};
52
53
static int parse_opt_empty(const struct option *opt, const char *arg, int unset)
54
0
{
55
0
  int *opt_value = opt->value;
56
57
0
  BUG_ON_OPT_NEG(unset);
58
59
0
  if (!strcmp(arg, "stop"))
60
0
    *opt_value = STOP_ON_EMPTY_COMMIT;
61
0
  else if (!strcmp(arg, "drop"))
62
0
    *opt_value = DROP_EMPTY_COMMIT;
63
0
  else if (!strcmp(arg, "keep"))
64
0
    *opt_value = KEEP_EMPTY_COMMIT;
65
0
  else
66
0
    return error(_("invalid value for '%s': '%s'"), "--empty", arg);
67
68
0
  return 0;
69
0
}
70
71
static int option_parse_m(const struct option *opt,
72
        const char *arg, int unset)
73
0
{
74
0
  struct replay_opts *replay = opt->value;
75
0
  char *end;
76
77
0
  if (unset) {
78
0
    replay->mainline = 0;
79
0
    return 0;
80
0
  }
81
82
0
  replay->mainline = strtol(arg, &end, 10);
83
0
  if (*end || replay->mainline <= 0)
84
0
    return error(_("option `%s' expects a number greater than zero"),
85
0
           opt->long_name);
86
87
0
  return 0;
88
0
}
89
90
LAST_ARG_MUST_BE_NULL
91
static void verify_opt_compatible(const char *me, const char *base_opt, ...)
92
0
{
93
0
  const char *this_opt;
94
0
  va_list ap;
95
96
0
  va_start(ap, base_opt);
97
0
  while ((this_opt = va_arg(ap, const char *))) {
98
0
    if (va_arg(ap, int))
99
0
      break;
100
0
  }
101
0
  va_end(ap);
102
103
0
  if (this_opt)
104
0
    die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt);
105
0
}
106
107
static int run_sequencer(int argc, const char **argv, const char *prefix,
108
       struct replay_opts *opts)
109
0
{
110
0
  const char * const * usage_str = revert_or_cherry_pick_usage(opts);
111
0
  const char *me = action_name(opts);
112
0
  const char *cleanup_arg = NULL;
113
0
  enum empty_action empty_opt = EMPTY_COMMIT_UNSPECIFIED;
114
0
  int cmd = 0;
115
0
  struct option base_options[] = {
116
0
    OPT_CMDMODE(0, "quit", &cmd, N_("end revert or cherry-pick sequence"), 'q'),
117
0
    OPT_CMDMODE(0, "continue", &cmd, N_("resume revert or cherry-pick sequence"), 'c'),
118
0
    OPT_CMDMODE(0, "abort", &cmd, N_("cancel revert or cherry-pick sequence"), 'a'),
119
0
    OPT_CMDMODE(0, "skip", &cmd, N_("skip current commit and continue"), 's'),
120
0
    OPT_CLEANUP(&cleanup_arg),
121
0
    OPT_BOOL('n', "no-commit", &opts->no_commit, N_("don't automatically commit")),
122
0
    OPT_BOOL('e', "edit", &opts->edit, N_("edit the commit message")),
123
0
    OPT_NOOP_NOARG('r', NULL),
124
0
    OPT_BOOL('s', "signoff", &opts->signoff, N_("add a Signed-off-by trailer")),
125
0
    OPT_CALLBACK('m', "mainline", opts, N_("parent-number"),
126
0
           N_("select mainline parent"), option_parse_m),
127
0
    OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto),
128
0
    OPT_STRING(0, "strategy", &opts->strategy, N_("strategy"), N_("merge strategy")),
129
0
    OPT_STRVEC('X', "strategy-option", &opts->xopts, N_("option"),
130
0
      N_("option for merge strategy")),
131
0
    { OPTION_STRING, 'S', "gpg-sign", &opts->gpg_sign, N_("key-id"),
132
0
      N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
133
0
    OPT_END()
134
0
  };
135
0
  struct option *options = base_options;
136
137
0
  if (opts->action == REPLAY_PICK) {
138
0
    struct option cp_extra[] = {
139
0
      OPT_BOOL('x', NULL, &opts->record_origin, N_("append commit name")),
140
0
      OPT_BOOL(0, "ff", &opts->allow_ff, N_("allow fast-forward")),
141
0
      OPT_BOOL(0, "allow-empty", &opts->allow_empty, N_("preserve initially empty commits")),
142
0
      OPT_BOOL(0, "allow-empty-message", &opts->allow_empty_message, N_("allow commits with empty messages")),
143
0
      OPT_BOOL(0, "keep-redundant-commits", &opts->keep_redundant_commits, N_("deprecated: use --empty=keep instead")),
144
0
      OPT_CALLBACK_F(0, "empty", &empty_opt, "(stop|drop|keep)",
145
0
               N_("how to handle commits that become empty"),
146
0
               PARSE_OPT_NONEG, parse_opt_empty),
147
0
      OPT_END(),
148
0
    };
149
0
    options = parse_options_concat(options, cp_extra);
150
0
  } else if (opts->action == REPLAY_REVERT) {
151
0
    struct option cp_extra[] = {
152
0
      OPT_BOOL(0, "reference", &opts->commit_use_reference,
153
0
         N_("use the 'reference' format to refer to commits")),
154
0
      OPT_END(),
155
0
    };
156
0
    options = parse_options_concat(options, cp_extra);
157
0
  }
158
159
0
  argc = parse_options(argc, argv, prefix, options, usage_str,
160
0
      PARSE_OPT_KEEP_ARGV0 |
161
0
      PARSE_OPT_KEEP_UNKNOWN_OPT);
162
163
0
  prepare_repo_settings(the_repository);
164
0
  the_repository->settings.command_requires_full_index = 0;
165
166
0
  if (opts->action == REPLAY_PICK) {
167
0
    opts->drop_redundant_commits = (empty_opt == DROP_EMPTY_COMMIT);
168
0
    opts->keep_redundant_commits = opts->keep_redundant_commits || (empty_opt == KEEP_EMPTY_COMMIT);
169
0
  }
170
171
  /* implies allow_empty */
172
0
  if (opts->keep_redundant_commits)
173
0
    opts->allow_empty = 1;
174
175
0
  if (cleanup_arg) {
176
0
    opts->default_msg_cleanup = get_cleanup_mode(cleanup_arg, 1);
177
0
    opts->explicit_cleanup = 1;
178
0
  }
179
180
  /* Check for incompatible command line arguments */
181
0
  if (cmd) {
182
0
    const char *this_operation;
183
0
    if (cmd == 'q')
184
0
      this_operation = "--quit";
185
0
    else if (cmd == 'c')
186
0
      this_operation = "--continue";
187
0
    else if (cmd == 's')
188
0
      this_operation = "--skip";
189
0
    else {
190
0
      assert(cmd == 'a');
191
0
      this_operation = "--abort";
192
0
    }
193
194
0
    verify_opt_compatible(me, this_operation,
195
0
        "--no-commit", opts->no_commit,
196
0
        "--signoff", opts->signoff,
197
0
        "--mainline", opts->mainline,
198
0
        "--strategy", opts->strategy ? 1 : 0,
199
0
        "--strategy-option", opts->xopts.nr ? 1 : 0,
200
0
        "-x", opts->record_origin,
201
0
        "--ff", opts->allow_ff,
202
0
        "--rerere-autoupdate", opts->allow_rerere_auto == RERERE_AUTOUPDATE,
203
0
        "--no-rerere-autoupdate", opts->allow_rerere_auto == RERERE_NOAUTOUPDATE,
204
0
        "--keep-redundant-commits", opts->keep_redundant_commits,
205
0
        "--empty", empty_opt != EMPTY_COMMIT_UNSPECIFIED,
206
0
        NULL);
207
0
  }
208
209
0
  if (!opts->strategy && opts->default_strategy) {
210
0
    opts->strategy = opts->default_strategy;
211
0
    opts->default_strategy = NULL;
212
0
  }
213
214
0
  if (opts->allow_ff)
215
0
    verify_opt_compatible(me, "--ff",
216
0
        "--signoff", opts->signoff,
217
0
        "--no-commit", opts->no_commit,
218
0
        "-x", opts->record_origin,
219
0
        "--edit", opts->edit > 0,
220
0
        NULL);
221
222
0
  if (cmd) {
223
0
    opts->revs = NULL;
224
0
  } else {
225
0
    struct setup_revision_opt s_r_opt;
226
0
    opts->revs = xmalloc(sizeof(*opts->revs));
227
0
    repo_init_revisions(the_repository, opts->revs, NULL);
228
0
    opts->revs->no_walk = 1;
229
0
    opts->revs->unsorted_input = 1;
230
0
    if (argc < 2)
231
0
      usage_with_options(usage_str, options);
232
0
    if (!strcmp(argv[1], "-"))
233
0
      argv[1] = "@{-1}";
234
0
    memset(&s_r_opt, 0, sizeof(s_r_opt));
235
0
    s_r_opt.assume_dashdash = 1;
236
0
    argc = setup_revisions(argc, argv, opts->revs, &s_r_opt);
237
0
  }
238
239
0
  if (argc > 1)
240
0
    usage_with_options(usage_str, options);
241
242
  /* These option values will be free()d */
243
0
  opts->gpg_sign = xstrdup_or_null(opts->gpg_sign);
244
0
  opts->strategy = xstrdup_or_null(opts->strategy);
245
0
  if (!opts->strategy && getenv("GIT_TEST_MERGE_ALGORITHM"))
246
0
    opts->strategy = xstrdup(getenv("GIT_TEST_MERGE_ALGORITHM"));
247
0
  free(options);
248
249
0
  if (cmd == 'q') {
250
0
    int ret = sequencer_remove_state(opts);
251
0
    if (!ret)
252
0
      remove_branch_state(the_repository, 0);
253
0
    return ret;
254
0
  }
255
0
  if (cmd == 'c')
256
0
    return sequencer_continue(the_repository, opts);
257
0
  if (cmd == 'a')
258
0
    return sequencer_rollback(the_repository, opts);
259
0
  if (cmd == 's')
260
0
    return sequencer_skip(the_repository, opts);
261
0
  return sequencer_pick_revisions(the_repository, opts);
262
0
}
263
264
int cmd_revert(int argc, const char **argv, const char *prefix)
265
0
{
266
0
  struct replay_opts opts = REPLAY_OPTS_INIT;
267
0
  int res;
268
269
0
  opts.action = REPLAY_REVERT;
270
0
  sequencer_init_config(&opts);
271
0
  res = run_sequencer(argc, argv, prefix, &opts);
272
0
  if (res < 0)
273
0
    die(_("revert failed"));
274
0
  replay_opts_release(&opts);
275
0
  return res;
276
0
}
277
278
int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
279
0
{
280
0
  struct replay_opts opts = REPLAY_OPTS_INIT;
281
0
  int res;
282
283
0
  opts.action = REPLAY_PICK;
284
0
  sequencer_init_config(&opts);
285
0
  res = run_sequencer(argc, argv, prefix, &opts);
286
0
  if (res < 0)
287
0
    die(_("cherry-pick failed"));
288
0
  replay_opts_release(&opts);
289
0
  return res;
290
0
}