-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsync.go
244 lines (188 loc) · 5.94 KB
/
sync.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
240
241
242
243
244
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"github.com/codegangsta/cli"
)
func sync(c *cli.Context) error {
repo := c.String("repo")
gopath := c.String("gopath")
ignoredSubmodules := c.StringSlice("ignore")
absRepo, err := filepath.Abs(repo)
if err != nil {
return fmt.Errorf("could not resolve repo: %s", err)
}
absGopath, err := filepath.Abs(gopath)
if err != nil {
return fmt.Errorf("could not resolve gopath: %s", err)
}
pkgRoots := map[string]*Repo{}
for _, dep := range c.Args() {
root, repo, err := getDepRoot(absRepo, absGopath, dep)
if err != nil {
return fmt.Errorf("failed to get dependency repo: %s", err)
}
pkgRoots[root] = repo
}
existingSubmodules, err := detectExistingGoSubmodules(repo, gopath, false)
if err != nil {
if fixErr := fixExistingSubmodules(repo); fixErr != nil {
return fmt.Errorf("failed to fix existing submodules: %s", fixErr)
}
existingSubmodules, err = detectExistingGoSubmodules(repo, gopath, true)
if err != nil {
return fmt.Errorf("failed to detect existing submodules: %s", err)
}
}
gitmodules := filepath.Join(repo, ".gitmodules")
submodulesToRemove := map[string]bool{}
for _, submodule := range existingSubmodules {
submodulesToRemove[submodule] = true
}
for _, submodule := range ignoredSubmodules {
_, exists := submodulesToRemove[submodule]
if exists {
delete(submodulesToRemove, submodule)
}
}
for pkgRoot, pkgRepo := range pkgRoots {
relRoot, err := filepath.Rel(absRepo, pkgRoot)
if err != nil {
return fmt.Errorf("could not resolve submodule: %s", err)
}
fmt.Println("\x1b[32msyncing " + relRoot + "\x1b[0m")
// keep this submodule
delete(submodulesToRemove, relRoot)
add := exec.Command("git", "add", pkgRoot)
add.Dir = repo
add.Stderr = os.Stderr
err = add.Run()
if err != nil {
return fmt.Errorf("error clearing submodule: %s", err)
}
if pkgRepo == nil {
// non-git dependency; vendored
continue
}
status := exec.Command("git", "status", "--porcelain")
status.Dir = filepath.Join(absRepo, relRoot)
statusOutput, err := status.Output()
if err != nil {
return fmt.Errorf("error fetching submodule status: %s", err)
}
if len(statusOutput) != 0 {
return fmt.Errorf("\x1b[31msubmodule is dirty: " + pkgRoot + "\x1b[0m")
}
gitConfig := exec.Command("git", "config", "--file", gitmodules, "submodule."+relRoot+".path", relRoot)
gitConfig.Stderr = os.Stderr
err = gitConfig.Run()
if err != nil {
return fmt.Errorf("error configuring submodule: %s", err)
}
url := httpsOrigin(pkgRepo.Origin)
if !c.Bool("force-https") {
gitConfig = exec.Command("git", "config", "--file", gitmodules, "submodule." + relRoot + ".url")
gitConfig.Stderr = os.Stderr
out, err := gitConfig.Output()
if err == nil {
url = strings.TrimRight(string(out), "\n")
}
}
gitConfig = exec.Command("git", "config", "--file", gitmodules, "submodule."+relRoot+".url", url)
gitConfig.Stderr = os.Stderr
err = gitConfig.Run()
if err != nil {
return fmt.Errorf("error configuring submodule: %s", err)
}
if pkgRepo.Branch != "" {
gitConfig = exec.Command("git", "config", "--file", gitmodules, "submodule."+relRoot+".branch", pkgRepo.Branch)
gitConfig.Stderr = os.Stderr
err = gitConfig.Run()
if err != nil {
return fmt.Errorf("error configuring submodule: %s", err)
}
}
gitAdd := exec.Command("git", "add", gitmodules)
gitAdd.Dir = repo
gitAdd.Stderr = os.Stderr
err = gitAdd.Run()
if err != nil {
return fmt.Errorf("error staging submodule config: %s", err)
}
}
for submodule, _ := range submodulesToRemove {
fmt.Println("\x1b[31mremoving " + submodule + "\x1b[0m")
rm := exec.Command("git", "rm", "--cached", "-f", submodule)
rm.Dir = repo
rm.Stderr = os.Stderr
err := rm.Run()
if err != nil {
return fmt.Errorf("error clearing submodule: %s", err)
}
gitConfig := exec.Command("git", "config", "--file", gitmodules, "--remove-section", "submodule."+submodule)
gitConfig.Dir = repo
gitConfig.Stderr = os.Stderr
err = gitConfig.Run()
if err != nil {
return fmt.Errorf("error removing submodule config: %s", err)
}
gitAdd := exec.Command("git", "add", gitmodules)
gitAdd.Dir = repo
gitAdd.Stderr = os.Stderr
err = gitAdd.Run()
if err != nil {
return fmt.Errorf("error staging submodule config: %s", err)
}
}
if err := fixExistingSubmodules(repo); err != nil {
return fmt.Errorf("failed to fix submodules: %s", err)
}
return nil
}
func detectExistingGoSubmodules(repo string, gopath string, printErrors bool) ([]string, error) {
srcPath := filepath.Join(gopath, "src")
submoduleStatus := exec.Command("git", "submodule", "status", srcPath)
submoduleStatus.Dir = repo
if printErrors {
submoduleStatus.Stderr = os.Stderr
}
statusOut, err := submoduleStatus.StdoutPipe()
if err != nil {
printErr(printErrors, "detectExistingGoSubmodules failed to get StdoutPipe: %s\n", err)
return nil, err
}
lineScanner := bufio.NewScanner(statusOut)
err = submoduleStatus.Start()
if err != nil {
printErr(printErrors, "detectExistingGoSubmodules failed to start git submodule status: %s\n", err)
return nil, err
}
submodules := []string{}
for lineScanner.Scan() {
segments := strings.Split(lineScanner.Text()[1:], " ")
if len(segments) < 2 {
return nil, fmt.Errorf("invalid git status output: %q", lineScanner.Text())
}
submodules = append(submodules, segments[1])
}
err = submoduleStatus.Wait()
if err != nil {
printErr(printErrors, "detectExistingGoSubmodules failed to wait for git submodule status: %s\n", err)
return nil, err
}
return submodules, nil
}
func printErr(print bool, format string, err error) {
if print {
fmt.Printf(format, err)
}
}
var sshGitURIRegexp = regexp.MustCompile(`([email protected]:|https?://github.com/)([^/]+)/(.*?)(\.git)?$`)
func httpsOrigin(uri string) string {
return sshGitURIRegexp.ReplaceAllString(uri, "https://github.com/$2/$3")
}