git-codereview: override existing locale with LC_ALL=C

The git-codereview tool shells out to git, then parses the output.
However, because git respects a user's locale settings its output might
not be in English, confusing git-codereview.

Explicitly set the LC_ALL environment variable to the "C" locale, which
is a version of US English that should be on all machines.

Fixes golang/go#33895

Change-Id: Id06a81046dba58131fc1de602dd9add687846da1
Reviewed-on: https://go-review.googlesource.com/c/review/+/192237
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
diff --git a/git-codereview/branch.go b/git-codereview/branch.go
index 0a277ce..a332256 100644
--- a/git-codereview/branch.go
+++ b/git-codereview/branch.go
@@ -78,6 +78,7 @@
 		envs = append(envs, "MSYS=noglob "+os.Getenv("MSYS"))
 		cmd.Env = envs
 	}
+	setEnglishLocale(cmd)
 
 	out, err := cmd.CombinedOutput()
 	if err == nil && len(out) > 0 {
diff --git a/git-codereview/review.go b/git-codereview/review.go
index a4ea988..afc61d9 100644
--- a/git-codereview/review.go
+++ b/git-codereview/review.go
@@ -196,6 +196,15 @@
 	}
 }
 
+func setEnglishLocale(cmd *exec.Cmd) {
+	// Override the existing locale to prevent non-English locales from
+	// interfering with string parsing. See golang.org/issue/33895.
+	if cmd.Env == nil {
+		cmd.Env = os.Environ()
+	}
+	cmd.Env = append(cmd.Env, "LC_ALL=C")
+}
+
 func run(command string, args ...string) {
 	if err := runErr(command, args...); err != nil {
 		if *verbose == 0 {
@@ -232,6 +241,7 @@
 	cmd.Stdin = os.Stdin
 	cmd.Stdout = stdout()
 	cmd.Stderr = stderr()
+	setEnglishLocale(cmd)
 	return cmd.Run()
 }
 
@@ -293,6 +303,7 @@
 	if dir != "." {
 		cmd.Dir = dir
 	}
+	setEnglishLocale(cmd)
 	b, err := cmd.CombinedOutput()
 	return string(b), err
 }
diff --git a/git-codereview/util_test.go b/git-codereview/util_test.go
index 7065da2..3f4dce6 100644
--- a/git-codereview/util_test.go
+++ b/git-codereview/util_test.go
@@ -236,6 +236,7 @@
 func trun(t *testing.T, dir string, cmdline ...string) string {
 	cmd := exec.Command(cmdline[0], cmdline[1:]...)
 	cmd.Dir = dir
+	setEnglishLocale(cmd)
 	out, err := cmd.CombinedOutput()
 	if err != nil {
 		if cmdline[0] == "git" {