Skip to content

Commit c1b72a7

Browse files
committed
gerrit: allow setAuth to fallthrough to netrc lookup
When git has no http cookies, the request for http cookies will fail because git will exit(1). Ignore this failure because the output is properly tested either way. This allows authentication to fallthrough to the netrc lookup. Correct the netrc lookup under windows. git reads the netrc file as "_netrc" in the users home directory. Add a warning at the end of the function that no authentication was set. Fixes golang/go#26782 Change-Id: I0ba94ff7fa4b6038d6117156dcc729ddf4616fdc Reviewed-on: https://go-review.googlesource.com/127855 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
1 parent 24a3043 commit c1b72a7

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

gerrit/auth.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"os"
1818
"os/exec"
1919
"path/filepath"
20+
"runtime"
2021
"strconv"
2122
"strings"
2223
"sync"
@@ -59,18 +60,25 @@ func GitCookieFileAuth(file string) Auth {
5960
return &gitCookieFileAuth{file: file}
6061
}
6162

63+
func netrcPath() string {
64+
if runtime.GOOS == "windows" {
65+
return filepath.Join(os.Getenv("USERPROFILE"), "_netrc")
66+
}
67+
return filepath.Join(os.Getenv("HOME"), ".netrc")
68+
}
69+
6270
type gitCookiesAuth struct{}
6371

6472
func (gitCookiesAuth) setAuth(c *Client, r *http.Request) {
6573
// First look in Git's http.cookiefile, which is where Gerrit
6674
// now tells users to store this information.
6775
git := exec.Command("git", "config", "http.cookiefile")
6876
git.Stderr = os.Stderr
69-
gitOut, err := git.Output()
70-
if err != nil {
71-
log.Printf("git config http.cookiefile failed: %s", err)
72-
return
73-
}
77+
78+
// Ignore a failure here, git will exit(1) if no cookies are
79+
// present and prevent the netrc from being read below.
80+
gitOut, _ := git.Output()
81+
7482
cookieFile := strings.TrimSpace(string(gitOut))
7583
if len(cookieFile) != 0 {
7684
auth := &gitCookieFileAuth{file: cookieFile}
@@ -90,7 +98,8 @@ func (gitCookiesAuth) setAuth(c *Client, r *http.Request) {
9098
// used to tell users to store the information, until the passwords
9199
// got so long that old versions of curl couldn't handle them.
92100
host := url.Host
93-
data, _ := ioutil.ReadFile(filepath.Join(os.Getenv("HOME"), ".netrc"))
101+
netrc := netrcPath()
102+
data, _ := ioutil.ReadFile(netrc)
94103
for _, line := range strings.Split(string(data), "\n") {
95104
if i := strings.Index(line, "#"); i >= 0 {
96105
line = line[:i]
@@ -101,6 +110,7 @@ func (gitCookiesAuth) setAuth(c *Client, r *http.Request) {
101110
return
102111
}
103112
}
113+
log.Printf("no authentication configured for Gerrit; tried both git config http.cookiefile and %s", netrc)
104114
}
105115

106116
type gitCookieFileAuth struct {

0 commit comments

Comments
 (0)