Skip to content

Commit c601e41

Browse files
committed
fix: simplify getting tags
- previously it would use commit date. New version just does it by alphabet - adds test for detached_tags
1 parent 250a48a commit c601e41

6 files changed

+30
-19
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
testdata/commits_on_branch/
22
testdata/git_tags/
3+
testdata/detached_tags

pkg/get_tags.go

+5-10
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,7 @@ func (g *Git) getTags() ([]*Tag, error) {
2020
var tags []*Tag
2121

2222
err = tagrefs.ForEach(func(t *plumbing.Reference) error {
23-
commitDate, err := g.commitDate(t.Hash())
24-
25-
if err != nil {
26-
return err
27-
}
28-
29-
tags = append(tags, &Tag{Name: t.Name().String(), Date: commitDate, Hash: t.Hash()})
23+
tags = append(tags, &Tag{Name: t.Name().String(), Hash: t.Hash()})
3024
return nil
3125
})
3226

@@ -40,17 +34,18 @@ func (g *Git) getTags() ([]*Tag, error) {
4034
if g.Debug {
4135
log.Println("Sorted tag output: ")
4236
for _, taginstance := range sortedTags {
43-
log.Printf("hash: %v time: %v", taginstance.Hash, taginstance.Date.UTC())
37+
log.Printf("hash: %v name: %v", taginstance.Hash, taginstance.Name)
4438
}
4539
}
4640

4741
return sortedTags, nil
4842
}
4943

50-
// sortTags sorts the tags according to when their parent commit happened.
44+
// sortTags sorts the tags in reverse.
5145
func sortTags(repo *git.Repository, tags []*Tag) []*Tag {
5246
sort.Slice(tags, func(i, j int) bool {
53-
return tags[i].Date.After(tags[j].Date)
47+
// Reverses the order
48+
return j < i
5449
})
5550

5651
return tags

pkg/previous_tag_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,21 @@ func TestPreviousTag(t *testing.T) {
2424
assert.Equal(t, "chore: first commit on master\n", commit.Message)
2525

2626
}
27+
28+
func TestPreviousTagDetachedTag(t *testing.T) {
29+
repo, _ := git.PlainOpen("../testdata/detached_tags")
30+
testGit := &Git{repo: repo, Debug: true}
31+
32+
head, err := repo.Head()
33+
34+
assert.NoError(t, err)
35+
36+
tag, err := testGit.PreviousTag(head.Hash())
37+
38+
assert.NoError(t, err)
39+
40+
commit, err := repo.CommitObject(tag.Hash)
41+
assert.NoError(t, err)
42+
assert.Equal(t, "chore: detached commit\n", commit.Message)
43+
44+
}

pkg/tag.go

-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package history
22

33
import (
4-
"time"
5-
64
"gopkg.in/src-d/go-git.v4/plumbing"
75
)
86

97
// Tag houses some common info about tags.
108
type Tag struct {
119
Name string
1210
Hash plumbing.Hash
13-
Date time.Time
1411
}

testdata/detached_tags.bundle

1.17 KB
Binary file not shown.

testdata/setup_test_repos.sh

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
rm -rf testdata/commits_on_branch
44
rm -rf testdata/git_tags
5+
rm -rf testdata/detached_tags
56

6-
if [ ! -d './testdata/commits_on_branch' ]; then
77
cd testdata
88

99
echo 'testdata/commits_on_branch_test/ directory does not exist at the root; creating...'
@@ -13,8 +13,8 @@ if [ ! -d './testdata/commits_on_branch' ]; then
1313
echo 'testdata/git_tags/ directory does not exist at the root; creating...'
1414
git clone git_tags.bundle
1515
echo 'done'
16-
17-
else
18-
echo 'testdata/commits_on_branch_test directory already exists; aborting.'
19-
exit -1
20-
fi
16+
17+
18+
echo 'testdata/detached_tags/ directory does not exist at the root; creating...'
19+
git clone detached_tags.bundle
20+
echo 'done'

0 commit comments

Comments
 (0)