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