-
-
Notifications
You must be signed in to change notification settings - Fork 362
/
vcs.go
239 lines (199 loc) · 4.77 KB
/
vcs.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"strings"
"sync"
"time"
gosrc "github.com/Morganamilo/go-srcinfo"
)
// Info contains the last commit sha of a repo
type vcsInfo map[string]shaInfos
type shaInfos map[string]shaInfo
type shaInfo struct {
Protocols []string `json:"protocols"`
Branch string `json:"branch"`
SHA string `json:"sha"`
}
// createDevelDB forces yay to create a DB of the existing development packages
func createDevelDB() error {
var mux sync.Mutex
var wg sync.WaitGroup
_, _, _, remoteNames, err := filterPackages()
if err != nil {
return err
}
info, err := aurInfoPrint(remoteNames)
if err != nil {
return err
}
bases := getBases(info)
toSkip := pkgbuildsToSkip(bases, sliceToStringSet(remoteNames))
downloadPkgbuilds(bases, toSkip, config.BuildDir)
srcinfos, _ := parseSrcinfoFiles(bases, false)
for _, pkgbuild := range srcinfos {
for _, pkg := range pkgbuild.Packages {
wg.Add(1)
go updateVCSData(pkg.Pkgname, pkgbuild.Source, &mux, &wg)
}
}
wg.Wait()
fmt.Println(bold(yellow(arrow) + bold(" GenDB finished. No packages were installed")))
return err
}
// parseSource returns the git url, default branch and protocols it supports
func parseSource(source string) (url string, branch string, protocols []string) {
split := strings.Split(source, "::")
source = split[len(split)-1]
split = strings.SplitN(source, "://", 2)
if len(split) != 2 {
return "", "", nil
}
protocols = strings.SplitN(split[0], "+", 2)
git := false
for _, protocol := range protocols {
if protocol == "git" {
git = true
break
}
}
protocols = protocols[len(protocols)-1:]
if !git {
return "", "", nil
}
split = strings.SplitN(split[1], "#", 2)
if len(split) == 2 {
secondSplit := strings.SplitN(split[1], "=", 2)
if secondSplit[0] != "branch" {
//source has #commit= or #tag= which makes them not vcs
//packages because they reference a specific point
return "", "", nil
}
if len(secondSplit) == 2 {
url = split[0]
branch = secondSplit[1]
}
} else {
url = split[0]
branch = "HEAD"
}
url = strings.Split(url, "?")[0]
branch = strings.Split(branch, "?")[0]
return
}
func updateVCSData(pkgName string, sources []gosrc.ArchString, mux *sync.Mutex, wg *sync.WaitGroup) {
defer wg.Done()
if savedInfo == nil {
mux.Lock()
savedInfo = make(vcsInfo)
mux.Unlock()
}
info := make(shaInfos)
checkSource := func(source gosrc.ArchString) {
defer wg.Done()
url, branch, protocols := parseSource(source.Value)
if url == "" || branch == "" {
return
}
commit := getCommit(url, branch, protocols)
if commit == "" {
return
}
mux.Lock()
info[url] = shaInfo{
protocols,
branch,
commit,
}
savedInfo[pkgName] = info
fmt.Println(bold(yellow(arrow)) + " Found git repo: " + cyan(url))
saveVCSInfo()
mux.Unlock()
}
for _, source := range sources {
wg.Add(1)
go checkSource(source)
}
}
func getCommit(url string, branch string, protocols []string) string {
if len(protocols) > 0 {
protocol := protocols[len(protocols)-1]
var outbuf bytes.Buffer
cmd := passToGit("", "ls-remote", protocol+"://"+url, branch)
cmd.Stdout = &outbuf
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
err := cmd.Start()
if err != nil {
return ""
}
//for some reason
//git://bitbucket.org/volumesoffun/polyvox.git` hangs on my
//machine but using http:// instead of git does not hang.
//Introduce a time out so this can not hang
timer := time.AfterFunc(5*time.Second, func() {
cmd.Process.Kill()
})
err = cmd.Wait()
timer.Stop()
if err != nil {
return ""
}
stdout := outbuf.String()
split := strings.Fields(stdout)
if len(split) < 2 {
return ""
}
commit := split[0]
return commit
}
return ""
}
func (infos shaInfos) needsUpdate() bool {
//used to signal we have gone through all sources and found nothing
finished := make(chan struct{})
alive := 0
//if we find an update we use this to exit early and return true
hasUpdate := make(chan struct{})
checkHash := func(url string, info shaInfo) {
hash := getCommit(url, info.Branch, info.Protocols)
if hash != "" && hash != info.SHA {
hasUpdate <- struct{}{}
} else {
finished <- struct{}{}
}
}
for url, info := range infos {
alive++
go checkHash(url, info)
}
for {
select {
case <-hasUpdate:
return true
case <-finished:
alive--
if alive == 0 {
return false
}
}
}
}
func saveVCSInfo() error {
marshalledinfo, err := json.MarshalIndent(savedInfo, "", "\t")
if err != nil || string(marshalledinfo) == "null" {
return err
}
in, err := os.OpenFile(vcsFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer in.Close()
_, err = in.Write(marshalledinfo)
if err != nil {
return err
}
err = in.Sync()
return err
}