]>
Commit | Line | Data |
---|---|---|
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" |
25a05a8c | 17 | #include "tag.h" |
54b0c1e0 CC |
18 | |
19 | static const char * const git_replace_usage[] = { | |
2477bebb | 20 | N_("git replace [-f] <object> <replacement>"), |
ab77c309 | 21 | N_("git replace [-f] --edit <object>"), |
4228e8bc | 22 | N_("git replace [-f] --graft <commit> [<parent>...]"), |
2477bebb | 23 | N_("git replace -d <object>..."), |
44f9f850 | 24 | N_("git replace [--format=<format>] [-l [<pattern>]]"), |
54b0c1e0 CC |
25 | NULL |
26 | }; | |
27 | ||
663a8566 | 28 | enum replace_format { |
3cc9d877 JK |
29 | REPLACE_FORMAT_SHORT, |
30 | REPLACE_FORMAT_MEDIUM, | |
31 | REPLACE_FORMAT_LONG | |
663a8566 | 32 | }; |
44f9f850 CC |
33 | |
34 | struct show_data { | |
35 | const char *pattern; | |
663a8566 | 36 | enum replace_format format; |
44f9f850 CC |
37 | }; |
38 | ||
d70d7a8a | 39 | static int show_reference(const char *refname, const struct object_id *oid, |
54b0c1e0 CC |
40 | int flag, void *cb_data) |
41 | { | |
44f9f850 CC |
42 | struct show_data *data = cb_data; |
43 | ||
55d34269 | 44 | if (!wildmatch(data->pattern, refname, 0)) { |
663a8566 | 45 | if (data->format == REPLACE_FORMAT_SHORT) |
44f9f850 | 46 | printf("%s\n", refname); |
663a8566 | 47 | else if (data->format == REPLACE_FORMAT_MEDIUM) |
d70d7a8a | 48 | printf("%s -> %s\n", refname, oid_to_hex(oid)); |
663a8566 | 49 | else { /* data->format == REPLACE_FORMAT_LONG */ |
d70d7a8a | 50 | struct object_id object; |
44f9f850 | 51 | enum object_type obj_type, repl_type; |
54b0c1e0 | 52 | |
e82caf38 | 53 | if (get_oid(refname, &object)) |
44f9f850 CC |
54 | return error("Failed to resolve '%s' as a valid ref.", refname); |
55 | ||
d70d7a8a MH |
56 | obj_type = sha1_object_info(object.hash, NULL); |
57 | repl_type = sha1_object_info(oid->hash, NULL); | |
44f9f850 | 58 | |
debca9d2 BW |
59 | printf("%s (%s) -> %s (%s)\n", refname, type_name(obj_type), |
60 | oid_to_hex(oid), type_name(repl_type)); | |
44f9f850 CC |
61 | } |
62 | } | |
54b0c1e0 CC |
63 | |
64 | return 0; | |
65 | } | |
66 | ||
44f9f850 | 67 | static int list_replace_refs(const char *pattern, const char *format) |
54b0c1e0 | 68 | { |
44f9f850 CC |
69 | struct show_data data; |
70 | ||
54b0c1e0 CC |
71 | if (pattern == NULL) |
72 | pattern = "*"; | |
44f9f850 CC |
73 | data.pattern = pattern; |
74 | ||
75 | if (format == NULL || *format == '\0' || !strcmp(format, "short")) | |
663a8566 | 76 | data.format = REPLACE_FORMAT_SHORT; |
44f9f850 | 77 | else if (!strcmp(format, "medium")) |
663a8566 CC |
78 | data.format = REPLACE_FORMAT_MEDIUM; |
79 | else if (!strcmp(format, "long")) | |
80 | data.format = REPLACE_FORMAT_LONG; | |
44f9f850 CC |
81 | else |
82 | die("invalid replace format '%s'\n" | |
663a8566 | 83 | "valid formats are 'short', 'medium' and 'long'\n", |
44f9f850 | 84 | format); |
54b0c1e0 | 85 | |
d70d7a8a | 86 | for_each_replace_ref(show_reference, (void *)&data); |
54b0c1e0 CC |
87 | |
88 | return 0; | |
89 | } | |
90 | ||
91 | typedef int (*each_replace_name_fn)(const char *name, const char *ref, | |
cea4332e | 92 | const struct object_id *oid); |
54b0c1e0 CC |
93 | |
94 | static int for_each_replace_name(const char **argv, each_replace_name_fn fn) | |
95 | { | |
9dfc3684 | 96 | const char **p, *full_hex; |
7f897b6f JK |
97 | struct strbuf ref = STRBUF_INIT; |
98 | size_t base_len; | |
54b0c1e0 | 99 | int had_error = 0; |
cea4332e | 100 | struct object_id oid; |
54b0c1e0 | 101 | |
7f897b6f JK |
102 | strbuf_addstr(&ref, git_replace_ref_base); |
103 | base_len = ref.len; | |
104 | ||
54b0c1e0 | 105 | for (p = argv; *p; p++) { |
cea4332e | 106 | if (get_oid(*p, &oid)) { |
9dfc3684 | 107 | error("Failed to resolve '%s' as a valid ref.", *p); |
54b0c1e0 CC |
108 | had_error = 1; |
109 | continue; | |
110 | } | |
7f897b6f JK |
111 | |
112 | strbuf_setlen(&ref, base_len); | |
113 | strbuf_addstr(&ref, oid_to_hex(&oid)); | |
114 | full_hex = ref.buf + base_len; | |
115 | ||
34c290a6 | 116 | if (read_ref(ref.buf, &oid)) { |
9dfc3684 | 117 | error("replace ref '%s' not found.", full_hex); |
54b0c1e0 CC |
118 | had_error = 1; |
119 | continue; | |
120 | } | |
7f897b6f | 121 | if (fn(full_hex, ref.buf, &oid)) |
54b0c1e0 CC |
122 | had_error = 1; |
123 | } | |
372b050b | 124 | strbuf_release(&ref); |
54b0c1e0 CC |
125 | return had_error; |
126 | } | |
127 | ||
128 | static int delete_replace_ref(const char *name, const char *ref, | |
cea4332e | 129 | const struct object_id *oid) |
54b0c1e0 | 130 | { |
2616a5e5 | 131 | if (delete_ref(NULL, ref, oid, 0)) |
54b0c1e0 CC |
132 | return 1; |
133 | printf("Deleted replace ref '%s'\n", name); | |
134 | return 0; | |
135 | } | |
136 | ||
cea4332e | 137 | static void check_ref_valid(struct object_id *object, |
138 | struct object_id *prev, | |
7f897b6f | 139 | struct strbuf *ref, |
b6e38840 CC |
140 | int force) |
141 | { | |
7f897b6f JK |
142 | strbuf_reset(ref); |
143 | strbuf_addf(ref, "%s%s", git_replace_ref_base, oid_to_hex(object)); | |
144 | if (check_refname_format(ref->buf, 0)) | |
145 | die("'%s' is not a valid ref name.", ref->buf); | |
146 | ||
34c290a6 | 147 | if (read_ref(ref->buf, prev)) |
cea4332e | 148 | oidclr(prev); |
b6e38840 | 149 | else if (!force) |
7f897b6f | 150 | die("replace ref '%s' already exists", ref->buf); |
b6e38840 CC |
151 | } |
152 | ||
cea4332e | 153 | static int replace_object_oid(const char *object_ref, |
154 | struct object_id *object, | |
479bd757 | 155 | const char *replace_ref, |
cea4332e | 156 | struct object_id *repl, |
479bd757 | 157 | int force) |
bebdd271 | 158 | { |
cea4332e | 159 | struct object_id prev; |
277336a5 | 160 | enum object_type obj_type, repl_type; |
7f897b6f | 161 | struct strbuf ref = STRBUF_INIT; |
867c2fac RS |
162 | struct ref_transaction *transaction; |
163 | struct strbuf err = STRBUF_INIT; | |
bebdd271 | 164 | |
cea4332e | 165 | obj_type = sha1_object_info(object->hash, NULL); |
166 | repl_type = sha1_object_info(repl->hash, NULL); | |
277336a5 CC |
167 | if (!force && obj_type != repl_type) |
168 | die("Objects must be of the same type.\n" | |
169 | "'%s' points to a replaced object of type '%s'\n" | |
170 | "while '%s' points to a replacement object of type '%s'.", | |
debca9d2 BW |
171 | object_ref, type_name(obj_type), |
172 | replace_ref, type_name(repl_type)); | |
277336a5 | 173 | |
7f897b6f | 174 | check_ref_valid(object, &prev, &ref, force); |
bebdd271 | 175 | |
867c2fac RS |
176 | transaction = ref_transaction_begin(&err); |
177 | if (!transaction || | |
89f3bbdd | 178 | ref_transaction_update(transaction, ref.buf, repl, &prev, |
1d147bdf | 179 | 0, NULL, &err) || |
db7516ab | 180 | ref_transaction_commit(transaction, &err)) |
867c2fac | 181 | die("%s", err.buf); |
bebdd271 | 182 | |
867c2fac | 183 | ref_transaction_free(transaction); |
7f897b6f | 184 | strbuf_release(&ref); |
bebdd271 CC |
185 | return 0; |
186 | } | |
187 | ||
479bd757 JK |
188 | static int replace_object(const char *object_ref, const char *replace_ref, int force) |
189 | { | |
cea4332e | 190 | struct object_id object, repl; |
479bd757 | 191 | |
cea4332e | 192 | if (get_oid(object_ref, &object)) |
479bd757 | 193 | die("Failed to resolve '%s' as a valid ref.", object_ref); |
cea4332e | 194 | if (get_oid(replace_ref, &repl)) |
479bd757 JK |
195 | die("Failed to resolve '%s' as a valid ref.", replace_ref); |
196 | ||
cea4332e | 197 | return replace_object_oid(object_ref, &object, replace_ref, &repl, force); |
479bd757 JK |
198 | } |
199 | ||
b892bb45 | 200 | /* |
2deda629 JK |
201 | * Write the contents of the object named by "sha1" to the file "filename". |
202 | * If "raw" is true, then the object's raw contents are printed according to | |
203 | * "type". Otherwise, we pretty-print the contents for human editing. | |
b892bb45 | 204 | */ |
cea4332e | 205 | static void export_object(const struct object_id *oid, enum object_type type, |
2deda629 | 206 | int raw, const char *filename) |
b892bb45 | 207 | { |
d3180279 | 208 | struct child_process cmd = CHILD_PROCESS_INIT; |
b892bb45 JK |
209 | int fd; |
210 | ||
211 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666); | |
212 | if (fd < 0) | |
213 | die_errno("unable to open %s for writing", filename); | |
214 | ||
36857e00 JK |
215 | argv_array_push(&cmd.args, "--no-replace-objects"); |
216 | argv_array_push(&cmd.args, "cat-file"); | |
2deda629 | 217 | if (raw) |
debca9d2 | 218 | argv_array_push(&cmd.args, type_name(type)); |
2deda629 JK |
219 | else |
220 | argv_array_push(&cmd.args, "-p"); | |
cea4332e | 221 | argv_array_push(&cmd.args, oid_to_hex(oid)); |
b892bb45 JK |
222 | cmd.git_cmd = 1; |
223 | cmd.out = fd; | |
224 | ||
225 | if (run_command(&cmd)) | |
226 | die("cat-file reported failure"); | |
b892bb45 JK |
227 | } |
228 | ||
229 | /* | |
230 | * Read a previously-exported (and possibly edited) object back from "filename", | |
231 | * interpreting it as "type", and writing the result to the object database. | |
232 | * The sha1 of the written object is returned via sha1. | |
233 | */ | |
cea4332e | 234 | static void import_object(struct object_id *oid, enum object_type type, |
2deda629 | 235 | int raw, const char *filename) |
b892bb45 JK |
236 | { |
237 | int fd; | |
238 | ||
239 | fd = open(filename, O_RDONLY); | |
240 | if (fd < 0) | |
241 | die_errno("unable to open %s for reading", filename); | |
242 | ||
2deda629 | 243 | if (!raw && type == OBJ_TREE) { |
b892bb45 | 244 | const char *argv[] = { "mktree", NULL }; |
d3180279 | 245 | struct child_process cmd = CHILD_PROCESS_INIT; |
b892bb45 JK |
246 | struct strbuf result = STRBUF_INIT; |
247 | ||
248 | cmd.argv = argv; | |
249 | cmd.git_cmd = 1; | |
250 | cmd.in = fd; | |
251 | cmd.out = -1; | |
252 | ||
253 | if (start_command(&cmd)) | |
254 | die("unable to spawn mktree"); | |
255 | ||
256 | if (strbuf_read(&result, cmd.out, 41) < 0) | |
257 | die_errno("unable to read from mktree"); | |
258 | close(cmd.out); | |
259 | ||
260 | if (finish_command(&cmd)) | |
261 | die("mktree reported failure"); | |
cea4332e | 262 | if (get_oid_hex(result.buf, oid) < 0) |
b892bb45 JK |
263 | die("mktree did not return an object name"); |
264 | ||
265 | strbuf_release(&result); | |
266 | } else { | |
267 | struct stat st; | |
268 | int flags = HASH_FORMAT_CHECK | HASH_WRITE_OBJECT; | |
269 | ||
270 | if (fstat(fd, &st) < 0) | |
271 | die_errno("unable to fstat %s", filename); | |
e3506559 | 272 | if (index_fd(oid, fd, &st, type, NULL, flags) < 0) |
b892bb45 JK |
273 | die("unable to write object to database"); |
274 | /* index_fd close()s fd for us */ | |
275 | } | |
276 | ||
277 | /* | |
278 | * No need to close(fd) here; both run-command and index-fd | |
279 | * will have done it for us. | |
280 | */ | |
281 | } | |
282 | ||
2deda629 | 283 | static int edit_and_replace(const char *object_ref, int force, int raw) |
b892bb45 JK |
284 | { |
285 | char *tmpfile = git_pathdup("REPLACE_EDITOBJ"); | |
286 | enum object_type type; | |
efdfe11f | 287 | struct object_id old_oid, new_oid, prev; |
7f897b6f | 288 | struct strbuf ref = STRBUF_INIT; |
b892bb45 | 289 | |
efdfe11f | 290 | if (get_oid(object_ref, &old_oid) < 0) |
b892bb45 JK |
291 | die("Not a valid object name: '%s'", object_ref); |
292 | ||
efdfe11f | 293 | type = sha1_object_info(old_oid.hash, NULL); |
b892bb45 | 294 | if (type < 0) |
efdfe11f | 295 | die("unable to get object type for %s", oid_to_hex(&old_oid)); |
b892bb45 | 296 | |
efdfe11f | 297 | check_ref_valid(&old_oid, &prev, &ref, force); |
7f897b6f | 298 | strbuf_release(&ref); |
24790835 | 299 | |
efdfe11f | 300 | export_object(&old_oid, type, raw, tmpfile); |
b892bb45 JK |
301 | if (launch_editor(tmpfile, NULL, NULL) < 0) |
302 | die("editing object file failed"); | |
efdfe11f | 303 | import_object(&new_oid, type, raw, tmpfile); |
b892bb45 JK |
304 | |
305 | free(tmpfile); | |
306 | ||
efdfe11f BW |
307 | if (!oidcmp(&old_oid, &new_oid)) |
308 | return error("new object is the same as the old one: '%s'", oid_to_hex(&old_oid)); | |
f22166b5 | 309 | |
efdfe11f | 310 | return replace_object_oid(object_ref, &old_oid, "replacement", &new_oid, force); |
b892bb45 JK |
311 | } |
312 | ||
4228e8bc CC |
313 | static void replace_parents(struct strbuf *buf, int argc, const char **argv) |
314 | { | |
315 | struct strbuf new_parents = STRBUF_INIT; | |
316 | const char *parent_start, *parent_end; | |
317 | int i; | |
318 | ||
319 | /* find existing parents */ | |
320 | parent_start = buf->buf; | |
cea4332e | 321 | parent_start += GIT_SHA1_HEXSZ + 6; /* "tree " + "hex sha1" + "\n" */ |
4228e8bc CC |
322 | parent_end = parent_start; |
323 | ||
324 | while (starts_with(parent_end, "parent ")) | |
325 | parent_end += 48; /* "parent " + "hex sha1" + "\n" */ | |
326 | ||
327 | /* prepare new parents */ | |
328 | for (i = 0; i < argc; i++) { | |
cea4332e | 329 | struct object_id oid; |
330 | if (get_oid(argv[i], &oid) < 0) | |
4228e8bc | 331 | die(_("Not a valid object name: '%s'"), argv[i]); |
bc83266a | 332 | lookup_commit_or_die(&oid, argv[i]); |
cea4332e | 333 | strbuf_addf(&new_parents, "parent %s\n", oid_to_hex(&oid)); |
4228e8bc CC |
334 | } |
335 | ||
336 | /* replace existing parents with new ones */ | |
337 | strbuf_splice(buf, parent_start - buf->buf, parent_end - parent_start, | |
338 | new_parents.buf, new_parents.len); | |
339 | ||
340 | strbuf_release(&new_parents); | |
341 | } | |
342 | ||
25a05a8c CC |
343 | struct check_mergetag_data { |
344 | int argc; | |
345 | const char **argv; | |
346 | }; | |
347 | ||
348 | static void check_one_mergetag(struct commit *commit, | |
349 | struct commit_extra_header *extra, | |
350 | void *data) | |
351 | { | |
352 | struct check_mergetag_data *mergetag_data = (struct check_mergetag_data *)data; | |
353 | const char *ref = mergetag_data->argv[0]; | |
cea4332e | 354 | struct object_id tag_oid; |
25a05a8c CC |
355 | struct tag *tag; |
356 | int i; | |
357 | ||
169c9c01 | 358 | hash_object_file(extra->value, extra->len, type_name(OBJ_TAG), &tag_oid); |
d3101b53 | 359 | tag = lookup_tag(&tag_oid); |
25a05a8c CC |
360 | if (!tag) |
361 | die(_("bad mergetag in commit '%s'"), ref); | |
362 | if (parse_tag_buffer(tag, extra->value, extra->len)) | |
363 | die(_("malformed mergetag in commit '%s'"), ref); | |
364 | ||
365 | /* iterate over new parents */ | |
366 | for (i = 1; i < mergetag_data->argc; i++) { | |
f2fd0760 | 367 | struct object_id oid; |
e82caf38 | 368 | if (get_oid(mergetag_data->argv[i], &oid) < 0) |
25a05a8c | 369 | die(_("Not a valid object name: '%s'"), mergetag_data->argv[i]); |
f2fd0760 | 370 | if (!oidcmp(&tag->tagged->oid, &oid)) |
25a05a8c CC |
371 | return; /* found */ |
372 | } | |
373 | ||
374 | die(_("original commit '%s' contains mergetag '%s' that is discarded; " | |
cea4332e | 375 | "use --edit instead of --graft"), ref, oid_to_hex(&tag_oid)); |
25a05a8c CC |
376 | } |
377 | ||
378 | static void check_mergetags(struct commit *commit, int argc, const char **argv) | |
379 | { | |
380 | struct check_mergetag_data mergetag_data; | |
381 | ||
382 | mergetag_data.argc = argc; | |
383 | mergetag_data.argv = argv; | |
384 | for_each_mergetag(check_one_mergetag, commit, &mergetag_data); | |
385 | } | |
386 | ||
4228e8bc CC |
387 | static int create_graft(int argc, const char **argv, int force) |
388 | { | |
efdfe11f | 389 | struct object_id old_oid, new_oid; |
4228e8bc CC |
390 | const char *old_ref = argv[0]; |
391 | struct commit *commit; | |
392 | struct strbuf buf = STRBUF_INIT; | |
393 | const char *buffer; | |
394 | unsigned long size; | |
395 | ||
efdfe11f | 396 | if (get_oid(old_ref, &old_oid) < 0) |
4228e8bc | 397 | die(_("Not a valid object name: '%s'"), old_ref); |
efdfe11f | 398 | commit = lookup_commit_or_die(&old_oid, old_ref); |
4228e8bc CC |
399 | |
400 | buffer = get_commit_buffer(commit, &size); | |
401 | strbuf_add(&buf, buffer, size); | |
402 | unuse_commit_buffer(commit, buffer); | |
403 | ||
404 | replace_parents(&buf, argc - 1, &argv[1]); | |
405 | ||
0b05ab6f CC |
406 | if (remove_signature(&buf)) { |
407 | warning(_("the original commit '%s' has a gpg signature."), old_ref); | |
408 | warning(_("the signature will be removed in the replacement commit!")); | |
409 | } | |
410 | ||
25a05a8c CC |
411 | check_mergetags(commit, argc, argv); |
412 | ||
169c9c01 | 413 | if (write_object_file(buf.buf, buf.len, commit_type, &new_oid)) |
4228e8bc CC |
414 | die(_("could not write replacement commit for: '%s'"), old_ref); |
415 | ||
416 | strbuf_release(&buf); | |
417 | ||
efdfe11f BW |
418 | if (!oidcmp(&old_oid, &new_oid)) |
419 | return error("new commit is the same as the old one: '%s'", oid_to_hex(&old_oid)); | |
4228e8bc | 420 | |
efdfe11f | 421 | return replace_object_oid(old_ref, &old_oid, "replacement", &new_oid, force); |
4228e8bc CC |
422 | } |
423 | ||
54b0c1e0 CC |
424 | int cmd_replace(int argc, const char **argv, const char *prefix) |
425 | { | |
70c7bd6d | 426 | int force = 0; |
2deda629 | 427 | int raw = 0; |
44f9f850 | 428 | const char *format = NULL; |
70c7bd6d JK |
429 | enum { |
430 | MODE_UNSPECIFIED = 0, | |
431 | MODE_LIST, | |
432 | MODE_DELETE, | |
b892bb45 | 433 | MODE_EDIT, |
4228e8bc | 434 | MODE_GRAFT, |
70c7bd6d JK |
435 | MODE_REPLACE |
436 | } cmdmode = MODE_UNSPECIFIED; | |
54b0c1e0 | 437 | struct option options[] = { |
70c7bd6d JK |
438 | OPT_CMDMODE('l', "list", &cmdmode, N_("list replace refs"), MODE_LIST), |
439 | OPT_CMDMODE('d', "delete", &cmdmode, N_("delete replace refs"), MODE_DELETE), | |
b892bb45 | 440 | OPT_CMDMODE('e', "edit", &cmdmode, N_("edit existing object"), MODE_EDIT), |
4228e8bc | 441 | OPT_CMDMODE('g', "graft", &cmdmode, N_("change a commit's parents"), MODE_GRAFT), |
1b354755 NTND |
442 | OPT_BOOL_F('f', "force", &force, N_("replace the ref if it exists"), |
443 | PARSE_OPT_NOCOMPLETE), | |
2deda629 | 444 | OPT_BOOL(0, "raw", &raw, N_("do not pretty-print contents for --edit")), |
44f9f850 | 445 | OPT_STRING(0, "format", &format, N_("format"), N_("use this format")), |
54b0c1e0 CC |
446 | OPT_END() |
447 | }; | |
448 | ||
afc711b8 | 449 | check_replace_refs = 0; |
36b14370 | 450 | git_config(git_default_config, NULL); |
769a4fa4 | 451 | |
451bb210 | 452 | argc = parse_options(argc, argv, prefix, options, git_replace_usage, 0); |
54b0c1e0 | 453 | |
70c7bd6d JK |
454 | if (!cmdmode) |
455 | cmdmode = argc ? MODE_REPLACE : MODE_LIST; | |
3f495f67 | 456 | |
70c7bd6d | 457 | if (format && cmdmode != MODE_LIST) |
3f495f67 | 458 | usage_msg_opt("--format cannot be used when not listing", |
44f9f850 CC |
459 | git_replace_usage, options); |
460 | ||
4228e8bc CC |
461 | if (force && |
462 | cmdmode != MODE_REPLACE && | |
463 | cmdmode != MODE_EDIT && | |
464 | cmdmode != MODE_GRAFT) | |
70c7bd6d | 465 | usage_msg_opt("-f only makes sense when writing a replacement", |
86af2caa | 466 | git_replace_usage, options); |
bebdd271 | 467 | |
2deda629 JK |
468 | if (raw && cmdmode != MODE_EDIT) |
469 | usage_msg_opt("--raw only makes sense with --edit", | |
470 | git_replace_usage, options); | |
471 | ||
70c7bd6d JK |
472 | switch (cmdmode) { |
473 | case MODE_DELETE: | |
54b0c1e0 | 474 | if (argc < 1) |
86af2caa CC |
475 | usage_msg_opt("-d needs at least one argument", |
476 | git_replace_usage, options); | |
54b0c1e0 | 477 | return for_each_replace_name(argv, delete_replace_ref); |
54b0c1e0 | 478 | |
70c7bd6d | 479 | case MODE_REPLACE: |
bebdd271 | 480 | if (argc != 2) |
86af2caa CC |
481 | usage_msg_opt("bad number of arguments", |
482 | git_replace_usage, options); | |
bebdd271 | 483 | return replace_object(argv[0], argv[1], force); |
bebdd271 | 484 | |
b892bb45 JK |
485 | case MODE_EDIT: |
486 | if (argc != 1) | |
487 | usage_msg_opt("-e needs exactly one argument", | |
488 | git_replace_usage, options); | |
2deda629 | 489 | return edit_and_replace(argv[0], force, raw); |
b892bb45 | 490 | |
4228e8bc CC |
491 | case MODE_GRAFT: |
492 | if (argc < 1) | |
493 | usage_msg_opt("-g needs at least one argument", | |
494 | git_replace_usage, options); | |
495 | return create_graft(argc, argv, force); | |
496 | ||
70c7bd6d JK |
497 | case MODE_LIST: |
498 | if (argc > 1) | |
499 | usage_msg_opt("only one pattern can be given with -l", | |
500 | git_replace_usage, options); | |
501 | return list_replace_refs(argv[0], format); | |
54b0c1e0 | 502 | |
70c7bd6d JK |
503 | default: |
504 | die("BUG: invalid cmdmode %d", (int)cmdmode); | |
505 | } | |
54b0c1e0 | 506 | } |