forked from tools/godep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
182 lines (171 loc) · 3.97 KB
/
update.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
package main
import (
"errors"
"log"
"os"
"path/filepath"
"regexp"
"strings"
)
var cmdUpdate = &Command{
Usage: "update [packages]",
Short: "use different revision of selected packages",
Long: `
Update changes the named dependency packages to use the
revision of each currently installed in GOPATH. New code will
be copied into Godeps and the new revision will be written to
the manifest.
For more about specifying packages, see 'go help packages'.
`,
Run: runUpdate,
}
func runUpdate(cmd *Command, args []string) {
err := update(args)
if err != nil {
log.Fatalln(err)
}
}
func update(args []string) error {
if len(args) == 0 {
args = []string{"."}
}
var g Godeps
manifest := filepath.Join("Godeps", "Godeps.json")
err := ReadGodeps(manifest, &g)
if os.IsNotExist(err) {
manifest = "Godeps"
err = ReadGodeps(manifest, &g)
}
if err != nil {
return err
}
for _, arg := range args {
any := markMatches(arg, g.Deps)
if !any {
log.Println("not in manifest:", arg)
}
}
deps, err := LoadVCSAndUpdate(g.Deps)
if err != nil {
return err
}
if len(deps) == 0 {
return errors.New("no packages can be updated")
}
f, err := os.Create(manifest)
if err != nil {
return err
}
_, err = g.WriteTo(f)
if err != nil {
return err
}
err = f.Close()
if err != nil {
return err
}
if manifest != "Godeps" {
srcdir := filepath.FromSlash("Godeps/_workspace/src")
copySrc(srcdir, deps)
}
return nil
}
// markMatches marks each entry in deps with an import path that
// matches pat. It returns whether any matches occurred.
func markMatches(pat string, deps []Dependency) (matched bool) {
f := matchPattern(pat)
for i, dep := range deps {
if f(dep.ImportPath) {
deps[i].matched = true
matched = true
}
}
return matched
}
// matchPattern(pattern)(name) reports whether
// name matches pattern. Pattern is a limited glob
// pattern in which '...' means 'any string' and there
// is no other special syntax.
// Taken from $GOROOT/src/cmd/go/main.go.
func matchPattern(pattern string) func(name string) bool {
re := regexp.QuoteMeta(pattern)
re = strings.Replace(re, `\.\.\.`, `.*`, -1)
// Special case: foo/... matches foo too.
if strings.HasSuffix(re, `/.*`) {
re = re[:len(re)-len(`/.*`)] + `(/.*)?`
}
reg := regexp.MustCompile(`^` + re + `$`)
return func(name string) bool {
return reg.MatchString(name)
}
}
func LoadVCSAndUpdate(deps []Dependency) ([]Dependency, error) {
var err1 error
var paths []string
for _, dep := range deps {
paths = append(paths, dep.ImportPath)
}
ps, err := LoadPackages(paths...)
if err != nil {
return nil, err
}
var candidates []*Dependency
var tocopy []Dependency
for i := range deps {
dep := &deps[i]
for _, pkg := range ps {
if dep.ImportPath == pkg.ImportPath {
dep.pkg = pkg
break
}
}
if dep.pkg == nil {
log.Println(dep.ImportPath + ": error listing package")
err1 = errors.New("error loading dependencies")
continue
}
if dep.pkg.Error.Err != "" {
log.Println(dep.pkg.Error.Err)
err1 = errors.New("error loading dependencies")
continue
}
vcs, reporoot, err := VCSFromDir(dep.pkg.Dir, filepath.Join(dep.pkg.Root, "src"))
if err != nil {
log.Println(err)
err1 = errors.New("error loading dependencies")
continue
}
dep.dir = dep.pkg.Dir
dep.ws = dep.pkg.Root
dep.root = filepath.ToSlash(reporoot)
dep.vcs = vcs
if dep.matched {
candidates = append(candidates, dep)
}
}
if err1 != nil {
return nil, err1
}
for _, dep := range candidates {
dep.dir = dep.pkg.Dir
dep.ws = dep.pkg.Root
id, err := dep.vcs.identify(dep.pkg.Dir)
if err != nil {
log.Println(err)
err1 = errors.New("error loading dependencies")
continue
}
if dep.vcs.isDirty(dep.pkg.Dir, id) {
log.Println("dirty working tree:", dep.pkg.Dir)
err1 = errors.New("error loading dependencies")
break
}
dep.Rev = id
dep.Comment = dep.vcs.describe(dep.pkg.Dir, id)
tocopy = append(tocopy, *dep)
}
if err1 != nil {
return nil, err1
}
return tocopy, nil
}