forked from voxpupuli/g10k
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.go
More file actions
210 lines (188 loc) · 6.88 KB
/
git.go
File metadata and controls
210 lines (188 loc) · 6.88 KB
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
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strconv"
"strings"
"sync"
"time"
"github.com/xorpaul/uiprogress"
)
func resolveGitRepositories(uniqueGitModules map[string]GitModule) {
defer timeTrack(time.Now(), funcName())
if len(uniqueGitModules) <= 0 {
Debugf("uniqueGitModules[] is empty, skipping...")
return
}
bar := uiprogress.AddBar(len(uniqueGitModules)).AppendCompleted().PrependElapsed()
bar.PrependFunc(func(b *uiprogress.Bar) string {
return fmt.Sprintf("Resolving Git modules (%d/%d)", b.Current(), len(uniqueGitModules))
})
// Dummy channel to coordinate the number of concurrent goroutines.
// This channel should be buffered otherwise we will be immediately blocked
// when trying to fill it.
Debugf("Resolving " + strconv.Itoa(len(uniqueGitModules)) + " Git modules with " + strconv.Itoa(config.Maxworker) + " workers")
concurrentGoroutines := make(chan struct{}, config.Maxworker)
// Fill the dummy channel with config.Maxworker empty struct.
for i := 0; i < config.Maxworker; i++ {
concurrentGoroutines <- struct{}{}
}
// The done channel indicates when a single goroutine has finished its job.
done := make(chan bool)
// The waitForAllJobs channel allows the main program
// to wait until we have indeed done all the jobs.
waitForAllJobs := make(chan bool)
// Collect all the jobs, and since the job is finished, we can
// release another spot for a goroutine.
go func() {
for _, gm := range uniqueGitModules {
go func(gm GitModule) {
<-done
// Say that another goroutine can now start.
concurrentGoroutines <- struct{}{}
}(gm)
}
// We have collected all the jobs, the program can now terminate
waitForAllJobs <- true
}()
wg := sync.WaitGroup{}
wg.Add(len(uniqueGitModules))
for url, gm := range uniqueGitModules {
Debugf("git repo url " + url)
privateKey := gm.privateKey
go func(url string, privateKey string, gm GitModule, bar *uiprogress.Bar) {
// Try to receive from the concurrentGoroutines channel. When we have something,
// it means we can start a new goroutine because another one finished.
// Otherwise, it will block the execution until an execution
// spot is available.
<-concurrentGoroutines
defer bar.Incr()
defer wg.Done()
if len(gm.privateKey) > 0 {
Debugf("git repo url " + url + " with ssh key " + privateKey)
} else {
Debugf("git repo url " + url + " without ssh key")
}
//log.Println(config)
// create save directory name from Git repo name
repoDir := strings.Replace(strings.Replace(url, "/", "_", -1), ":", "-", -1)
workDir := config.ModulesCacheDir + repoDir
doMirrorOrUpdate(url, workDir, privateKey, gm.ignoreUnreachable, 1)
// doCloneOrPull(source, workDir, targetDir, sa.Remote, branch, sa.PrivateKey)
}(url, privateKey, gm, bar)
done <- true
}
// Wait for all jobs to finish
<-waitForAllJobs
wg.Wait()
}
func doMirrorOrUpdate(url string, workDir string, sshPrivateKey string, allowFail bool, retryCount int) bool {
needSSHKey := true
if strings.Contains(url, "github.com") || len(sshPrivateKey) == 0 {
needSSHKey = false
}
er := ExecResult{}
gitCmd := "git clone --mirror " + url + " " + workDir
if isDir(workDir) {
gitCmd = "git --git-dir " + workDir + " remote update --prune"
}
if needSSHKey {
er = executeCommand("ssh-agent bash -c 'ssh-add "+sshPrivateKey+"; "+gitCmd+"'", config.Timeout, allowFail)
} else {
er = executeCommand(gitCmd, config.Timeout, allowFail)
}
if er.returnCode != 0 {
if config.UseCacheFallback {
Warnf("WARN: git repository " + url + " does not exist or is unreachable at this moment!")
Warnf("WARN: Trying to use cache for " + url + " git repository")
} else if config.RetryGitCommands && retryCount > 0 {
Warnf("WARN: git command failed: " + gitCmd + " deleting local cached repository and retrying...")
purgeDir(workDir, "doMirrorOrUpdate, because git command failed, retrying")
return doMirrorOrUpdate(url, workDir, sshPrivateKey, false, retryCount-1)
}
Warnf("WARN: git repository " + url + " does not exist or is unreachable at this moment!")
return false
}
return true
}
func syncToModuleDir(srcDir string, targetDir string, tree string, allowFail bool, ignoreUnreachable bool, correspondingPuppetEnvironment string) bool {
mutex.Lock()
syncGitCount++
mutex.Unlock()
if !isDir(srcDir) {
if config.UseCacheFallback {
Fatalf("Could not find cached git module " + srcDir)
}
}
logCmd := "git --git-dir " + srcDir + " rev-parse --verify '" + tree
if config.GitObjectSyntaxNotSupported != true {
logCmd = logCmd + "^{object}'"
} else {
logCmd = logCmd + "'"
}
er := executeCommand(logCmd, config.Timeout, allowFail)
hashFile := targetDir + "/.latest_commit"
needToSync := true
if er.returnCode != 0 {
if allowFail && ignoreUnreachable {
Debugf("Failed to populate module " + targetDir + " but ignore-unreachable is set. Continuing...")
purgeDir(targetDir, "syncToModuleDir, because ignore-unreachable is set for this module")
}
return false
}
if len(er.output) > 0 {
targetHash, _ := ioutil.ReadFile(hashFile)
if string(targetHash) == er.output {
needToSync = false
//Debugf("Skipping, because no diff found between " + srcDir + "(" + er.output + ") and " + targetDir + "(" + string(targetHash) + ")")
}
}
if needToSync && er.returnCode == 0 {
Infof("Need to sync " + targetDir)
mutex.Lock()
needSyncDirs = append(needSyncDirs, targetDir)
if _, ok := needSyncEnvs[correspondingPuppetEnvironment]; !ok {
needSyncEnvs[correspondingPuppetEnvironment] = empty
}
needSyncGitCount++
mutex.Unlock()
if !dryRun {
createOrPurgeDir(targetDir, "syncToModuleDir()")
gitArchiveArgs := []string{"--git-dir", srcDir, "archive", tree}
cmd := exec.Command("git", gitArchiveArgs...)
Debugf("Executing git --git-dir " + srcDir + " archive " + tree)
cmdOut, err := cmd.StdoutPipe()
if err != nil {
if !allowFail {
Infof("Failed to populate module " + targetDir + " but ignore-unreachable is set. Continuing...")
} else {
return false
}
Fatalf("syncToModuleDir(): Failed to execute command: git --git-dir " + srcDir + " archive " + tree + " Error: " + err.Error())
}
cmd.Start()
before := time.Now()
unTar(cmdOut, targetDir)
duration := time.Since(before).Seconds()
mutex.Lock()
ioGitTime += duration
mutex.Unlock()
err = cmd.Wait()
if err != nil {
Fatalf("syncToModuleDir(): Failed to execute command: git --git-dir " + srcDir + " archive " + tree + " Error: " + err.Error())
}
Verbosef("syncToModuleDir(): Executing git --git-dir " + srcDir + " archive " + tree + " took " + strconv.FormatFloat(duration, 'f', 5, 64) + "s")
er = executeCommand(logCmd, config.Timeout, false)
if len(er.output) > 0 {
Debugf("Writing hash " + er.output + " from command " + logCmd + " to " + hashFile)
f, _ := os.Create(hashFile)
defer f.Close()
f.WriteString(er.output)
f.Sync()
}
}
}
return true
}