forked from xorpaul/g10k
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.go
135 lines (121 loc) · 4.02 KB
/
git.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
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strconv"
"strings"
"sync"
"time"
"github.com/xorpaul/uiprogress"
)
func resolveGitRepositories(uniqueGitModules map[string]GitModule) {
if len(uniqueGitModules) <= 0 {
Debugf("uniqueGitModules[] is empty, skipping...")
return
}
var wgGit sync.WaitGroup
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))
})
for url, gm := range uniqueGitModules {
wgGit.Add(1)
privateKey := gm.privateKey
go func(url string, privateKey string, gm GitModule) {
defer wgGit.Done()
defer bar.Incr()
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)
// doCloneOrPull(source, workDir, targetDir, sa.Remote, branch, sa.PrivateKey)
}(url, privateKey, gm)
}
wgGit.Wait()
}
func doMirrorOrUpdate(url string, workDir string, sshPrivateKey string, allowFail bool) bool {
needSSHKey := true
if strings.Contains(url, "github.com") || len(sshPrivateKey) == 0 {
needSSHKey = false
}
er := ExecResult{}
gitCmd := "git clone --mirror " + url + " " + workDir
if fileExists(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 {
fmt.Println("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) bool {
mutex.Lock()
syncGitCount++
mutex.Unlock()
logCmd := "git --git-dir " + srcDir + " log -n1 --pretty=format:%H " + tree
er := executeCommand(logCmd, config.Timeout, allowFail)
hashFile := targetDir + "/.latest_commit"
needToSync := true
if er.returnCode != 0 && allowFail {
if ignoreUnreachable {
Infof("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()
needSyncGitCount++
mutex.Unlock()
if !dryRun {
createOrPurgeDir(targetDir, "syncToModuleDir()")
cmd := "git --git-dir " + srcDir + " archive " + tree + " | tar -x -C " + targetDir
before := time.Now()
out, err := exec.Command("bash", "-c", cmd).CombinedOutput()
duration := time.Since(before).Seconds()
mutex.Lock()
ioGitTime += duration
mutex.Unlock()
Verbosef("syncToModuleDir(): Executing " + cmd + " took " + strconv.FormatFloat(duration, 'f', 5, 64) + "s")
if err != nil {
if !allowFail {
Fatalf("syncToModuleDir(): Failed to execute command: " + cmd + " Output: " + string(out))
} else {
Infof("Failed to populate module " + targetDir + " but ignore-unreachable is set. Continuing...")
return false
}
}
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
}