builtin/show: do not prune by pathspec
[git/mjg.git] / builtin / replace.c
bloba44f4e7ea9ff55dbd1f102aca5e5f5a046391328
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 */
10 #define USE_THE_REPOSITORY_VARIABLE
11 #include "builtin.h"
12 #include "config.h"
13 #include "editor.h"
14 #include "gettext.h"
15 #include "hex.h"
16 #include "refs.h"
17 #include "parse-options.h"
18 #include "path.h"
19 #include "run-command.h"
20 #include "object-file.h"
21 #include "object-name.h"
22 #include "object-store-ll.h"
23 #include "replace-object.h"
24 #include "tag.h"
25 #include "wildmatch.h"
27 static const char * const git_replace_usage[] = {
28 N_("git replace [-f] <object> <replacement>"),
29 N_("git replace [-f] --edit <object>"),
30 N_("git replace [-f] --graft <commit> [<parent>...]"),
31 "git replace [-f] --convert-graft-file",
32 N_("git replace -d <object>..."),
33 N_("git replace [--format=<format>] [-l [<pattern>]]"),
34 NULL
37 enum replace_format {
38 REPLACE_FORMAT_SHORT,
39 REPLACE_FORMAT_MEDIUM,
40 REPLACE_FORMAT_LONG
43 struct show_data {
44 struct repository *repo;
45 const char *pattern;
46 enum replace_format format;
49 static int show_reference(const char *refname,
50 const char *referent UNUSED,
51 const struct object_id *oid,
52 int flag UNUSED, void *cb_data)
54 struct show_data *data = cb_data;
56 if (!wildmatch(data->pattern, refname, 0)) {
57 if (data->format == REPLACE_FORMAT_SHORT)
58 printf("%s\n", refname);
59 else if (data->format == REPLACE_FORMAT_MEDIUM)
60 printf("%s -> %s\n", refname, oid_to_hex(oid));
61 else { /* data->format == REPLACE_FORMAT_LONG */
62 struct object_id object;
63 enum object_type obj_type, repl_type;
65 if (repo_get_oid(data->repo, refname, &object))
66 return error(_("failed to resolve '%s' as a valid ref"), refname);
68 obj_type = oid_object_info(data->repo, &object, NULL);
69 repl_type = oid_object_info(data->repo, oid, NULL);
71 printf("%s (%s) -> %s (%s)\n", refname, type_name(obj_type),
72 oid_to_hex(oid), type_name(repl_type));
76 return 0;
79 static int list_replace_refs(const char *pattern, const char *format)
81 struct show_data data;
83 data.repo = the_repository;
84 if (!pattern)
85 pattern = "*";
86 data.pattern = pattern;
88 if (format == NULL || *format == '\0' || !strcmp(format, "short"))
89 data.format = REPLACE_FORMAT_SHORT;
90 else if (!strcmp(format, "medium"))
91 data.format = REPLACE_FORMAT_MEDIUM;
92 else if (!strcmp(format, "long"))
93 data.format = REPLACE_FORMAT_LONG;
95 * Please update _git_replace() in git-completion.bash when
96 * you add new format
98 else
99 return error(_("invalid replace format '%s'\n"
100 "valid formats are 'short', 'medium' and 'long'"),
101 format);
103 refs_for_each_replace_ref(get_main_ref_store(the_repository),
104 show_reference, (void *)&data);
106 return 0;
109 typedef int (*each_replace_name_fn)(const char *name, const char *ref,
110 const struct object_id *oid);
112 static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
114 const char **p, *full_hex;
115 struct strbuf ref = STRBUF_INIT;
116 size_t base_len;
117 int had_error = 0;
118 struct object_id oid;
119 const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
121 strbuf_addstr(&ref, git_replace_ref_base);
122 base_len = ref.len;
124 for (p = argv; *p; p++) {
125 if (repo_get_oid(the_repository, *p, &oid)) {
126 error("failed to resolve '%s' as a valid ref", *p);
127 had_error = 1;
128 continue;
131 strbuf_setlen(&ref, base_len);
132 strbuf_addstr(&ref, oid_to_hex(&oid));
133 full_hex = ref.buf + base_len;
135 if (refs_read_ref(get_main_ref_store(the_repository), ref.buf, &oid)) {
136 error(_("replace ref '%s' not found"), full_hex);
137 had_error = 1;
138 continue;
140 if (fn(full_hex, ref.buf, &oid))
141 had_error = 1;
143 strbuf_release(&ref);
144 return had_error;
147 static int delete_replace_ref(const char *name, const char *ref,
148 const struct object_id *oid)
150 if (refs_delete_ref(get_main_ref_store(the_repository), NULL, ref, oid, 0))
151 return 1;
152 printf_ln(_("Deleted replace ref '%s'"), name);
153 return 0;
156 static int check_ref_valid(struct object_id *object,
157 struct object_id *prev,
158 struct strbuf *ref,
159 int force)
161 const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
163 strbuf_reset(ref);
164 strbuf_addf(ref, "%s%s", git_replace_ref_base, oid_to_hex(object));
165 if (check_refname_format(ref->buf, 0))
166 return error(_("'%s' is not a valid ref name"), ref->buf);
168 if (refs_read_ref(get_main_ref_store(the_repository), ref->buf, prev))
169 oidclr(prev, the_repository->hash_algo);
170 else if (!force)
171 return error(_("replace ref '%s' already exists"), ref->buf);
172 return 0;
175 static int replace_object_oid(const char *object_ref,
176 struct object_id *object,
177 const char *replace_ref,
178 struct object_id *repl,
179 int force)
181 struct object_id prev;
182 enum object_type obj_type, repl_type;
183 struct strbuf ref = STRBUF_INIT;
184 struct ref_transaction *transaction;
185 struct strbuf err = STRBUF_INIT;
186 int res = 0;
188 obj_type = oid_object_info(the_repository, object, NULL);
189 repl_type = oid_object_info(the_repository, repl, NULL);
190 if (!force && obj_type != repl_type)
191 return error(_("Objects must be of the same type.\n"
192 "'%s' points to a replaced object of type '%s'\n"
193 "while '%s' points to a replacement object of "
194 "type '%s'."),
195 object_ref, type_name(obj_type),
196 replace_ref, type_name(repl_type));
198 if (check_ref_valid(object, &prev, &ref, force)) {
199 strbuf_release(&ref);
200 return -1;
203 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
204 &err);
205 if (!transaction ||
206 ref_transaction_update(transaction, ref.buf, repl, &prev,
207 NULL, NULL, 0, NULL, &err) ||
208 ref_transaction_commit(transaction, &err))
209 res = error("%s", err.buf);
211 ref_transaction_free(transaction);
212 strbuf_release(&ref);
213 return res;
216 static int replace_object(const char *object_ref, const char *replace_ref, int force)
218 struct object_id object, repl;
220 if (repo_get_oid(the_repository, object_ref, &object))
221 return error(_("failed to resolve '%s' as a valid ref"),
222 object_ref);
223 if (repo_get_oid(the_repository, replace_ref, &repl))
224 return error(_("failed to resolve '%s' as a valid ref"),
225 replace_ref);
227 return replace_object_oid(object_ref, &object, replace_ref, &repl, force);
231 * Write the contents of the object named by "sha1" to the file "filename".
232 * If "raw" is true, then the object's raw contents are printed according to
233 * "type". Otherwise, we pretty-print the contents for human editing.
235 static int export_object(const struct object_id *oid, enum object_type type,
236 int raw, const char *filename)
238 struct child_process cmd = CHILD_PROCESS_INIT;
239 int fd;
241 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
242 if (fd < 0)
243 return error_errno(_("unable to open %s for writing"), filename);
245 strvec_push(&cmd.args, "--no-replace-objects");
246 strvec_push(&cmd.args, "cat-file");
247 if (raw)
248 strvec_push(&cmd.args, type_name(type));
249 else
250 strvec_push(&cmd.args, "-p");
251 strvec_push(&cmd.args, oid_to_hex(oid));
252 cmd.git_cmd = 1;
253 cmd.out = fd;
255 if (run_command(&cmd))
256 return error(_("cat-file reported failure"));
257 return 0;
261 * Read a previously-exported (and possibly edited) object back from "filename",
262 * interpreting it as "type", and writing the result to the object database.
263 * The sha1 of the written object is returned via sha1.
265 static int import_object(struct object_id *oid, enum object_type type,
266 int raw, const char *filename)
268 int fd;
270 fd = open(filename, O_RDONLY);
271 if (fd < 0)
272 return error_errno(_("unable to open %s for reading"), filename);
274 if (!raw && type == OBJ_TREE) {
275 struct child_process cmd = CHILD_PROCESS_INIT;
276 struct strbuf result = STRBUF_INIT;
278 strvec_push(&cmd.args, "mktree");
279 cmd.git_cmd = 1;
280 cmd.in = fd;
281 cmd.out = -1;
283 if (start_command(&cmd)) {
284 close(fd);
285 return error(_("unable to spawn mktree"));
288 if (strbuf_read(&result, cmd.out, the_hash_algo->hexsz + 1) < 0) {
289 error_errno(_("unable to read from mktree"));
290 close(fd);
291 close(cmd.out);
292 return -1;
294 close(cmd.out);
296 if (finish_command(&cmd)) {
297 strbuf_release(&result);
298 return error(_("mktree reported failure"));
300 if (get_oid_hex(result.buf, oid) < 0) {
301 strbuf_release(&result);
302 return error(_("mktree did not return an object name"));
305 strbuf_release(&result);
306 } else {
307 struct stat st;
308 int flags = HASH_FORMAT_CHECK | HASH_WRITE_OBJECT;
310 if (fstat(fd, &st) < 0) {
311 error_errno(_("unable to fstat %s"), filename);
312 close(fd);
313 return -1;
315 if (index_fd(the_repository->index, oid, fd, &st, type, NULL, flags) < 0)
316 return error(_("unable to write object to database"));
317 /* index_fd close()s fd for us */
321 * No need to close(fd) here; both run-command and index-fd
322 * will have done it for us.
324 return 0;
327 static int edit_and_replace(const char *object_ref, int force, int raw)
329 char *tmpfile;
330 enum object_type type;
331 struct object_id old_oid, new_oid, prev;
332 struct strbuf ref = STRBUF_INIT;
334 if (repo_get_oid(the_repository, object_ref, &old_oid) < 0)
335 return error(_("not a valid object name: '%s'"), object_ref);
337 type = oid_object_info(the_repository, &old_oid, NULL);
338 if (type < 0)
339 return error(_("unable to get object type for %s"),
340 oid_to_hex(&old_oid));
342 if (check_ref_valid(&old_oid, &prev, &ref, force)) {
343 strbuf_release(&ref);
344 return -1;
346 strbuf_release(&ref);
348 tmpfile = git_pathdup("REPLACE_EDITOBJ");
349 if (export_object(&old_oid, type, raw, tmpfile)) {
350 free(tmpfile);
351 return -1;
353 if (launch_editor(tmpfile, NULL, NULL) < 0) {
354 free(tmpfile);
355 return error(_("editing object file failed"));
357 if (import_object(&new_oid, type, raw, tmpfile)) {
358 free(tmpfile);
359 return -1;
361 free(tmpfile);
363 if (oideq(&old_oid, &new_oid))
364 return error(_("new object is the same as the old one: '%s'"), oid_to_hex(&old_oid));
366 return replace_object_oid(object_ref, &old_oid, "replacement", &new_oid, force);
369 static int replace_parents(struct strbuf *buf, int argc, const char **argv)
371 struct strbuf new_parents = STRBUF_INIT;
372 const char *parent_start, *parent_end;
373 int i;
374 const unsigned hexsz = the_hash_algo->hexsz;
376 /* find existing parents */
377 parent_start = buf->buf;
378 parent_start += hexsz + 6; /* "tree " + "hex sha1" + "\n" */
379 parent_end = parent_start;
381 while (starts_with(parent_end, "parent "))
382 parent_end += hexsz + 8; /* "parent " + "hex sha1" + "\n" */
384 /* prepare new parents */
385 for (i = 0; i < argc; i++) {
386 struct object_id oid;
387 struct commit *commit;
389 if (repo_get_oid(the_repository, argv[i], &oid) < 0) {
390 strbuf_release(&new_parents);
391 return error(_("not a valid object name: '%s'"),
392 argv[i]);
394 commit = lookup_commit_reference(the_repository, &oid);
395 if (!commit) {
396 strbuf_release(&new_parents);
397 return error(_("could not parse %s as a commit"), argv[i]);
399 strbuf_addf(&new_parents, "parent %s\n", oid_to_hex(&commit->object.oid));
402 /* replace existing parents with new ones */
403 strbuf_splice(buf, parent_start - buf->buf, parent_end - parent_start,
404 new_parents.buf, new_parents.len);
406 strbuf_release(&new_parents);
407 return 0;
410 struct check_mergetag_data {
411 int argc;
412 const char **argv;
415 static int check_one_mergetag(struct commit *commit UNUSED,
416 struct commit_extra_header *extra,
417 void *data)
419 struct check_mergetag_data *mergetag_data = (struct check_mergetag_data *)data;
420 const char *ref = mergetag_data->argv[0];
421 struct object_id tag_oid;
422 struct tag *tag;
423 int i;
425 hash_object_file(the_hash_algo, extra->value, extra->len,
426 OBJ_TAG, &tag_oid);
427 tag = lookup_tag(the_repository, &tag_oid);
428 if (!tag)
429 return error(_("bad mergetag in commit '%s'"), ref);
430 if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))
431 return error(_("malformed mergetag in commit '%s'"), ref);
433 /* iterate over new parents */
434 for (i = 1; i < mergetag_data->argc; i++) {
435 struct object_id oid;
436 if (repo_get_oid(the_repository, mergetag_data->argv[i], &oid) < 0)
437 return error(_("not a valid object name: '%s'"),
438 mergetag_data->argv[i]);
439 if (oideq(get_tagged_oid(tag), &oid))
440 return 0; /* found */
443 return error(_("original commit '%s' contains mergetag '%s' that is "
444 "discarded; use --edit instead of --graft"), ref,
445 oid_to_hex(&tag_oid));
448 static int check_mergetags(struct commit *commit, int argc, const char **argv)
450 struct check_mergetag_data mergetag_data;
452 mergetag_data.argc = argc;
453 mergetag_data.argv = argv;
454 return for_each_mergetag(check_one_mergetag, commit, &mergetag_data);
457 static int create_graft(int argc, const char **argv, int force, int gentle)
459 struct object_id old_oid, new_oid;
460 const char *old_ref = argv[0];
461 struct commit *commit;
462 struct strbuf buf = STRBUF_INIT;
463 const char *buffer;
464 unsigned long size;
466 if (repo_get_oid(the_repository, old_ref, &old_oid) < 0)
467 return error(_("not a valid object name: '%s'"), old_ref);
468 commit = lookup_commit_reference(the_repository, &old_oid);
469 if (!commit)
470 return error(_("could not parse %s"), old_ref);
472 buffer = repo_get_commit_buffer(the_repository, commit, &size);
473 strbuf_add(&buf, buffer, size);
474 repo_unuse_commit_buffer(the_repository, commit, buffer);
476 if (replace_parents(&buf, argc - 1, &argv[1]) < 0) {
477 strbuf_release(&buf);
478 return -1;
481 if (remove_signature(&buf)) {
482 warning(_("the original commit '%s' has a gpg signature"), old_ref);
483 warning(_("the signature will be removed in the replacement commit!"));
486 if (check_mergetags(commit, argc, argv)) {
487 strbuf_release(&buf);
488 return -1;
491 if (write_object_file(buf.buf, buf.len, OBJ_COMMIT, &new_oid)) {
492 strbuf_release(&buf);
493 return error(_("could not write replacement commit for: '%s'"),
494 old_ref);
497 strbuf_release(&buf);
499 if (oideq(&commit->object.oid, &new_oid)) {
500 if (gentle) {
501 warning(_("graft for '%s' unnecessary"),
502 oid_to_hex(&commit->object.oid));
503 return 0;
505 return error(_("new commit is the same as the old one: '%s'"),
506 oid_to_hex(&commit->object.oid));
509 return replace_object_oid(old_ref, &commit->object.oid,
510 "replacement", &new_oid, force);
513 static int convert_graft_file(int force)
515 const char *graft_file = repo_get_graft_file(the_repository);
516 FILE *fp = fopen_or_warn(graft_file, "r");
517 struct strbuf buf = STRBUF_INIT, err = STRBUF_INIT;
518 struct strvec args = STRVEC_INIT;
520 if (!fp)
521 return -1;
523 no_graft_file_deprecated_advice = 1;
524 while (strbuf_getline(&buf, fp) != EOF) {
525 if (*buf.buf == '#')
526 continue;
528 strvec_split(&args, buf.buf);
529 if (args.nr && create_graft(args.nr, args.v, force, 1))
530 strbuf_addf(&err, "\n\t%s", buf.buf);
531 strvec_clear(&args);
533 fclose(fp);
535 strbuf_release(&buf);
537 if (!err.len)
538 return unlink_or_warn(graft_file);
540 warning(_("could not convert the following graft(s):\n%s"), err.buf);
541 strbuf_release(&err);
543 return -1;
546 int cmd_replace(int argc,
547 const char **argv,
548 const char *prefix,
549 struct repository *repo UNUSED)
551 int force = 0;
552 int raw = 0;
553 const char *format = NULL;
554 enum {
555 MODE_UNSPECIFIED = 0,
556 MODE_LIST,
557 MODE_DELETE,
558 MODE_EDIT,
559 MODE_GRAFT,
560 MODE_CONVERT_GRAFT_FILE,
561 MODE_REPLACE
562 } cmdmode = MODE_UNSPECIFIED;
563 struct option options[] = {
564 OPT_CMDMODE('l', "list", &cmdmode, N_("list replace refs"), MODE_LIST),
565 OPT_CMDMODE('d', "delete", &cmdmode, N_("delete replace refs"), MODE_DELETE),
566 OPT_CMDMODE('e', "edit", &cmdmode, N_("edit existing object"), MODE_EDIT),
567 OPT_CMDMODE('g', "graft", &cmdmode, N_("change a commit's parents"), MODE_GRAFT),
568 OPT_CMDMODE(0, "convert-graft-file", &cmdmode, N_("convert existing graft file"), MODE_CONVERT_GRAFT_FILE),
569 OPT_BOOL_F('f', "force", &force, N_("replace the ref if it exists"),
570 PARSE_OPT_NOCOMPLETE),
571 OPT_BOOL(0, "raw", &raw, N_("do not pretty-print contents for --edit")),
572 OPT_STRING(0, "format", &format, N_("format"), N_("use this format")),
573 OPT_END()
576 disable_replace_refs();
577 git_config(git_default_config, NULL);
579 argc = parse_options(argc, argv, prefix, options, git_replace_usage, 0);
581 if (!cmdmode)
582 cmdmode = argc ? MODE_REPLACE : MODE_LIST;
584 if (format && cmdmode != MODE_LIST)
585 usage_msg_opt(_("--format cannot be used when not listing"),
586 git_replace_usage, options);
588 if (force &&
589 cmdmode != MODE_REPLACE &&
590 cmdmode != MODE_EDIT &&
591 cmdmode != MODE_GRAFT &&
592 cmdmode != MODE_CONVERT_GRAFT_FILE)
593 usage_msg_opt(_("-f only makes sense when writing a replacement"),
594 git_replace_usage, options);
596 if (raw && cmdmode != MODE_EDIT)
597 usage_msg_opt(_("--raw only makes sense with --edit"),
598 git_replace_usage, options);
600 switch (cmdmode) {
601 case MODE_DELETE:
602 if (argc < 1)
603 usage_msg_opt(_("-d needs at least one argument"),
604 git_replace_usage, options);
605 return for_each_replace_name(argv, delete_replace_ref);
607 case MODE_REPLACE:
608 if (argc != 2)
609 usage_msg_opt(_("bad number of arguments"),
610 git_replace_usage, options);
611 return replace_object(argv[0], argv[1], force);
613 case MODE_EDIT:
614 if (argc != 1)
615 usage_msg_opt(_("-e needs exactly one argument"),
616 git_replace_usage, options);
617 return edit_and_replace(argv[0], force, raw);
619 case MODE_GRAFT:
620 if (argc < 1)
621 usage_msg_opt(_("-g needs at least one argument"),
622 git_replace_usage, options);
623 return create_graft(argc, argv, force, 0);
625 case MODE_CONVERT_GRAFT_FILE:
626 if (argc != 0)
627 usage_msg_opt(_("--convert-graft-file takes no argument"),
628 git_replace_usage, options);
629 return !!convert_graft_file(force);
631 case MODE_LIST:
632 if (argc > 1)
633 usage_msg_opt(_("only one pattern can be given with -l"),
634 git_replace_usage, options);
635 return list_replace_refs(argv[0], format);
637 default:
638 BUG("invalid cmdmode %d", (int)cmdmode);