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