-
Notifications
You must be signed in to change notification settings - Fork 16
/
release.go
79 lines (66 loc) · 1.91 KB
/
release.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package wormhole
import (
"fmt"
"os"
"strings"
"github.com/superfly/wormhole/messages"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
)
func computeRelease(id, desc, branch string) (*messages.Release, error) {
release := &messages.Release{}
if len(id) > 0 {
release.ID = id
}
if len(desc) > 0 {
release.Description = desc
}
if len(branch) > 0 {
release.Branch = branch
}
if _, err := os.Stat(".git"); !os.IsNotExist(err) {
release.VCSType = "git"
repo, err := git.PlainOpen(".")
if err != nil {
return nil, fmt.Errorf("Could not open repository: %s", err.Error())
}
head, err := repo.Head()
if err != nil {
return nil, fmt.Errorf("Could not get repo head: %s", err.Error())
}
oid := head.Hash()
release.VCSRevision = oid.String()
tip, err := repo.CommitObject(oid)
if err != nil {
return nil, fmt.Errorf("Could not get current commit: %s", err.Error())
}
refs, err := repo.References()
if err != nil {
return nil, fmt.Errorf("Could not get current refs: %s", err.Error())
}
var branches []string
refs.ForEach(func(ref *plumbing.Reference) error {
if ref.Name().IsBranch() && head.Hash().String() == ref.Hash().String() {
branch := strings.TrimPrefix(ref.Name().String(), "refs/heads/")
branches = append(branches, branch)
}
return nil
})
author := tip.Author
release.VCSRevisionAuthorEmail = author.Email
release.VCSRevisionAuthorName = author.Name
release.VCSRevisionTime = author.When
release.VCSRevisionMessage = tip.Message
// TODO: be smarter about branches, and maybe let users override this
if release.Branch == "" && len(branches) > 0 {
release.Branch = branches[0]
}
}
if release.ID == "" && release.VCSRevision != "" {
release.ID = release.VCSRevision
}
if release.Description == "" && release.VCSRevisionMessage != "" {
release.Description = release.VCSRevisionMessage
}
return release, nil
}