git-codereview: fix .netrc authentication

.netrc credentials were clobbered by missing Git cookiefile. The command
to get the output of `git config http.cookiefile` was exiting the
program before it got a chance to see if any .netrc file were defined.

Remove redundancy in handling error of `git config remote.origin.url`.

Change-Id: I1e08710b4e9b154b8fa5ebfb3c2ce72ef4dd2360
Reviewed-on: https://go-review.googlesource.com/1867
Reviewed-by: Andrew Gerrand <adg@golang.org>
diff --git a/git-codereview/api.go b/git-codereview/api.go
index b52dffb..f79d770 100644
--- a/git-codereview/api.go
+++ b/git-codereview/api.go
@@ -40,10 +40,9 @@
 		return
 	}
 
+	// Gerrit must be set as Git's origin remote.
 	origin := getOutput("git", "config", "remote.origin.url")
-	if origin == "" {
-		dief("git config remote.origin.url: origin not found")
-	}
+
 	if strings.Contains(origin, "//github.com/") {
 		dief("git origin must be a Gerrit host, not GitHub: %s", origin)
 	}
@@ -85,7 +84,7 @@
 
 	// First look in Git's http.cookiefile, which is where Gerrit
 	// now tells users to store this information.
-	if cookieFile := getOutput("git", "config", "http.cookiefile"); cookieFile != "" {
+	if cookieFile, _ := getOutputErr("git", "config", "http.cookiefile"); cookieFile != "" {
 		data, _ := ioutil.ReadFile(cookieFile)
 		for _, line := range strings.Split(string(data), "\n") {
 			f := strings.Split(line, "\t")
diff --git a/git-codereview/review.go b/git-codereview/review.go
index 0b0ee8e..a2a60fb 100644
--- a/git-codereview/review.go
+++ b/git-codereview/review.go
@@ -195,9 +195,21 @@
 
 // getOutput runs the specified command and returns its combined standard
 // output and standard error outputs.
+// It dies on command errors.
 // NOTE: It should only be used to run commands that return information,
 // **not** commands that make any actual changes.
 func getOutput(command string, args ...string) string {
+	s, err := getOutputErr(command, args...)
+	if err != nil {
+		fmt.Fprintf(os.Stderr, "%v\n%s\n", commandString(command, args), s)
+		dief("%v", err)
+	}
+	return s
+}
+
+// Given a command and its arguments, getOutputErr returns the same
+// trimmed output as getOutput, but it returns any error instead of exiting.
+func getOutputErr(command string, args ...string) (string, error) {
 	// NOTE: We only show these non-state-modifying commands with -v -v.
 	// Otherwise things like 'git sync -v' show all our internal "find out about
 	// the git repo" commands, which is confusing if you are just trying to find
@@ -206,11 +218,7 @@
 		fmt.Fprintln(os.Stderr, commandString(command, args))
 	}
 	b, err := exec.Command(command, args...).CombinedOutput()
-	if err != nil {
-		fmt.Fprintf(os.Stderr, "%v\n%s\n", commandString(command, args), b)
-		dief("%v", err)
-	}
-	return string(bytes.TrimSpace(b))
+	return string(bytes.TrimSpace(b)), err
 }
 
 // getLines is like getOutput but it returns only non-empty output lines,