git-codereview: pass args to installHook

This enables passing -v to
'git codereview hooks'
to see what commands are being run.

It also means that automatic installation of
hooks should respect -n.

Change-Id: I812685b43e9ea678641f64b2182b957342a4e43c
Reviewed-on: https://go-review.googlesource.com/24002
Reviewed-by: Andrew Gerrand <adg@golang.org>
diff --git a/git-codereview/change.go b/git-codereview/change.go
index 0d47857..fac0f94 100644
--- a/git-codereview/change.go
+++ b/git-codereview/change.go
@@ -21,7 +21,6 @@
 	if len(flags.Args()) > 1 {
 		fmt.Fprintf(stderr(), "Usage: %s change %s [branch]\n", os.Args[0], globalFlags)
 		os.Exit(2)
-
 	}
 
 	// Checkout or create branch, if specified.
diff --git a/git-codereview/hook.go b/git-codereview/hook.go
index f10c070..04fa87c 100644
--- a/git-codereview/hook.go
+++ b/git-codereview/hook.go
@@ -21,7 +21,8 @@
 	"pre-commit",
 }
 
-func installHook() {
+func installHook(args []string) {
+	flags.Parse(args)
 	hooksDir := gitPath("hooks")
 	for _, hookFile := range hookFiles {
 		filename := filepath.Join(hooksDir, hookFile)
diff --git a/git-codereview/review.go b/git-codereview/review.go
index 779b77c..c83a95b 100644
--- a/git-codereview/review.go
+++ b/git-codereview/review.go
@@ -127,7 +127,17 @@
 
 	// Install hooks automatically, but only if this is a Gerrit repo.
 	if haveGerrit() {
-		installHook()
+		// Don't pass installHook args directly,
+		// since args might contain args meant for other commands.
+		// Filter down to just global flags.
+		var hookArgs []string
+		for _, arg := range args {
+			switch arg {
+			case "-n", "-v":
+				hookArgs = append(hookArgs, arg)
+			}
+		}
+		installHook(hookArgs)
 	}
 
 	switch command {
@@ -140,7 +150,7 @@
 	case "hook-invoke":
 		cmdHookInvoke(args)
 	case "hooks":
-		installHook() // in case above was bypassed
+		installHook(args) // in case above was bypassed
 	case "mail", "m":
 		cmdMail(args)
 	case "pending":