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