Skip to content

Commit bb609ca

Browse files
guillep2ksapk
authored andcommitted
Backport: Strict name matching for Repository.GetTagID() (#8082)
* Strict name matching for Repository.GetTagID() * Add test for GetTagID()
1 parent e7f6da3 commit bb609ca

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

modules/git/repo_tag.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,14 @@ func (repo *Repository) GetTagID(name string) (string, error) {
157157
if err != nil {
158158
return "", err
159159
}
160-
fields := strings.Fields(stdout)
161-
if len(fields) != 2 {
162-
return "", ErrNotExist{ID: name}
160+
// Make sure exact match is used: "v1" != "release/v1"
161+
for _, line := range strings.Split(stdout, "\n") {
162+
fields := strings.Fields(line)
163+
if len(fields) == 2 && fields[1] == "refs/tags/"+name {
164+
return fields[0], nil
165+
}
163166
}
164-
return fields[0], nil
167+
return "", ErrNotExist{ID: name}
165168
}
166169

167170
// GetTag returns a Git tag by given name.

modules/git/repo_tag_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ func TestRepository_GetTag(t *testing.T) {
6262
assert.NotEqual(t, aTagID, aTag.Object.String())
6363
assert.EqualValues(t, aTagCommitID, aTag.Object.String())
6464
assert.EqualValues(t, "tag", aTag.Type)
65+
66+
rTagCommitID := "8006ff9adbf0cb94da7dad9e537e53817f9fa5c0"
67+
rTagName := "release/" + lTagName
68+
bareRepo1.CreateTag(rTagName, rTagCommitID)
69+
rTagID, err := bareRepo1.GetTagID(rTagName)
70+
assert.NoError(t, err)
71+
assert.EqualValues(t, rTagCommitID, rTagID)
72+
oTagID, err := bareRepo1.GetTagID(lTagName)
73+
assert.NoError(t, err)
74+
assert.EqualValues(t, lTagCommitID, oTagID)
6575
}
6676

6777
func TestRepository_GetAnnotatedTag(t *testing.T) {

0 commit comments

Comments
 (0)