Skip to content

Commit

Permalink
fix bug when get tag id (go-gitea#147)
Browse files Browse the repository at this point in the history
* fix bug when get tag id

* fix wrong test

* use GetTagCommitID on getTag but not git command
  • Loading branch information
lunny committed Mar 2, 2019
1 parent e03eb39 commit 8983773
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
9 changes: 8 additions & 1 deletion repo_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ func (repo *Repository) GetBranchCommitID(name string) (string, error) {

// GetTagCommitID returns last commit ID string of given tag.
func (repo *Repository) GetTagCommitID(name string) (string, error) {
return repo.GetRefCommitID(TagPrefix + name)
stdout, err := NewCommand("rev-list", "-n", "1", name).RunInDir(repo.Path)
if err != nil {
if strings.Contains(err.Error(), "unknown revision or path") {
return "", ErrNotExist{name, ""}
}
return "", err
}
return strings.TrimSpace(stdout), nil
}

// parseCommitData parses commit information from the (uncompressed) raw
Expand Down
4 changes: 2 additions & 2 deletions repo_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ func (repo *Repository) getTag(id SHA1) (*Tag, error) {

// GetTag returns a Git tag by given name.
func (repo *Repository) GetTag(name string) (*Tag, error) {
stdout, err := NewCommand("show-ref", "--tags", name).RunInDir(repo.Path)
idStr, err := repo.GetTagCommitID(name)
if err != nil {
return nil, err
}

id, err := NewIDFromString(strings.Split(stdout, " ")[0])
id, err := NewIDFromString(idStr)
if err != nil {
return nil, err
}
Expand Down
15 changes: 14 additions & 1 deletion repo_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ func TestRepository_GetTags(t *testing.T) {
assert.NoError(t, err)
assert.Len(t, tags, 1)
assert.EqualValues(t, "test", tags[0].Name)
assert.EqualValues(t, "3ad28a9149a2864384548f3d17ed7f38014c9e8a", tags[0].ID.String())
assert.EqualValues(t, "37991dec2c8e592043f47155ce4808d4580f9123", tags[0].ID.String())
assert.EqualValues(t, "commit", tags[0].Type)
}

func TestRepository_GetTag(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1")
bareRepo1, err := OpenRepository(bareRepo1Path)
assert.NoError(t, err)

tag, err := bareRepo1.GetTag("test")
assert.NoError(t, err)
assert.NotNil(t, tag)
assert.EqualValues(t, "test", tag.Name)
assert.EqualValues(t, "37991dec2c8e592043f47155ce4808d4580f9123", tag.ID.String())
assert.EqualValues(t, "commit", tag.Type)
}

0 comments on commit 8983773

Please sign in to comment.