diff --git a/payload.go b/payload.go index 03a6758..74c39aa 100644 --- a/payload.go +++ b/payload.go @@ -416,9 +416,11 @@ func (proj *Project) verifyCommit( parentTree *object.Tree, isNonFF bool, ) error { - parentTree, err := proj.parentTree(commit.Object) - if err != nil { - return fmt.Errorf("retrieving parent tree of commit: %w", err) + if parentTree == nil { + var err error + if parentTree, err = proj.parentTree(commit.Object); err != nil { + return fmt.Errorf("retrieving parent tree of commit: %w", err) + } } var sigFS fs.FS @@ -573,21 +575,31 @@ func (proj *Project) VerifyCanSetBranchHEADTo(branchName plumbing.ReferenceName, return fmt.Errorf("retrieving commit object %q: %w", oldCommitRef.Hash(), err) } - newCommitObj, err := proj.GitRepo.CommitObject(hash) + newCommit, err := proj.GetCommit(hash) if err != nil { - return fmt.Errorf("retrieving commit object %q: %w", hash, err) + return fmt.Errorf("retrieving commit %q: %w", hash, err) + } + + if isAncestor, err := newCommit.Object.IsAncestor(oldCommitObj); err != nil { + return fmt.Errorf("determining if %q is an ancestor of %q: %w", + newCommit.Hash, oldCommitObj.Hash, err) + } else if isAncestor { + // if the new commit is an ancestor of the old one then the branch is + // being force-pushed to a previous commit. This is weird to handle + // using VerifyCommits, so just call verifyCommit directly. + return proj.verifyCommit(branchName, newCommit, nil, true) } - mbCommits, err := oldCommitObj.MergeBase(newCommitObj) + mbCommits, err := oldCommitObj.MergeBase(newCommit.Object) if err != nil { return fmt.Errorf("determining merge-base between %q and %q: %w", - oldCommitObj.Hash, newCommitObj.Hash, err) + oldCommitObj.Hash, newCommit.Hash, err) } else if len(mbCommits) == 0 { return fmt.Errorf("%q and %q have no ancestors in common", - oldCommitObj.Hash, newCommitObj.Hash) + oldCommitObj.Hash, newCommit.Hash) } else if len(mbCommits) == 2 { return fmt.Errorf("%q and %q have more than one ancestor in common", - oldCommitObj.Hash, newCommitObj.Hash) + oldCommitObj.Hash, newCommit.Hash) } commits, err := proj.GetCommitRange(mbCommits[0].Hash, hash) diff --git a/payload_test.go b/payload_test.go index 53c7151..a333848 100644 --- a/payload_test.go +++ b/payload_test.go @@ -147,7 +147,7 @@ func TestNonFastForwardCommits(t *testing.T) { } } -func TestCanSetBranchHEADTo(t *testing.T) { +func TestVerifyCanSetBranchHEADTo(t *testing.T) { type toTest struct { // branchName and hash are the arguments passed into // VerifyCanSetBranchHEADTo. @@ -391,6 +391,24 @@ func TestCanSetBranchHEADTo(t *testing.T) { } }, }, + { + descr: "branch nonff to previous commit", + init: func(h *harness, rootSig sigcred.Signifier) toTest { + h.assertCommitChange(verifySkip, "init", rootSig) + + other := plumbing.NewBranchReferenceName("other") + h.checkout(other) + h.stage(map[string]string{"foo": "foo"}) + fooCommit := h.assertCommitChange(verifySkip, "foo", rootSig) + h.stage(map[string]string{"bar": "bar"}) + h.assertCommitChange(verifySkip, "bar", rootSig) + + return toTest{ + branchName: other, + hash: fooCommit.Hash, + } + }, + }, } for _, test := range tests {