1
0
mirror of https://github.com/golang/go synced 2024-09-30 14:28:33 -06:00

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 <bradfitz@golang.org>
This commit is contained in:
Steve Francia 2017-06-07 19:57:47 -04:00
parent 987877771e
commit 2a3bccfb1b

View File

@ -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