cmd/git-contrib-init: support http.cookiefile config for gitcookies

Change-Id: I097905122e1cb7298c31c330731f0fc3c6fc9b59
Reviewed-on: https://go-review.googlesource.com/46235
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
diff --git a/cmd/go-contrib-init/contrib.go b/cmd/go-contrib-init/contrib.go
index 3e92f61..e9a2906 100644
--- a/cmd/go-contrib-init/contrib.go
+++ b/cmd/go-contrib-init/contrib.go
@@ -80,7 +80,27 @@
 		"  * Run go-contrib-init again.\n")
 }
 
+func expandUser(s string) string {
+	env := "HOME"
+	if runtime.GOOS == "windows" {
+		env = "USERPROFILE"
+	} else if runtime.GOOS == "plan9" {
+		env = "home"
+	}
+	if home := os.Getenv(env); home != "" {
+		return strings.Replace(s, "~", home, 1)
+	}
+	return s
+}
+
 func cookiesFile() string {
+	out, _ := exec.Command("git", "config", "http.cookiefile").Output()
+	if s := strings.TrimSpace(string(out)); s != "" {
+		if strings.Contains(s, "~") {
+			s = expandUser(s)
+		}
+		return s
+	}
 	if runtime.GOOS == "windows" {
 		return filepath.Join(os.Getenv("USERPROFILE"), ".gitcookies")
 	}