git-codereview: fix commitmsg hook when verbose
If the commit message is verbose it shows the diff being committed. This
can be shown by either `git commit --verbose` or setting the following
in your git config:
[commit]
verbose = true
The diff is uncomented and shown after a "everything below will be
removed" comment section. Identify it and do remove everything under it,
as otherwise the diff would get mixed up with the commit message.
Fixes golang/go#16376.
Change-Id: Ia69aeaf36a1c8471da423a142fb233168d455a5d
Reviewed-on: https://go-review.googlesource.com/25342
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
diff --git a/git-codereview/hook.go b/git-codereview/hook.go
index 63e8fd7..0e8f075 100644
--- a/git-codereview/hook.go
+++ b/git-codereview/hook.go
@@ -212,6 +212,8 @@
var (
fixupBang = []byte("fixup!")
squashBang = []byte("squash!")
+
+ ignoreBelow = []byte("\n# ------------------------ >8 ------------------------\n")
)
// isFixup reports whether text is a Git fixup! or squash! commit,
@@ -220,8 +222,14 @@
return bytes.HasPrefix(text, fixupBang) || bytes.HasPrefix(text, squashBang)
}
-// stripComments strips lines that begin with "#".
+// stripComments strips lines that begin with "#" and removes the
+// "everything below will be removed" section containing the diff when
+// using commit --verbose.
func stripComments(in []byte) []byte {
+ // Issue 16376
+ if i := bytes.Index(in, ignoreBelow); i >= 0 {
+ in = in[:i+1]
+ }
return regexp.MustCompile(`(?m)^#.*\n`).ReplaceAll(in, nil)
}
diff --git a/git-codereview/hook_test.go b/git-codereview/hook_test.go
index ca1fff3..0d171c0 100644
--- a/git-codereview/hook_test.go
+++ b/git-codereview/hook_test.go
@@ -66,6 +66,11 @@
{in: "all: gofmt\nahhh", want: "all: gofmt\n\nahhh"},
{in: "all: gofmt\n\nahhh", want: "all: gofmt\n\nahhh"},
{in: "all: gofmt\n\n\nahhh", want: "all: gofmt\n\n\nahhh"},
+ // Issue 16376
+ {
+ in: "all: gofmt\n# ------------------------ >8 ------------------------\ndiff",
+ want: "all: gofmt\n",
+ },
}
for _, tt := range rewrites {
write(t, gt.client+"/in.txt", tt.in)