Skip to content

Commit

Permalink
git-codereview: reword: apply commit message hook to edits
Browse files Browse the repository at this point in the history
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 <[email protected]>
Run-TryBot: Russ Cox <[email protected]>
TryBot-Result: Go Bot <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
  • Loading branch information
rsc committed Sep 21, 2021
1 parent 2e4fd9a commit 39ade5b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
24 changes: 17 additions & 7 deletions git-codereview/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,22 @@ func hookCommitMsg(args []string) {
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.
Expand Down Expand Up @@ -226,12 +241,7 @@ func hookCommitMsg(args []string) {
}
}

// 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.
Expand Down
4 changes: 2 additions & 2 deletions git-codereview/reword.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func cmdReword(args []string) {
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 {
Expand Down Expand Up @@ -166,7 +166,7 @@ func cmdReword(args []string) {
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)))
}
}

Expand Down
19 changes: 18 additions & 1 deletion git-codereview/reword_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestReword(t *testing.T) {
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)
Expand Down Expand Up @@ -63,6 +64,22 @@ func TestReword(t *testing.T) {

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)
}
}

0 comments on commit 39ade5b

Please sign in to comment.