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.
14 #include "environment.h"
18 #include "parse-options.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"
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>]]"),
41 REPLACE_FORMAT_MEDIUM
,
47 enum replace_format format
;
50 static int show_reference(struct repository
*r
, const char *refname
,
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(r
, refname
, &object
))
66 return error(_("failed to resolve '%s' as a valid ref"), refname
);
68 obj_type
= oid_object_info(r
, &object
, NULL
);
69 repl_type
= oid_object_info(r
, oid
, NULL
);
71 printf("%s (%s) -> %s (%s)\n", refname
, type_name(obj_type
),
72 oid_to_hex(oid
), type_name(repl_type
));
79 static int list_replace_refs(const char *pattern
, const char *format
)
81 struct show_data data
;
85 data
.pattern
= pattern
;
87 if (format
== NULL
|| *format
== '\0' || !strcmp(format
, "short"))
88 data
.format
= REPLACE_FORMAT_SHORT
;
89 else if (!strcmp(format
, "medium"))
90 data
.format
= REPLACE_FORMAT_MEDIUM
;
91 else if (!strcmp(format
, "long"))
92 data
.format
= REPLACE_FORMAT_LONG
;
94 * Please update _git_replace() in git-completion.bash when
98 return error(_("invalid replace format '%s'\n"
99 "valid formats are 'short', 'medium' and 'long'"),
102 for_each_replace_ref(the_repository
, show_reference
, (void *)&data
);
107 typedef int (*each_replace_name_fn
)(const char *name
, const char *ref
,
108 const struct object_id
*oid
);
110 static int for_each_replace_name(const char **argv
, each_replace_name_fn fn
)
112 const char **p
, *full_hex
;
113 struct strbuf ref
= STRBUF_INIT
;
116 struct object_id oid
;
117 const char *git_replace_ref_base
= ref_namespace
[NAMESPACE_REPLACE
].ref
;
119 strbuf_addstr(&ref
, git_replace_ref_base
);
122 for (p
= argv
; *p
; p
++) {
123 if (repo_get_oid(the_repository
, *p
, &oid
)) {
124 error("failed to resolve '%s' as a valid ref", *p
);
129 strbuf_setlen(&ref
, base_len
);
130 strbuf_addstr(&ref
, oid_to_hex(&oid
));
131 full_hex
= ref
.buf
+ base_len
;
133 if (refs_read_ref(get_main_ref_store(the_repository
), ref
.buf
, &oid
)) {
134 error(_("replace ref '%s' not found"), full_hex
);
138 if (fn(full_hex
, ref
.buf
, &oid
))
141 strbuf_release(&ref
);
145 static int delete_replace_ref(const char *name
, const char *ref
,
146 const struct object_id
*oid
)
148 if (refs_delete_ref(get_main_ref_store(the_repository
), NULL
, ref
, oid
, 0))
150 printf_ln(_("Deleted replace ref '%s'"), name
);
154 static int check_ref_valid(struct object_id
*object
,
155 struct object_id
*prev
,
159 const char *git_replace_ref_base
= ref_namespace
[NAMESPACE_REPLACE
].ref
;
162 strbuf_addf(ref
, "%s%s", git_replace_ref_base
, oid_to_hex(object
));
163 if (check_refname_format(ref
->buf
, 0))
164 return error(_("'%s' is not a valid ref name"), ref
->buf
);
166 if (refs_read_ref(get_main_ref_store(the_repository
), ref
->buf
, prev
))
169 return error(_("replace ref '%s' already exists"), ref
->buf
);
173 static int replace_object_oid(const char *object_ref
,
174 struct object_id
*object
,
175 const char *replace_ref
,
176 struct object_id
*repl
,
179 struct object_id prev
;
180 enum object_type obj_type
, repl_type
;
181 struct strbuf ref
= STRBUF_INIT
;
182 struct ref_transaction
*transaction
;
183 struct strbuf err
= STRBUF_INIT
;
186 obj_type
= oid_object_info(the_repository
, object
, NULL
);
187 repl_type
= oid_object_info(the_repository
, repl
, NULL
);
188 if (!force
&& obj_type
!= repl_type
)
189 return error(_("Objects must be of the same type.\n"
190 "'%s' points to a replaced object of type '%s'\n"
191 "while '%s' points to a replacement object of "
193 object_ref
, type_name(obj_type
),
194 replace_ref
, type_name(repl_type
));
196 if (check_ref_valid(object
, &prev
, &ref
, force
)) {
197 strbuf_release(&ref
);
201 transaction
= ref_store_transaction_begin(get_main_ref_store(the_repository
),
204 ref_transaction_update(transaction
, ref
.buf
, repl
, &prev
,
205 NULL
, NULL
, 0, NULL
, &err
) ||
206 ref_transaction_commit(transaction
, &err
))
207 res
= error("%s", err
.buf
);
209 ref_transaction_free(transaction
);
210 strbuf_release(&ref
);
214 static int replace_object(const char *object_ref
, const char *replace_ref
, int force
)
216 struct object_id object
, repl
;
218 if (repo_get_oid(the_repository
, object_ref
, &object
))
219 return error(_("failed to resolve '%s' as a valid ref"),
221 if (repo_get_oid(the_repository
, replace_ref
, &repl
))
222 return error(_("failed to resolve '%s' as a valid ref"),
225 return replace_object_oid(object_ref
, &object
, replace_ref
, &repl
, force
);
229 * Write the contents of the object named by "sha1" to the file "filename".
230 * If "raw" is true, then the object's raw contents are printed according to
231 * "type". Otherwise, we pretty-print the contents for human editing.
233 static int export_object(const struct object_id
*oid
, enum object_type type
,
234 int raw
, const char *filename
)
236 struct child_process cmd
= CHILD_PROCESS_INIT
;
239 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
241 return error_errno(_("unable to open %s for writing"), filename
);
243 strvec_push(&cmd
.args
, "--no-replace-objects");
244 strvec_push(&cmd
.args
, "cat-file");
246 strvec_push(&cmd
.args
, type_name(type
));
248 strvec_push(&cmd
.args
, "-p");
249 strvec_push(&cmd
.args
, oid_to_hex(oid
));
253 if (run_command(&cmd
))
254 return error(_("cat-file reported failure"));
259 * Read a previously-exported (and possibly edited) object back from "filename",
260 * interpreting it as "type", and writing the result to the object database.
261 * The sha1 of the written object is returned via sha1.
263 static int import_object(struct object_id
*oid
, enum object_type type
,
264 int raw
, const char *filename
)
268 fd
= open(filename
, O_RDONLY
);
270 return error_errno(_("unable to open %s for reading"), filename
);
272 if (!raw
&& type
== OBJ_TREE
) {
273 struct child_process cmd
= CHILD_PROCESS_INIT
;
274 struct strbuf result
= STRBUF_INIT
;
276 strvec_push(&cmd
.args
, "mktree");
281 if (start_command(&cmd
)) {
283 return error(_("unable to spawn mktree"));
286 if (strbuf_read(&result
, cmd
.out
, the_hash_algo
->hexsz
+ 1) < 0) {
287 error_errno(_("unable to read from mktree"));
294 if (finish_command(&cmd
)) {
295 strbuf_release(&result
);
296 return error(_("mktree reported failure"));
298 if (get_oid_hex(result
.buf
, oid
) < 0) {
299 strbuf_release(&result
);
300 return error(_("mktree did not return an object name"));
303 strbuf_release(&result
);
306 int flags
= HASH_FORMAT_CHECK
| HASH_WRITE_OBJECT
;
308 if (fstat(fd
, &st
) < 0) {
309 error_errno(_("unable to fstat %s"), filename
);
313 if (index_fd(the_repository
->index
, oid
, fd
, &st
, type
, NULL
, flags
) < 0)
314 return error(_("unable to write object to database"));
315 /* index_fd close()s fd for us */
319 * No need to close(fd) here; both run-command and index-fd
320 * will have done it for us.
325 static int edit_and_replace(const char *object_ref
, int force
, int raw
)
328 enum object_type type
;
329 struct object_id old_oid
, new_oid
, prev
;
330 struct strbuf ref
= STRBUF_INIT
;
332 if (repo_get_oid(the_repository
, object_ref
, &old_oid
) < 0)
333 return error(_("not a valid object name: '%s'"), object_ref
);
335 type
= oid_object_info(the_repository
, &old_oid
, NULL
);
337 return error(_("unable to get object type for %s"),
338 oid_to_hex(&old_oid
));
340 if (check_ref_valid(&old_oid
, &prev
, &ref
, force
)) {
341 strbuf_release(&ref
);
344 strbuf_release(&ref
);
346 tmpfile
= git_pathdup("REPLACE_EDITOBJ");
347 if (export_object(&old_oid
, type
, raw
, tmpfile
)) {
351 if (launch_editor(tmpfile
, NULL
, NULL
) < 0) {
353 return error(_("editing object file failed"));
355 if (import_object(&new_oid
, type
, raw
, tmpfile
)) {
361 if (oideq(&old_oid
, &new_oid
))
362 return error(_("new object is the same as the old one: '%s'"), oid_to_hex(&old_oid
));
364 return replace_object_oid(object_ref
, &old_oid
, "replacement", &new_oid
, force
);
367 static int replace_parents(struct strbuf
*buf
, int argc
, const char **argv
)
369 struct strbuf new_parents
= STRBUF_INIT
;
370 const char *parent_start
, *parent_end
;
372 const unsigned hexsz
= the_hash_algo
->hexsz
;
374 /* find existing parents */
375 parent_start
= buf
->buf
;
376 parent_start
+= hexsz
+ 6; /* "tree " + "hex sha1" + "\n" */
377 parent_end
= parent_start
;
379 while (starts_with(parent_end
, "parent "))
380 parent_end
+= hexsz
+ 8; /* "parent " + "hex sha1" + "\n" */
382 /* prepare new parents */
383 for (i
= 0; i
< argc
; i
++) {
384 struct object_id oid
;
385 struct commit
*commit
;
387 if (repo_get_oid(the_repository
, argv
[i
], &oid
) < 0) {
388 strbuf_release(&new_parents
);
389 return error(_("not a valid object name: '%s'"),
392 commit
= lookup_commit_reference(the_repository
, &oid
);
394 strbuf_release(&new_parents
);
395 return error(_("could not parse %s as a commit"), argv
[i
]);
397 strbuf_addf(&new_parents
, "parent %s\n", oid_to_hex(&commit
->object
.oid
));
400 /* replace existing parents with new ones */
401 strbuf_splice(buf
, parent_start
- buf
->buf
, parent_end
- parent_start
,
402 new_parents
.buf
, new_parents
.len
);
404 strbuf_release(&new_parents
);
408 struct check_mergetag_data
{
413 static int check_one_mergetag(struct commit
*commit UNUSED
,
414 struct commit_extra_header
*extra
,
417 struct check_mergetag_data
*mergetag_data
= (struct check_mergetag_data
*)data
;
418 const char *ref
= mergetag_data
->argv
[0];
419 struct object_id tag_oid
;
423 hash_object_file(the_hash_algo
, extra
->value
, extra
->len
,
425 tag
= lookup_tag(the_repository
, &tag_oid
);
427 return error(_("bad mergetag in commit '%s'"), ref
);
428 if (parse_tag_buffer(the_repository
, tag
, extra
->value
, extra
->len
))
429 return error(_("malformed mergetag in commit '%s'"), ref
);
431 /* iterate over new parents */
432 for (i
= 1; i
< mergetag_data
->argc
; i
++) {
433 struct object_id oid
;
434 if (repo_get_oid(the_repository
, mergetag_data
->argv
[i
], &oid
) < 0)
435 return error(_("not a valid object name: '%s'"),
436 mergetag_data
->argv
[i
]);
437 if (oideq(get_tagged_oid(tag
), &oid
))
438 return 0; /* found */
441 return error(_("original commit '%s' contains mergetag '%s' that is "
442 "discarded; use --edit instead of --graft"), ref
,
443 oid_to_hex(&tag_oid
));
446 static int check_mergetags(struct commit
*commit
, int argc
, const char **argv
)
448 struct check_mergetag_data mergetag_data
;
450 mergetag_data
.argc
= argc
;
451 mergetag_data
.argv
= argv
;
452 return for_each_mergetag(check_one_mergetag
, commit
, &mergetag_data
);
455 static int create_graft(int argc
, const char **argv
, int force
, int gentle
)
457 struct object_id old_oid
, new_oid
;
458 const char *old_ref
= argv
[0];
459 struct commit
*commit
;
460 struct strbuf buf
= STRBUF_INIT
;
464 if (repo_get_oid(the_repository
, old_ref
, &old_oid
) < 0)
465 return error(_("not a valid object name: '%s'"), old_ref
);
466 commit
= lookup_commit_reference(the_repository
, &old_oid
);
468 return error(_("could not parse %s"), old_ref
);
470 buffer
= repo_get_commit_buffer(the_repository
, commit
, &size
);
471 strbuf_add(&buf
, buffer
, size
);
472 repo_unuse_commit_buffer(the_repository
, commit
, buffer
);
474 if (replace_parents(&buf
, argc
- 1, &argv
[1]) < 0) {
475 strbuf_release(&buf
);
479 if (remove_signature(&buf
)) {
480 warning(_("the original commit '%s' has a gpg signature"), old_ref
);
481 warning(_("the signature will be removed in the replacement commit!"));
484 if (check_mergetags(commit
, argc
, argv
)) {
485 strbuf_release(&buf
);
489 if (write_object_file(buf
.buf
, buf
.len
, OBJ_COMMIT
, &new_oid
)) {
490 strbuf_release(&buf
);
491 return error(_("could not write replacement commit for: '%s'"),
495 strbuf_release(&buf
);
497 if (oideq(&commit
->object
.oid
, &new_oid
)) {
499 warning(_("graft for '%s' unnecessary"),
500 oid_to_hex(&commit
->object
.oid
));
503 return error(_("new commit is the same as the old one: '%s'"),
504 oid_to_hex(&commit
->object
.oid
));
507 return replace_object_oid(old_ref
, &commit
->object
.oid
,
508 "replacement", &new_oid
, force
);
511 static int convert_graft_file(int force
)
513 const char *graft_file
= get_graft_file(the_repository
);
514 FILE *fp
= fopen_or_warn(graft_file
, "r");
515 struct strbuf buf
= STRBUF_INIT
, err
= STRBUF_INIT
;
516 struct strvec args
= STRVEC_INIT
;
521 no_graft_file_deprecated_advice
= 1;
522 while (strbuf_getline(&buf
, fp
) != EOF
) {
526 strvec_split(&args
, buf
.buf
);
527 if (args
.nr
&& create_graft(args
.nr
, args
.v
, force
, 1))
528 strbuf_addf(&err
, "\n\t%s", buf
.buf
);
533 strbuf_release(&buf
);
536 return unlink_or_warn(graft_file
);
538 warning(_("could not convert the following graft(s):\n%s"), err
.buf
);
539 strbuf_release(&err
);
544 int cmd_replace(int argc
, const char **argv
, const char *prefix
)
548 const char *format
= NULL
;
550 MODE_UNSPECIFIED
= 0,
555 MODE_CONVERT_GRAFT_FILE
,
557 } cmdmode
= MODE_UNSPECIFIED
;
558 struct option options
[] = {
559 OPT_CMDMODE('l', "list", &cmdmode
, N_("list replace refs"), MODE_LIST
),
560 OPT_CMDMODE('d', "delete", &cmdmode
, N_("delete replace refs"), MODE_DELETE
),
561 OPT_CMDMODE('e', "edit", &cmdmode
, N_("edit existing object"), MODE_EDIT
),
562 OPT_CMDMODE('g', "graft", &cmdmode
, N_("change a commit's parents"), MODE_GRAFT
),
563 OPT_CMDMODE(0, "convert-graft-file", &cmdmode
, N_("convert existing graft file"), MODE_CONVERT_GRAFT_FILE
),
564 OPT_BOOL_F('f', "force", &force
, N_("replace the ref if it exists"),
565 PARSE_OPT_NOCOMPLETE
),
566 OPT_BOOL(0, "raw", &raw
, N_("do not pretty-print contents for --edit")),
567 OPT_STRING(0, "format", &format
, N_("format"), N_("use this format")),
571 disable_replace_refs();
572 git_config(git_default_config
, NULL
);
574 argc
= parse_options(argc
, argv
, prefix
, options
, git_replace_usage
, 0);
577 cmdmode
= argc
? MODE_REPLACE
: MODE_LIST
;
579 if (format
&& cmdmode
!= MODE_LIST
)
580 usage_msg_opt(_("--format cannot be used when not listing"),
581 git_replace_usage
, options
);
584 cmdmode
!= MODE_REPLACE
&&
585 cmdmode
!= MODE_EDIT
&&
586 cmdmode
!= MODE_GRAFT
&&
587 cmdmode
!= MODE_CONVERT_GRAFT_FILE
)
588 usage_msg_opt(_("-f only makes sense when writing a replacement"),
589 git_replace_usage
, options
);
591 if (raw
&& cmdmode
!= MODE_EDIT
)
592 usage_msg_opt(_("--raw only makes sense with --edit"),
593 git_replace_usage
, options
);
598 usage_msg_opt(_("-d needs at least one argument"),
599 git_replace_usage
, options
);
600 return for_each_replace_name(argv
, delete_replace_ref
);
604 usage_msg_opt(_("bad number of arguments"),
605 git_replace_usage
, options
);
606 return replace_object(argv
[0], argv
[1], force
);
610 usage_msg_opt(_("-e needs exactly one argument"),
611 git_replace_usage
, options
);
612 return edit_and_replace(argv
[0], force
, raw
);
616 usage_msg_opt(_("-g needs at least one argument"),
617 git_replace_usage
, options
);
618 return create_graft(argc
, argv
, force
, 0);
620 case MODE_CONVERT_GRAFT_FILE
:
622 usage_msg_opt(_("--convert-graft-file takes no argument"),
623 git_replace_usage
, options
);
624 return !!convert_graft_file(force
);
628 usage_msg_opt(_("only one pattern can be given with -l"),
629 git_replace_usage
, options
);
630 return list_replace_refs(argv
[0], format
);
633 BUG("invalid cmdmode %d", (int)cmdmode
);