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