Skip to content

Commit

Permalink
Merge branch 'cc/replace-graft-peel-tags'
Browse files Browse the repository at this point in the history
When given a tag that points at a commit-ish, "git replace --graft"
failed to peel the tag before writing a replace ref, which did not
make sense because the old graft mechanism the feature wants to
mimick only allowed to replace one commit object with another.
This has been fixed.

* cc/replace-graft-peel-tags:
  replace: peel tag when passing a tag first to --graft
  replace: peel tag when passing a tag as parent to --graft
  t6050: redirect expected error output to a file
  t6050: use test_line_count instead of wc -l
  • Loading branch information
gitster committed May 8, 2019
2 parents 1b40314 + ee521ec commit ce2a18f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
20 changes: 13 additions & 7 deletions builtin/replace.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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)
Expand Down
31 changes: 28 additions & 3 deletions t/t6050-replace.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand Down Expand Up @@ -393,16 +394,40 @@ 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 &&
commit_has_parents $HASH5 $HASH4 $HASH3 &&
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 &&
Expand Down

0 comments on commit ce2a18f

Please sign in to comment.