git-codereview: reword: apply commit message hook to edits

If you run "git codereview reword" you should get the same
"Fixes" fixing and Change-Id insertion as with regular commit
message editing.

Change-Id: I77220d42b49f575c07a7c1ef786c3775ee75133c
Reviewed-on: https://go-review.googlesource.com/c/review/+/295310
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
diff --git a/git-codereview/hook.go b/git-codereview/hook.go
index 8bf2a51..1ad0f09 100644
--- a/git-codereview/hook.go
+++ b/git-codereview/hook.go
@@ -169,7 +169,22 @@
 	if err != nil {
 		dief("%v", err)
 	}
-	data := append([]byte{}, oldData...)
+
+	data := fixCommitMessage(oldData)
+
+	// Write back.
+	if !bytes.Equal(data, oldData) {
+		if err := ioutil.WriteFile(file, data, 0666); err != nil {
+			dief("%v", err)
+		}
+	}
+}
+
+// fixCommitMessage fixes various commit message issues,
+// including adding a Change-Id line and rewriting #12345
+// into repo#12345 as directed by codereview.cfg.
+func fixCommitMessage(msg []byte) []byte {
+	data := append([]byte{}, msg...)
 	data = stripComments(data)
 
 	// Empty message not allowed.
@@ -226,12 +241,7 @@
 		}
 	}
 
-	// Write back.
-	if !bytes.Equal(data, oldData) {
-		if err := ioutil.WriteFile(file, data, 0666); err != nil {
-			dief("%v", err)
-		}
-	}
+	return data
 }
 
 // randomBytes returns 20 random bytes suitable for use in a Change-Id line.
diff --git a/git-codereview/reword.go b/git-codereview/reword.go
index 9083cde..5948502 100644
--- a/git-codereview/reword.go
+++ b/git-codereview/reword.go
@@ -104,7 +104,7 @@
 		if edited == "" {
 			dief("edited message is empty")
 		}
-		newMsg[c] = edited
+		newMsg[c] = string(fixCommitMessage([]byte(edited)))
 		fmt.Fprintf(&buf, "# %s\n\n%s\n\n", c.Subject, edited)
 		saveBuf()
 	} else {
@@ -166,7 +166,7 @@
 			if c == nil {
 				dief("cannot find commit for header: %s\n%s", strings.TrimSpace(hdr), note)
 			}
-			newMsg[c] = body
+			newMsg[c] = string(fixCommitMessage([]byte(body)))
 		}
 	}
 
diff --git a/git-codereview/reword_test.go b/git-codereview/reword_test.go
index c370b37..e4ee817 100644
--- a/git-codereview/reword_test.go
+++ b/git-codereview/reword_test.go
@@ -32,6 +32,7 @@
 	testPrintedStderr(t, "cannot rebase with uncommitted work")
 
 	os.Setenv("GIT_EDITOR", "sed -i.bak -e s/msg/MESSAGE/")
+	defer os.Unsetenv("GIT_EDITOR")
 
 	testMain(t, "reword", "MSG3", "MSG4")
 	testNoStdout(t)
@@ -63,6 +64,22 @@
 
 	out := trun(t, gt.client, "git", "log", "-n1")
 	if !strings.Contains(out, fakeName) {
-		t.Fatalf("reword lost author name (%s): %v\n", fakeName, out)
+		t.Fatalf("reword lost author name (%s):\n%s", fakeName, out)
+	}
+
+	write(t, gt.client+"/codereview.cfg", "issuerepo: my/issues\ngerrit: on\n", 0644)
+
+	os.Setenv("GIT_EDITOR", "sed -i.bak -e 's/Change-Id:.*/Fixes #12345/'")
+	testMain(t, "reword", "HEAD") // editing single commit message
+	out = trun(t, gt.client, "git", "log", "-n1", "HEAD")
+	if !strings.Contains(out, "Fixes my/issues#12345") || !strings.Contains(out, "Change-Id:") {
+		t.Fatalf("reword single commit did not run commit message hook:\n%s", out)
+	}
+
+	trun(t, gt.client, "git", "reset", "--hard", "MSG4")
+	testMain(t, "reword") // editing all commit messages
+	out = trun(t, gt.client, "git", "log", "-n1", "HEAD")
+	if !strings.Contains(out, "Fixes my/issues#12345") || !strings.Contains(out, "Change-Id:") {
+		t.Fatalf("reword multiple commits did not run commit message hook:\n%s", out)
 	}
 }