-
Notifications
You must be signed in to change notification settings - Fork 23
/
cmdget.go
436 lines (407 loc) · 12.1 KB
/
cmdget.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"gopkg.in/errgo.v2/fmt/errors"
"github.com/rogpeppe/go-internal/modfile"
"github.com/rogpeppe/go-internal/module"
)
var getCommand = &Command{
UsageLine: "get [-vcs] [-u] [-f] [module...]",
Short: "start hacking a module",
Long: `
The get command checks out Go module dependencies
into a directory where they can be edited.
It uses $GOHACK/<module> as the destination directory,
or $HOME/gohack/<module> if $GOHACK is empty.
By default it copies module source code from the existing
source directory in $GOPATH/pkg/mod. If the -vcs
flag is specified, it also checks out the version control information into that
directory and updates it to the expected version. If the directory
already exists, it will be updated in place.
`[1:],
}
func init() {
getCommand.Run = runGet // break init cycle
}
var (
// TODO implement getUpdate so that we can use gohack -f without
// overwriting source code.
// getUpdate = getCommand.Flag.Bool("u", false, "update to current version")
getForce = getCommand.Flag.Bool("f", false, "force update to current version even if not clean")
getVCS = getCommand.Flag.Bool("vcs", false, "get VCS information too")
)
func runGet(cmd *Command, args []string) int {
if err := runGet1(args); err != nil {
errorf("%v", err)
}
return 0
}
func runGet1(args []string) error {
if len(args) == 0 {
return errors.Newf("get requires at least one module argument")
}
var repls []*modReplace
mods, err := listModules("all")
if err != nil {
// TODO this happens when a replacement directory has been removed.
// Perhaps we should be more resilient in that case?
return errors.Notef(err, nil, "cannot get module info")
}
for _, mpath := range args {
m := mods[mpath]
if m == nil {
errorf("module %q does not appear to be in use", mpath)
continue
}
// Early check that we can replace the module, so we don't
// do all the work to check it out only to find we can't
// add the replace directive.
if err := checkCanReplace(mainModFile, mpath); err != nil {
errorf("%v", err)
continue
}
if m.Replace != nil && m.Replace.Path == m.Replace.Dir {
// TODO if -u flag specified, update to the current version instead of printing an error.
errorf("%q is already replaced by %q - are you already gohacking it?", mpath, m.Replace.Dir)
continue
}
var repl *modReplace
if *getVCS {
repl1, err := updateVCSDir(m)
if err != nil {
errorf("cannot update VCS dir for %s: %v", m.Path, err)
continue
}
repl = repl1
} else {
repl1, err := updateFromLocalDir(m)
if err != nil {
errorf("cannot update %s from local cache: %v", m.Path, err)
continue
}
repl = repl1
}
// Automatically generate a go.mod file if one doesn't already exist,
// because otherwise the directory cannot be used as a module.
if err := ensureGoModFile(repl.modulePath, repl.dir); err != nil {
errorf("%v", err)
}
repls = append(repls, repl)
}
if len(repls) == 0 {
return errors.New("all modules failed; not replacing anything")
}
if err := replace(mainModFile, repls); err != nil {
return errors.Notef(err, nil, "cannot replace")
}
if err := writeModFile(mainModFile); err != nil {
return errors.Wrap(err)
}
for _, info := range repls {
fmt.Printf("%s => %s\n", info.modulePath, info.replDir)
}
return nil
}
func updateFromLocalDir(m *listModule) (*modReplace, error) {
if m.Dir == "" {
return nil, errors.Newf("no local source code found")
}
srcHash, err := hashDir(m.Dir, m.Path)
if err != nil {
return nil, errors.Notef(err, nil, "cannot hash %q", m.Dir)
}
destDir, replDir, err := moduleDir(m.Path)
if err != nil {
return nil, errors.Notef(err, nil, "failed to determine target directory for %v", m.Path)
}
_, err = os.Stat(destDir)
if err != nil && !os.IsNotExist(err) {
return nil, errors.Wrap(err)
}
repl := &modReplace{
modulePath: m.Path,
dir: destDir,
replDir: replDir,
}
if err != nil {
// Destination doesn't exist. Copy the entire directory.
if err := copyAll(destDir, m.Dir); err != nil {
return nil, errors.Wrap(err)
}
} else {
if !*getForce {
// Destination already exists; try to update it.
isEmpty, err := isEmptyDir(destDir)
if err != nil {
return nil, errors.Wrap(err)
}
if !isEmpty {
// The destination directory already exists and has something in.
destHash, err := checkCleanWithoutVCS(destDir, m.Path)
if err != nil {
return nil, errors.Wrap(err)
}
if destHash == srcHash {
// Everything is exactly as we want it already.
return repl, nil
}
}
}
// As it's empty, clean or we're forcing clean, we can safely replace its
// contents with the current version.
if err := updateDirWithoutVCS(destDir, m.Dir); err != nil {
return nil, errors.Notef(err, nil, "cannot update %q from %q", destDir, m.Dir)
}
}
// Write a hash file so we can tell if someone has changed the
// directory later, so we avoid overwriting their changes.
if err := writeHashFile(destDir, srcHash); err != nil {
return nil, errors.Wrap(err)
}
return repl, nil
}
func checkCleanWithoutVCS(dir string, modulePath string) (hash string, err error) {
wantHash, err := readHashFile(dir)
if err != nil {
if !os.IsNotExist(errors.Cause(err)) {
return "", errors.Wrap(err)
}
return "", errors.Newf("%q already exists; not overwriting", dir)
}
gotHash, err := hashDir(dir, modulePath)
if err != nil {
return "", errors.Notef(err, nil, "cannot hash %q", dir)
}
if gotHash != wantHash {
return "", errors.Newf("%q is not clean; not overwriting", dir)
}
return wantHash, nil
}
func updateDirWithoutVCS(destDir, srcDir string) error {
if err := os.RemoveAll(destDir); err != nil {
return errors.Wrap(err)
}
if err := copyAll(destDir, srcDir); err != nil {
return errors.Wrap(err)
}
return nil
}
// TODO decide on a good name for this.
const hashFile = ".gohack-modhash"
func readHashFile(dir string) (string, error) {
data, err := ioutil.ReadFile(filepath.Join(dir, hashFile))
if err != nil {
return "", errors.Note(err, os.IsNotExist, "")
}
return strings.TrimSpace(string(data)), nil
}
func writeHashFile(dir string, hash string) error {
if err := ioutil.WriteFile(filepath.Join(dir, hashFile), []byte(hash), 0666); err != nil {
return errors.Wrap(err)
}
return nil
}
func updateVCSDir(m *listModule) (*modReplace, error) {
info, err := getVCSInfoForModule(m)
if err != nil {
return nil, errors.Notef(err, nil, "cannot get info")
}
if err := updateModule(info); err != nil {
return nil, errors.Wrap(err)
}
return &modReplace{
modulePath: m.Path,
dir: info.dir,
replDir: info.replDir,
}, nil
}
type modReplace struct {
// modulePath is the module path
modulePath string
// dir holds the absolute path to the replacement directory.
dir string
// replDir holds the path to use for the module in the go.mod replace directive.
replDir string
}
func replace(f *modfile.File, repls []*modReplace) error {
for _, repl := range repls {
if err := replaceModule(f, repl.modulePath, repl.replDir); err != nil {
return errors.Wrap(err)
}
}
return nil
}
func updateModule(info *moduleVCSInfo) error {
// Remove an auto-generated go.mod file if there is one
// to avoid confusing VCS logic.
if _, err := removeAutoGoMod(info); err != nil {
return errors.Wrap(err)
}
if info.alreadyExists && !info.clean && *getForce {
if err := info.vcs.Clean(info.dir); err != nil {
return fmt.Errorf("cannot clean: %v", err)
}
}
isTag := true
updateTo := info.module.Version
if IsPseudoVersion(updateTo) {
revID, err := PseudoVersionRev(updateTo)
if err != nil {
return errors.Wrap(err)
}
isTag = false
updateTo = revID
} else {
// Not a pseudo-version. However, this can still be in the form
// of "<validtag>+incompatible", so trim the suffix.
updateTo = strings.TrimSuffix(updateTo, "+incompatible")
}
if err := info.vcs.Update(info.dir, isTag, updateTo); err == nil {
fmt.Printf("updated hack version of %s to %s\n", info.module.Path, info.module.Version)
return nil
}
if !info.alreadyExists {
fmt.Printf("creating %s@%s\n", info.module.Path, info.module.Version)
if err := createRepo(info); err != nil {
return fmt.Errorf("cannot create repo: %v", err)
}
} else {
fmt.Printf("fetching %s@%s\n", info.module.Path, info.module.Version)
if err := info.vcs.Fetch(info.dir); err != nil {
return err
}
}
return info.vcs.Update(info.dir, isTag, updateTo)
}
func createRepo(info *moduleVCSInfo) error {
// Some version control tools require the parent of the target to exist.
parent, _ := filepath.Split(info.dir)
if err := os.MkdirAll(parent, 0777); err != nil {
return err
}
if err := info.vcs.Create(info.root.Repo, info.dir); err != nil {
return errors.Wrap(err)
}
return nil
}
func ensureGoModFile(modPath, dir string) error {
goModPath := filepath.Join(dir, "go.mod")
if _, err := os.Stat(goModPath); err == nil {
return nil
}
if err := ioutil.WriteFile(goModPath, []byte(autoGoMod(modPath)), 0666); err != nil {
return errors.Wrap(err)
}
return nil
}
// removeAutoGoMod removes the module directory's go.mod
// file if it looks like it's been autogenerated by us.
// It reports whether the file was removed.
func removeAutoGoMod(m *moduleVCSInfo) (bool, error) {
goModPath := filepath.Join(m.dir, "go.mod")
ok, err := isAutoGoMod(goModPath, m.module.Path)
if err != nil || !ok {
return false, err
}
if err := os.Remove(goModPath); err != nil {
return false, errors.Wrap(err)
}
return true, nil
}
// isAutoGoMod reports whether the file at path
// looks like it's a go.mod file auto-generated by gohack.
func isAutoGoMod(path string, modulePath string) (bool, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
if !os.IsNotExist(err) {
return false, errors.Wrap(err)
}
return false, nil
}
if string(data) != autoGoMod(modulePath) {
return false, nil
}
return true, nil
}
// autoGoMod returns the contents of the go.mod file that
// would be auto-generated for the module with the given
// path.
func autoGoMod(mpath string) string {
return "// Generated by gohack; DO NOT EDIT.\nmodule " + mpath + "\n"
}
// checkCanReplace checks whether it may be possible to replace
// the module in the given go.mod file.
func checkCanReplace(f *modfile.File, mod string) error {
var found *modfile.Replace
for _, r := range f.Replace {
if r.Old.Path != mod {
continue
}
if found != nil {
return errors.Newf("found multiple existing replacements for %q", mod)
}
if r.New.Version == "" {
return errors.Newf("%s is already replaced by %s", mod, r.New.Path)
}
}
return nil
}
// replaceModule adds or modifies a replace statement in f for mod
// to be replaced with dir.
func replaceModule(f *modfile.File, mod string, dir string) error {
var found *modfile.Replace
for _, r := range f.Replace {
if r.Old.Path != mod {
continue
}
// These checks shouldn't fail when checkCanReplace has been
// called previously, but check anyway just to be sure.
if found != nil || r.New.Version == "" {
panic(errors.Newf("unexpected bad replace for %q (checkCanReplace not called?)", mod))
}
found = r
}
if found == nil {
// No existing replace statement. Just add a new one.
if err := f.AddReplace(mod, "", dir, ""); err != nil {
return errors.Wrap(err)
}
return nil
}
// There's an existing replacement for the same target, so modify it
// but preserve the original replacement information around in a comment.
token := fmt.Sprintf("// was %s => %s", versionPath(found.Old), versionPath(found.New))
comments := &found.Syntax.Comments
if len(comments.Suffix) > 0 {
// There's already a comment, so preserve it.
comments.Suffix[0].Token = token + " " + comments.Suffix[0].Token
} else {
comments.Suffix = []modfile.Comment{{
Token: token,
}}
}
found.Old.Version = ""
found.New.Path = dir
found.New.Version = ""
if !found.Syntax.InBlock {
found.Syntax.Token = []string{"replace"}
} else {
found.Syntax.Token = nil
}
found.Syntax.Token = append(found.Syntax.Token, []string{
mod,
"=>",
dir,
}...)
return nil
}
func versionPath(v module.Version) string {
if v.Version == "" {
return v.Path
}
return v.Path + " " + v.Version
}