-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathscm.go
244 lines (193 loc) · 5.01 KB
/
scm.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
// LOL so we're gonna try and avoid THIS situation http://golang.org/src/cmd/go/vcs.go#L331
import (
"fmt"
"os"
"os/exec"
"path"
"strings"
)
const (
GitTag = "git"
HgTag = "hg"
SvnTag = "svn"
BzrTag = "bzr"
HiddenGit = ".git"
HiddenHg = ".hg"
HiddenSvn = ".svn"
HiddenBzr = ".bzr"
)
type Scm interface {
Init(d *Dep) error
Checkout(d *Dep) error
Fetch(path string) error
DownloadCommand(source, path string) *exec.Cmd
}
func dependencyPath(importPath string) string {
return path.Join(pwd, VendorDir, "src", importPath)
}
func scmStageDir(depPath, scmDir string) string {
return path.Join(depPath, scmDir)
}
func downloadDependency(d *Dep, depPath, scmType string, scm Scm) (err error) {
stage, err := os.Stat(scmStageDir(depPath, scmType))
if stage != nil && stage.IsDir() {
err = scm.Fetch(depPath)
} else if err != nil && !os.IsNotExist(err) {
err = fmt.Errorf("Error while examining dependency path for %s: %s", d.Import, err)
} else {
fmtcolor(Gray, "downloading %s\n", d.Source)
cmd := scm.DownloadCommand(d.Source, depPath)
if err = cmd.Run(); err != nil {
return fmt.Errorf("Error downloading dependency: %s", err)
}
}
return
}
func initScm(d *Dep, scmType string, scm Scm) error {
path := dependencyPath(d.Import)
if err := os.MkdirAll(path, 0755); err != nil {
return fmt.Errorf("Error creating import dir %s", err)
} else {
return downloadDependency(d, path, scmType, scm)
}
}
func runInPath(path string, fn func() error) error {
err := os.Chdir(path)
if err != nil {
return err
}
defer os.Chdir(pwd)
return fn()
}
type Git struct{}
func (g Git) Init(d *Dep) error {
return initScm(d, HiddenGit, g)
}
func (g Git) DownloadCommand(source, path string) *exec.Cmd {
return exec.Command("git", "clone", source, path)
}
func (g Git) Checkout(d *Dep) error {
cmd := exec.Command("git", "checkout", d.CheckoutSpec)
return cmd.Run()
}
func (g Git) Fetch(path string) error {
return runInPath(path, func() error {
return exec.Command("git", "fetch").Run()
})
}
type Hg struct{}
func (h Hg) Init(d *Dep) error {
return initScm(d, HiddenHg, h)
}
func (h Hg) DownloadCommand(source, path string) *exec.Cmd {
return exec.Command("hg", "clone", source, path)
}
func (h Hg) Checkout(d *Dep) error {
var cmd *exec.Cmd
if d.CheckoutFlag == CommitFlag {
cmd = exec.Command("hg", "update", "-c", d.CheckoutSpec)
} else {
cmd = exec.Command("hg", "checkout", d.CheckoutSpec)
}
return cmd.Run()
}
func (h Hg) Fetch(path string) error {
return runInPath(path, func() error {
return exec.Command("hg", "pull").Run()
})
}
type Svn struct {
}
func (s Svn) Init(d *Dep) error {
return initScm(d, HiddenSvn, s)
}
func (s Svn) DownloadCommand(source, path string) *exec.Cmd {
return exec.Command("svn", "checkout", source, path)
}
func (s Svn) Checkout(d *Dep) error {
var cmd *exec.Cmd
switch d.CheckoutFlag {
case CommitFlag:
cmd = exec.Command("svn", "up", "-r", d.CheckoutSpec)
case BranchFlag:
cmd = exec.Command("svn", "switch", "^/branches/"+d.CheckoutSpec)
case TagFlag:
cmd = exec.Command("svn", "switch", "^/tags/"+d.CheckoutSpec)
}
return cmd.Run()
}
func (s Svn) Fetch(path string) error {
return runInPath(path, func() error {
return exec.Command("svn", "update").Run()
})
}
type Bzr struct {
}
func (b Bzr) Init(d *Dep) error {
return initScm(d, HiddenBzr, b)
}
func (b Bzr) DownloadCommand(source, path string) *exec.Cmd {
return exec.Command("bzr", "branch", source, path)
}
func (b Bzr) Checkout(d *Dep) error {
var cmd *exec.Cmd
switch d.CheckoutFlag {
case CommitFlag:
cmd = exec.Command("bzr", "update", "-r", d.CheckoutSpec)
case BranchFlag:
cmd = exec.Command("bzr", "update", "-r", "branch:"+d.CheckoutSpec)
case TagFlag:
cmd = exec.Command("bzr", "update", "-r", "tag:"+d.CheckoutSpec)
}
return cmd.Run()
}
func (b Bzr) Fetch(path string) error {
return runInPath(path, func() error {
return exec.Command("bzr", "pull").Run()
})
}
// The Go scm embeds another scm and only implements Init so that
// deps that don't specify a scm keep working like they did before
type Go struct {
Scm
}
func (g Go) Init(d *Dep) error {
return g.DownloadCommand(d.Import, "").Run()
}
func (g Go) DownloadCommand(source, path string) *exec.Cmd {
return exec.Command("go", "get", "-d", "-u", source)
}
func NewScm(d *Dep) (Scm, error) {
switch d.Scm {
case GitTag:
return Scms[GitTag], nil
case HgTag:
return Scms[HgTag], nil
case SvnTag:
return Scms[SvnTag], nil
}
scm := scmInSource(d)
if d.Scm == "go" {
return Go{scm}, nil
} else if scm != nil {
return scm, nil
}
return nil, fmt.Errorf("unknown scm for %s", d.Import)
}
// Traverse the source tree backwards until
// it finds the right directory
// or it arrives to the base of the import.
func scmInSource(d *Dep) Scm {
parts := strings.Split(d.Import, "/")
initPath := d.Src()
for _, _ = range parts {
for key, scm := range Scms {
if d.scmPath(path.Join(initPath, HiddenDirs[key])) {
return scm
}
}
initPath = path.Join(initPath, "..")
}
return nil
}