Skip to content

Commit

Permalink
Backport: Strict name matching for Repository.GetTagID() (#8082)
Browse files Browse the repository at this point in the history
* Strict name matching for Repository.GetTagID()

* Add test for GetTagID()
  • Loading branch information
guillep2k authored and sapk committed Sep 4, 2019
1 parent e7f6da3 commit bb609ca
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
11 changes: 7 additions & 4 deletions modules/git/repo_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,14 @@ func (repo *Repository) GetTagID(name string) (string, error) {
if err != nil {
return "", err
}
fields := strings.Fields(stdout)
if len(fields) != 2 {
return "", ErrNotExist{ID: name}
// Make sure exact match is used: "v1" != "release/v1"
for _, line := range strings.Split(stdout, "\n") {
fields := strings.Fields(line)
if len(fields) == 2 && fields[1] == "refs/tags/"+name {
return fields[0], nil
}
}
return fields[0], nil
return "", ErrNotExist{ID: name}
}

// GetTag returns a Git tag by given name.
Expand Down
10 changes: 10 additions & 0 deletions modules/git/repo_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ func TestRepository_GetTag(t *testing.T) {
assert.NotEqual(t, aTagID, aTag.Object.String())
assert.EqualValues(t, aTagCommitID, aTag.Object.String())
assert.EqualValues(t, "tag", aTag.Type)

rTagCommitID := "8006ff9adbf0cb94da7dad9e537e53817f9fa5c0"
rTagName := "release/" + lTagName
bareRepo1.CreateTag(rTagName, rTagCommitID)
rTagID, err := bareRepo1.GetTagID(rTagName)
assert.NoError(t, err)
assert.EqualValues(t, rTagCommitID, rTagID)
oTagID, err := bareRepo1.GetTagID(lTagName)
assert.NoError(t, err)
assert.EqualValues(t, lTagCommitID, oTagID)
}

func TestRepository_GetAnnotatedTag(t *testing.T) {
Expand Down

0 comments on commit bb609ca

Please sign in to comment.