Skip to content

Commit 1cfae61

Browse files
ensure no file is processed twice
1 parent 549dc87 commit 1cfae61

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ require (
99
github.com/pkg/errors v0.9.1
1010
golang.org/x/mod v0.2.0
1111
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
12-
golang.org/x/tools v0.0.0-20200329025819-fd4102a86c65
12+
golang.org/x/tools v0.0.0-20200417140056-c07e33ef3290
1313
gopkg.in/urfave/cli.v2 v2.0.0-20180128182452-d3ae77c26ac8
1414
)

go.sum

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135/go.mod h1:od
1212
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
1313
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
1414
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
15+
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
1516
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
1617
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
1718
golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ=
@@ -36,6 +37,8 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
3637
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
3738
golang.org/x/tools v0.0.0-20200329025819-fd4102a86c65 h1:1KSbntBked74wYsKq0jzXYy7ZwcjAUtrl7EmPE97Iiw=
3839
golang.org/x/tools v0.0.0-20200329025819-fd4102a86c65/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8=
40+
golang.org/x/tools v0.0.0-20200417140056-c07e33ef3290 h1:NXNmtp0ToD36cui5IqWy95LC4Y6vT/4y3RnPxlQPinU=
41+
golang.org/x/tools v0.0.0-20200417140056-c07e33ef3290/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
3942
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
4043
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
4144
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=

major/major.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,14 @@ func Run(dir, op, modName string, tag int, buildFlags []string) error {
4343
if err != nil {
4444
return errors.Wrap(err, "could not load package")
4545
}
46+
ids := map[string]struct{}{}
47+
files := map[string]struct{}{}
4648
for _, p := range pkgs {
47-
err = updateImportPath(p, modName, newModPath)
49+
if _, ok := ids[p.ID]; ok {
50+
continue
51+
}
52+
ids[p.ID] = struct{}{}
53+
err = updateImportPath(p, modName, newModPath, files)
4854
if err != nil {
4955
return err
5056
}
@@ -127,8 +133,13 @@ func getPrevious(s string) string {
127133
return strings.Join(ss[:len(ss)-1], "/") + "/v" + strconv.Itoa(newV)
128134
}
129135

130-
func updateImportPath(p *packages.Package, old, new string) error {
136+
func updateImportPath(p *packages.Package, old, new string, files map[string]struct{}) error {
131137
for _, syn := range p.Syntax {
138+
goFileName := p.Fset.File(syn.Pos()).Name()
139+
if _, ok := files[goFileName]; ok {
140+
continue
141+
}
142+
files[goFileName] = struct{}{}
132143
var rewritten bool
133144
for _, i := range syn.Imports {
134145
imp := strings.Replace(i.Path.Value, `"`, ``, 2)
@@ -144,7 +155,6 @@ func updateImportPath(p *packages.Package, old, new string) error {
144155
continue
145156
}
146157

147-
goFileName := p.Fset.File(syn.Pos()).Name()
148158
f, err := os.Create(goFileName)
149159
if err != nil {
150160
return errors.Wrapf(err, "could not create go file %v", goFileName)

0 commit comments

Comments
 (0)