generated from fallion/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprevious_tag.go
39 lines (30 loc) · 985 Bytes
/
previous_tag.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package git
import (
"errors"
"github.com/apex/log"
"github.com/go-git/go-git/v5/plumbing"
)
var (
// ErrPrevTagNotAvailable is returned when no previous tag is found.
ErrPrevTagNotAvailable = errors.New("previous tag is not available")
)
// PreviousTag sorts tags based on when their commit happened and returns the one previous
// to the current.
func (g *Git) PreviousTag(currentHash plumbing.Hash) (*Tag, error) {
tags, err := g.getTags()
if err != nil {
log.Debugf("[ERR] getting previous tag failed: %v", err)
return nil, err
}
// If there are fewer than two tags assume that the currentCommit is the newest tag
if len(tags) < 2 {
log.Debug("[ERR] previous tag not available")
return nil, ErrPrevTagNotAvailable
}
if tags[0].Hash != currentHash {
log.Debug("[ERR] current commit does not have a tag attached, building from this commit")
return tags[0], nil
}
log.Debugf("success: previous tag found at %v", tags[1].Hash)
return tags[1], nil
}