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.
15 #include "parse-options.h"
16 #include "run-command.h"
17 #include "object-store.h"
18 #include "repository.h"
21 static const char * const git_replace_usage
[] = {
22 N_("git replace [-f] <object> <replacement>"),
23 N_("git replace [-f] --edit <object>"),
24 N_("git replace [-f] --graft <commit> [<parent>...]"),
25 "git replace [-f] --convert-graft-file",
26 N_("git replace -d <object>..."),
27 N_("git replace [--format=<format>] [-l [<pattern>]]"),
33 REPLACE_FORMAT_MEDIUM
,
39 enum replace_format format
;
42 static int show_reference(struct repository
*r
, const char *refname
,
43 const struct object_id
*oid
,
44 int flag
, void *cb_data
)
46 struct show_data
*data
= cb_data
;
48 if (!wildmatch(data
->pattern
, refname
, 0)) {
49 if (data
->format
== REPLACE_FORMAT_SHORT
)
50 printf("%s\n", refname
);
51 else if (data
->format
== REPLACE_FORMAT_MEDIUM
)
52 printf("%s -> %s\n", refname
, oid_to_hex(oid
));
53 else { /* data->format == REPLACE_FORMAT_LONG */
54 struct object_id object
;
55 enum object_type obj_type
, repl_type
;
57 if (get_oid(refname
, &object
))
58 return error(_("failed to resolve '%s' as a valid ref"), refname
);
60 obj_type
= oid_object_info(r
, &object
, NULL
);
61 repl_type
= oid_object_info(r
, oid
, NULL
);
63 printf("%s (%s) -> %s (%s)\n", refname
, type_name(obj_type
),
64 oid_to_hex(oid
), type_name(repl_type
));
71 static int list_replace_refs(const char *pattern
, const char *format
)
73 struct show_data data
;
77 data
.pattern
= pattern
;
79 if (format
== NULL
|| *format
== '\0' || !strcmp(format
, "short"))
80 data
.format
= REPLACE_FORMAT_SHORT
;
81 else if (!strcmp(format
, "medium"))
82 data
.format
= REPLACE_FORMAT_MEDIUM
;
83 else if (!strcmp(format
, "long"))
84 data
.format
= REPLACE_FORMAT_LONG
;
86 * Please update _git_replace() in git-completion.bash when
90 return error(_("invalid replace format '%s'\n"
91 "valid formats are 'short', 'medium' and 'long'"),
94 for_each_replace_ref(the_repository
, show_reference
, (void *)&data
);
99 typedef int (*each_replace_name_fn
)(const char *name
, const char *ref
,
100 const struct object_id
*oid
);
102 static int for_each_replace_name(const char **argv
, each_replace_name_fn fn
)
104 const char **p
, *full_hex
;
105 struct strbuf ref
= STRBUF_INIT
;
108 struct object_id oid
;
109 const char *git_replace_ref_base
= ref_namespace
[NAMESPACE_REPLACE
].ref
;
111 strbuf_addstr(&ref
, git_replace_ref_base
);
114 for (p
= argv
; *p
; p
++) {
115 if (get_oid(*p
, &oid
)) {
116 error("failed to resolve '%s' as a valid ref", *p
);
121 strbuf_setlen(&ref
, base_len
);
122 strbuf_addstr(&ref
, oid_to_hex(&oid
));
123 full_hex
= ref
.buf
+ base_len
;
125 if (read_ref(ref
.buf
, &oid
)) {
126 error(_("replace ref '%s' not found"), full_hex
);
130 if (fn(full_hex
, ref
.buf
, &oid
))
133 strbuf_release(&ref
);
137 static int delete_replace_ref(const char *name
, const char *ref
,
138 const struct object_id
*oid
)
140 if (delete_ref(NULL
, ref
, oid
, 0))
142 printf_ln(_("Deleted replace ref '%s'"), name
);
146 static int check_ref_valid(struct object_id
*object
,
147 struct object_id
*prev
,
151 const char *git_replace_ref_base
= ref_namespace
[NAMESPACE_REPLACE
].ref
;
154 strbuf_addf(ref
, "%s%s", git_replace_ref_base
, oid_to_hex(object
));
155 if (check_refname_format(ref
->buf
, 0))
156 return error(_("'%s' is not a valid ref name"), ref
->buf
);
158 if (read_ref(ref
->buf
, prev
))
161 return error(_("replace ref '%s' already exists"), ref
->buf
);
165 static int replace_object_oid(const char *object_ref
,
166 struct object_id
*object
,
167 const char *replace_ref
,
168 struct object_id
*repl
,
171 struct object_id prev
;
172 enum object_type obj_type
, repl_type
;
173 struct strbuf ref
= STRBUF_INIT
;
174 struct ref_transaction
*transaction
;
175 struct strbuf err
= STRBUF_INIT
;
178 obj_type
= oid_object_info(the_repository
, object
, NULL
);
179 repl_type
= oid_object_info(the_repository
, repl
, NULL
);
180 if (!force
&& obj_type
!= repl_type
)
181 return error(_("Objects must be of the same type.\n"
182 "'%s' points to a replaced object of type '%s'\n"
183 "while '%s' points to a replacement object of "
185 object_ref
, type_name(obj_type
),
186 replace_ref
, type_name(repl_type
));
188 if (check_ref_valid(object
, &prev
, &ref
, force
)) {
189 strbuf_release(&ref
);
193 transaction
= ref_transaction_begin(&err
);
195 ref_transaction_update(transaction
, ref
.buf
, repl
, &prev
,
197 ref_transaction_commit(transaction
, &err
))
198 res
= error("%s", err
.buf
);
200 ref_transaction_free(transaction
);
201 strbuf_release(&ref
);
205 static int replace_object(const char *object_ref
, const char *replace_ref
, int force
)
207 struct object_id object
, repl
;
209 if (get_oid(object_ref
, &object
))
210 return error(_("failed to resolve '%s' as a valid ref"),
212 if (get_oid(replace_ref
, &repl
))
213 return error(_("failed to resolve '%s' as a valid ref"),
216 return replace_object_oid(object_ref
, &object
, replace_ref
, &repl
, force
);
220 * Write the contents of the object named by "sha1" to the file "filename".
221 * If "raw" is true, then the object's raw contents are printed according to
222 * "type". Otherwise, we pretty-print the contents for human editing.
224 static int export_object(const struct object_id
*oid
, enum object_type type
,
225 int raw
, const char *filename
)
227 struct child_process cmd
= CHILD_PROCESS_INIT
;
230 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
232 return error_errno(_("unable to open %s for writing"), filename
);
234 strvec_push(&cmd
.args
, "--no-replace-objects");
235 strvec_push(&cmd
.args
, "cat-file");
237 strvec_push(&cmd
.args
, type_name(type
));
239 strvec_push(&cmd
.args
, "-p");
240 strvec_push(&cmd
.args
, oid_to_hex(oid
));
244 if (run_command(&cmd
))
245 return error(_("cat-file reported failure"));
250 * Read a previously-exported (and possibly edited) object back from "filename",
251 * interpreting it as "type", and writing the result to the object database.
252 * The sha1 of the written object is returned via sha1.
254 static int import_object(struct object_id
*oid
, enum object_type type
,
255 int raw
, const char *filename
)
259 fd
= open(filename
, O_RDONLY
);
261 return error_errno(_("unable to open %s for reading"), filename
);
263 if (!raw
&& type
== OBJ_TREE
) {
264 struct child_process cmd
= CHILD_PROCESS_INIT
;
265 struct strbuf result
= STRBUF_INIT
;
267 strvec_push(&cmd
.args
, "mktree");
272 if (start_command(&cmd
)) {
274 return error(_("unable to spawn mktree"));
277 if (strbuf_read(&result
, cmd
.out
, the_hash_algo
->hexsz
+ 1) < 0) {
278 error_errno(_("unable to read from mktree"));
285 if (finish_command(&cmd
)) {
286 strbuf_release(&result
);
287 return error(_("mktree reported failure"));
289 if (get_oid_hex(result
.buf
, oid
) < 0) {
290 strbuf_release(&result
);
291 return error(_("mktree did not return an object name"));
294 strbuf_release(&result
);
297 int flags
= HASH_FORMAT_CHECK
| HASH_WRITE_OBJECT
;
299 if (fstat(fd
, &st
) < 0) {
300 error_errno(_("unable to fstat %s"), filename
);
304 if (index_fd(the_repository
->index
, oid
, fd
, &st
, type
, NULL
, flags
) < 0)
305 return error(_("unable to write object to database"));
306 /* index_fd close()s fd for us */
310 * No need to close(fd) here; both run-command and index-fd
311 * will have done it for us.
316 static int edit_and_replace(const char *object_ref
, int force
, int raw
)
319 enum object_type type
;
320 struct object_id old_oid
, new_oid
, prev
;
321 struct strbuf ref
= STRBUF_INIT
;
323 if (get_oid(object_ref
, &old_oid
) < 0)
324 return error(_("not a valid object name: '%s'"), object_ref
);
326 type
= oid_object_info(the_repository
, &old_oid
, NULL
);
328 return error(_("unable to get object type for %s"),
329 oid_to_hex(&old_oid
));
331 if (check_ref_valid(&old_oid
, &prev
, &ref
, force
)) {
332 strbuf_release(&ref
);
335 strbuf_release(&ref
);
337 tmpfile
= git_pathdup("REPLACE_EDITOBJ");
338 if (export_object(&old_oid
, type
, raw
, tmpfile
)) {
342 if (launch_editor(tmpfile
, NULL
, NULL
) < 0) {
344 return error(_("editing object file failed"));
346 if (import_object(&new_oid
, type
, raw
, tmpfile
)) {
352 if (oideq(&old_oid
, &new_oid
))
353 return error(_("new object is the same as the old one: '%s'"), oid_to_hex(&old_oid
));
355 return replace_object_oid(object_ref
, &old_oid
, "replacement", &new_oid
, force
);
358 static int replace_parents(struct strbuf
*buf
, int argc
, const char **argv
)
360 struct strbuf new_parents
= STRBUF_INIT
;
361 const char *parent_start
, *parent_end
;
363 const unsigned hexsz
= the_hash_algo
->hexsz
;
365 /* find existing parents */
366 parent_start
= buf
->buf
;
367 parent_start
+= hexsz
+ 6; /* "tree " + "hex sha1" + "\n" */
368 parent_end
= parent_start
;
370 while (starts_with(parent_end
, "parent "))
371 parent_end
+= hexsz
+ 8; /* "parent " + "hex sha1" + "\n" */
373 /* prepare new parents */
374 for (i
= 0; i
< argc
; i
++) {
375 struct object_id oid
;
376 struct commit
*commit
;
378 if (get_oid(argv
[i
], &oid
) < 0) {
379 strbuf_release(&new_parents
);
380 return error(_("not a valid object name: '%s'"),
383 commit
= lookup_commit_reference(the_repository
, &oid
);
385 strbuf_release(&new_parents
);
386 return error(_("could not parse %s as a commit"), argv
[i
]);
388 strbuf_addf(&new_parents
, "parent %s\n", oid_to_hex(&commit
->object
.oid
));
391 /* replace existing parents with new ones */
392 strbuf_splice(buf
, parent_start
- buf
->buf
, parent_end
- parent_start
,
393 new_parents
.buf
, new_parents
.len
);
395 strbuf_release(&new_parents
);
399 struct check_mergetag_data
{
404 static int check_one_mergetag(struct commit
*commit
,
405 struct commit_extra_header
*extra
,
408 struct check_mergetag_data
*mergetag_data
= (struct check_mergetag_data
*)data
;
409 const char *ref
= mergetag_data
->argv
[0];
410 struct object_id tag_oid
;
414 hash_object_file(the_hash_algo
, extra
->value
, extra
->len
,
416 tag
= lookup_tag(the_repository
, &tag_oid
);
418 return error(_("bad mergetag in commit '%s'"), ref
);
419 if (parse_tag_buffer(the_repository
, tag
, extra
->value
, extra
->len
))
420 return error(_("malformed mergetag in commit '%s'"), ref
);
422 /* iterate over new parents */
423 for (i
= 1; i
< mergetag_data
->argc
; i
++) {
424 struct object_id oid
;
425 if (get_oid(mergetag_data
->argv
[i
], &oid
) < 0)
426 return error(_("not a valid object name: '%s'"),
427 mergetag_data
->argv
[i
]);
428 if (oideq(get_tagged_oid(tag
), &oid
))
429 return 0; /* found */
432 return error(_("original commit '%s' contains mergetag '%s' that is "
433 "discarded; use --edit instead of --graft"), ref
,
434 oid_to_hex(&tag_oid
));
437 static int check_mergetags(struct commit
*commit
, int argc
, const char **argv
)
439 struct check_mergetag_data mergetag_data
;
441 mergetag_data
.argc
= argc
;
442 mergetag_data
.argv
= argv
;
443 return for_each_mergetag(check_one_mergetag
, commit
, &mergetag_data
);
446 static int create_graft(int argc
, const char **argv
, int force
, int gentle
)
448 struct object_id old_oid
, new_oid
;
449 const char *old_ref
= argv
[0];
450 struct commit
*commit
;
451 struct strbuf buf
= STRBUF_INIT
;
455 if (get_oid(old_ref
, &old_oid
) < 0)
456 return error(_("not a valid object name: '%s'"), old_ref
);
457 commit
= lookup_commit_reference(the_repository
, &old_oid
);
459 return error(_("could not parse %s"), old_ref
);
461 buffer
= get_commit_buffer(commit
, &size
);
462 strbuf_add(&buf
, buffer
, size
);
463 unuse_commit_buffer(commit
, buffer
);
465 if (replace_parents(&buf
, argc
- 1, &argv
[1]) < 0) {
466 strbuf_release(&buf
);
470 if (remove_signature(&buf
)) {
471 warning(_("the original commit '%s' has a gpg signature"), old_ref
);
472 warning(_("the signature will be removed in the replacement commit!"));
475 if (check_mergetags(commit
, argc
, argv
)) {
476 strbuf_release(&buf
);
480 if (write_object_file(buf
.buf
, buf
.len
, OBJ_COMMIT
, &new_oid
)) {
481 strbuf_release(&buf
);
482 return error(_("could not write replacement commit for: '%s'"),
486 strbuf_release(&buf
);
488 if (oideq(&commit
->object
.oid
, &new_oid
)) {
490 warning(_("graft for '%s' unnecessary"),
491 oid_to_hex(&commit
->object
.oid
));
494 return error(_("new commit is the same as the old one: '%s'"),
495 oid_to_hex(&commit
->object
.oid
));
498 return replace_object_oid(old_ref
, &commit
->object
.oid
,
499 "replacement", &new_oid
, force
);
502 static int convert_graft_file(int force
)
504 const char *graft_file
= get_graft_file(the_repository
);
505 FILE *fp
= fopen_or_warn(graft_file
, "r");
506 struct strbuf buf
= STRBUF_INIT
, err
= STRBUF_INIT
;
507 struct strvec args
= STRVEC_INIT
;
512 no_graft_file_deprecated_advice
= 1;
513 while (strbuf_getline(&buf
, fp
) != EOF
) {
517 strvec_split(&args
, buf
.buf
);
518 if (args
.nr
&& create_graft(args
.nr
, args
.v
, force
, 1))
519 strbuf_addf(&err
, "\n\t%s", buf
.buf
);
524 strbuf_release(&buf
);
527 return unlink_or_warn(graft_file
);
529 warning(_("could not convert the following graft(s):\n%s"), err
.buf
);
530 strbuf_release(&err
);
535 int cmd_replace(int argc
, const char **argv
, const char *prefix
)
539 const char *format
= NULL
;
541 MODE_UNSPECIFIED
= 0,
546 MODE_CONVERT_GRAFT_FILE
,
548 } cmdmode
= MODE_UNSPECIFIED
;
549 struct option options
[] = {
550 OPT_CMDMODE('l', "list", &cmdmode
, N_("list replace refs"), MODE_LIST
),
551 OPT_CMDMODE('d', "delete", &cmdmode
, N_("delete replace refs"), MODE_DELETE
),
552 OPT_CMDMODE('e', "edit", &cmdmode
, N_("edit existing object"), MODE_EDIT
),
553 OPT_CMDMODE('g', "graft", &cmdmode
, N_("change a commit's parents"), MODE_GRAFT
),
554 OPT_CMDMODE(0, "convert-graft-file", &cmdmode
, N_("convert existing graft file"), MODE_CONVERT_GRAFT_FILE
),
555 OPT_BOOL_F('f', "force", &force
, N_("replace the ref if it exists"),
556 PARSE_OPT_NOCOMPLETE
),
557 OPT_BOOL(0, "raw", &raw
, N_("do not pretty-print contents for --edit")),
558 OPT_STRING(0, "format", &format
, N_("format"), N_("use this format")),
562 read_replace_refs
= 0;
563 git_config(git_default_config
, NULL
);
565 argc
= parse_options(argc
, argv
, prefix
, options
, git_replace_usage
, 0);
568 cmdmode
= argc
? MODE_REPLACE
: MODE_LIST
;
570 if (format
&& cmdmode
!= MODE_LIST
)
571 usage_msg_opt(_("--format cannot be used when not listing"),
572 git_replace_usage
, options
);
575 cmdmode
!= MODE_REPLACE
&&
576 cmdmode
!= MODE_EDIT
&&
577 cmdmode
!= MODE_GRAFT
&&
578 cmdmode
!= MODE_CONVERT_GRAFT_FILE
)
579 usage_msg_opt(_("-f only makes sense when writing a replacement"),
580 git_replace_usage
, options
);
582 if (raw
&& cmdmode
!= MODE_EDIT
)
583 usage_msg_opt(_("--raw only makes sense with --edit"),
584 git_replace_usage
, options
);
589 usage_msg_opt(_("-d needs at least one argument"),
590 git_replace_usage
, options
);
591 return for_each_replace_name(argv
, delete_replace_ref
);
595 usage_msg_opt(_("bad number of arguments"),
596 git_replace_usage
, options
);
597 return replace_object(argv
[0], argv
[1], force
);
601 usage_msg_opt(_("-e needs exactly one argument"),
602 git_replace_usage
, options
);
603 return edit_and_replace(argv
[0], force
, raw
);
607 usage_msg_opt(_("-g needs at least one argument"),
608 git_replace_usage
, options
);
609 return create_graft(argc
, argv
, force
, 0);
611 case MODE_CONVERT_GRAFT_FILE
:
613 usage_msg_opt(_("--convert-graft-file takes no argument"),
614 git_replace_usage
, options
);
615 return !!convert_graft_file(force
);
619 usage_msg_opt(_("only one pattern can be given with -l"),
620 git_replace_usage
, options
);
621 return list_replace_refs(argv
[0], format
);
624 BUG("invalid cmdmode %d", (int)cmdmode
);