git-codereview: recognize sso://go/ and rpc://go/

Google engineers are now required to use these access methods
instead of https://go.googlesource.com/, so recognize them as
aliases.

(It is possible for every engineer to configure their Git clients with
insteadOf clauses to hide these from the codereview plugin, but
it's far less error-prone to just handle it here.)

Change-Id: Ic1c9a6b45aa61b11ff25ce21bca6e49344974b04
Reviewed-on: https://go-review.googlesource.com/c/review/+/499923
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Peter Weinberger <pjw@google.com>
Run-TryBot: Russ Cox <rsc@golang.org>
diff --git a/git-codereview/api.go b/git-codereview/api.go
index 947369a..9f7fe23 100644
--- a/git-codereview/api.go
+++ b/git-codereview/api.go
@@ -88,6 +88,15 @@
 		return fmt.Errorf("git origin must be a Gerrit host, not GitHub: %s", origin)
 	}
 
+	// Google employees are required to use sso://go/ or rpc://go/
+	// instead of https://go.googlesource.com/ for git operations.
+	// Normally that happens with a "insteadOf" in $HOME/.gitconfig,
+	// but in case people do a git clone from these directly, convert to
+	// their real meaning.
+	if strings.HasPrefix(origin, "sso://go/") || strings.HasPrefix(origin, "rpc://go/") {
+		origin = "https://go.googlesource.com/" + origin[len("sso://go/"):]
+	}
+
 	if googlesourceIndex := strings.Index(origin, ".googlesource.com"); googlesourceIndex >= 0 {
 		if !strings.HasPrefix(origin, "https://") {
 			return fmt.Errorf("git origin must be an https:// URL: %s", origin)
@@ -157,7 +166,6 @@
 	if auth.user != "" || auth.cookieName != "" {
 		return
 	}
-
 	loadGerritOrigin()
 
 	// First look in Git's http.cookiefile, which is where Gerrit
diff --git a/git-codereview/api_test.go b/git-codereview/api_test.go
index 8494e90..319815a 100644
--- a/git-codereview/api_test.go
+++ b/git-codereview/api_test.go
@@ -36,12 +36,12 @@
 		password:   "pw",
 	},
 	{
-		cookiefile: "go.googlesource.com	TRUE	/	TRUE	2147483647	o2	git-u2=pw\n",
+		cookiefile:  "go.googlesource.com	TRUE	/	TRUE	2147483647	o2	git-u2=pw\n",
 		cookieName:  "o2",
 		cookieValue: "git-u2=pw",
 	},
 	{
-		cookiefile: ".googlesource.com	TRUE	/	TRUE	2147483647	o3	git-u3=pw\n",
+		cookiefile:  ".googlesource.com	TRUE	/	TRUE	2147483647	o3	git-u3=pw\n",
 		cookieName:  "o3",
 		cookieValue: "git-u3=pw",
 	},
@@ -64,8 +64,8 @@
 		password:   "pw",
 	},
 	{
-		netrc: "BOGUS",
-		cookiefile: "go.googlesource.com	TRUE	/	TRUE	2147483647	o7	git-u7=pw\n",
+		netrc:       "BOGUS",
+		cookiefile:  "go.googlesource.com	TRUE	/	TRUE	2147483647	o7	git-u7=pw\n",
 		cookieName:  "o7",
 		cookieValue: "git-u7=pw",
 	},
@@ -146,6 +146,22 @@
 			project:   "crypto",
 		},
 		{
+			// Clone with sso://go/ (Google-internal but common with Go developers)
+			origin:    "",
+			originUrl: "sso://go/tools",
+			host:      "go.googlesource.com",
+			url:       "https://go-review.googlesource.com",
+			project:   "tools",
+		},
+		{
+			// Clone with rpc://go/ (Google-internal but common with Go developers)
+			origin:    "",
+			originUrl: "rpc://go/tools",
+			host:      "go.googlesource.com",
+			url:       "https://go-review.googlesource.com",
+			project:   "tools",
+		},
+		{
 			// Gerrit origin is set.
 			// Gerrit is hosted on a sub-domain.
 			origin:    "https://gerrit.mysite.com",