Skip to content

Commit ee521ec

Browse files
chriscoolgitster
authored andcommitted
replace: peel tag when passing a tag first to --graft
When passing a tag as the first argument to `git replace --graft`, it can be useful to accept it and use the underlying commit as a the commit that will be replaced. This already works for lightweight tags, but unfortunately for annotated tags we have been using the hash of the tag object instead of the hash of the underlying commit. Especially we would pass the hash of the tag object to replace_object_oid() where we would likely fail with an error like: "error: Objects must be of the same type. 'annotated_replaced_object' points to a replaced object of type 'tag' while 'replacement' points to a replacement object of type 'commit'." This patch fixes that by using the hash of the underlying commit when an annotated tag is passed. Signed-off-by: Christian Couder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f8e44a8 commit ee521ec

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

builtin/replace.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,15 +477,18 @@ static int create_graft(int argc, const char **argv, int force, int gentle)
477477

478478
strbuf_release(&buf);
479479

480-
if (oideq(&old_oid, &new_oid)) {
480+
if (oideq(&commit->object.oid, &new_oid)) {
481481
if (gentle) {
482-
warning(_("graft for '%s' unnecessary"), oid_to_hex(&old_oid));
482+
warning(_("graft for '%s' unnecessary"),
483+
oid_to_hex(&commit->object.oid));
483484
return 0;
484485
}
485-
return error(_("new commit is the same as the old one: '%s'"), oid_to_hex(&old_oid));
486+
return error(_("new commit is the same as the old one: '%s'"),
487+
oid_to_hex(&commit->object.oid));
486488
}
487489

488-
return replace_object_oid(old_ref, &old_oid, "replacement", &new_oid, force);
490+
return replace_object_oid(old_ref, &commit->object.oid,
491+
"replacement", &new_oid, force);
489492
}
490493

491494
static int convert_graft_file(int force)

t/t6050-replace.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,17 @@ test_expect_success '--graft using a tag as the new parent' '
417417
git replace -d $HASH7
418418
'
419419

420+
test_expect_success '--graft using a tag as the replaced object' '
421+
git tag replaced_object $HASH7 &&
422+
git replace --graft replaced_object $HASH5 &&
423+
commit_has_parents $HASH7 $HASH5 &&
424+
git replace -d $HASH7 &&
425+
git tag -a -m "annotated replaced object tag" annotated_replaced_object $HASH7 &&
426+
git replace --graft annotated_replaced_object $HASH5 &&
427+
commit_has_parents $HASH7 $HASH5 &&
428+
git replace -d $HASH7
429+
'
430+
420431
test_expect_success GPG 'set up a signed commit' '
421432
echo "line 17" >>hello &&
422433
echo "line 18" >>hello &&

0 commit comments

Comments
 (0)