From 2a3bccfb1bf676aea1cfefb8eae1c7f4c566d22d Mon Sep 17 00:00:00 2001 From: Steve Francia Date: Wed, 7 Jun 2017 19:57:47 -0400 Subject: [PATCH] cmd/go-contrib-init: check working dir and autodectect repo Change-Id: I730216e2954ff591a57314e82ffd7b43d1da8ed4 Reviewed-on: https://go-review.googlesource.com/45084 Reviewed-by: Brad Fitzpatrick --- cmd/go-contrib-init/contrib.go | 91 +++++++++++++++++++++++++++++++++- 1 file changed, 89 insertions(+), 2 deletions(-) diff --git a/cmd/go-contrib-init/contrib.go b/cmd/go-contrib-init/contrib.go index 80649503d1..b7a07a015d 100644 --- a/cmd/go-contrib-init/contrib.go +++ b/cmd/go-contrib-init/contrib.go @@ -22,7 +22,7 @@ import ( ) var ( - repo = flag.String("repo", "go", "Which go repo you want to contribute to. Use \"go\" for the core, or e.g. \"net\" for golang.org/x/net/*") + repo = flag.String("repo", detectrepo(), "Which go repo you want to contribute to. Use \"go\" for the core, or e.g. \"net\" for golang.org/x/net/*") dry = flag.Bool("dry-run", false, "Fail with problems instead of trying to fix things.") ) @@ -40,6 +40,27 @@ func main() { "Next steps: https://golang.org/doc/contribute.html#commit_changes\n") } +func detectrepo() string { + wd, err := os.Getwd() + if err != nil { + return "go" + } + + for _, path := range filepath.SplitList(os.Getenv("GOPATH")) { + rightdir := filepath.Join(path, "src", "golang.org", "x") + if strings.HasPrefix(wd, rightdir) { + tail := wd[len(rightdir)+1:] + end := strings.Index(tail, string(os.PathSeparator)) + if end > 0 { + repo := tail[:end] + return repo + } + } + } + + return "go" +} + func checkCLA() { slurp, err := ioutil.ReadFile(cookiesFile()) if err != nil && !os.IsNotExist(err) { @@ -89,7 +110,73 @@ func checkGoroot() { } func checkWorkingDir() { - // TODO + if *repo == "go" { + if inGoPath() { + log.Fatal("You can't work on Go from within your GOPATH. Please checkout Go outside of your GOPATH") + } + return + } + wd, err := os.Getwd() + if err != nil { + log.Fatal(err) + } + + gopath := firstGoPath() + if gopath == "" { + log.Fatal("Your GOPATH is not set, please set it") + } + + rightdir := filepath.Join(gopath, "src", "golang.org", "x", *repo) + if !strings.HasPrefix(wd, rightdir) { + dirExists, err := exists(rightdir) + if err != nil { + log.Fatal(err) + } + if !dirExists { + log.Fatalf("The repo you want to work on is currently not on your system.\n"+ + "Run %q to obtain this repo\n"+ + "then go to the directory %q\n", + "go get -d golang.org/x/"+*repo, rightdir) + } + log.Fatalf("Your current directory is:%q\n"+ + "Working on golang/x/%v requires you be in %q\n", + wd, *repo, rightdir) + } +} + +func firstGoPath() string { + list := filepath.SplitList(os.Getenv("GOPATH")) + if len(list) < 1 { + return "" + } + return list[0] +} + +func exists(path string) (bool, error) { + _, err := os.Stat(path) + if os.IsNotExist(err) { + return false, nil + } + return true, err +} + +func inGoPath() bool { + wd, err := os.Getwd() + if err != nil { + log.Fatal(err) + } + + if os.Getenv("GOPATH") == "" { + return false + } + + for _, path := range filepath.SplitList(os.Getenv("GOPATH")) { + if strings.HasPrefix(wd, path) { + return true + } + } + + return false } // mostly check that they didn't clone from github