1
0
mirror of https://github.com/golang/go synced 2024-11-18 09:04:49 -07:00

refactor/rename: attempt to fix windows build...

...by using buildutil.ContainingPackage instead of guessImportPath.
The former should be more portable.

(I meant to do this earlier, so this is also a nice cleanup.)

LGTM=gri
R=gri, sameer
CC=golang-codereviews
https://golang.org/cl/148050043
This commit is contained in:
Alan Donovan 2014-09-24 16:18:58 -04:00
parent 717118b6c8
commit a69ece7231
2 changed files with 10 additions and 67 deletions

View File

@ -115,12 +115,12 @@ func parseFromFlag(ctxt *build.Context, fromFlag string) (*spec, error) {
if !buildutil.FileExists(ctxt, spec.filename) {
return nil, fmt.Errorf("no such file: %s", spec.filename)
}
// Guess the default package.
var err error
if spec.pkg, err = guessImportPath(spec.filename, ctxt); err != nil {
return nil, fmt.Errorf("-from: couldn't guess package from filename: %s: %s",
spec.filename, err)
bp, err := buildutil.ContainingPackage(ctxt, wd, spec.filename)
if err != nil {
return nil, err
}
spec.pkg = bp.ImportPath
} else if a, b := splitAtLastDot(main); b == "" {
// importpath e.g. "encoding/json"
@ -194,12 +194,12 @@ func parseOffsetFlag(ctxt *build.Context, offsetFlag string) (*spec, error) {
if !buildutil.FileExists(ctxt, spec.filename) {
return nil, fmt.Errorf("no such file: %s", spec.filename)
}
// Guess the default package.
var err error
if spec.pkg, err = guessImportPath(spec.filename, ctxt); err != nil {
return nil, fmt.Errorf("couldn't guess package from filename: %s: %s",
spec.filename, err)
bp, err := buildutil.ContainingPackage(ctxt, wd, spec.filename)
if err != nil {
return nil, err
}
spec.pkg = bp.ImportPath
for _, r := range parts[1] {
if !isDigit(r) {

View File

@ -5,8 +5,6 @@
package rename
import (
"fmt"
"go/build"
"os"
"path/filepath"
"reflect"
@ -79,61 +77,6 @@ func isDigit(ch rune) bool {
// -- Plundered from code.google.com/p/go.tools/oracle -----------------
// guessImportPath finds the package containing filename, and returns
// its import path relative to it.
func guessImportPath(filename string, ctxt *build.Context) (importPath string, err error) {
// TODO(adonovan): move this to package "buildutil"; factor in common with oracle.
// bp, err := buildutil.ContainingPackage(ctxt, wd, filename)
// if err != nil {
// return
// }
// return bp.ImportPath, nil
absFile, err := filepath.Abs(filename)
if err != nil {
err = fmt.Errorf("can't form absolute path of %s", filename)
return
}
absFileDir := segments(filepath.Dir(absFile))
// Find the innermost directory in $GOPATH that encloses filename.
minD := 1024
for _, gopathDir := range ctxt.SrcDirs() {
// We can assume $GOPATH and $GOROOT dirs are absolute,
// thus gopathDir too, and that it exists.
d := prefixLen(segments(gopathDir), absFileDir)
// If there are multiple matches,
// prefer the innermost enclosing directory
// (smallest d).
if d >= 0 && d < minD {
minD = d
importPath = strings.Join(absFileDir[len(absFileDir)-minD:], string(os.PathSeparator))
}
}
if importPath == "" {
err = fmt.Errorf("can't find package for file %s", filename)
}
return
}
func segments(path string) []string {
return strings.Split(path, string(os.PathSeparator))
}
// prefixLen returns the length of the remainder of y if x is a prefix
// of y, a negative number otherwise.
func prefixLen(x, y []string) int {
d := len(y) - len(x)
if d >= 0 {
for i := range x {
if y[i] != x[i] {
return -1 // not a prefix
}
}
}
return d
}
// sameFile returns true if x and y have the same basename and denote
// the same file.
//