forked from liamjbennett/sous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
103 lines (95 loc) · 2.67 KB
/
context.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package git
import (
"path/filepath"
"github.com/opentable/sous/lib"
"github.com/opentable/sous/util/firsterr"
)
// SourceContext gathers together a number of bits of information about the
// repository such as its current branch, revision, nearest tag, nearest semver
// tag, etc.
func (r *Repo) SourceContext() (*sous.SourceContext, error) {
var (
revision, branch,
nearestTagName, nearestTagRevision,
repoRelativeDir string
files, modifiedFiles, newFiles []string
allTags []sous.Tag
remotes Remotes
//unpushedCommits []string
)
c := r.Client
if err := firsterr.Parallel().Set(
func(err *error) { branch, *err = c.CurrentBranch() },
func(err *error) { revision, *err = c.Revision() },
func(err *error) {
repoRelativeDir, *err = filepath.Rel(r.Root, c.Sh.Dir())
if repoRelativeDir == "." {
repoRelativeDir = ""
}
},
func(err *error) {
allTags, *err = r.Client.ListTags()
if *err != nil || len(allTags) == 0 {
return
}
nearestTagName, *err = c.NearestTag()
if *err != nil {
return
}
ntr, terr := c.RevisionAt(nearestTagName)
if terr == nil {
nearestTagRevision = ntr
}
},
func(err *error) { files, *err = c.ListFiles() },
func(err *error) { modifiedFiles, *err = c.ModifiedFiles() },
func(err *error) { newFiles, *err = c.NewFiles() },
func(err *error) { remotes, *err = c.ListRemotes() },
//func(err *error) { unpushedCommits, *err = c.ListUnpushedCommits() },
); err != nil {
return nil, err
}
primaryRemoteURL := guessPrimaryRemote(remotes)
return &sous.SourceContext{
RootDir: r.Root,
OffsetDir: repoRelativeDir,
Branch: branch,
Revision: revision,
Files: files,
ModifiedFiles: modifiedFiles,
NewFiles: newFiles,
Tags: allTags,
NearestTagName: nearestTagName,
NearestTagRevision: nearestTagRevision,
PrimaryRemoteURL: primaryRemoteURL,
RemoteURLs: allFetchURLs(remotes),
DirtyWorkingTree: len(modifiedFiles)+len(newFiles) != 0,
}, nil
}
func allFetchURLs(remotes Remotes) []string {
var remURLs []string
for _, r := range remotes {
u, err := CanonicalRepoURL(r.FetchURL)
if err != nil {
continue
}
remURLs = append(remURLs, u)
}
return remURLs
}
func guessPrimaryRemote(remotes map[string]Remote) string {
primaryRemote, ok := remotes["upstream"]
if !ok {
primaryRemote, ok = remotes["origin"]
}
if !ok {
return ""
}
if primaryRemote.FetchURL == "" {
return ""
}
// We don't care about this error, empty string is
// an acceptable return value.
u, _ := CanonicalRepoURL(primaryRemote.FetchURL)
return u
}