git-codereview: use --path option of git config to expand ~
This change largely reverts CL 115576, and applies a simpler version.
It uses the --path option of git config to achieve equivalent behavior.
The --path option is available as far back as git 1.7.0, according to
https://git-scm.com/docs/git-config/1.7.0, making it safe to rely on:
The type specifier can be [...] --path, which does some path expansion
(see --path below). If no type specifier is passed, no checks or
transformations are performed on the value.
--path
git-config will expand leading ~ to the value of $HOME, and ~user
to the home directory for the specified user.
Change-Id: I74bb543ab488d24fe4cf66da0c6dbc087dc368a6
Reviewed-on: https://go-review.googlesource.com/116016
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
diff --git a/git-codereview/api.go b/git-codereview/api.go
index 65bc3a2..8440cb5 100644
--- a/git-codereview/api.go
+++ b/git-codereview/api.go
@@ -143,21 +143,9 @@
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, "~/"))
- }
+ if cookieFile, _ := trimErr(cmdOutputErr("git", "config", "--path", "http.cookiefile")); cookieFile != "" {
data, _ := ioutil.ReadFile(cookieFile)
maxMatch := -1
for _, line := range lines(string(data)) {
@@ -179,6 +167,14 @@
// 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 {