Skip to content

Commit ce2a18f

Browse files
committed
Merge branch 'cc/replace-graft-peel-tags'
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
2 parents 1b40314 + ee521ec commit ce2a18f

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

builtin/replace.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -370,16 +370,19 @@ static int replace_parents(struct strbuf *buf, int argc, const char **argv)
370370
/* prepare new parents */
371371
for (i = 0; i < argc; i++) {
372372
struct object_id oid;
373+
struct commit *commit;
374+
373375
if (get_oid(argv[i], &oid) < 0) {
374376
strbuf_release(&new_parents);
375377
return error(_("not a valid object name: '%s'"),
376378
argv[i]);
377379
}
378-
if (!lookup_commit_reference(the_repository, &oid)) {
380+
commit = lookup_commit_reference(the_repository, &oid);
381+
if (!commit) {
379382
strbuf_release(&new_parents);
380-
return error(_("could not parse %s"), argv[i]);
383+
return error(_("could not parse %s as a commit"), argv[i]);
381384
}
382-
strbuf_addf(&new_parents, "parent %s\n", oid_to_hex(&oid));
385+
strbuf_addf(&new_parents, "parent %s\n", oid_to_hex(&commit->object.oid));
383386
}
384387

385388
/* replace existing parents with new ones */
@@ -478,15 +481,18 @@ static int create_graft(int argc, const char **argv, int force, int gentle)
478481

479482
strbuf_release(&buf);
480483

481-
if (oideq(&old_oid, &new_oid)) {
484+
if (oideq(&commit->object.oid, &new_oid)) {
482485
if (gentle) {
483-
warning(_("graft for '%s' unnecessary"), oid_to_hex(&old_oid));
486+
warning(_("graft for '%s' unnecessary"),
487+
oid_to_hex(&commit->object.oid));
484488
return 0;
485489
}
486-
return error(_("new commit is the same as the old one: '%s'"), oid_to_hex(&old_oid));
490+
return error(_("new commit is the same as the old one: '%s'"),
491+
oid_to_hex(&commit->object.oid));
487492
}
488493

489-
return replace_object_oid(old_ref, &old_oid, "replacement", &new_oid, force);
494+
return replace_object_oid(old_ref, &commit->object.oid,
495+
"replacement", &new_oid, force);
490496
}
491497

492498
static int convert_graft_file(int force)

t/t6050-replace.sh

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ commit_peeling_shows_parents ()
4040
test "$_found" = "$_parent" || return 1
4141
_parent_number=$(( $_parent_number + 1 ))
4242
done &&
43-
test_must_fail git rev-parse --verify $_commit^$_parent_number
43+
test_must_fail git rev-parse --verify $_commit^$_parent_number 2>err &&
44+
test_i18ngrep "Needed a single revision" err
4445
}
4546

4647
commit_has_parents ()
@@ -393,16 +394,40 @@ test_expect_success 'replace ref cleanup' '
393394
'
394395

395396
test_expect_success '--graft with and without already replaced object' '
396-
test $(git log --oneline | wc -l) = 7 &&
397+
git log --oneline >log &&
398+
test_line_count = 7 log &&
397399
git replace --graft $HASH5 &&
398-
test $(git log --oneline | wc -l) = 3 &&
400+
git log --oneline >log &&
401+
test_line_count = 3 log &&
399402
commit_has_parents $HASH5 &&
400403
test_must_fail git replace --graft $HASH5 $HASH4 $HASH3 &&
401404
git replace --force -g $HASH5 $HASH4 $HASH3 &&
402405
commit_has_parents $HASH5 $HASH4 $HASH3 &&
403406
git replace -d $HASH5
404407
'
405408

409+
test_expect_success '--graft using a tag as the new parent' '
410+
git tag new_parent $HASH5 &&
411+
git replace --graft $HASH7 new_parent &&
412+
commit_has_parents $HASH7 $HASH5 &&
413+
git replace -d $HASH7 &&
414+
git tag -a -m "annotated new parent tag" annotated_new_parent $HASH5 &&
415+
git replace --graft $HASH7 annotated_new_parent &&
416+
commit_has_parents $HASH7 $HASH5 &&
417+
git replace -d $HASH7
418+
'
419+
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+
406431
test_expect_success GPG 'set up a signed commit' '
407432
echo "line 17" >>hello &&
408433
echo "line 18" >>hello &&

0 commit comments

Comments
 (0)