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