Skip to content

Commit 5dda951

Browse files
authored
Fix gogit GetRefCommitID (#32705)
1 parent 17053e9 commit 5dda951

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

modules/git/repo_commit_gogit.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,16 @@ import (
1414
"github.com/go-git/go-git/v5/plumbing/object"
1515
)
1616

17-
// GetRefCommitID returns the last commit ID string of given reference (branch or tag).
17+
// GetRefCommitID returns the last commit ID string of given reference.
1818
func (repo *Repository) GetRefCommitID(name string) (string, error) {
19-
ref, err := repo.gogitRepo.Reference(plumbing.ReferenceName(name), true)
19+
if plumbing.IsHash(name) {
20+
return name, nil
21+
}
22+
refName := plumbing.ReferenceName(name)
23+
if err := refName.Validate(); err != nil {
24+
return "", err
25+
}
26+
ref, err := repo.gogitRepo.Reference(refName, true)
2027
if err != nil {
2128
if err == plumbing.ErrReferenceNotFound {
2229
return "", ErrNotExist{

modules/git/repo_commit_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,28 @@ func TestRepository_CommitsBetweenIDs(t *testing.T) {
101101
assert.Len(t, commits, c.ExpectedCommits, "case %d", i)
102102
}
103103
}
104+
105+
func TestGetRefCommitID(t *testing.T) {
106+
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
107+
bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
108+
assert.NoError(t, err)
109+
defer bareRepo1.Close()
110+
111+
// these test case are specific to the repo1_bare test repo
112+
testCases := []struct {
113+
Ref string
114+
ExpectedCommitID string
115+
}{
116+
{RefNameFromBranch("master").String(), "ce064814f4a0d337b333e646ece456cd39fab612"},
117+
{RefNameFromBranch("branch1").String(), "2839944139e0de9737a044f78b0e4b40d989a9e3"},
118+
{RefNameFromTag("test").String(), "3ad28a9149a2864384548f3d17ed7f38014c9e8a"},
119+
{"ce064814f4a0d337b333e646ece456cd39fab612", "ce064814f4a0d337b333e646ece456cd39fab612"},
120+
}
121+
122+
for _, testCase := range testCases {
123+
commitID, err := bareRepo1.GetRefCommitID(testCase.Ref)
124+
if assert.NoError(t, err) {
125+
assert.Equal(t, testCase.ExpectedCommitID, commitID)
126+
}
127+
}
128+
}

0 commit comments

Comments
 (0)