Sixth batch
[git/git.git] / builtin / replace.c
CommitLineData
54b0c1e0
CC
1/*
2 * Builtin "git replace"
3 *
4 * Copyright (c) 2008 Christian Couder <chriscool@tuxfamily.org>
5 *
09b7e220 6 * Based on builtin/tag.c by Kristian Høgsberg <krh@redhat.com>
54b0c1e0
CC
7 * 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"
b2141fc1 12#include "config.h"
54b0c1e0
CC
13#include "builtin.h"
14#include "refs.h"
15#include "parse-options.h"
b892bb45 16#include "run-command.h"
60ce76d3
SB
17#include "object-store.h"
18#include "repository.h"
25a05a8c 19#include "tag.h"
54b0c1e0
CC
20
21static const char * const git_replace_usage[] = {
2477bebb 22 N_("git replace [-f] <object> <replacement>"),
ab77c309 23 N_("git replace [-f] --edit <object>"),
4228e8bc 24 N_("git replace [-f] --graft <commit> [<parent>...]"),
fb404291 25 N_("git replace [-f] --convert-graft-file"),
2477bebb 26 N_("git replace -d <object>..."),
44f9f850 27 N_("git replace [--format=<format>] [-l [<pattern>]]"),
54b0c1e0
CC
28 NULL
29};
30
663a8566 31enum replace_format {
3cc9d877
JK
32 REPLACE_FORMAT_SHORT,
33 REPLACE_FORMAT_MEDIUM,
34 REPLACE_FORMAT_LONG
663a8566 35};
44f9f850
CC
36
37struct show_data {
38 const char *pattern;
663a8566 39 enum replace_format format;
44f9f850
CC
40};
41
212e0f7e
SB
42static int show_reference(struct repository *r, const char *refname,
43 const struct object_id *oid,
54b0c1e0
CC
44 int flag, void *cb_data)
45{
44f9f850
CC
46 struct show_data *data = cb_data;
47
55d34269 48 if (!wildmatch(data->pattern, refname, 0)) {
663a8566 49 if (data->format == REPLACE_FORMAT_SHORT)
44f9f850 50 printf("%s\n", refname);
663a8566 51 else if (data->format == REPLACE_FORMAT_MEDIUM)
d70d7a8a 52 printf("%s -> %s\n", refname, oid_to_hex(oid));
663a8566 53 else { /* data->format == REPLACE_FORMAT_LONG */
d70d7a8a 54 struct object_id object;
44f9f850 55 enum object_type obj_type, repl_type;
54b0c1e0 56
e82caf38 57 if (get_oid(refname, &object))
225c62e0 58 return error(_("failed to resolve '%s' as a valid ref"), refname);
44f9f850 59
212e0f7e
SB
60 obj_type = oid_object_info(r, &object, NULL);
61 repl_type = oid_object_info(r, oid, NULL);
44f9f850 62
debca9d2
BW
63 printf("%s (%s) -> %s (%s)\n", refname, type_name(obj_type),
64 oid_to_hex(oid), type_name(repl_type));
44f9f850
CC
65 }
66 }
54b0c1e0
CC
67
68 return 0;
69}
70
44f9f850 71static int list_replace_refs(const char *pattern, const char *format)
54b0c1e0 72{
44f9f850
CC
73 struct show_data data;
74
54b0c1e0
CC
75 if (pattern == NULL)
76 pattern = "*";
44f9f850
CC
77 data.pattern = pattern;
78
79 if (format == NULL || *format == '\0' || !strcmp(format, "short"))
663a8566 80 data.format = REPLACE_FORMAT_SHORT;
44f9f850 81 else if (!strcmp(format, "medium"))
663a8566
CC
82 data.format = REPLACE_FORMAT_MEDIUM;
83 else if (!strcmp(format, "long"))
84 data.format = REPLACE_FORMAT_LONG;
5a59a230
NTND
85 /*
86 * Please update _git_replace() in git-completion.bash when
87 * you add new format
88 */
44f9f850 89 else
225c62e0
NTND
90 return error(_("invalid replace format '%s'\n"
91 "valid formats are 'short', 'medium' and 'long'"),
e24e8719 92 format);
54b0c1e0 93
60ce76d3 94 for_each_replace_ref(the_repository, show_reference, (void *)&data);
54b0c1e0
CC
95
96 return 0;
97}
98
99typedef int (*each_replace_name_fn)(const char *name, const char *ref,
cea4332e 100 const struct object_id *oid);
54b0c1e0
CC
101
102static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
103{
9dfc3684 104 const char **p, *full_hex;
7f897b6f
JK
105 struct strbuf ref = STRBUF_INIT;
106 size_t base_len;
54b0c1e0 107 int had_error = 0;
cea4332e 108 struct object_id oid;
54b0c1e0 109
7f897b6f
JK
110 strbuf_addstr(&ref, git_replace_ref_base);
111 base_len = ref.len;
112
54b0c1e0 113 for (p = argv; *p; p++) {
cea4332e 114 if (get_oid(*p, &oid)) {
1a07e59c 115 error("failed to resolve '%s' as a valid ref", *p);
54b0c1e0
CC
116 had_error = 1;
117 continue;
118 }
7f897b6f
JK
119
120 strbuf_setlen(&ref, base_len);
121 strbuf_addstr(&ref, oid_to_hex(&oid));
122 full_hex = ref.buf + base_len;
123
34c290a6 124 if (read_ref(ref.buf, &oid)) {
225c62e0 125 error(_("replace ref '%s' not found"), full_hex);
54b0c1e0
CC
126 had_error = 1;
127 continue;
128 }
7f897b6f 129 if (fn(full_hex, ref.buf, &oid))
54b0c1e0
CC
130 had_error = 1;
131 }
372b050b 132 strbuf_release(&ref);
54b0c1e0
CC
133 return had_error;
134}
135
136static int delete_replace_ref(const char *name, const char *ref,
cea4332e 137 const struct object_id *oid)
54b0c1e0 138{
2616a5e5 139 if (delete_ref(NULL, ref, oid, 0))
54b0c1e0 140 return 1;
225c62e0 141 printf_ln(_("Deleted replace ref '%s'"), name);
54b0c1e0
CC
142 return 0;
143}
144
e24e8719 145static int check_ref_valid(struct object_id *object,
cea4332e 146 struct object_id *prev,
7f897b6f 147 struct strbuf *ref,
b6e38840
CC
148 int force)
149{
7f897b6f
JK
150 strbuf_reset(ref);
151 strbuf_addf(ref, "%s%s", git_replace_ref_base, oid_to_hex(object));
152 if (check_refname_format(ref->buf, 0))
225c62e0 153 return error(_("'%s' is not a valid ref name"), ref->buf);
7f897b6f 154
34c290a6 155 if (read_ref(ref->buf, prev))
cea4332e 156 oidclr(prev);
b6e38840 157 else if (!force)
225c62e0 158 return error(_("replace ref '%s' already exists"), ref->buf);
e24e8719 159 return 0;
b6e38840
CC
160}
161
cea4332e 162static int replace_object_oid(const char *object_ref,
163 struct object_id *object,
479bd757 164 const char *replace_ref,
cea4332e 165 struct object_id *repl,
479bd757 166 int force)
bebdd271 167{
cea4332e 168 struct object_id prev;
277336a5 169 enum object_type obj_type, repl_type;
7f897b6f 170 struct strbuf ref = STRBUF_INIT;
867c2fac
RS
171 struct ref_transaction *transaction;
172 struct strbuf err = STRBUF_INIT;
e24e8719 173 int res = 0;
bebdd271 174
0df8e965
SB
175 obj_type = oid_object_info(the_repository, object, NULL);
176 repl_type = oid_object_info(the_repository, repl, NULL);
277336a5 177 if (!force && obj_type != repl_type)
225c62e0
NTND
178 return error(_("Objects must be of the same type.\n"
179 "'%s' points to a replaced object of type '%s'\n"
180 "while '%s' points to a replacement object of "
181 "type '%s'."),
e24e8719
JS
182 object_ref, type_name(obj_type),
183 replace_ref, type_name(repl_type));
184
185 if (check_ref_valid(object, &prev, &ref, force)) {
186 strbuf_release(&ref);
187 return -1;
188 }
bebdd271 189
867c2fac
RS
190 transaction = ref_transaction_begin(&err);
191 if (!transaction ||
89f3bbdd 192 ref_transaction_update(transaction, ref.buf, repl, &prev,
1d147bdf 193 0, NULL, &err) ||
db7516ab 194 ref_transaction_commit(transaction, &err))
e24e8719 195 res = error("%s", err.buf);
bebdd271 196
867c2fac 197 ref_transaction_free(transaction);
7f897b6f 198 strbuf_release(&ref);
e24e8719 199 return res;
bebdd271
CC
200}
201
479bd757
JK
202static int replace_object(const char *object_ref, const char *replace_ref, int force)
203{
cea4332e 204 struct object_id object, repl;
479bd757 205
cea4332e 206 if (get_oid(object_ref, &object))
225c62e0 207 return error(_("failed to resolve '%s' as a valid ref"),
e24e8719 208 object_ref);
cea4332e 209 if (get_oid(replace_ref, &repl))
225c62e0 210 return error(_("failed to resolve '%s' as a valid ref"),
e24e8719 211 replace_ref);
479bd757 212
cea4332e 213 return replace_object_oid(object_ref, &object, replace_ref, &repl, force);
479bd757
JK
214}
215
b892bb45 216/*
2deda629
JK
217 * Write the contents of the object named by "sha1" to the file "filename".
218 * If "raw" is true, then the object's raw contents are printed according to
219 * "type". Otherwise, we pretty-print the contents for human editing.
b892bb45 220 */
e24e8719 221static int export_object(const struct object_id *oid, enum object_type type,
2deda629 222 int raw, const char *filename)
b892bb45 223{
d3180279 224 struct child_process cmd = CHILD_PROCESS_INIT;
b892bb45
JK
225 int fd;
226
227 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
228 if (fd < 0)
225c62e0 229 return error_errno(_("unable to open %s for writing"), filename);
b892bb45 230
36857e00
JK
231 argv_array_push(&cmd.args, "--no-replace-objects");
232 argv_array_push(&cmd.args, "cat-file");
2deda629 233 if (raw)
debca9d2 234 argv_array_push(&cmd.args, type_name(type));
2deda629
JK
235 else
236 argv_array_push(&cmd.args, "-p");
cea4332e 237 argv_array_push(&cmd.args, oid_to_hex(oid));
b892bb45
JK
238 cmd.git_cmd = 1;
239 cmd.out = fd;
240
241 if (run_command(&cmd))
225c62e0 242 return error(_("cat-file reported failure"));
e24e8719 243 return 0;
b892bb45
JK
244}
245
246/*
247 * Read a previously-exported (and possibly edited) object back from "filename",
248 * interpreting it as "type", and writing the result to the object database.
249 * The sha1 of the written object is returned via sha1.
250 */
e24e8719 251static int import_object(struct object_id *oid, enum object_type type,
2deda629 252 int raw, const char *filename)
b892bb45
JK
253{
254 int fd;
255
256 fd = open(filename, O_RDONLY);
257 if (fd < 0)
225c62e0 258 return error_errno(_("unable to open %s for reading"), filename);
b892bb45 259
2deda629 260 if (!raw && type == OBJ_TREE) {
b892bb45 261 const char *argv[] = { "mktree", NULL };
d3180279 262 struct child_process cmd = CHILD_PROCESS_INIT;
b892bb45
JK
263 struct strbuf result = STRBUF_INIT;
264
265 cmd.argv = argv;
266 cmd.git_cmd = 1;
267 cmd.in = fd;
268 cmd.out = -1;
269
e24e8719
JS
270 if (start_command(&cmd)) {
271 close(fd);
225c62e0 272 return error(_("unable to spawn mktree"));
e24e8719 273 }
b892bb45 274
e24e8719 275 if (strbuf_read(&result, cmd.out, 41) < 0) {
225c62e0 276 error_errno(_("unable to read from mktree"));
e24e8719
JS
277 close(fd);
278 close(cmd.out);
279 return -1;
280 }
b892bb45
JK
281 close(cmd.out);
282
e24e8719
JS
283 if (finish_command(&cmd)) {
284 strbuf_release(&result);
225c62e0 285 return error(_("mktree reported failure"));
e24e8719
JS
286 }
287 if (get_oid_hex(result.buf, oid) < 0) {
288 strbuf_release(&result);
225c62e0 289 return error(_("mktree did not return an object name"));
e24e8719 290 }
b892bb45
JK
291
292 strbuf_release(&result);
293 } else {
294 struct stat st;
295 int flags = HASH_FORMAT_CHECK | HASH_WRITE_OBJECT;
296
e24e8719 297 if (fstat(fd, &st) < 0) {
225c62e0 298 error_errno(_("unable to fstat %s"), filename);
e24e8719
JS
299 close(fd);
300 return -1;
301 }
f8adbec9 302 if (index_fd(the_repository->index, oid, fd, &st, type, NULL, flags) < 0)
225c62e0 303 return error(_("unable to write object to database"));
b892bb45
JK
304 /* index_fd close()s fd for us */
305 }
306
307 /*
308 * No need to close(fd) here; both run-command and index-fd
309 * will have done it for us.
310 */
e24e8719 311 return 0;
b892bb45
JK
312}
313
2deda629 314static int edit_and_replace(const char *object_ref, int force, int raw)
b892bb45 315{
e24e8719 316 char *tmpfile;
b892bb45 317 enum object_type type;
efdfe11f 318 struct object_id old_oid, new_oid, prev;
7f897b6f 319 struct strbuf ref = STRBUF_INIT;
b892bb45 320
efdfe11f 321 if (get_oid(object_ref, &old_oid) < 0)
225c62e0 322 return error(_("not a valid object name: '%s'"), object_ref);
b892bb45 323
0df8e965 324 type = oid_object_info(the_repository, &old_oid, NULL);
b892bb45 325 if (type < 0)
225c62e0 326 return error(_("unable to get object type for %s"),
e24e8719 327 oid_to_hex(&old_oid));
b892bb45 328
e24e8719
JS
329 if (check_ref_valid(&old_oid, &prev, &ref, force)) {
330 strbuf_release(&ref);
331 return -1;
332 }
7f897b6f 333 strbuf_release(&ref);
24790835 334
e24e8719
JS
335 tmpfile = git_pathdup("REPLACE_EDITOBJ");
336 if (export_object(&old_oid, type, raw, tmpfile)) {
337 free(tmpfile);
338 return -1;
339 }
340 if (launch_editor(tmpfile, NULL, NULL) < 0) {
341 free(tmpfile);
225c62e0 342 return error(_("editing object file failed"));
e24e8719
JS
343 }
344 if (import_object(&new_oid, type, raw, tmpfile)) {
345 free(tmpfile);
346 return -1;
347 }
b892bb45
JK
348 free(tmpfile);
349
4a7e27e9 350 if (oideq(&old_oid, &new_oid))
225c62e0 351 return error(_("new object is the same as the old one: '%s'"), oid_to_hex(&old_oid));
f22166b5 352
efdfe11f 353 return replace_object_oid(object_ref, &old_oid, "replacement", &new_oid, force);
b892bb45
JK
354}
355
e24e8719 356static int replace_parents(struct strbuf *buf, int argc, const char **argv)
4228e8bc
CC
357{
358 struct strbuf new_parents = STRBUF_INIT;
359 const char *parent_start, *parent_end;
360 int i;
361
362 /* find existing parents */
363 parent_start = buf->buf;
cea4332e 364 parent_start += GIT_SHA1_HEXSZ + 6; /* "tree " + "hex sha1" + "\n" */
4228e8bc
CC
365 parent_end = parent_start;
366
367 while (starts_with(parent_end, "parent "))
368 parent_end += 48; /* "parent " + "hex sha1" + "\n" */
369
370 /* prepare new parents */
371 for (i = 0; i < argc; i++) {
cea4332e 372 struct object_id oid;
f8e44a81
CC
373 struct commit *commit;
374
e24e8719
JS
375 if (get_oid(argv[i], &oid) < 0) {
376 strbuf_release(&new_parents);
1a07e59c 377 return error(_("not a valid object name: '%s'"),
e24e8719
JS
378 argv[i]);
379 }
f8e44a81
CC
380 commit = lookup_commit_reference(the_repository, &oid);
381 if (!commit) {
e24e8719 382 strbuf_release(&new_parents);
f8e44a81 383 return error(_("could not parse %s as a commit"), argv[i]);
e24e8719 384 }
f8e44a81 385 strbuf_addf(&new_parents, "parent %s\n", oid_to_hex(&commit->object.oid));
4228e8bc
CC
386 }
387
388 /* replace existing parents with new ones */
389 strbuf_splice(buf, parent_start - buf->buf, parent_end - parent_start,
390 new_parents.buf, new_parents.len);
391
392 strbuf_release(&new_parents);
e24e8719 393 return 0;
4228e8bc
CC
394}
395
25a05a8c
CC
396struct check_mergetag_data {
397 int argc;
398 const char **argv;
399};
400
fef461ea 401static int check_one_mergetag(struct commit *commit,
25a05a8c
CC
402 struct commit_extra_header *extra,
403 void *data)
404{
405 struct check_mergetag_data *mergetag_data = (struct check_mergetag_data *)data;
406 const char *ref = mergetag_data->argv[0];
cea4332e 407 struct object_id tag_oid;
25a05a8c
CC
408 struct tag *tag;
409 int i;
410
169c9c01 411 hash_object_file(extra->value, extra->len, type_name(OBJ_TAG), &tag_oid);
ce71efb7 412 tag = lookup_tag(the_repository, &tag_oid);
25a05a8c 413 if (!tag)
e24e8719 414 return error(_("bad mergetag in commit '%s'"), ref);
0e740fed 415 if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))
e24e8719 416 return error(_("malformed mergetag in commit '%s'"), ref);
25a05a8c
CC
417
418 /* iterate over new parents */
419 for (i = 1; i < mergetag_data->argc; i++) {
f2fd0760 420 struct object_id oid;
e82caf38 421 if (get_oid(mergetag_data->argv[i], &oid) < 0)
1a07e59c 422 return error(_("not a valid object name: '%s'"),
e24e8719 423 mergetag_data->argv[i]);
c77722b3 424 if (oideq(get_tagged_oid(tag), &oid))
fef461ea 425 return 0; /* found */
25a05a8c
CC
426 }
427
e24e8719
JS
428 return error(_("original commit '%s' contains mergetag '%s' that is "
429 "discarded; use --edit instead of --graft"), ref,
430 oid_to_hex(&tag_oid));
25a05a8c
CC
431}
432
fef461ea 433static int check_mergetags(struct commit *commit, int argc, const char **argv)
25a05a8c
CC
434{
435 struct check_mergetag_data mergetag_data;
436
437 mergetag_data.argc = argc;
438 mergetag_data.argv = argv;
fef461ea 439 return for_each_mergetag(check_one_mergetag, commit, &mergetag_data);
25a05a8c
CC
440}
441
041c98e2 442static int create_graft(int argc, const char **argv, int force, int gentle)
4228e8bc 443{
efdfe11f 444 struct object_id old_oid, new_oid;
4228e8bc
CC
445 const char *old_ref = argv[0];
446 struct commit *commit;
447 struct strbuf buf = STRBUF_INIT;
448 const char *buffer;
449 unsigned long size;
450
efdfe11f 451 if (get_oid(old_ref, &old_oid) < 0)
1a07e59c 452 return error(_("not a valid object name: '%s'"), old_ref);
2122f675 453 commit = lookup_commit_reference(the_repository, &old_oid);
e24e8719
JS
454 if (!commit)
455 return error(_("could not parse %s"), old_ref);
4228e8bc
CC
456
457 buffer = get_commit_buffer(commit, &size);
458 strbuf_add(&buf, buffer, size);
459 unuse_commit_buffer(commit, buffer);
460
e24e8719
JS
461 if (replace_parents(&buf, argc - 1, &argv[1]) < 0) {
462 strbuf_release(&buf);
463 return -1;
464 }
4228e8bc 465
0b05ab6f 466 if (remove_signature(&buf)) {
1a07e59c 467 warning(_("the original commit '%s' has a gpg signature"), old_ref);
0b05ab6f
CC
468 warning(_("the signature will be removed in the replacement commit!"));
469 }
470
e24e8719
JS
471 if (check_mergetags(commit, argc, argv)) {
472 strbuf_release(&buf);
473 return -1;
474 }
25a05a8c 475
e24e8719
JS
476 if (write_object_file(buf.buf, buf.len, commit_type, &new_oid)) {
477 strbuf_release(&buf);
478 return error(_("could not write replacement commit for: '%s'"),
479 old_ref);
480 }
4228e8bc
CC
481
482 strbuf_release(&buf);
483
ee521ec4 484 if (oideq(&commit->object.oid, &new_oid)) {
041c98e2 485 if (gentle) {
ee521ec4
CC
486 warning(_("graft for '%s' unnecessary"),
487 oid_to_hex(&commit->object.oid));
041c98e2
JS
488 return 0;
489 }
ee521ec4
CC
490 return error(_("new commit is the same as the old one: '%s'"),
491 oid_to_hex(&commit->object.oid));
041c98e2 492 }
4228e8bc 493
ee521ec4
CC
494 return replace_object_oid(old_ref, &commit->object.oid,
495 "replacement", &new_oid, force);
4228e8bc
CC
496}
497
fb404291
JS
498static int convert_graft_file(int force)
499{
b16b60f7 500 const char *graft_file = get_graft_file(the_repository);
fb404291
JS
501 FILE *fp = fopen_or_warn(graft_file, "r");
502 struct strbuf buf = STRBUF_INIT, err = STRBUF_INIT;
503 struct argv_array args = ARGV_ARRAY_INIT;
504
505 if (!fp)
506 return -1;
507
8821e90a 508 advice_graft_file_deprecated = 0;
fb404291
JS
509 while (strbuf_getline(&buf, fp) != EOF) {
510 if (*buf.buf == '#')
511 continue;
512
513 argv_array_split(&args, buf.buf);
514 if (args.argc && create_graft(args.argc, args.argv, force, 1))
515 strbuf_addf(&err, "\n\t%s", buf.buf);
516 argv_array_clear(&args);
517 }
518 fclose(fp);
519
520 strbuf_release(&buf);
521
522 if (!err.len)
523 return unlink_or_warn(graft_file);
524
525 warning(_("could not convert the following graft(s):\n%s"), err.buf);
526 strbuf_release(&err);
527
528 return -1;
529}
530
54b0c1e0
CC
531int cmd_replace(int argc, const char **argv, const char *prefix)
532{
70c7bd6d 533 int force = 0;
2deda629 534 int raw = 0;
44f9f850 535 const char *format = NULL;
70c7bd6d
JK
536 enum {
537 MODE_UNSPECIFIED = 0,
538 MODE_LIST,
539 MODE_DELETE,
b892bb45 540 MODE_EDIT,
4228e8bc 541 MODE_GRAFT,
fb404291 542 MODE_CONVERT_GRAFT_FILE,
70c7bd6d
JK
543 MODE_REPLACE
544 } cmdmode = MODE_UNSPECIFIED;
54b0c1e0 545 struct option options[] = {
70c7bd6d
JK
546 OPT_CMDMODE('l', "list", &cmdmode, N_("list replace refs"), MODE_LIST),
547 OPT_CMDMODE('d', "delete", &cmdmode, N_("delete replace refs"), MODE_DELETE),
b892bb45 548 OPT_CMDMODE('e', "edit", &cmdmode, N_("edit existing object"), MODE_EDIT),
4228e8bc 549 OPT_CMDMODE('g', "graft", &cmdmode, N_("change a commit's parents"), MODE_GRAFT),
fb404291 550 OPT_CMDMODE(0, "convert-graft-file", &cmdmode, N_("convert existing graft file"), MODE_CONVERT_GRAFT_FILE),
1b354755
NTND
551 OPT_BOOL_F('f', "force", &force, N_("replace the ref if it exists"),
552 PARSE_OPT_NOCOMPLETE),
2deda629 553 OPT_BOOL(0, "raw", &raw, N_("do not pretty-print contents for --edit")),
44f9f850 554 OPT_STRING(0, "format", &format, N_("format"), N_("use this format")),
54b0c1e0
CC
555 OPT_END()
556 };
557
6ebd1caf 558 read_replace_refs = 0;
36b14370 559 git_config(git_default_config, NULL);
769a4fa4 560
451bb210 561 argc = parse_options(argc, argv, prefix, options, git_replace_usage, 0);
54b0c1e0 562
70c7bd6d
JK
563 if (!cmdmode)
564 cmdmode = argc ? MODE_REPLACE : MODE_LIST;
3f495f67 565
70c7bd6d 566 if (format && cmdmode != MODE_LIST)
225c62e0 567 usage_msg_opt(_("--format cannot be used when not listing"),
44f9f850
CC
568 git_replace_usage, options);
569
4228e8bc
CC
570 if (force &&
571 cmdmode != MODE_REPLACE &&
572 cmdmode != MODE_EDIT &&
fb404291
JS
573 cmdmode != MODE_GRAFT &&
574 cmdmode != MODE_CONVERT_GRAFT_FILE)
225c62e0 575 usage_msg_opt(_("-f only makes sense when writing a replacement"),
86af2caa 576 git_replace_usage, options);
bebdd271 577
2deda629 578 if (raw && cmdmode != MODE_EDIT)
225c62e0 579 usage_msg_opt(_("--raw only makes sense with --edit"),
2deda629
JK
580 git_replace_usage, options);
581
70c7bd6d
JK
582 switch (cmdmode) {
583 case MODE_DELETE:
54b0c1e0 584 if (argc < 1)
225c62e0 585 usage_msg_opt(_("-d needs at least one argument"),
86af2caa 586 git_replace_usage, options);
54b0c1e0 587 return for_each_replace_name(argv, delete_replace_ref);
54b0c1e0 588
70c7bd6d 589 case MODE_REPLACE:
bebdd271 590 if (argc != 2)
225c62e0 591 usage_msg_opt(_("bad number of arguments"),
86af2caa 592 git_replace_usage, options);
bebdd271 593 return replace_object(argv[0], argv[1], force);
bebdd271 594
b892bb45
JK
595 case MODE_EDIT:
596 if (argc != 1)
225c62e0 597 usage_msg_opt(_("-e needs exactly one argument"),
b892bb45 598 git_replace_usage, options);
2deda629 599 return edit_and_replace(argv[0], force, raw);
b892bb45 600
4228e8bc
CC
601 case MODE_GRAFT:
602 if (argc < 1)
225c62e0 603 usage_msg_opt(_("-g needs at least one argument"),
4228e8bc 604 git_replace_usage, options);
041c98e2 605 return create_graft(argc, argv, force, 0);
4228e8bc 606
fb404291
JS
607 case MODE_CONVERT_GRAFT_FILE:
608 if (argc != 0)
225c62e0 609 usage_msg_opt(_("--convert-graft-file takes no argument"),
fb404291
JS
610 git_replace_usage, options);
611 return !!convert_graft_file(force);
4228e8bc 612
70c7bd6d
JK
613 case MODE_LIST:
614 if (argc > 1)
225c62e0 615 usage_msg_opt(_("only one pattern can be given with -l"),
70c7bd6d
JK
616 git_replace_usage, options);
617 return list_replace_refs(argv[0], format);
54b0c1e0 618
70c7bd6d 619 default:
d398f2ea 620 BUG("invalid cmdmode %d", (int)cmdmode);
70c7bd6d 621 }
54b0c1e0 622}