cmd/go-contrib-init: add some git origin checks
Updates golang/go#17802
Change-Id: I70d30c5ff12837d51d13b5ca7e73be96eb535286
Reviewed-on: https://go-review.googlesource.com/45079
Reviewed-by: Steve Francia <spf@golang.org>
diff --git a/cmd/go-contrib-init/contrib.go b/cmd/go-contrib-init/contrib.go
index 6f304ad..1b533dd 100644
--- a/cmd/go-contrib-init/contrib.go
+++ b/cmd/go-contrib-init/contrib.go
@@ -11,9 +11,11 @@
import (
"bytes"
"flag"
+ "fmt"
"io/ioutil"
"log"
"os"
+ "os/exec"
"path/filepath"
"runtime"
"strings"
@@ -28,6 +30,7 @@
checkCLA()
checkGoroot()
checkWorkingDir()
+ checkGitOrigin()
}
func checkCLA() {
@@ -81,3 +84,40 @@
func checkWorkingDir() {
// TODO
}
+
+// mostly check that they didn't clone from github
+func checkGitOrigin() {
+ if _, err := exec.LookPath("git"); err != nil {
+ log.Fatalf("You don't appear to have git installed. Do that.")
+ }
+ if _, err := os.Stat(".git"); err != nil {
+ log.Fatalf("You are not currently in a git checkout of https://go.googlesource.com/%s", *repo)
+ }
+ remotes, err := exec.Command("git", "remote", "-v").Output()
+ if err != nil {
+ log.Fatalf("Error running git remote -v: %v", cmdErr(err))
+ }
+ matches := 0
+ wantRemote := "https://go.googlesource.com/" + *repo
+ for _, line := range strings.Split(string(remotes), "\n") {
+ line = strings.TrimSpace(line)
+ if !strings.HasPrefix(line, "origin") {
+ continue
+ }
+ if !strings.Contains(line, wantRemote) {
+ curRemote := strings.Fields(strings.TrimPrefix(line, "origin"))[0]
+ log.Fatalf("Current directory's git was cloned from %q; origin should be %q", curRemote, wantRemote)
+ }
+ matches++
+ }
+ if matches == 0 {
+ log.Fatalf("git remote -v output didn't contain expected %q. Got:\n%s", wantRemote, remotes)
+ }
+}
+
+func cmdErr(err error) string {
+ if ee, ok := err.(*exec.ExitError); ok && len(ee.Stderr) > 0 {
+ return fmt.Sprintf("%s: %s", err, ee.Stderr)
+ }
+ return fmt.Sprint(err)
+}