Permalink
Browse files

let go get get source

  • Loading branch information...
1 parent ac4c36d commit abf2b85038ce6ef5c4b00e435774cfd6c48ca977 @kr committed Mar 30, 2012
Showing with 42 additions and 16 deletions.
  1. +3 −4 Readme.md
  2. +8 −6 doc.go
  3. +31 −6 main.go
View
@@ -7,14 +7,15 @@ import paths as necessary.
Example usage:
```bash
+$ go get github.com/bmizerany/pat
$ goven github.com/bmizerany/pat
```
-This takes the named package, clones its code into
+This takes the named package, copies its code into
a subdirectory, removes the VCS metadata, and
rewrites import paths in the current directory tree
to refer to the new location. For example, vendoring
-`github.com/bmizerany/pat` into `example.com/foo` would
+`github.com/bmizerany/pat` in `example.com/foo` would
produce an import path of
`example.com/foo/github.com/bmizerany/pat`.
@@ -27,8 +28,6 @@ $ go install github.com/kr/goven
## Bugs
-- only works with git
-- always clones the default branch (usually master)
- probably doesn't work on Windows
If you use this tool, I suggest doing it on a clean checkout.
View
14 doc.go
@@ -3,12 +3,14 @@
// import paths as necessary.
//
// Example usage:
+// go get github.com/bmizerany/pat
// goven github.com/bmizerany/pat
//
-// It takes the named package, clones its code into
-// a subdirectory, removes the VCS metadata, and
-// rewrites import paths in the current directory tree
-// to refer to the new location. For example, vendoring
-// github.com/kr/pretty into example.com/foo would
-// produce an import path of example.com/foo/github.com/kr/pretty.
+// It takes the named package, copies its files from the
+// usual place in GOPATH into the current directory,
+// removes the VCS metadata, and rewrites import paths
+// in the current directory tree to refer to the new location.
+// For example, vendoring github.com/bmizerany/pat into
+// example.com/foo would produce an import path of
+// example.com/foo/github.com/bmizerany/pat.
package main
View
37 main.go
@@ -15,10 +15,10 @@ import (
"strings"
)
-var godir, imp string
+var curgodir, imp string
var (
- fetch = flag.Bool("fetch", true, "fetch the code")
+ copy = flag.Bool("copy", true, "copy the code")
rewrite = flag.Bool("rewrite", true, "rewrite include paths")
)
@@ -38,18 +38,28 @@ func main() {
}
imp = flag.Arg(0)
- godir, err = lookupDir()
+ pkgdir := which(imp)
+ if pkgdir == "" {
+ log.Fatal("could not find package")
+ }
+
+ curgodir, err = lookupDir()
if err != nil {
log.Fatal(err)
}
- if *fetch {
+ if *copy {
err = os.RemoveAll(imp)
if err != nil {
log.Fatal(err)
}
- err = run("git", "clone", "git://"+imp+".git", imp)
+ err = os.MkdirAll(imp, 0770)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ err = run("cp", "-r", pkgdir, imp)
if err != nil {
log.Fatal(err)
}
@@ -73,6 +83,21 @@ func main() {
}
}
+func which(pkg string) string {
+ for _, top := range strings.Split(os.Getenv("GOPATH"), ":") {
+ dir := top + "/src/" + pkg
+ _, err := os.Stat(dir)
+ if err == nil {
+ return dir
+ }
+ p := err.(*os.PathError)
+ if !os.IsNotExist(p.Err) {
+ log.Print(err)
+ }
+ }
+ return ""
+}
+
func lookupDir() (string, error) {
gopath := os.Getenv("GOPATH")
if gopath == "" {
@@ -131,7 +156,7 @@ func mangleFile(path string) error {
return err // can't happen
}
if strings.HasPrefix(path, imp) {
- s.Path.Value = strconv.Quote(godir + "/" + path)
+ s.Path.Value = strconv.Quote(curgodir + "/" + path)
changed = true
}
}

0 comments on commit abf2b85

Please sign in to comment.