git-codereview: disable gofmt hook when $GIT_GOFMT_HOOK=off

Change-Id: I9b0b178188a8095af14445f90a16dc32050556f5
Reviewed-on: https://go-review.googlesource.com/2784
Reviewed-by: Andrew Gerrand <adg@golang.org>
diff --git a/git-codereview/hook.go b/git-codereview/hook.go
index 31dff11..ae6a6cd 100644
--- a/git-codereview/hook.go
+++ b/git-codereview/hook.go
@@ -195,6 +195,11 @@
 }
 
 func hookGofmt() {
+	if os.Getenv("GIT_GOFMT_HOOK") == "off" {
+		fmt.Fprintf(stderr(), "git-gofmt-hook disabled by $GIT_GOFMT_HOOK=off\n")
+		return
+	}
+
 	files, stderr := runGofmt(gofmtPreCommit)
 
 	if stderr != "" {
diff --git a/git-codereview/hook_test.go b/git-codereview/hook_test.go
index 671af94..45c7a9d 100644
--- a/git-codereview/hook_test.go
+++ b/git-codereview/hook_test.go
@@ -166,6 +166,23 @@
 	testNoStderr(t)
 }
 
+func TestHookPreCommitEnv(t *testing.T) {
+	// If $GIT_GOFMT_HOOK == "off", gofmt hook should not complain.
+
+	gt := newGitTest(t)
+	defer gt.done()
+	gt.work(t)
+
+	write(t, gt.client+"/bad.go", badGo)
+	trun(t, gt.client, "git", "add", ".")
+	os.Setenv("GIT_GOFMT_HOOK", "off")
+	defer os.Unsetenv("GIT_GOFMT_HOOK")
+
+	testMain(t, "hook-invoke", "pre-commit")
+	testNoStdout(t)
+	testPrintedStderr(t, "git-gofmt-hook disabled by $GIT_GOFMT_HOOK=off")
+}
+
 func TestHookPreCommitUnstaged(t *testing.T) {
 	gt := newGitTest(t)
 	defer gt.done()