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