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