diff --git a/builtin/replace.c b/builtin/replace.c index f5701629a8..644b21ca8d 100644 --- a/builtin/replace.c +++ b/builtin/replace.c @@ -370,16 +370,19 @@ static int replace_parents(struct strbuf *buf, int argc, const char **argv) /* prepare new parents */ for (i = 0; i < argc; i++) { struct object_id oid; + struct commit *commit; + if (get_oid(argv[i], &oid) < 0) { strbuf_release(&new_parents); return error(_("not a valid object name: '%s'"), argv[i]); } - if (!lookup_commit_reference(the_repository, &oid)) { + commit = lookup_commit_reference(the_repository, &oid); + if (!commit) { strbuf_release(&new_parents); - return error(_("could not parse %s"), argv[i]); + return error(_("could not parse %s as a commit"), argv[i]); } - strbuf_addf(&new_parents, "parent %s\n", oid_to_hex(&oid)); + strbuf_addf(&new_parents, "parent %s\n", oid_to_hex(&commit->object.oid)); } /* replace existing parents with new ones */ @@ -478,15 +481,18 @@ static int create_graft(int argc, const char **argv, int force, int gentle) strbuf_release(&buf); - if (oideq(&old_oid, &new_oid)) { + if (oideq(&commit->object.oid, &new_oid)) { if (gentle) { - warning(_("graft for '%s' unnecessary"), oid_to_hex(&old_oid)); + warning(_("graft for '%s' unnecessary"), + oid_to_hex(&commit->object.oid)); return 0; } - return error(_("new commit is the same as the old one: '%s'"), oid_to_hex(&old_oid)); + return error(_("new commit is the same as the old one: '%s'"), + oid_to_hex(&commit->object.oid)); } - return replace_object_oid(old_ref, &old_oid, "replacement", &new_oid, force); + return replace_object_oid(old_ref, &commit->object.oid, + "replacement", &new_oid, force); } static int convert_graft_file(int force) diff --git a/t/t6050-replace.sh b/t/t6050-replace.sh index d638119750..e7e64e085d 100755 --- a/t/t6050-replace.sh +++ b/t/t6050-replace.sh @@ -40,7 +40,8 @@ commit_peeling_shows_parents () test "$_found" = "$_parent" || return 1 _parent_number=$(( $_parent_number + 1 )) done && - test_must_fail git rev-parse --verify $_commit^$_parent_number + test_must_fail git rev-parse --verify $_commit^$_parent_number 2>err && + test_i18ngrep "Needed a single revision" err } commit_has_parents () @@ -393,9 +394,11 @@ test_expect_success 'replace ref cleanup' ' ' test_expect_success '--graft with and without already replaced object' ' - test $(git log --oneline | wc -l) = 7 && + git log --oneline >log && + test_line_count = 7 log && git replace --graft $HASH5 && - test $(git log --oneline | wc -l) = 3 && + git log --oneline >log && + test_line_count = 3 log && commit_has_parents $HASH5 && test_must_fail git replace --graft $HASH5 $HASH4 $HASH3 && git replace --force -g $HASH5 $HASH4 $HASH3 && @@ -403,6 +406,28 @@ test_expect_success '--graft with and without already replaced object' ' git replace -d $HASH5 ' +test_expect_success '--graft using a tag as the new parent' ' + git tag new_parent $HASH5 && + git replace --graft $HASH7 new_parent && + commit_has_parents $HASH7 $HASH5 && + git replace -d $HASH7 && + git tag -a -m "annotated new parent tag" annotated_new_parent $HASH5 && + git replace --graft $HASH7 annotated_new_parent && + commit_has_parents $HASH7 $HASH5 && + git replace -d $HASH7 +' + +test_expect_success '--graft using a tag as the replaced object' ' + git tag replaced_object $HASH7 && + git replace --graft replaced_object $HASH5 && + commit_has_parents $HASH7 $HASH5 && + git replace -d $HASH7 && + git tag -a -m "annotated replaced object tag" annotated_replaced_object $HASH7 && + git replace --graft annotated_replaced_object $HASH5 && + commit_has_parents $HASH7 $HASH5 && + git replace -d $HASH7 +' + test_expect_success GPG 'set up a signed commit' ' echo "line 17" >>hello && echo "line 18" >>hello &&