Skip to content

Commit

Permalink
Merge pull request #31 from rogpeppe/017-fix-undo-bug
Browse files Browse the repository at this point in the history
gohack undo: restore old replace comment correctly
  • Loading branch information
rogpeppe authored Oct 18, 2018
2 parents 2a6a2af + 32c38d0 commit dad0e3a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
9 changes: 6 additions & 3 deletions cmdundo.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,17 @@ func cmdUndo1(modules []string) error {
prevReplace := splitWasComment(comments.Suffix[0].Token)
if prevReplace != nil && prevReplace.Old.Path == r.Old.Path {
// We're popping the old replace statement.
if !r.Syntax.InBlock {
// When we're not in a block, we don't need the "replace" token.
if r.Syntax.InBlock {
// When we're in a block, we don't need the "replace" token.
prevReplace.Syntax.Token = prevReplace.Syntax.Token[1:]
}
// Preserve any before and after comments.
prevReplace.Syntax.Before = r.Syntax.Before
prevReplace.Syntax.After = r.Syntax.After
*r = *prevReplace
r.Old = prevReplace.Old
r.New = prevReplace.New
r.Syntax.Comments.Suffix = prevReplace.Syntax.Comments.Suffix
r.Syntax.Token = prevReplace.Syntax.Token
} else {
// It's not a "was" comment. Just remove it (after this loop so we don't
// interfere with the current range statement).
Expand Down
29 changes: 29 additions & 0 deletions testdata/undo-hack-inblock.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
cd repo
gohack undo
stdout 'dropped gopkg.in/errgo.v2'

grep '^\tgopkg.in/errgo.v2 => github.com/rogpeppe/test2/arble v0.0.0-20181008213029-f6022c873160$' go.mod

-- repo/main.go --
package main
import _ "gopkg.in/errgo.v2"

-- repo/go.mod --
module example.com/repo

require (
gopkg.in/errgo.v2 v2.1.0
rsc.io/sampler v1.3.0
)

replace (
rsc.io/sampler v1.3.0 => rsc.io/sampler v1.2.1
gopkg.in/errgo.v2 => ../foo // was gopkg.in/errgo.v2 => github.com/rogpeppe/test2/arble v0.0.0-20181008213029-f6022c873160
)

-- foo/foo.go --
package foo

-- foo/go.mod --
module example.com/foo

22 changes: 22 additions & 0 deletions testdata/undo-hack.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
cd repo
gohack undo
stdout 'dropped gopkg.in/errgo.v2'

grep '^replace gopkg.in/errgo.v2 => github.com/rogpeppe/test2/arble v0.0.0-20181008213029-f6022c873160$' go.mod

-- repo/main.go --
package main
import _ "gopkg.in/errgo.v2"

-- repo/go.mod --
module example.com/repo

require gopkg.in/errgo.v2 v2.1.0

replace gopkg.in/errgo.v2 => ../foo // was gopkg.in/errgo.v2 => github.com/rogpeppe/test2/arble v0.0.0-20181008213029-f6022c873160

-- foo/foo.go --
package foo

-- foo/go.mod --
module example.com/foo

0 comments on commit dad0e3a

Please sign in to comment.