Skip to content
This repository was archived by the owner on Apr 12, 2019. It is now read-only.

Commit 8983773

Browse files
authored
fix bug when get tag id (#147)
* fix bug when get tag id * fix wrong test * use GetTagCommitID on getTag but not git command
1 parent e03eb39 commit 8983773

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

repo_commit.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@ func (repo *Repository) GetBranchCommitID(name string) (string, error) {
3232

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

3845
// parseCommitData parses commit information from the (uncompressed) raw

repo_tag.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ func (repo *Repository) getTag(id SHA1) (*Tag, error) {
7676

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

84-
id, err := NewIDFromString(strings.Split(stdout, " ")[0])
84+
id, err := NewIDFromString(idStr)
8585
if err != nil {
8686
return nil, err
8787
}

repo_tag_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ func TestRepository_GetTags(t *testing.T) {
2020
assert.NoError(t, err)
2121
assert.Len(t, tags, 1)
2222
assert.EqualValues(t, "test", tags[0].Name)
23-
assert.EqualValues(t, "3ad28a9149a2864384548f3d17ed7f38014c9e8a", tags[0].ID.String())
23+
assert.EqualValues(t, "37991dec2c8e592043f47155ce4808d4580f9123", tags[0].ID.String())
2424
assert.EqualValues(t, "commit", tags[0].Type)
2525
}
26+
27+
func TestRepository_GetTag(t *testing.T) {
28+
bareRepo1Path := filepath.Join(testReposDir, "repo1")
29+
bareRepo1, err := OpenRepository(bareRepo1Path)
30+
assert.NoError(t, err)
31+
32+
tag, err := bareRepo1.GetTag("test")
33+
assert.NoError(t, err)
34+
assert.NotNil(t, tag)
35+
assert.EqualValues(t, "test", tag.Name)
36+
assert.EqualValues(t, "37991dec2c8e592043f47155ce4808d4580f9123", tag.ID.String())
37+
assert.EqualValues(t, "commit", tag.Type)
38+
}

0 commit comments

Comments
 (0)