git-codereview: support ~ in http.cookiefile

http.cookiefile is a pathname value, which means a leading tilde is
automatically expanded by git-config.

https://git-scm.com/docs/git-config#git-config-httpcookieFile
https://git-scm.com/docs/git-config#git-config-pathname

Change-Id: Ia208b8a8a7dd5e07de58481b9010051569d0d8c8
Reviewed-on: https://go-review.googlesource.com/115576
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
diff --git a/git-codereview/api.go b/git-codereview/api.go
index 77d2c08..c8f6da3 100644
--- a/git-codereview/api.go
+++ b/git-codereview/api.go
@@ -144,9 +144,21 @@
 
 	loadGerritOrigin()
 
+	homeDir := testHomeDir
+	if homeDir == "" {
+		usr, err := user.Current()
+		if err != nil {
+			dief("failed to get current user home directory to look for auth tokens: %v", err)
+		}
+		homeDir = usr.HomeDir
+	}
+
 	// First look in Git's http.cookiefile, which is where Gerrit
 	// now tells users to store this information.
 	if cookieFile, _ := trimErr(cmdOutputErr("git", "config", "http.cookiefile")); cookieFile != "" {
+		if strings.HasPrefix(cookieFile, "~/") {
+			cookieFile = filepath.Join(homeDir, strings.TrimPrefix(cookieFile, "~/"))
+		}
 		data, _ := ioutil.ReadFile(cookieFile)
 		maxMatch := -1
 		for _, line := range lines(string(data)) {
@@ -168,14 +180,6 @@
 	// used to tell users to store the information, until the passwords
 	// got so long that old versions of curl couldn't handle them.
 	netrc := netrcName()
-	homeDir := testHomeDir
-	if homeDir == "" {
-		usr, err := user.Current()
-		if err != nil {
-			dief("failed to get current user home directory to look for %q: %v", netrc, err)
-		}
-		homeDir = usr.HomeDir
-	}
 	data, _ := ioutil.ReadFile(filepath.Join(homeDir, netrc))
 	for _, line := range lines(string(data)) {
 		if i := strings.Index(line, "#"); i >= 0 {
diff --git a/git-codereview/api_test.go b/git-codereview/api_test.go
index 02132a2..997a347 100644
--- a/git-codereview/api_test.go
+++ b/git-codereview/api_test.go
@@ -110,7 +110,7 @@
 			if tt.cookiefile != "MISSING" {
 				write(t, gt.client+"/.cookies", tt.cookiefile)
 			}
-			trun(t, gt.client, "git", "config", "http.cookiefile", gt.client+"/.cookies")
+			trun(t, gt.client, "git", "config", "http.cookiefile", "~/.cookies")
 		}
 
 		// Run command via testMain to trap stdout, stderr, death.