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 "environment.h"
19 #include "parse-options.h"
20 #include "run-command.h"
21 #include "object-file.h"
22 #include "object-name.h"
23 #include "object-store.h"
24 #include "replace-object.h"
25 #include "repository.h"
28 static const char * const git_replace_usage
[] = {
29 N_("git replace [-f] <object> <replacement>"),
30 N_("git replace [-f] --edit <object>"),
31 N_("git replace [-f] --graft <commit> [<parent>...]"),
32 "git replace [-f] --convert-graft-file",
33 N_("git replace -d <object>..."),
34 N_("git replace [--format=<format>] [-l [<pattern>]]"),
40 REPLACE_FORMAT_MEDIUM
,
46 enum replace_format format
;
49 static int show_reference(struct repository
*r
, const char *refname
,
50 const struct object_id
*oid
,
51 int flag
, void *cb_data
)
53 struct show_data
*data
= cb_data
;
55 if (!wildmatch(data
->pattern
, refname
, 0)) {
56 if (data
->format
== REPLACE_FORMAT_SHORT
)
57 printf("%s\n", refname
);
58 else if (data
->format
== REPLACE_FORMAT_MEDIUM
)
59 printf("%s -> %s\n", refname
, oid_to_hex(oid
));
60 else { /* data->format == REPLACE_FORMAT_LONG */
61 struct object_id object
;
62 enum object_type obj_type
, repl_type
;
64 if (repo_get_oid(r
, refname
, &object
))
65 return error(_("failed to resolve '%s' as a valid ref"), refname
);
67 obj_type
= oid_object_info(r
, &object
, NULL
);
68 repl_type
= oid_object_info(r
, oid
, NULL
);
70 printf("%s (%s) -> %s (%s)\n", refname
, type_name(obj_type
),
71 oid_to_hex(oid
), type_name(repl_type
));
78 static int list_replace_refs(const char *pattern
, const char *format
)
80 struct show_data data
;
84 data
.pattern
= pattern
;
86 if (format
== NULL
|| *format
== '\0' || !strcmp(format
, "short"))
87 data
.format
= REPLACE_FORMAT_SHORT
;
88 else if (!strcmp(format
, "medium"))
89 data
.format
= REPLACE_FORMAT_MEDIUM
;
90 else if (!strcmp(format
, "long"))
91 data
.format
= REPLACE_FORMAT_LONG
;
93 * Please update _git_replace() in git-completion.bash when
97 return error(_("invalid replace format '%s'\n"
98 "valid formats are 'short', 'medium' and 'long'"),
101 for_each_replace_ref(the_repository
, show_reference
, (void *)&data
);
106 typedef int (*each_replace_name_fn
)(const char *name
, const char *ref
,
107 const struct object_id
*oid
);
109 static int for_each_replace_name(const char **argv
, each_replace_name_fn fn
)
111 const char **p
, *full_hex
;
112 struct strbuf ref
= STRBUF_INIT
;
115 struct object_id oid
;
116 const char *git_replace_ref_base
= ref_namespace
[NAMESPACE_REPLACE
].ref
;
118 strbuf_addstr(&ref
, git_replace_ref_base
);
121 for (p
= argv
; *p
; p
++) {
122 if (repo_get_oid(the_repository
, *p
, &oid
)) {
123 error("failed to resolve '%s' as a valid ref", *p
);
128 strbuf_setlen(&ref
, base_len
);
129 strbuf_addstr(&ref
, oid_to_hex(&oid
));
130 full_hex
= ref
.buf
+ base_len
;
132 if (read_ref(ref
.buf
, &oid
)) {
133 error(_("replace ref '%s' not found"), full_hex
);
137 if (fn(full_hex
, ref
.buf
, &oid
))
140 strbuf_release(&ref
);
144 static int delete_replace_ref(const char *name
, const char *ref
,
145 const struct object_id
*oid
)
147 if (delete_ref(NULL
, ref
, oid
, 0))
149 printf_ln(_("Deleted replace ref '%s'"), name
);
153 static int check_ref_valid(struct object_id
*object
,
154 struct object_id
*prev
,
158 const char *git_replace_ref_base
= ref_namespace
[NAMESPACE_REPLACE
].ref
;
161 strbuf_addf(ref
, "%s%s", git_replace_ref_base
, oid_to_hex(object
));
162 if (check_refname_format(ref
->buf
, 0))
163 return error(_("'%s' is not a valid ref name"), ref
->buf
);
165 if (read_ref(ref
->buf
, prev
))
168 return error(_("replace ref '%s' already exists"), ref
->buf
);
172 static int replace_object_oid(const char *object_ref
,
173 struct object_id
*object
,
174 const char *replace_ref
,
175 struct object_id
*repl
,
178 struct object_id prev
;
179 enum object_type obj_type
, repl_type
;
180 struct strbuf ref
= STRBUF_INIT
;
181 struct ref_transaction
*transaction
;
182 struct strbuf err
= STRBUF_INIT
;
185 obj_type
= oid_object_info(the_repository
, object
, NULL
);
186 repl_type
= oid_object_info(the_repository
, repl
, NULL
);
187 if (!force
&& obj_type
!= repl_type
)
188 return error(_("Objects must be of the same type.\n"
189 "'%s' points to a replaced object of type '%s'\n"
190 "while '%s' points to a replacement object of "
192 object_ref
, type_name(obj_type
),
193 replace_ref
, type_name(repl_type
));
195 if (check_ref_valid(object
, &prev
, &ref
, force
)) {
196 strbuf_release(&ref
);
200 transaction
= ref_transaction_begin(&err
);
202 ref_transaction_update(transaction
, ref
.buf
, repl
, &prev
,
204 ref_transaction_commit(transaction
, &err
))
205 res
= error("%s", err
.buf
);
207 ref_transaction_free(transaction
);
208 strbuf_release(&ref
);
212 static int replace_object(const char *object_ref
, const char *replace_ref
, int force
)
214 struct object_id object
, repl
;
216 if (repo_get_oid(the_repository
, object_ref
, &object
))
217 return error(_("failed to resolve '%s' as a valid ref"),
219 if (repo_get_oid(the_repository
, replace_ref
, &repl
))
220 return error(_("failed to resolve '%s' as a valid ref"),
223 return replace_object_oid(object_ref
, &object
, replace_ref
, &repl
, force
);
227 * Write the contents of the object named by "sha1" to the file "filename".
228 * If "raw" is true, then the object's raw contents are printed according to
229 * "type". Otherwise, we pretty-print the contents for human editing.
231 static int export_object(const struct object_id
*oid
, enum object_type type
,
232 int raw
, const char *filename
)
234 struct child_process cmd
= CHILD_PROCESS_INIT
;
237 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
239 return error_errno(_("unable to open %s for writing"), filename
);
241 strvec_push(&cmd
.args
, "--no-replace-objects");
242 strvec_push(&cmd
.args
, "cat-file");
244 strvec_push(&cmd
.args
, type_name(type
));
246 strvec_push(&cmd
.args
, "-p");
247 strvec_push(&cmd
.args
, oid_to_hex(oid
));
251 if (run_command(&cmd
))
252 return error(_("cat-file reported failure"));
257 * Read a previously-exported (and possibly edited) object back from "filename",
258 * interpreting it as "type", and writing the result to the object database.
259 * The sha1 of the written object is returned via sha1.
261 static int import_object(struct object_id
*oid
, enum object_type type
,
262 int raw
, const char *filename
)
266 fd
= open(filename
, O_RDONLY
);
268 return error_errno(_("unable to open %s for reading"), filename
);
270 if (!raw
&& type
== OBJ_TREE
) {
271 struct child_process cmd
= CHILD_PROCESS_INIT
;
272 struct strbuf result
= STRBUF_INIT
;
274 strvec_push(&cmd
.args
, "mktree");
279 if (start_command(&cmd
)) {
281 return error(_("unable to spawn mktree"));
284 if (strbuf_read(&result
, cmd
.out
, the_hash_algo
->hexsz
+ 1) < 0) {
285 error_errno(_("unable to read from mktree"));
292 if (finish_command(&cmd
)) {
293 strbuf_release(&result
);
294 return error(_("mktree reported failure"));
296 if (get_oid_hex(result
.buf
, oid
) < 0) {
297 strbuf_release(&result
);
298 return error(_("mktree did not return an object name"));
301 strbuf_release(&result
);
304 int flags
= HASH_FORMAT_CHECK
| HASH_WRITE_OBJECT
;
306 if (fstat(fd
, &st
) < 0) {
307 error_errno(_("unable to fstat %s"), filename
);
311 if (index_fd(the_repository
->index
, oid
, fd
, &st
, type
, NULL
, flags
) < 0)
312 return error(_("unable to write object to database"));
313 /* index_fd close()s fd for us */
317 * No need to close(fd) here; both run-command and index-fd
318 * will have done it for us.
323 static int edit_and_replace(const char *object_ref
, int force
, int raw
)
326 enum object_type type
;
327 struct object_id old_oid
, new_oid
, prev
;
328 struct strbuf ref
= STRBUF_INIT
;
330 if (repo_get_oid(the_repository
, object_ref
, &old_oid
) < 0)
331 return error(_("not a valid object name: '%s'"), object_ref
);
333 type
= oid_object_info(the_repository
, &old_oid
, NULL
);
335 return error(_("unable to get object type for %s"),
336 oid_to_hex(&old_oid
));
338 if (check_ref_valid(&old_oid
, &prev
, &ref
, force
)) {
339 strbuf_release(&ref
);
342 strbuf_release(&ref
);
344 tmpfile
= git_pathdup("REPLACE_EDITOBJ");
345 if (export_object(&old_oid
, type
, raw
, tmpfile
)) {
349 if (launch_editor(tmpfile
, NULL
, NULL
) < 0) {
351 return error(_("editing object file failed"));
353 if (import_object(&new_oid
, type
, raw
, tmpfile
)) {
359 if (oideq(&old_oid
, &new_oid
))
360 return error(_("new object is the same as the old one: '%s'"), oid_to_hex(&old_oid
));
362 return replace_object_oid(object_ref
, &old_oid
, "replacement", &new_oid
, force
);
365 static int replace_parents(struct strbuf
*buf
, int argc
, const char **argv
)
367 struct strbuf new_parents
= STRBUF_INIT
;
368 const char *parent_start
, *parent_end
;
370 const unsigned hexsz
= the_hash_algo
->hexsz
;
372 /* find existing parents */
373 parent_start
= buf
->buf
;
374 parent_start
+= hexsz
+ 6; /* "tree " + "hex sha1" + "\n" */
375 parent_end
= parent_start
;
377 while (starts_with(parent_end
, "parent "))
378 parent_end
+= hexsz
+ 8; /* "parent " + "hex sha1" + "\n" */
380 /* prepare new parents */
381 for (i
= 0; i
< argc
; i
++) {
382 struct object_id oid
;
383 struct commit
*commit
;
385 if (repo_get_oid(the_repository
, argv
[i
], &oid
) < 0) {
386 strbuf_release(&new_parents
);
387 return error(_("not a valid object name: '%s'"),
390 commit
= lookup_commit_reference(the_repository
, &oid
);
392 strbuf_release(&new_parents
);
393 return error(_("could not parse %s as a commit"), argv
[i
]);
395 strbuf_addf(&new_parents
, "parent %s\n", oid_to_hex(&commit
->object
.oid
));
398 /* replace existing parents with new ones */
399 strbuf_splice(buf
, parent_start
- buf
->buf
, parent_end
- parent_start
,
400 new_parents
.buf
, new_parents
.len
);
402 strbuf_release(&new_parents
);
406 struct check_mergetag_data
{
411 static int check_one_mergetag(struct commit
*commit
,
412 struct commit_extra_header
*extra
,
415 struct check_mergetag_data
*mergetag_data
= (struct check_mergetag_data
*)data
;
416 const char *ref
= mergetag_data
->argv
[0];
417 struct object_id tag_oid
;
421 hash_object_file(the_hash_algo
, extra
->value
, extra
->len
,
423 tag
= lookup_tag(the_repository
, &tag_oid
);
425 return error(_("bad mergetag in commit '%s'"), ref
);
426 if (parse_tag_buffer(the_repository
, tag
, extra
->value
, extra
->len
))
427 return error(_("malformed mergetag in commit '%s'"), ref
);
429 /* iterate over new parents */
430 for (i
= 1; i
< mergetag_data
->argc
; i
++) {
431 struct object_id oid
;
432 if (repo_get_oid(the_repository
, mergetag_data
->argv
[i
], &oid
) < 0)
433 return error(_("not a valid object name: '%s'"),
434 mergetag_data
->argv
[i
]);
435 if (oideq(get_tagged_oid(tag
), &oid
))
436 return 0; /* found */
439 return error(_("original commit '%s' contains mergetag '%s' that is "
440 "discarded; use --edit instead of --graft"), ref
,
441 oid_to_hex(&tag_oid
));
444 static int check_mergetags(struct commit
*commit
, int argc
, const char **argv
)
446 struct check_mergetag_data mergetag_data
;
448 mergetag_data
.argc
= argc
;
449 mergetag_data
.argv
= argv
;
450 return for_each_mergetag(check_one_mergetag
, commit
, &mergetag_data
);
453 static int create_graft(int argc
, const char **argv
, int force
, int gentle
)
455 struct object_id old_oid
, new_oid
;
456 const char *old_ref
= argv
[0];
457 struct commit
*commit
;
458 struct strbuf buf
= STRBUF_INIT
;
462 if (repo_get_oid(the_repository
, old_ref
, &old_oid
) < 0)
463 return error(_("not a valid object name: '%s'"), old_ref
);
464 commit
= lookup_commit_reference(the_repository
, &old_oid
);
466 return error(_("could not parse %s"), old_ref
);
468 buffer
= repo_get_commit_buffer(the_repository
, commit
, &size
);
469 strbuf_add(&buf
, buffer
, size
);
470 repo_unuse_commit_buffer(the_repository
, commit
, buffer
);
472 if (replace_parents(&buf
, argc
- 1, &argv
[1]) < 0) {
473 strbuf_release(&buf
);
477 if (remove_signature(&buf
)) {
478 warning(_("the original commit '%s' has a gpg signature"), old_ref
);
479 warning(_("the signature will be removed in the replacement commit!"));
482 if (check_mergetags(commit
, argc
, argv
)) {
483 strbuf_release(&buf
);
487 if (write_object_file(buf
.buf
, buf
.len
, OBJ_COMMIT
, &new_oid
)) {
488 strbuf_release(&buf
);
489 return error(_("could not write replacement commit for: '%s'"),
493 strbuf_release(&buf
);
495 if (oideq(&commit
->object
.oid
, &new_oid
)) {
497 warning(_("graft for '%s' unnecessary"),
498 oid_to_hex(&commit
->object
.oid
));
501 return error(_("new commit is the same as the old one: '%s'"),
502 oid_to_hex(&commit
->object
.oid
));
505 return replace_object_oid(old_ref
, &commit
->object
.oid
,
506 "replacement", &new_oid
, force
);
509 static int convert_graft_file(int force
)
511 const char *graft_file
= get_graft_file(the_repository
);
512 FILE *fp
= fopen_or_warn(graft_file
, "r");
513 struct strbuf buf
= STRBUF_INIT
, err
= STRBUF_INIT
;
514 struct strvec args
= STRVEC_INIT
;
519 no_graft_file_deprecated_advice
= 1;
520 while (strbuf_getline(&buf
, fp
) != EOF
) {
524 strvec_split(&args
, buf
.buf
);
525 if (args
.nr
&& create_graft(args
.nr
, args
.v
, force
, 1))
526 strbuf_addf(&err
, "\n\t%s", buf
.buf
);
531 strbuf_release(&buf
);
534 return unlink_or_warn(graft_file
);
536 warning(_("could not convert the following graft(s):\n%s"), err
.buf
);
537 strbuf_release(&err
);
542 int cmd_replace(int argc
, const char **argv
, const char *prefix
)
546 const char *format
= NULL
;
548 MODE_UNSPECIFIED
= 0,
553 MODE_CONVERT_GRAFT_FILE
,
555 } cmdmode
= MODE_UNSPECIFIED
;
556 struct option options
[] = {
557 OPT_CMDMODE('l', "list", &cmdmode
, N_("list replace refs"), MODE_LIST
),
558 OPT_CMDMODE('d', "delete", &cmdmode
, N_("delete replace refs"), MODE_DELETE
),
559 OPT_CMDMODE('e', "edit", &cmdmode
, N_("edit existing object"), MODE_EDIT
),
560 OPT_CMDMODE('g', "graft", &cmdmode
, N_("change a commit's parents"), MODE_GRAFT
),
561 OPT_CMDMODE(0, "convert-graft-file", &cmdmode
, N_("convert existing graft file"), MODE_CONVERT_GRAFT_FILE
),
562 OPT_BOOL_F('f', "force", &force
, N_("replace the ref if it exists"),
563 PARSE_OPT_NOCOMPLETE
),
564 OPT_BOOL(0, "raw", &raw
, N_("do not pretty-print contents for --edit")),
565 OPT_STRING(0, "format", &format
, N_("format"), N_("use this format")),
569 read_replace_refs
= 0;
570 git_config(git_default_config
, NULL
);
572 argc
= parse_options(argc
, argv
, prefix
, options
, git_replace_usage
, 0);
575 cmdmode
= argc
? MODE_REPLACE
: MODE_LIST
;
577 if (format
&& cmdmode
!= MODE_LIST
)
578 usage_msg_opt(_("--format cannot be used when not listing"),
579 git_replace_usage
, options
);
582 cmdmode
!= MODE_REPLACE
&&
583 cmdmode
!= MODE_EDIT
&&
584 cmdmode
!= MODE_GRAFT
&&
585 cmdmode
!= MODE_CONVERT_GRAFT_FILE
)
586 usage_msg_opt(_("-f only makes sense when writing a replacement"),
587 git_replace_usage
, options
);
589 if (raw
&& cmdmode
!= MODE_EDIT
)
590 usage_msg_opt(_("--raw only makes sense with --edit"),
591 git_replace_usage
, options
);
596 usage_msg_opt(_("-d needs at least one argument"),
597 git_replace_usage
, options
);
598 return for_each_replace_name(argv
, delete_replace_ref
);
602 usage_msg_opt(_("bad number of arguments"),
603 git_replace_usage
, options
);
604 return replace_object(argv
[0], argv
[1], force
);
608 usage_msg_opt(_("-e needs exactly one argument"),
609 git_replace_usage
, options
);
610 return edit_and_replace(argv
[0], force
, raw
);
614 usage_msg_opt(_("-g needs at least one argument"),
615 git_replace_usage
, options
);
616 return create_graft(argc
, argv
, force
, 0);
618 case MODE_CONVERT_GRAFT_FILE
:
620 usage_msg_opt(_("--convert-graft-file takes no argument"),
621 git_replace_usage
, options
);
622 return !!convert_graft_file(force
);
626 usage_msg_opt(_("only one pattern can be given with -l"),
627 git_replace_usage
, options
);
628 return list_replace_refs(argv
[0], format
);
631 BUG("invalid cmdmode %d", (int)cmdmode
);