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.
16 #include "parse-options.h"
17 #include "run-command.h"
18 #include "object-store.h"
19 #include "replace-object.h"
20 #include "repository.h"
23 static const char * const git_replace_usage
[] = {
24 N_("git replace [-f] <object> <replacement>"),
25 N_("git replace [-f] --edit <object>"),
26 N_("git replace [-f] --graft <commit> [<parent>...]"),
27 "git replace [-f] --convert-graft-file",
28 N_("git replace -d <object>..."),
29 N_("git replace [--format=<format>] [-l [<pattern>]]"),
35 REPLACE_FORMAT_MEDIUM
,
41 enum replace_format format
;
44 static int show_reference(struct repository
*r
, const char *refname
,
45 const struct object_id
*oid
,
46 int flag
, void *cb_data
)
48 struct show_data
*data
= cb_data
;
50 if (!wildmatch(data
->pattern
, refname
, 0)) {
51 if (data
->format
== REPLACE_FORMAT_SHORT
)
52 printf("%s\n", refname
);
53 else if (data
->format
== REPLACE_FORMAT_MEDIUM
)
54 printf("%s -> %s\n", refname
, oid_to_hex(oid
));
55 else { /* data->format == REPLACE_FORMAT_LONG */
56 struct object_id object
;
57 enum object_type obj_type
, repl_type
;
59 if (get_oid(refname
, &object
))
60 return error(_("failed to resolve '%s' as a valid ref"), refname
);
62 obj_type
= oid_object_info(r
, &object
, NULL
);
63 repl_type
= oid_object_info(r
, oid
, NULL
);
65 printf("%s (%s) -> %s (%s)\n", refname
, type_name(obj_type
),
66 oid_to_hex(oid
), type_name(repl_type
));
73 static int list_replace_refs(const char *pattern
, const char *format
)
75 struct show_data data
;
79 data
.pattern
= pattern
;
81 if (format
== NULL
|| *format
== '\0' || !strcmp(format
, "short"))
82 data
.format
= REPLACE_FORMAT_SHORT
;
83 else if (!strcmp(format
, "medium"))
84 data
.format
= REPLACE_FORMAT_MEDIUM
;
85 else if (!strcmp(format
, "long"))
86 data
.format
= REPLACE_FORMAT_LONG
;
88 * Please update _git_replace() in git-completion.bash when
92 return error(_("invalid replace format '%s'\n"
93 "valid formats are 'short', 'medium' and 'long'"),
96 for_each_replace_ref(the_repository
, show_reference
, (void *)&data
);
101 typedef int (*each_replace_name_fn
)(const char *name
, const char *ref
,
102 const struct object_id
*oid
);
104 static int for_each_replace_name(const char **argv
, each_replace_name_fn fn
)
106 const char **p
, *full_hex
;
107 struct strbuf ref
= STRBUF_INIT
;
110 struct object_id oid
;
111 const char *git_replace_ref_base
= ref_namespace
[NAMESPACE_REPLACE
].ref
;
113 strbuf_addstr(&ref
, git_replace_ref_base
);
116 for (p
= argv
; *p
; p
++) {
117 if (get_oid(*p
, &oid
)) {
118 error("failed to resolve '%s' as a valid ref", *p
);
123 strbuf_setlen(&ref
, base_len
);
124 strbuf_addstr(&ref
, oid_to_hex(&oid
));
125 full_hex
= ref
.buf
+ base_len
;
127 if (read_ref(ref
.buf
, &oid
)) {
128 error(_("replace ref '%s' not found"), full_hex
);
132 if (fn(full_hex
, ref
.buf
, &oid
))
135 strbuf_release(&ref
);
139 static int delete_replace_ref(const char *name
, const char *ref
,
140 const struct object_id
*oid
)
142 if (delete_ref(NULL
, ref
, oid
, 0))
144 printf_ln(_("Deleted replace ref '%s'"), name
);
148 static int check_ref_valid(struct object_id
*object
,
149 struct object_id
*prev
,
153 const char *git_replace_ref_base
= ref_namespace
[NAMESPACE_REPLACE
].ref
;
156 strbuf_addf(ref
, "%s%s", git_replace_ref_base
, oid_to_hex(object
));
157 if (check_refname_format(ref
->buf
, 0))
158 return error(_("'%s' is not a valid ref name"), ref
->buf
);
160 if (read_ref(ref
->buf
, prev
))
163 return error(_("replace ref '%s' already exists"), ref
->buf
);
167 static int replace_object_oid(const char *object_ref
,
168 struct object_id
*object
,
169 const char *replace_ref
,
170 struct object_id
*repl
,
173 struct object_id prev
;
174 enum object_type obj_type
, repl_type
;
175 struct strbuf ref
= STRBUF_INIT
;
176 struct ref_transaction
*transaction
;
177 struct strbuf err
= STRBUF_INIT
;
180 obj_type
= oid_object_info(the_repository
, object
, NULL
);
181 repl_type
= oid_object_info(the_repository
, repl
, NULL
);
182 if (!force
&& obj_type
!= repl_type
)
183 return error(_("Objects must be of the same type.\n"
184 "'%s' points to a replaced object of type '%s'\n"
185 "while '%s' points to a replacement object of "
187 object_ref
, type_name(obj_type
),
188 replace_ref
, type_name(repl_type
));
190 if (check_ref_valid(object
, &prev
, &ref
, force
)) {
191 strbuf_release(&ref
);
195 transaction
= ref_transaction_begin(&err
);
197 ref_transaction_update(transaction
, ref
.buf
, repl
, &prev
,
199 ref_transaction_commit(transaction
, &err
))
200 res
= error("%s", err
.buf
);
202 ref_transaction_free(transaction
);
203 strbuf_release(&ref
);
207 static int replace_object(const char *object_ref
, const char *replace_ref
, int force
)
209 struct object_id object
, repl
;
211 if (get_oid(object_ref
, &object
))
212 return error(_("failed to resolve '%s' as a valid ref"),
214 if (get_oid(replace_ref
, &repl
))
215 return error(_("failed to resolve '%s' as a valid ref"),
218 return replace_object_oid(object_ref
, &object
, replace_ref
, &repl
, force
);
222 * Write the contents of the object named by "sha1" to the file "filename".
223 * If "raw" is true, then the object's raw contents are printed according to
224 * "type". Otherwise, we pretty-print the contents for human editing.
226 static int export_object(const struct object_id
*oid
, enum object_type type
,
227 int raw
, const char *filename
)
229 struct child_process cmd
= CHILD_PROCESS_INIT
;
232 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
234 return error_errno(_("unable to open %s for writing"), filename
);
236 strvec_push(&cmd
.args
, "--no-replace-objects");
237 strvec_push(&cmd
.args
, "cat-file");
239 strvec_push(&cmd
.args
, type_name(type
));
241 strvec_push(&cmd
.args
, "-p");
242 strvec_push(&cmd
.args
, oid_to_hex(oid
));
246 if (run_command(&cmd
))
247 return error(_("cat-file reported failure"));
252 * Read a previously-exported (and possibly edited) object back from "filename",
253 * interpreting it as "type", and writing the result to the object database.
254 * The sha1 of the written object is returned via sha1.
256 static int import_object(struct object_id
*oid
, enum object_type type
,
257 int raw
, const char *filename
)
261 fd
= open(filename
, O_RDONLY
);
263 return error_errno(_("unable to open %s for reading"), filename
);
265 if (!raw
&& type
== OBJ_TREE
) {
266 struct child_process cmd
= CHILD_PROCESS_INIT
;
267 struct strbuf result
= STRBUF_INIT
;
269 strvec_push(&cmd
.args
, "mktree");
274 if (start_command(&cmd
)) {
276 return error(_("unable to spawn mktree"));
279 if (strbuf_read(&result
, cmd
.out
, the_hash_algo
->hexsz
+ 1) < 0) {
280 error_errno(_("unable to read from mktree"));
287 if (finish_command(&cmd
)) {
288 strbuf_release(&result
);
289 return error(_("mktree reported failure"));
291 if (get_oid_hex(result
.buf
, oid
) < 0) {
292 strbuf_release(&result
);
293 return error(_("mktree did not return an object name"));
296 strbuf_release(&result
);
299 int flags
= HASH_FORMAT_CHECK
| HASH_WRITE_OBJECT
;
301 if (fstat(fd
, &st
) < 0) {
302 error_errno(_("unable to fstat %s"), filename
);
306 if (index_fd(the_repository
->index
, oid
, fd
, &st
, type
, NULL
, flags
) < 0)
307 return error(_("unable to write object to database"));
308 /* index_fd close()s fd for us */
312 * No need to close(fd) here; both run-command and index-fd
313 * will have done it for us.
318 static int edit_and_replace(const char *object_ref
, int force
, int raw
)
321 enum object_type type
;
322 struct object_id old_oid
, new_oid
, prev
;
323 struct strbuf ref
= STRBUF_INIT
;
325 if (get_oid(object_ref
, &old_oid
) < 0)
326 return error(_("not a valid object name: '%s'"), object_ref
);
328 type
= oid_object_info(the_repository
, &old_oid
, NULL
);
330 return error(_("unable to get object type for %s"),
331 oid_to_hex(&old_oid
));
333 if (check_ref_valid(&old_oid
, &prev
, &ref
, force
)) {
334 strbuf_release(&ref
);
337 strbuf_release(&ref
);
339 tmpfile
= git_pathdup("REPLACE_EDITOBJ");
340 if (export_object(&old_oid
, type
, raw
, tmpfile
)) {
344 if (launch_editor(tmpfile
, NULL
, NULL
) < 0) {
346 return error(_("editing object file failed"));
348 if (import_object(&new_oid
, type
, raw
, tmpfile
)) {
354 if (oideq(&old_oid
, &new_oid
))
355 return error(_("new object is the same as the old one: '%s'"), oid_to_hex(&old_oid
));
357 return replace_object_oid(object_ref
, &old_oid
, "replacement", &new_oid
, force
);
360 static int replace_parents(struct strbuf
*buf
, int argc
, const char **argv
)
362 struct strbuf new_parents
= STRBUF_INIT
;
363 const char *parent_start
, *parent_end
;
365 const unsigned hexsz
= the_hash_algo
->hexsz
;
367 /* find existing parents */
368 parent_start
= buf
->buf
;
369 parent_start
+= hexsz
+ 6; /* "tree " + "hex sha1" + "\n" */
370 parent_end
= parent_start
;
372 while (starts_with(parent_end
, "parent "))
373 parent_end
+= hexsz
+ 8; /* "parent " + "hex sha1" + "\n" */
375 /* prepare new parents */
376 for (i
= 0; i
< argc
; i
++) {
377 struct object_id oid
;
378 struct commit
*commit
;
380 if (get_oid(argv
[i
], &oid
) < 0) {
381 strbuf_release(&new_parents
);
382 return error(_("not a valid object name: '%s'"),
385 commit
= lookup_commit_reference(the_repository
, &oid
);
387 strbuf_release(&new_parents
);
388 return error(_("could not parse %s as a commit"), argv
[i
]);
390 strbuf_addf(&new_parents
, "parent %s\n", oid_to_hex(&commit
->object
.oid
));
393 /* replace existing parents with new ones */
394 strbuf_splice(buf
, parent_start
- buf
->buf
, parent_end
- parent_start
,
395 new_parents
.buf
, new_parents
.len
);
397 strbuf_release(&new_parents
);
401 struct check_mergetag_data
{
406 static int check_one_mergetag(struct commit
*commit
,
407 struct commit_extra_header
*extra
,
410 struct check_mergetag_data
*mergetag_data
= (struct check_mergetag_data
*)data
;
411 const char *ref
= mergetag_data
->argv
[0];
412 struct object_id tag_oid
;
416 hash_object_file(the_hash_algo
, extra
->value
, extra
->len
,
418 tag
= lookup_tag(the_repository
, &tag_oid
);
420 return error(_("bad mergetag in commit '%s'"), ref
);
421 if (parse_tag_buffer(the_repository
, tag
, extra
->value
, extra
->len
))
422 return error(_("malformed mergetag in commit '%s'"), ref
);
424 /* iterate over new parents */
425 for (i
= 1; i
< mergetag_data
->argc
; i
++) {
426 struct object_id oid
;
427 if (get_oid(mergetag_data
->argv
[i
], &oid
) < 0)
428 return error(_("not a valid object name: '%s'"),
429 mergetag_data
->argv
[i
]);
430 if (oideq(get_tagged_oid(tag
), &oid
))
431 return 0; /* found */
434 return error(_("original commit '%s' contains mergetag '%s' that is "
435 "discarded; use --edit instead of --graft"), ref
,
436 oid_to_hex(&tag_oid
));
439 static int check_mergetags(struct commit
*commit
, int argc
, const char **argv
)
441 struct check_mergetag_data mergetag_data
;
443 mergetag_data
.argc
= argc
;
444 mergetag_data
.argv
= argv
;
445 return for_each_mergetag(check_one_mergetag
, commit
, &mergetag_data
);
448 static int create_graft(int argc
, const char **argv
, int force
, int gentle
)
450 struct object_id old_oid
, new_oid
;
451 const char *old_ref
= argv
[0];
452 struct commit
*commit
;
453 struct strbuf buf
= STRBUF_INIT
;
457 if (get_oid(old_ref
, &old_oid
) < 0)
458 return error(_("not a valid object name: '%s'"), old_ref
);
459 commit
= lookup_commit_reference(the_repository
, &old_oid
);
461 return error(_("could not parse %s"), old_ref
);
463 buffer
= get_commit_buffer(commit
, &size
);
464 strbuf_add(&buf
, buffer
, size
);
465 unuse_commit_buffer(commit
, buffer
);
467 if (replace_parents(&buf
, argc
- 1, &argv
[1]) < 0) {
468 strbuf_release(&buf
);
472 if (remove_signature(&buf
)) {
473 warning(_("the original commit '%s' has a gpg signature"), old_ref
);
474 warning(_("the signature will be removed in the replacement commit!"));
477 if (check_mergetags(commit
, argc
, argv
)) {
478 strbuf_release(&buf
);
482 if (write_object_file(buf
.buf
, buf
.len
, OBJ_COMMIT
, &new_oid
)) {
483 strbuf_release(&buf
);
484 return error(_("could not write replacement commit for: '%s'"),
488 strbuf_release(&buf
);
490 if (oideq(&commit
->object
.oid
, &new_oid
)) {
492 warning(_("graft for '%s' unnecessary"),
493 oid_to_hex(&commit
->object
.oid
));
496 return error(_("new commit is the same as the old one: '%s'"),
497 oid_to_hex(&commit
->object
.oid
));
500 return replace_object_oid(old_ref
, &commit
->object
.oid
,
501 "replacement", &new_oid
, force
);
504 static int convert_graft_file(int force
)
506 const char *graft_file
= get_graft_file(the_repository
);
507 FILE *fp
= fopen_or_warn(graft_file
, "r");
508 struct strbuf buf
= STRBUF_INIT
, err
= STRBUF_INIT
;
509 struct strvec args
= STRVEC_INIT
;
514 no_graft_file_deprecated_advice
= 1;
515 while (strbuf_getline(&buf
, fp
) != EOF
) {
519 strvec_split(&args
, buf
.buf
);
520 if (args
.nr
&& create_graft(args
.nr
, args
.v
, force
, 1))
521 strbuf_addf(&err
, "\n\t%s", buf
.buf
);
526 strbuf_release(&buf
);
529 return unlink_or_warn(graft_file
);
531 warning(_("could not convert the following graft(s):\n%s"), err
.buf
);
532 strbuf_release(&err
);
537 int cmd_replace(int argc
, const char **argv
, const char *prefix
)
541 const char *format
= NULL
;
543 MODE_UNSPECIFIED
= 0,
548 MODE_CONVERT_GRAFT_FILE
,
550 } cmdmode
= MODE_UNSPECIFIED
;
551 struct option options
[] = {
552 OPT_CMDMODE('l', "list", &cmdmode
, N_("list replace refs"), MODE_LIST
),
553 OPT_CMDMODE('d', "delete", &cmdmode
, N_("delete replace refs"), MODE_DELETE
),
554 OPT_CMDMODE('e', "edit", &cmdmode
, N_("edit existing object"), MODE_EDIT
),
555 OPT_CMDMODE('g', "graft", &cmdmode
, N_("change a commit's parents"), MODE_GRAFT
),
556 OPT_CMDMODE(0, "convert-graft-file", &cmdmode
, N_("convert existing graft file"), MODE_CONVERT_GRAFT_FILE
),
557 OPT_BOOL_F('f', "force", &force
, N_("replace the ref if it exists"),
558 PARSE_OPT_NOCOMPLETE
),
559 OPT_BOOL(0, "raw", &raw
, N_("do not pretty-print contents for --edit")),
560 OPT_STRING(0, "format", &format
, N_("format"), N_("use this format")),
564 read_replace_refs
= 0;
565 git_config(git_default_config
, NULL
);
567 argc
= parse_options(argc
, argv
, prefix
, options
, git_replace_usage
, 0);
570 cmdmode
= argc
? MODE_REPLACE
: MODE_LIST
;
572 if (format
&& cmdmode
!= MODE_LIST
)
573 usage_msg_opt(_("--format cannot be used when not listing"),
574 git_replace_usage
, options
);
577 cmdmode
!= MODE_REPLACE
&&
578 cmdmode
!= MODE_EDIT
&&
579 cmdmode
!= MODE_GRAFT
&&
580 cmdmode
!= MODE_CONVERT_GRAFT_FILE
)
581 usage_msg_opt(_("-f only makes sense when writing a replacement"),
582 git_replace_usage
, options
);
584 if (raw
&& cmdmode
!= MODE_EDIT
)
585 usage_msg_opt(_("--raw only makes sense with --edit"),
586 git_replace_usage
, options
);
591 usage_msg_opt(_("-d needs at least one argument"),
592 git_replace_usage
, options
);
593 return for_each_replace_name(argv
, delete_replace_ref
);
597 usage_msg_opt(_("bad number of arguments"),
598 git_replace_usage
, options
);
599 return replace_object(argv
[0], argv
[1], force
);
603 usage_msg_opt(_("-e needs exactly one argument"),
604 git_replace_usage
, options
);
605 return edit_and_replace(argv
[0], force
, raw
);
609 usage_msg_opt(_("-g needs at least one argument"),
610 git_replace_usage
, options
);
611 return create_graft(argc
, argv
, force
, 0);
613 case MODE_CONVERT_GRAFT_FILE
:
615 usage_msg_opt(_("--convert-graft-file takes no argument"),
616 git_replace_usage
, options
);
617 return !!convert_graft_file(force
);
621 usage_msg_opt(_("only one pattern can be given with -l"),
622 git_replace_usage
, options
);
623 return list_replace_refs(argv
[0], format
);
626 BUG("invalid cmdmode %d", (int)cmdmode
);