-
Notifications
You must be signed in to change notification settings - Fork 3
/
save.go
324 lines (294 loc) · 6.92 KB
/
save.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package main
import (
"errors"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"sort"
"strings"
"github.com/kr/fs"
)
var cmdSave = &Command{
Usage: "save [-r] [-copy=false] [packages]",
Short: "list and copy dependencies into Deps",
Long: `
Save writes a list of the dependencies of the named packages along
with the exact source control revision of each dependency to a file
named "Deps".
The dependency list is a JSON document with the following structure:
type Deps struct {
ImportPath string
GoVersion string // Abridged output of 'go version'.
Packages []string // Arguments to deppy save, if any.
Deps []struct {
ImportPath string
Comment string // Tag or description of commit.
Rev string // VCS-specific commit ID.
}
}
Any dependencies already present in the list will be left unchanged.
For more about specifying packages, see 'go help packages'.
`,
Run: runSave,
}
var (
saveCopy = true
)
func init() {
cmdSave.Flag.BoolVar(&saveCopy, "copy", false, "copy source code")
}
func runSave(cmd *Command, args []string) {
err := save(args)
if err != nil {
log.Fatalln(err)
}
}
func save(pkgs []string) error {
if saveCopy {
log.Println(strings.TrimSpace(copyWarning))
}
dot, err := LoadPackages(".")
if err != nil {
return err
}
ver, err := goVersion()
if err != nil {
return err
}
manifest := "Deps"
var gold Deps
gnew := &Deps{
ImportPath: dot[0].ImportPath,
GoVersion: ver,
}
if len(pkgs) > 0 {
gnew.Packages = pkgs
} else {
pkgs = []string{"."}
}
a, err := LoadPackages(pkgs...)
if err != nil {
return err
}
err = gnew.Load(a)
if err != nil {
return err
}
if a := badSandboxVCS(gnew.Deps); a != nil {
log.Println("Unsupported sandbox VCS:", strings.Join(a, ", "))
return errors.New("error")
}
if gnew.Deps == nil {
gnew.Deps = make([]Dependency, 0) // produce json [], not null
}
err = carryVersions(&gold, gnew)
if err != nil {
return err
}
err = os.RemoveAll("Deps")
if err != nil {
log.Println(err)
}
f, err := os.Create(manifest)
if err != nil {
return err
}
_, err = gnew.WriteTo(f)
if err != nil {
return err
}
err = f.Close()
if err != nil {
return err
}
var rewritePaths []string
return rewrite(a, dot[0].ImportPath, rewritePaths)
}
type revError struct {
ImportPath string
HaveRev string
WantRev string
}
func (v *revError) Error() string {
return v.ImportPath + ": revision is " + v.HaveRev + ", want " + v.WantRev
}
// carryVersions copies Rev and Comment from a to b for
// each dependency with an identical ImportPath. For any
// dependency in b that appears to be from the same repo
// as one in a (for example, a parent or child directory),
// the Rev must already match - otherwise it is an error.
func carryVersions(a, b *Deps) error {
for i := range b.Deps {
err := carryVersion(a, &b.Deps[i])
if err != nil {
return err
}
}
return nil
}
func carryVersion(a *Deps, db *Dependency) error {
// First see if this exact package is already in the list.
for _, da := range a.Deps {
if db.ImportPath == da.ImportPath {
db.Rev = da.Rev
db.Comment = da.Comment
return nil
}
}
// No exact match, check for child or sibling package.
// We can't handle mismatched versions for packages in
// the same repo, so report that as an error.
for _, da := range a.Deps {
switch {
case strings.HasPrefix(db.ImportPath, da.ImportPath+"/"):
if da.Rev != db.Rev {
return &revError{db.ImportPath, db.Rev, da.Rev}
}
case strings.HasPrefix(da.ImportPath, db.root+"/"):
if da.Rev != db.Rev {
return &revError{db.ImportPath, db.Rev, da.Rev}
}
}
}
// No related package in the list, must be a new repo.
return nil
}
// subDeps returns a - b, using ImportPath for equality.
func subDeps(a, b []Dependency) (diff []Dependency) {
Diff:
for _, da := range a {
for _, db := range b {
if da.ImportPath == db.ImportPath {
continue Diff
}
}
diff = append(diff, da)
}
return diff
}
// badSandboxVCS returns a list of VCSes that don't work
// with the `deppy go` sandbox code.
func badSandboxVCS(deps []Dependency) (a []string) {
for _, d := range deps {
if d.vcs.CreateCmd == "" {
a = append(a, d.vcs.vcs.Name)
}
}
sort.Strings(a)
return uniq(a)
}
func removeSrc(srcdir string, deps []Dependency) error {
for _, dep := range deps {
path := filepath.FromSlash(dep.ImportPath)
err := os.RemoveAll(filepath.Join(srcdir, path))
if err != nil {
return err
}
}
return nil
}
func copySrc(dir string, deps []Dependency) error {
ok := true
for _, dep := range deps {
srcdir := filepath.Join(dep.ws, "src")
rel, err := filepath.Rel(srcdir, dep.dir)
if err != nil { // this should never happen
return err
}
dstpkgroot := filepath.Join(dir, rel)
err = os.RemoveAll(dstpkgroot)
if err != nil {
log.Println(err)
ok = false
}
w := fs.Walk(dep.dir)
for w.Step() {
err = copyPkgFile(dir, srcdir, w)
if err != nil {
log.Println(err)
ok = false
}
}
}
if !ok {
return errors.New("error copying source code")
}
return nil
}
func copyPkgFile(dstroot, srcroot string, w *fs.Walker) error {
if w.Err() != nil {
return w.Err()
}
if c := w.Stat().Name()[0]; c == '.' || c == '_' {
// Skip directories using a rule similar to how
// the go tool enumerates packages.
// See $GOROOT/src/cmd/go/main.go:/matchPackagesInFs
w.SkipDir()
}
if w.Stat().IsDir() {
return nil
}
rel, err := filepath.Rel(srcroot, w.Path())
if err != nil { // this should never happen
return err
}
return copyFile(filepath.Join(dstroot, rel), w.Path())
}
// copyFile copies a regular file from src to dst.
// dst is opened with os.Create.
func copyFile(dst, src string) error {
err := os.MkdirAll(filepath.Dir(dst), 0777)
if err != nil {
return err
}
linkDst, err := os.Readlink(src)
if err == nil {
return os.Symlink(linkDst, dst)
}
r, err := os.Open(src)
if err != nil {
return err
}
defer r.Close()
w, err := os.Create(dst)
if err != nil {
return err
}
_, err = io.Copy(w, r)
err1 := w.Close()
if err == nil {
err = err1
}
return err
}
// Func writeVCSIgnore writes "ignore" files inside dir for known VCSs,
// so that dir/pkg and dir/bin don't accidentally get committed.
// It logs any errors it encounters.
func writeVCSIgnore(dir string) {
// Currently git is the only VCS for which we know how to do this.
// Mercurial and Bazaar have similar mechasims, but they apparently
// require writing files outside of dir.
const ignore = "/pkg\n/bin\n"
name := filepath.Join(dir, ".gitignore")
err := writeFile(name, ignore)
if err != nil {
log.Println(err)
}
}
// writeFile is like ioutil.WriteFile but it creates
// intermediate directories with os.MkdirAll.
func writeFile(name, body string) error {
err := os.MkdirAll(filepath.Dir(name), 0777)
if err != nil {
return err
}
return ioutil.WriteFile(name, []byte(body), 0666)
}
const (
copyWarning = `
deprecated flag -copy=true
The flag -copy=true does not exist. It's just gone. Wow!
`
)