builtin/show: do not prune by pathspec
[git/mjg.git] / builtin / replace.c
blobce9f6974d26bd3ef2135324a86b34ecbdb8a6711
1 /*
2 * Builtin "git replace"
4 * Copyright (c) 2008 Christian Couder <chriscool@tuxfamily.org>
6 * Based on builtin/tag.c by Kristian Høgsberg <krh@redhat.com>
7 * and Carlos Rica <jasampler@gmail.com> that was itself based on
8 * git-tag.sh and mktag.c by Linus Torvalds.
9 */
11 #include "builtin.h"
12 #include "config.h"
13 #include "editor.h"
14 #include "environment.h"
15 #include "gettext.h"
16 #include "hex.h"
17 #include "refs.h"
18 #include "parse-options.h"
19 #include "path.h"
20 #include "run-command.h"
21 #include "object-file.h"
22 #include "object-name.h"
23 #include "object-store-ll.h"
24 #include "replace-object.h"
25 #include "repository.h"
26 #include "tag.h"
27 #include "wildmatch.h"
29 static const char * const git_replace_usage[] = {
30 N_("git replace [-f] <object> <replacement>"),
31 N_("git replace [-f] --edit <object>"),
32 N_("git replace [-f] --graft <commit> [<parent>...]"),
33 "git replace [-f] --convert-graft-file",
34 N_("git replace -d <object>..."),
35 N_("git replace [--format=<format>] [-l [<pattern>]]"),
36 NULL
39 enum replace_format {
40 REPLACE_FORMAT_SHORT,
41 REPLACE_FORMAT_MEDIUM,
42 REPLACE_FORMAT_LONG
45 struct show_data {
46 struct repository *repo;
47 const char *pattern;
48 enum replace_format format;
51 static int show_reference(const char *refname,
52 const struct object_id *oid,
53 int flag UNUSED, void *cb_data)
55 struct show_data *data = cb_data;
57 if (!wildmatch(data->pattern, refname, 0)) {
58 if (data->format == REPLACE_FORMAT_SHORT)
59 printf("%s\n", refname);
60 else if (data->format == REPLACE_FORMAT_MEDIUM)
61 printf("%s -> %s\n", refname, oid_to_hex(oid));
62 else { /* data->format == REPLACE_FORMAT_LONG */
63 struct object_id object;
64 enum object_type obj_type, repl_type;
66 if (repo_get_oid(data->repo, refname, &object))
67 return error(_("failed to resolve '%s' as a valid ref"), refname);
69 obj_type = oid_object_info(data->repo, &object, NULL);
70 repl_type = oid_object_info(data->repo, oid, NULL);
72 printf("%s (%s) -> %s (%s)\n", refname, type_name(obj_type),
73 oid_to_hex(oid), type_name(repl_type));
77 return 0;
80 static int list_replace_refs(const char *pattern, const char *format)
82 struct show_data data;
84 data.repo = the_repository;
85 if (!pattern)
86 pattern = "*";
87 data.pattern = pattern;
89 if (format == NULL || *format == '\0' || !strcmp(format, "short"))
90 data.format = REPLACE_FORMAT_SHORT;
91 else if (!strcmp(format, "medium"))
92 data.format = REPLACE_FORMAT_MEDIUM;
93 else if (!strcmp(format, "long"))
94 data.format = REPLACE_FORMAT_LONG;
96 * Please update _git_replace() in git-completion.bash when
97 * you add new format
99 else
100 return error(_("invalid replace format '%s'\n"
101 "valid formats are 'short', 'medium' and 'long'"),
102 format);
104 refs_for_each_replace_ref(get_main_ref_store(the_repository),
105 show_reference, (void *)&data);
107 return 0;
110 typedef int (*each_replace_name_fn)(const char *name, const char *ref,
111 const struct object_id *oid);
113 static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
115 const char **p, *full_hex;
116 struct strbuf ref = STRBUF_INIT;
117 size_t base_len;
118 int had_error = 0;
119 struct object_id oid;
120 const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
122 strbuf_addstr(&ref, git_replace_ref_base);
123 base_len = ref.len;
125 for (p = argv; *p; p++) {
126 if (repo_get_oid(the_repository, *p, &oid)) {
127 error("failed to resolve '%s' as a valid ref", *p);
128 had_error = 1;
129 continue;
132 strbuf_setlen(&ref, base_len);
133 strbuf_addstr(&ref, oid_to_hex(&oid));
134 full_hex = ref.buf + base_len;
136 if (refs_read_ref(get_main_ref_store(the_repository), ref.buf, &oid)) {
137 error(_("replace ref '%s' not found"), full_hex);
138 had_error = 1;
139 continue;
141 if (fn(full_hex, ref.buf, &oid))
142 had_error = 1;
144 strbuf_release(&ref);
145 return had_error;
148 static int delete_replace_ref(const char *name, const char *ref,
149 const struct object_id *oid)
151 if (refs_delete_ref(get_main_ref_store(the_repository), NULL, ref, oid, 0))
152 return 1;
153 printf_ln(_("Deleted replace ref '%s'"), name);
154 return 0;
157 static int check_ref_valid(struct object_id *object,
158 struct object_id *prev,
159 struct strbuf *ref,
160 int force)
162 const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
164 strbuf_reset(ref);
165 strbuf_addf(ref, "%s%s", git_replace_ref_base, oid_to_hex(object));
166 if (check_refname_format(ref->buf, 0))
167 return error(_("'%s' is not a valid ref name"), ref->buf);
169 if (refs_read_ref(get_main_ref_store(the_repository), ref->buf, prev))
170 oidclr(prev);
171 else if (!force)
172 return error(_("replace ref '%s' already exists"), ref->buf);
173 return 0;
176 static int replace_object_oid(const char *object_ref,
177 struct object_id *object,
178 const char *replace_ref,
179 struct object_id *repl,
180 int force)
182 struct object_id prev;
183 enum object_type obj_type, repl_type;
184 struct strbuf ref = STRBUF_INIT;
185 struct ref_transaction *transaction;
186 struct strbuf err = STRBUF_INIT;
187 int res = 0;
189 obj_type = oid_object_info(the_repository, object, NULL);
190 repl_type = oid_object_info(the_repository, repl, NULL);
191 if (!force && obj_type != repl_type)
192 return error(_("Objects must be of the same type.\n"
193 "'%s' points to a replaced object of type '%s'\n"
194 "while '%s' points to a replacement object of "
195 "type '%s'."),
196 object_ref, type_name(obj_type),
197 replace_ref, type_name(repl_type));
199 if (check_ref_valid(object, &prev, &ref, force)) {
200 strbuf_release(&ref);
201 return -1;
204 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
205 &err);
206 if (!transaction ||
207 ref_transaction_update(transaction, ref.buf, repl, &prev,
208 NULL, NULL, 0, NULL, &err) ||
209 ref_transaction_commit(transaction, &err))
210 res = error("%s", err.buf);
212 ref_transaction_free(transaction);
213 strbuf_release(&ref);
214 return res;
217 static int replace_object(const char *object_ref, const char *replace_ref, int force)
219 struct object_id object, repl;
221 if (repo_get_oid(the_repository, object_ref, &object))
222 return error(_("failed to resolve '%s' as a valid ref"),
223 object_ref);
224 if (repo_get_oid(the_repository, replace_ref, &repl))
225 return error(_("failed to resolve '%s' as a valid ref"),
226 replace_ref);
228 return replace_object_oid(object_ref, &object, replace_ref, &repl, force);
232 * Write the contents of the object named by "sha1" to the file "filename".
233 * If "raw" is true, then the object's raw contents are printed according to
234 * "type". Otherwise, we pretty-print the contents for human editing.
236 static int export_object(const struct object_id *oid, enum object_type type,
237 int raw, const char *filename)
239 struct child_process cmd = CHILD_PROCESS_INIT;
240 int fd;
242 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
243 if (fd < 0)
244 return error_errno(_("unable to open %s for writing"), filename);
246 strvec_push(&cmd.args, "--no-replace-objects");
247 strvec_push(&cmd.args, "cat-file");
248 if (raw)
249 strvec_push(&cmd.args, type_name(type));
250 else
251 strvec_push(&cmd.args, "-p");
252 strvec_push(&cmd.args, oid_to_hex(oid));
253 cmd.git_cmd = 1;
254 cmd.out = fd;
256 if (run_command(&cmd))
257 return error(_("cat-file reported failure"));
258 return 0;
262 * Read a previously-exported (and possibly edited) object back from "filename",
263 * interpreting it as "type", and writing the result to the object database.
264 * The sha1 of the written object is returned via sha1.
266 static int import_object(struct object_id *oid, enum object_type type,
267 int raw, const char *filename)
269 int fd;
271 fd = open(filename, O_RDONLY);
272 if (fd < 0)
273 return error_errno(_("unable to open %s for reading"), filename);
275 if (!raw && type == OBJ_TREE) {
276 struct child_process cmd = CHILD_PROCESS_INIT;
277 struct strbuf result = STRBUF_INIT;
279 strvec_push(&cmd.args, "mktree");
280 cmd.git_cmd = 1;
281 cmd.in = fd;
282 cmd.out = -1;
284 if (start_command(&cmd)) {
285 close(fd);
286 return error(_("unable to spawn mktree"));
289 if (strbuf_read(&result, cmd.out, the_hash_algo->hexsz + 1) < 0) {
290 error_errno(_("unable to read from mktree"));
291 close(fd);
292 close(cmd.out);
293 return -1;
295 close(cmd.out);
297 if (finish_command(&cmd)) {
298 strbuf_release(&result);
299 return error(_("mktree reported failure"));
301 if (get_oid_hex(result.buf, oid) < 0) {
302 strbuf_release(&result);
303 return error(_("mktree did not return an object name"));
306 strbuf_release(&result);
307 } else {
308 struct stat st;
309 int flags = HASH_FORMAT_CHECK | HASH_WRITE_OBJECT;
311 if (fstat(fd, &st) < 0) {
312 error_errno(_("unable to fstat %s"), filename);
313 close(fd);
314 return -1;
316 if (index_fd(the_repository->index, oid, fd, &st, type, NULL, flags) < 0)
317 return error(_("unable to write object to database"));
318 /* index_fd close()s fd for us */
322 * No need to close(fd) here; both run-command and index-fd
323 * will have done it for us.
325 return 0;
328 static int edit_and_replace(const char *object_ref, int force, int raw)
330 char *tmpfile;
331 enum object_type type;
332 struct object_id old_oid, new_oid, prev;
333 struct strbuf ref = STRBUF_INIT;
335 if (repo_get_oid(the_repository, object_ref, &old_oid) < 0)
336 return error(_("not a valid object name: '%s'"), object_ref);
338 type = oid_object_info(the_repository, &old_oid, NULL);
339 if (type < 0)
340 return error(_("unable to get object type for %s"),
341 oid_to_hex(&old_oid));
343 if (check_ref_valid(&old_oid, &prev, &ref, force)) {
344 strbuf_release(&ref);
345 return -1;
347 strbuf_release(&ref);
349 tmpfile = git_pathdup("REPLACE_EDITOBJ");
350 if (export_object(&old_oid, type, raw, tmpfile)) {
351 free(tmpfile);
352 return -1;
354 if (launch_editor(tmpfile, NULL, NULL) < 0) {
355 free(tmpfile);
356 return error(_("editing object file failed"));
358 if (import_object(&new_oid, type, raw, tmpfile)) {
359 free(tmpfile);
360 return -1;
362 free(tmpfile);
364 if (oideq(&old_oid, &new_oid))
365 return error(_("new object is the same as the old one: '%s'"), oid_to_hex(&old_oid));
367 return replace_object_oid(object_ref, &old_oid, "replacement", &new_oid, force);
370 static int replace_parents(struct strbuf *buf, int argc, const char **argv)
372 struct strbuf new_parents = STRBUF_INIT;
373 const char *parent_start, *parent_end;
374 int i;
375 const unsigned hexsz = the_hash_algo->hexsz;
377 /* find existing parents */
378 parent_start = buf->buf;
379 parent_start += hexsz + 6; /* "tree " + "hex sha1" + "\n" */
380 parent_end = parent_start;
382 while (starts_with(parent_end, "parent "))
383 parent_end += hexsz + 8; /* "parent " + "hex sha1" + "\n" */
385 /* prepare new parents */
386 for (i = 0; i < argc; i++) {
387 struct object_id oid;
388 struct commit *commit;
390 if (repo_get_oid(the_repository, argv[i], &oid) < 0) {
391 strbuf_release(&new_parents);
392 return error(_("not a valid object name: '%s'"),
393 argv[i]);
395 commit = lookup_commit_reference(the_repository, &oid);
396 if (!commit) {
397 strbuf_release(&new_parents);
398 return error(_("could not parse %s as a commit"), argv[i]);
400 strbuf_addf(&new_parents, "parent %s\n", oid_to_hex(&commit->object.oid));
403 /* replace existing parents with new ones */
404 strbuf_splice(buf, parent_start - buf->buf, parent_end - parent_start,
405 new_parents.buf, new_parents.len);
407 strbuf_release(&new_parents);
408 return 0;
411 struct check_mergetag_data {
412 int argc;
413 const char **argv;
416 static int check_one_mergetag(struct commit *commit UNUSED,
417 struct commit_extra_header *extra,
418 void *data)
420 struct check_mergetag_data *mergetag_data = (struct check_mergetag_data *)data;
421 const char *ref = mergetag_data->argv[0];
422 struct object_id tag_oid;
423 struct tag *tag;
424 int i;
426 hash_object_file(the_hash_algo, extra->value, extra->len,
427 OBJ_TAG, &tag_oid);
428 tag = lookup_tag(the_repository, &tag_oid);
429 if (!tag)
430 return error(_("bad mergetag in commit '%s'"), ref);
431 if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))
432 return error(_("malformed mergetag in commit '%s'"), ref);
434 /* iterate over new parents */
435 for (i = 1; i < mergetag_data->argc; i++) {
436 struct object_id oid;
437 if (repo_get_oid(the_repository, mergetag_data->argv[i], &oid) < 0)
438 return error(_("not a valid object name: '%s'"),
439 mergetag_data->argv[i]);
440 if (oideq(get_tagged_oid(tag), &oid))
441 return 0; /* found */
444 return error(_("original commit '%s' contains mergetag '%s' that is "
445 "discarded; use --edit instead of --graft"), ref,
446 oid_to_hex(&tag_oid));
449 static int check_mergetags(struct commit *commit, int argc, const char **argv)
451 struct check_mergetag_data mergetag_data;
453 mergetag_data.argc = argc;
454 mergetag_data.argv = argv;
455 return for_each_mergetag(check_one_mergetag, commit, &mergetag_data);
458 static int create_graft(int argc, const char **argv, int force, int gentle)
460 struct object_id old_oid, new_oid;
461 const char *old_ref = argv[0];
462 struct commit *commit;
463 struct strbuf buf = STRBUF_INIT;
464 const char *buffer;
465 unsigned long size;
467 if (repo_get_oid(the_repository, old_ref, &old_oid) < 0)
468 return error(_("not a valid object name: '%s'"), old_ref);
469 commit = lookup_commit_reference(the_repository, &old_oid);
470 if (!commit)
471 return error(_("could not parse %s"), old_ref);
473 buffer = repo_get_commit_buffer(the_repository, commit, &size);
474 strbuf_add(&buf, buffer, size);
475 repo_unuse_commit_buffer(the_repository, commit, buffer);
477 if (replace_parents(&buf, argc - 1, &argv[1]) < 0) {
478 strbuf_release(&buf);
479 return -1;
482 if (remove_signature(&buf)) {
483 warning(_("the original commit '%s' has a gpg signature"), old_ref);
484 warning(_("the signature will be removed in the replacement commit!"));
487 if (check_mergetags(commit, argc, argv)) {
488 strbuf_release(&buf);
489 return -1;
492 if (write_object_file(buf.buf, buf.len, OBJ_COMMIT, &new_oid)) {
493 strbuf_release(&buf);
494 return error(_("could not write replacement commit for: '%s'"),
495 old_ref);
498 strbuf_release(&buf);
500 if (oideq(&commit->object.oid, &new_oid)) {
501 if (gentle) {
502 warning(_("graft for '%s' unnecessary"),
503 oid_to_hex(&commit->object.oid));
504 return 0;
506 return error(_("new commit is the same as the old one: '%s'"),
507 oid_to_hex(&commit->object.oid));
510 return replace_object_oid(old_ref, &commit->object.oid,
511 "replacement", &new_oid, force);
514 static int convert_graft_file(int force)
516 const char *graft_file = get_graft_file(the_repository);
517 FILE *fp = fopen_or_warn(graft_file, "r");
518 struct strbuf buf = STRBUF_INIT, err = STRBUF_INIT;
519 struct strvec args = STRVEC_INIT;
521 if (!fp)
522 return -1;
524 no_graft_file_deprecated_advice = 1;
525 while (strbuf_getline(&buf, fp) != EOF) {
526 if (*buf.buf == '#')
527 continue;
529 strvec_split(&args, buf.buf);
530 if (args.nr && create_graft(args.nr, args.v, force, 1))
531 strbuf_addf(&err, "\n\t%s", buf.buf);
532 strvec_clear(&args);
534 fclose(fp);
536 strbuf_release(&buf);
538 if (!err.len)
539 return unlink_or_warn(graft_file);
541 warning(_("could not convert the following graft(s):\n%s"), err.buf);
542 strbuf_release(&err);
544 return -1;
547 int cmd_replace(int argc, const char **argv, const char *prefix)
549 int force = 0;
550 int raw = 0;
551 const char *format = NULL;
552 enum {
553 MODE_UNSPECIFIED = 0,
554 MODE_LIST,
555 MODE_DELETE,
556 MODE_EDIT,
557 MODE_GRAFT,
558 MODE_CONVERT_GRAFT_FILE,
559 MODE_REPLACE
560 } cmdmode = MODE_UNSPECIFIED;
561 struct option options[] = {
562 OPT_CMDMODE('l', "list", &cmdmode, N_("list replace refs"), MODE_LIST),
563 OPT_CMDMODE('d', "delete", &cmdmode, N_("delete replace refs"), MODE_DELETE),
564 OPT_CMDMODE('e', "edit", &cmdmode, N_("edit existing object"), MODE_EDIT),
565 OPT_CMDMODE('g', "graft", &cmdmode, N_("change a commit's parents"), MODE_GRAFT),
566 OPT_CMDMODE(0, "convert-graft-file", &cmdmode, N_("convert existing graft file"), MODE_CONVERT_GRAFT_FILE),
567 OPT_BOOL_F('f', "force", &force, N_("replace the ref if it exists"),
568 PARSE_OPT_NOCOMPLETE),
569 OPT_BOOL(0, "raw", &raw, N_("do not pretty-print contents for --edit")),
570 OPT_STRING(0, "format", &format, N_("format"), N_("use this format")),
571 OPT_END()
574 disable_replace_refs();
575 git_config(git_default_config, NULL);
577 argc = parse_options(argc, argv, prefix, options, git_replace_usage, 0);
579 if (!cmdmode)
580 cmdmode = argc ? MODE_REPLACE : MODE_LIST;
582 if (format && cmdmode != MODE_LIST)
583 usage_msg_opt(_("--format cannot be used when not listing"),
584 git_replace_usage, options);
586 if (force &&
587 cmdmode != MODE_REPLACE &&
588 cmdmode != MODE_EDIT &&
589 cmdmode != MODE_GRAFT &&
590 cmdmode != MODE_CONVERT_GRAFT_FILE)
591 usage_msg_opt(_("-f only makes sense when writing a replacement"),
592 git_replace_usage, options);
594 if (raw && cmdmode != MODE_EDIT)
595 usage_msg_opt(_("--raw only makes sense with --edit"),
596 git_replace_usage, options);
598 switch (cmdmode) {
599 case MODE_DELETE:
600 if (argc < 1)
601 usage_msg_opt(_("-d needs at least one argument"),
602 git_replace_usage, options);
603 return for_each_replace_name(argv, delete_replace_ref);
605 case MODE_REPLACE:
606 if (argc != 2)
607 usage_msg_opt(_("bad number of arguments"),
608 git_replace_usage, options);
609 return replace_object(argv[0], argv[1], force);
611 case MODE_EDIT:
612 if (argc != 1)
613 usage_msg_opt(_("-e needs exactly one argument"),
614 git_replace_usage, options);
615 return edit_and_replace(argv[0], force, raw);
617 case MODE_GRAFT:
618 if (argc < 1)
619 usage_msg_opt(_("-g needs at least one argument"),
620 git_replace_usage, options);
621 return create_graft(argc, argv, force, 0);
623 case MODE_CONVERT_GRAFT_FILE:
624 if (argc != 0)
625 usage_msg_opt(_("--convert-graft-file takes no argument"),
626 git_replace_usage, options);
627 return !!convert_graft_file(force);
629 case MODE_LIST:
630 if (argc > 1)
631 usage_msg_opt(_("only one pattern can be given with -l"),
632 git_replace_usage, options);
633 return list_replace_refs(argv[0], format);
635 default:
636 BUG("invalid cmdmode %d", (int)cmdmode);