blob: b58c714cb8935efd08c81c8ebfd8922e1704c8f4 [file] [log] [blame]
Christian Couder54b0c1e2009-02-02 06:12:44 +01001/*
2 * Builtin "git replace"
3 *
4 * Copyright (c) 2008 Christian Couder <chriscool@tuxfamily.org>
5 *
Phil Hord09b7e222013-06-18 13:44:58 -04006 * Based on builtin/tag.c by Kristian Høgsberg <krh@redhat.com>
Christian Couder54b0c1e2009-02-02 06:12:44 +01007 * and Carlos Rica <jasampler@gmail.com> that was itself based on
8 * git-tag.sh and mktag.c by Linus Torvalds.
9 */
10
11#include "cache.h"
12#include "builtin.h"
13#include "refs.h"
14#include "parse-options.h"
Jeff Kingb892bb42014-04-26 22:00:57 +020015#include "run-command.h"
Christian Couder25a05a82014-07-19 17:01:14 +020016#include "tag.h"
Christian Couder54b0c1e2009-02-02 06:12:44 +010017
18static const char * const git_replace_usage[] = {
Nguyễn Thái Ngọc Duy2477beb2012-08-20 19:32:36 +070019 N_("git replace [-f] <object> <replacement>"),
Christian Couderab77c302014-05-17 14:16:38 +020020 N_("git replace [-f] --edit <object>"),
Christian Couder4228e8b2014-07-19 17:01:08 +020021 N_("git replace [-f] --graft <commit> [<parent>...]"),
Nguyễn Thái Ngọc Duy2477beb2012-08-20 19:32:36 +070022 N_("git replace -d <object>..."),
Christian Couder44f9f852013-12-11 08:46:10 +010023 N_("git replace [--format=<format>] [-l [<pattern>]]"),
Christian Couder54b0c1e2009-02-02 06:12:44 +010024 NULL
25};
26
Christian Couder663a8562013-12-28 12:00:05 +010027enum replace_format {
Jeff King3cc9d872014-06-24 05:43:07 -040028 REPLACE_FORMAT_SHORT,
29 REPLACE_FORMAT_MEDIUM,
30 REPLACE_FORMAT_LONG
Christian Couder663a8562013-12-28 12:00:05 +010031};
Christian Couder44f9f852013-12-11 08:46:10 +010032
33struct show_data {
34 const char *pattern;
Christian Couder663a8562013-12-28 12:00:05 +010035 enum replace_format format;
Christian Couder44f9f852013-12-11 08:46:10 +010036};
37
Michael Haggertyd70d7a82015-05-25 18:38:44 +000038static int show_reference(const char *refname, const struct object_id *oid,
Christian Couder54b0c1e2009-02-02 06:12:44 +010039 int flag, void *cb_data)
40{
Christian Couder44f9f852013-12-11 08:46:10 +010041 struct show_data *data = cb_data;
Christian Couder54b0c1e2009-02-02 06:12:44 +010042
Nguyễn Thái Ngọc Duyeb078942014-02-15 09:01:46 +070043 if (!wildmatch(data->pattern, refname, 0, NULL)) {
Christian Couder663a8562013-12-28 12:00:05 +010044 if (data->format == REPLACE_FORMAT_SHORT)
Christian Couder44f9f852013-12-11 08:46:10 +010045 printf("%s\n", refname);
Christian Couder663a8562013-12-28 12:00:05 +010046 else if (data->format == REPLACE_FORMAT_MEDIUM)
Michael Haggertyd70d7a82015-05-25 18:38:44 +000047 printf("%s -> %s\n", refname, oid_to_hex(oid));
Christian Couder663a8562013-12-28 12:00:05 +010048 else { /* data->format == REPLACE_FORMAT_LONG */
Michael Haggertyd70d7a82015-05-25 18:38:44 +000049 struct object_id object;
Christian Couder44f9f852013-12-11 08:46:10 +010050 enum object_type obj_type, repl_type;
51
Michael Haggertyd70d7a82015-05-25 18:38:44 +000052 if (get_sha1(refname, object.hash))
Christian Couder44f9f852013-12-11 08:46:10 +010053 return error("Failed to resolve '%s' as a valid ref.", refname);
54
Michael Haggertyd70d7a82015-05-25 18:38:44 +000055 obj_type = sha1_object_info(object.hash, NULL);
56 repl_type = sha1_object_info(oid->hash, NULL);
Christian Couder44f9f852013-12-11 08:46:10 +010057
58 printf("%s (%s) -> %s (%s)\n", refname, typename(obj_type),
Michael Haggertyd70d7a82015-05-25 18:38:44 +000059 oid_to_hex(oid), typename(repl_type));
Christian Couder44f9f852013-12-11 08:46:10 +010060 }
61 }
Christian Couder54b0c1e2009-02-02 06:12:44 +010062
63 return 0;
64}
65
Christian Couder44f9f852013-12-11 08:46:10 +010066static int list_replace_refs(const char *pattern, const char *format)
Christian Couder54b0c1e2009-02-02 06:12:44 +010067{
Christian Couder44f9f852013-12-11 08:46:10 +010068 struct show_data data;
69
Christian Couder54b0c1e2009-02-02 06:12:44 +010070 if (pattern == NULL)
71 pattern = "*";
Christian Couder44f9f852013-12-11 08:46:10 +010072 data.pattern = pattern;
Christian Couder54b0c1e2009-02-02 06:12:44 +010073
Christian Couder44f9f852013-12-11 08:46:10 +010074 if (format == NULL || *format == '\0' || !strcmp(format, "short"))
Christian Couder663a8562013-12-28 12:00:05 +010075 data.format = REPLACE_FORMAT_SHORT;
Christian Couder44f9f852013-12-11 08:46:10 +010076 else if (!strcmp(format, "medium"))
Christian Couder663a8562013-12-28 12:00:05 +010077 data.format = REPLACE_FORMAT_MEDIUM;
78 else if (!strcmp(format, "long"))
79 data.format = REPLACE_FORMAT_LONG;
Christian Couder44f9f852013-12-11 08:46:10 +010080 else
81 die("invalid replace format '%s'\n"
Christian Couder663a8562013-12-28 12:00:05 +010082 "valid formats are 'short', 'medium' and 'long'\n",
Christian Couder44f9f852013-12-11 08:46:10 +010083 format);
84
Michael Haggertyd70d7a82015-05-25 18:38:44 +000085 for_each_replace_ref(show_reference, (void *)&data);
Christian Couder54b0c1e2009-02-02 06:12:44 +010086
87 return 0;
88}
89
90typedef int (*each_replace_name_fn)(const char *name, const char *ref,
91 const unsigned char *sha1);
92
93static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
94{
Michael J Gruber9dfc3682012-11-13 11:34:11 +010095 const char **p, *full_hex;
Christian Couder54b0c1e2009-02-02 06:12:44 +010096 char ref[PATH_MAX];
97 int had_error = 0;
98 unsigned char sha1[20];
99
100 for (p = argv; *p; p++) {
Michael J Gruber9dfc3682012-11-13 11:34:11 +0100101 if (get_sha1(*p, sha1)) {
102 error("Failed to resolve '%s' as a valid ref.", *p);
Christian Couder54b0c1e2009-02-02 06:12:44 +0100103 had_error = 1;
104 continue;
105 }
Michael J Gruber9dfc3682012-11-13 11:34:11 +0100106 full_hex = sha1_to_hex(sha1);
Mike Hommey58d121b2015-06-12 06:34:59 +0900107 snprintf(ref, sizeof(ref), "%s%s", git_replace_ref_base, full_hex);
Michael J Gruber9dfc3682012-11-13 11:34:11 +0100108 /* read_ref() may reuse the buffer */
Mike Hommey58d121b2015-06-12 06:34:59 +0900109 full_hex = ref + strlen(git_replace_ref_base);
Nguyễn Thái Ngọc Duyc6893322011-11-13 17:22:14 +0700110 if (read_ref(ref, sha1)) {
Michael J Gruber9dfc3682012-11-13 11:34:11 +0100111 error("replace ref '%s' not found.", full_hex);
Christian Couder54b0c1e2009-02-02 06:12:44 +0100112 had_error = 1;
113 continue;
114 }
Michael J Gruber9dfc3682012-11-13 11:34:11 +0100115 if (fn(full_hex, ref, sha1))
Christian Couder54b0c1e2009-02-02 06:12:44 +0100116 had_error = 1;
117 }
118 return had_error;
119}
120
121static int delete_replace_ref(const char *name, const char *ref,
122 const unsigned char *sha1)
123{
124 if (delete_ref(ref, sha1, 0))
125 return 1;
126 printf("Deleted replace ref '%s'\n", name);
127 return 0;
128}
129
Christian Couderb6e38842014-05-17 14:16:35 +0200130static void check_ref_valid(unsigned char object[20],
131 unsigned char prev[20],
132 char *ref,
133 int ref_size,
134 int force)
135{
136 if (snprintf(ref, ref_size,
Mike Hommey58d121b2015-06-12 06:34:59 +0900137 "%s%s", git_replace_ref_base,
Christian Couderb6e38842014-05-17 14:16:35 +0200138 sha1_to_hex(object)) > ref_size - 1)
139 die("replace ref name too long: %.*s...", 50, ref);
140 if (check_refname_format(ref, 0))
141 die("'%s' is not a valid ref name.", ref);
142
143 if (read_ref(ref, prev))
144 hashclr(prev);
145 else if (!force)
146 die("replace ref '%s' already exists", ref);
147}
148
Jeff King479bd752014-04-26 22:00:56 +0200149static int replace_object_sha1(const char *object_ref,
150 unsigned char object[20],
151 const char *replace_ref,
152 unsigned char repl[20],
153 int force)
Christian Couderbebdd272009-02-02 06:12:53 +0100154{
Jeff King479bd752014-04-26 22:00:56 +0200155 unsigned char prev[20];
Christian Couder277336a2013-09-06 07:10:53 +0200156 enum object_type obj_type, repl_type;
Christian Couderbebdd272009-02-02 06:12:53 +0100157 char ref[PATH_MAX];
Ronnie Sahlberg867c2fa2014-04-16 15:32:29 -0700158 struct ref_transaction *transaction;
159 struct strbuf err = STRBUF_INIT;
Christian Couderbebdd272009-02-02 06:12:53 +0100160
Christian Couder277336a2013-09-06 07:10:53 +0200161 obj_type = sha1_object_info(object, NULL);
162 repl_type = sha1_object_info(repl, NULL);
163 if (!force && obj_type != repl_type)
164 die("Objects must be of the same type.\n"
165 "'%s' points to a replaced object of type '%s'\n"
166 "while '%s' points to a replacement object of type '%s'.",
167 object_ref, typename(obj_type),
168 replace_ref, typename(repl_type));
169
Christian Couderb6e38842014-05-17 14:16:35 +0200170 check_ref_valid(object, prev, ref, sizeof(ref), force);
Christian Couderbebdd272009-02-02 06:12:53 +0100171
Ronnie Sahlberg867c2fa2014-04-16 15:32:29 -0700172 transaction = ref_transaction_begin(&err);
173 if (!transaction ||
Ronnie Sahlbergdb7516a2014-04-30 12:22:42 -0700174 ref_transaction_update(transaction, ref, repl, prev,
Michael Haggerty1d147bd2015-02-17 18:00:15 +0100175 0, NULL, &err) ||
Ronnie Sahlbergdb7516a2014-04-30 12:22:42 -0700176 ref_transaction_commit(transaction, &err))
Ronnie Sahlberg867c2fa2014-04-16 15:32:29 -0700177 die("%s", err.buf);
Christian Couderbebdd272009-02-02 06:12:53 +0100178
Ronnie Sahlberg867c2fa2014-04-16 15:32:29 -0700179 ref_transaction_free(transaction);
Christian Couderbebdd272009-02-02 06:12:53 +0100180 return 0;
181}
182
Jeff King479bd752014-04-26 22:00:56 +0200183static int replace_object(const char *object_ref, const char *replace_ref, int force)
184{
185 unsigned char object[20], repl[20];
186
187 if (get_sha1(object_ref, object))
188 die("Failed to resolve '%s' as a valid ref.", object_ref);
189 if (get_sha1(replace_ref, repl))
190 die("Failed to resolve '%s' as a valid ref.", replace_ref);
191
192 return replace_object_sha1(object_ref, object, replace_ref, repl, force);
193}
194
Jeff Kingb892bb42014-04-26 22:00:57 +0200195/*
Jeff King2deda622014-06-24 05:46:31 -0400196 * Write the contents of the object named by "sha1" to the file "filename".
197 * If "raw" is true, then the object's raw contents are printed according to
198 * "type". Otherwise, we pretty-print the contents for human editing.
Jeff Kingb892bb42014-04-26 22:00:57 +0200199 */
Jeff King2deda622014-06-24 05:46:31 -0400200static void export_object(const unsigned char *sha1, enum object_type type,
201 int raw, const char *filename)
Jeff Kingb892bb42014-04-26 22:00:57 +0200202{
René Scharfed3180272014-08-19 21:09:35 +0200203 struct child_process cmd = CHILD_PROCESS_INIT;
Jeff Kingb892bb42014-04-26 22:00:57 +0200204 int fd;
205
206 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
207 if (fd < 0)
208 die_errno("unable to open %s for writing", filename);
209
Jeff King36857e02014-06-24 05:46:05 -0400210 argv_array_push(&cmd.args, "--no-replace-objects");
211 argv_array_push(&cmd.args, "cat-file");
Jeff King2deda622014-06-24 05:46:31 -0400212 if (raw)
213 argv_array_push(&cmd.args, typename(type));
214 else
215 argv_array_push(&cmd.args, "-p");
Jeff King36857e02014-06-24 05:46:05 -0400216 argv_array_push(&cmd.args, sha1_to_hex(sha1));
Jeff Kingb892bb42014-04-26 22:00:57 +0200217 cmd.git_cmd = 1;
218 cmd.out = fd;
219
220 if (run_command(&cmd))
221 die("cat-file reported failure");
Jeff Kingb892bb42014-04-26 22:00:57 +0200222}
223
224/*
225 * Read a previously-exported (and possibly edited) object back from "filename",
226 * interpreting it as "type", and writing the result to the object database.
227 * The sha1 of the written object is returned via sha1.
228 */
229static void import_object(unsigned char *sha1, enum object_type type,
Jeff King2deda622014-06-24 05:46:31 -0400230 int raw, const char *filename)
Jeff Kingb892bb42014-04-26 22:00:57 +0200231{
232 int fd;
233
234 fd = open(filename, O_RDONLY);
235 if (fd < 0)
236 die_errno("unable to open %s for reading", filename);
237
Jeff King2deda622014-06-24 05:46:31 -0400238 if (!raw && type == OBJ_TREE) {
Jeff Kingb892bb42014-04-26 22:00:57 +0200239 const char *argv[] = { "mktree", NULL };
René Scharfed3180272014-08-19 21:09:35 +0200240 struct child_process cmd = CHILD_PROCESS_INIT;
Jeff Kingb892bb42014-04-26 22:00:57 +0200241 struct strbuf result = STRBUF_INIT;
242
243 cmd.argv = argv;
244 cmd.git_cmd = 1;
245 cmd.in = fd;
246 cmd.out = -1;
247
248 if (start_command(&cmd))
249 die("unable to spawn mktree");
250
251 if (strbuf_read(&result, cmd.out, 41) < 0)
252 die_errno("unable to read from mktree");
253 close(cmd.out);
254
255 if (finish_command(&cmd))
256 die("mktree reported failure");
257 if (get_sha1_hex(result.buf, sha1) < 0)
258 die("mktree did not return an object name");
259
260 strbuf_release(&result);
261 } else {
262 struct stat st;
263 int flags = HASH_FORMAT_CHECK | HASH_WRITE_OBJECT;
264
265 if (fstat(fd, &st) < 0)
266 die_errno("unable to fstat %s", filename);
267 if (index_fd(sha1, fd, &st, type, NULL, flags) < 0)
268 die("unable to write object to database");
269 /* index_fd close()s fd for us */
270 }
271
272 /*
273 * No need to close(fd) here; both run-command and index-fd
274 * will have done it for us.
275 */
276}
277
Jeff King2deda622014-06-24 05:46:31 -0400278static int edit_and_replace(const char *object_ref, int force, int raw)
Jeff Kingb892bb42014-04-26 22:00:57 +0200279{
280 char *tmpfile = git_pathdup("REPLACE_EDITOBJ");
281 enum object_type type;
Christian Couder24790832014-05-17 14:16:36 +0200282 unsigned char old[20], new[20], prev[20];
283 char ref[PATH_MAX];
Jeff Kingb892bb42014-04-26 22:00:57 +0200284
285 if (get_sha1(object_ref, old) < 0)
286 die("Not a valid object name: '%s'", object_ref);
287
288 type = sha1_object_info(old, NULL);
289 if (type < 0)
290 die("unable to get object type for %s", sha1_to_hex(old));
291
Christian Couder24790832014-05-17 14:16:36 +0200292 check_ref_valid(old, prev, ref, sizeof(ref), force);
293
Jeff King2deda622014-06-24 05:46:31 -0400294 export_object(old, type, raw, tmpfile);
Jeff Kingb892bb42014-04-26 22:00:57 +0200295 if (launch_editor(tmpfile, NULL, NULL) < 0)
296 die("editing object file failed");
Jeff King2deda622014-06-24 05:46:31 -0400297 import_object(new, type, raw, tmpfile);
Jeff Kingb892bb42014-04-26 22:00:57 +0200298
299 free(tmpfile);
300
Christian Couderf22166b2014-05-17 14:16:34 +0200301 if (!hashcmp(old, new))
302 return error("new object is the same as the old one: '%s'", sha1_to_hex(old));
303
Jeff Kingb892bb42014-04-26 22:00:57 +0200304 return replace_object_sha1(object_ref, old, "replacement", new, force);
305}
306
Christian Couder4228e8b2014-07-19 17:01:08 +0200307static void replace_parents(struct strbuf *buf, int argc, const char **argv)
308{
309 struct strbuf new_parents = STRBUF_INIT;
310 const char *parent_start, *parent_end;
311 int i;
312
313 /* find existing parents */
314 parent_start = buf->buf;
315 parent_start += 46; /* "tree " + "hex sha1" + "\n" */
316 parent_end = parent_start;
317
318 while (starts_with(parent_end, "parent "))
319 parent_end += 48; /* "parent " + "hex sha1" + "\n" */
320
321 /* prepare new parents */
322 for (i = 0; i < argc; i++) {
323 unsigned char sha1[20];
324 if (get_sha1(argv[i], sha1) < 0)
325 die(_("Not a valid object name: '%s'"), argv[i]);
326 lookup_commit_or_die(sha1, argv[i]);
327 strbuf_addf(&new_parents, "parent %s\n", sha1_to_hex(sha1));
328 }
329
330 /* replace existing parents with new ones */
331 strbuf_splice(buf, parent_start - buf->buf, parent_end - parent_start,
332 new_parents.buf, new_parents.len);
333
334 strbuf_release(&new_parents);
335}
336
Christian Couder25a05a82014-07-19 17:01:14 +0200337struct check_mergetag_data {
338 int argc;
339 const char **argv;
340};
341
342static void check_one_mergetag(struct commit *commit,
343 struct commit_extra_header *extra,
344 void *data)
345{
346 struct check_mergetag_data *mergetag_data = (struct check_mergetag_data *)data;
347 const char *ref = mergetag_data->argv[0];
348 unsigned char tag_sha1[20];
349 struct tag *tag;
350 int i;
351
352 hash_sha1_file(extra->value, extra->len, typename(OBJ_TAG), tag_sha1);
353 tag = lookup_tag(tag_sha1);
354 if (!tag)
355 die(_("bad mergetag in commit '%s'"), ref);
356 if (parse_tag_buffer(tag, extra->value, extra->len))
357 die(_("malformed mergetag in commit '%s'"), ref);
358
359 /* iterate over new parents */
360 for (i = 1; i < mergetag_data->argc; i++) {
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000361 struct object_id oid;
362 if (get_sha1(mergetag_data->argv[i], oid.hash) < 0)
Christian Couder25a05a82014-07-19 17:01:14 +0200363 die(_("Not a valid object name: '%s'"), mergetag_data->argv[i]);
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000364 if (!oidcmp(&tag->tagged->oid, &oid))
Christian Couder25a05a82014-07-19 17:01:14 +0200365 return; /* found */
366 }
367
368 die(_("original commit '%s' contains mergetag '%s' that is discarded; "
369 "use --edit instead of --graft"), ref, sha1_to_hex(tag_sha1));
370}
371
372static void check_mergetags(struct commit *commit, int argc, const char **argv)
373{
374 struct check_mergetag_data mergetag_data;
375
376 mergetag_data.argc = argc;
377 mergetag_data.argv = argv;
378 for_each_mergetag(check_one_mergetag, commit, &mergetag_data);
379}
380
Christian Couder4228e8b2014-07-19 17:01:08 +0200381static int create_graft(int argc, const char **argv, int force)
382{
383 unsigned char old[20], new[20];
384 const char *old_ref = argv[0];
385 struct commit *commit;
386 struct strbuf buf = STRBUF_INIT;
387 const char *buffer;
388 unsigned long size;
389
390 if (get_sha1(old_ref, old) < 0)
391 die(_("Not a valid object name: '%s'"), old_ref);
392 commit = lookup_commit_or_die(old, old_ref);
393
394 buffer = get_commit_buffer(commit, &size);
395 strbuf_add(&buf, buffer, size);
396 unuse_commit_buffer(commit, buffer);
397
398 replace_parents(&buf, argc - 1, &argv[1]);
399
Christian Couder0b05ab62014-07-19 17:01:12 +0200400 if (remove_signature(&buf)) {
401 warning(_("the original commit '%s' has a gpg signature."), old_ref);
402 warning(_("the signature will be removed in the replacement commit!"));
403 }
404
Christian Couder25a05a82014-07-19 17:01:14 +0200405 check_mergetags(commit, argc, argv);
406
Christian Couder4228e8b2014-07-19 17:01:08 +0200407 if (write_sha1_file(buf.buf, buf.len, commit_type, new))
408 die(_("could not write replacement commit for: '%s'"), old_ref);
409
410 strbuf_release(&buf);
411
412 if (!hashcmp(old, new))
413 return error("new commit is the same as the old one: '%s'", sha1_to_hex(old));
414
415 return replace_object_sha1(old_ref, old, "replacement", new, force);
416}
417
Christian Couder54b0c1e2009-02-02 06:12:44 +0100418int cmd_replace(int argc, const char **argv, const char *prefix)
419{
Jeff King70c7bd62014-04-26 22:00:55 +0200420 int force = 0;
Jeff King2deda622014-06-24 05:46:31 -0400421 int raw = 0;
Christian Couder44f9f852013-12-11 08:46:10 +0100422 const char *format = NULL;
Jeff King70c7bd62014-04-26 22:00:55 +0200423 enum {
424 MODE_UNSPECIFIED = 0,
425 MODE_LIST,
426 MODE_DELETE,
Jeff Kingb892bb42014-04-26 22:00:57 +0200427 MODE_EDIT,
Christian Couder4228e8b2014-07-19 17:01:08 +0200428 MODE_GRAFT,
Jeff King70c7bd62014-04-26 22:00:55 +0200429 MODE_REPLACE
430 } cmdmode = MODE_UNSPECIFIED;
Christian Couder54b0c1e2009-02-02 06:12:44 +0100431 struct option options[] = {
Jeff King70c7bd62014-04-26 22:00:55 +0200432 OPT_CMDMODE('l', "list", &cmdmode, N_("list replace refs"), MODE_LIST),
433 OPT_CMDMODE('d', "delete", &cmdmode, N_("delete replace refs"), MODE_DELETE),
Jeff Kingb892bb42014-04-26 22:00:57 +0200434 OPT_CMDMODE('e', "edit", &cmdmode, N_("edit existing object"), MODE_EDIT),
Christian Couder4228e8b2014-07-19 17:01:08 +0200435 OPT_CMDMODE('g', "graft", &cmdmode, N_("change a commit's parents"), MODE_GRAFT),
Jonathan Nieder80f165a2013-09-24 23:35:24 -0700436 OPT_BOOL('f', "force", &force, N_("replace the ref if it exists")),
Jeff King2deda622014-06-24 05:46:31 -0400437 OPT_BOOL(0, "raw", &raw, N_("do not pretty-print contents for --edit")),
Christian Couder44f9f852013-12-11 08:46:10 +0100438 OPT_STRING(0, "format", &format, N_("format"), N_("use this format")),
Christian Couder54b0c1e2009-02-02 06:12:44 +0100439 OPT_END()
440 };
441
Michael Haggertyafc711b2014-02-18 12:24:55 +0100442 check_replace_refs = 0;
Johannes Schindelin36b14372016-04-20 08:38:03 +0200443 git_config(git_default_config, NULL);
Christian Couder769a4fa2013-12-11 08:46:12 +0100444
Christian Couder451bb212009-02-02 06:12:58 +0100445 argc = parse_options(argc, argv, prefix, options, git_replace_usage, 0);
Christian Couder54b0c1e2009-02-02 06:12:44 +0100446
Jeff King70c7bd62014-04-26 22:00:55 +0200447 if (!cmdmode)
448 cmdmode = argc ? MODE_REPLACE : MODE_LIST;
Jeff King3f495f62014-04-26 22:00:54 +0200449
Jeff King70c7bd62014-04-26 22:00:55 +0200450 if (format && cmdmode != MODE_LIST)
Jeff King3f495f62014-04-26 22:00:54 +0200451 usage_msg_opt("--format cannot be used when not listing",
Christian Couder44f9f852013-12-11 08:46:10 +0100452 git_replace_usage, options);
453
Christian Couder4228e8b2014-07-19 17:01:08 +0200454 if (force &&
455 cmdmode != MODE_REPLACE &&
456 cmdmode != MODE_EDIT &&
457 cmdmode != MODE_GRAFT)
Jeff King70c7bd62014-04-26 22:00:55 +0200458 usage_msg_opt("-f only makes sense when writing a replacement",
Christian Couder86af2ca2009-02-02 06:13:06 +0100459 git_replace_usage, options);
Christian Couderbebdd272009-02-02 06:12:53 +0100460
Jeff King2deda622014-06-24 05:46:31 -0400461 if (raw && cmdmode != MODE_EDIT)
462 usage_msg_opt("--raw only makes sense with --edit",
463 git_replace_usage, options);
464
Jeff King70c7bd62014-04-26 22:00:55 +0200465 switch (cmdmode) {
466 case MODE_DELETE:
Christian Couder54b0c1e2009-02-02 06:12:44 +0100467 if (argc < 1)
Christian Couder86af2ca2009-02-02 06:13:06 +0100468 usage_msg_opt("-d needs at least one argument",
469 git_replace_usage, options);
Christian Couder54b0c1e2009-02-02 06:12:44 +0100470 return for_each_replace_name(argv, delete_replace_ref);
Christian Couder54b0c1e2009-02-02 06:12:44 +0100471
Jeff King70c7bd62014-04-26 22:00:55 +0200472 case MODE_REPLACE:
Christian Couderbebdd272009-02-02 06:12:53 +0100473 if (argc != 2)
Christian Couder86af2ca2009-02-02 06:13:06 +0100474 usage_msg_opt("bad number of arguments",
475 git_replace_usage, options);
Christian Couderbebdd272009-02-02 06:12:53 +0100476 return replace_object(argv[0], argv[1], force);
Jeff King70c7bd62014-04-26 22:00:55 +0200477
Jeff Kingb892bb42014-04-26 22:00:57 +0200478 case MODE_EDIT:
479 if (argc != 1)
480 usage_msg_opt("-e needs exactly one argument",
481 git_replace_usage, options);
Jeff King2deda622014-06-24 05:46:31 -0400482 return edit_and_replace(argv[0], force, raw);
Jeff Kingb892bb42014-04-26 22:00:57 +0200483
Christian Couder4228e8b2014-07-19 17:01:08 +0200484 case MODE_GRAFT:
485 if (argc < 1)
486 usage_msg_opt("-g needs at least one argument",
487 git_replace_usage, options);
488 return create_graft(argc, argv, force);
489
Jeff King70c7bd62014-04-26 22:00:55 +0200490 case MODE_LIST:
491 if (argc > 1)
492 usage_msg_opt("only one pattern can be given with -l",
493 git_replace_usage, options);
494 return list_replace_refs(argv[0], format);
495
496 default:
497 die("BUG: invalid cmdmode %d", (int)cmdmode);
Christian Couderbebdd272009-02-02 06:12:53 +0100498 }
Christian Couder54b0c1e2009-02-02 06:12:44 +0100499}