Skip to content

Commit ffe0894

Browse files
GiteaBotwolfogre
andauthored
Fix missing commit message body when the message has leading newlines (#25418) (#25422)
Backport #25418 by @wolfogre Commit with `echo "\nmessage after a blank line\nsecond line of the message" | git commit --cleanup=verbatim -F -` and push. <img width="1139" alt="image" src="https://github.com/go-gitea/gitea/assets/9418365/f9a2c28c-e307-4c78-9e31-3d3ace7b9274"> Co-authored-by: Jason Song <[email protected]>
1 parent 8302b95 commit ffe0894

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

modules/templates/util_render.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,16 @@ func RenderCommitMessageLinkSubject(ctx context.Context, msg, urlPrefix, urlDefa
8181

8282
// RenderCommitBody extracts the body of a commit message without its title.
8383
func RenderCommitBody(ctx context.Context, msg, urlPrefix string, metas map[string]string) template.HTML {
84-
msgLine := strings.TrimRightFunc(msg, unicode.IsSpace)
84+
msgLine := strings.TrimSpace(msg)
8585
lineEnd := strings.IndexByte(msgLine, '\n')
8686
if lineEnd > 0 {
8787
msgLine = msgLine[lineEnd+1:]
8888
} else {
89-
return template.HTML("")
89+
return ""
9090
}
9191
msgLine = strings.TrimLeftFunc(msgLine, unicode.IsSpace)
9292
if len(msgLine) == 0 {
93-
return template.HTML("")
93+
return ""
9494
}
9595

9696
renderedMessage, err := markup.RenderCommitMessage(&markup.RenderContext{

modules/templates/util_render_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package templates
5+
6+
import (
7+
"context"
8+
"html/template"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestRenderCommitBody(t *testing.T) {
15+
type args struct {
16+
ctx context.Context
17+
msg string
18+
urlPrefix string
19+
metas map[string]string
20+
}
21+
tests := []struct {
22+
name string
23+
args args
24+
want template.HTML
25+
}{
26+
{
27+
name: "multiple lines",
28+
args: args{
29+
ctx: context.Background(),
30+
msg: "first line\nsecond line",
31+
},
32+
want: "second line",
33+
},
34+
{
35+
name: "multiple lines with leading newlines",
36+
args: args{
37+
ctx: context.Background(),
38+
msg: "\n\n\n\nfirst line\nsecond line",
39+
},
40+
want: "second line",
41+
},
42+
{
43+
name: "multiple lines with trailing newlines",
44+
args: args{
45+
ctx: context.Background(),
46+
msg: "first line\nsecond line\n\n\n",
47+
},
48+
want: "second line",
49+
},
50+
}
51+
for _, tt := range tests {
52+
t.Run(tt.name, func(t *testing.T) {
53+
assert.Equalf(t, tt.want, RenderCommitBody(tt.args.ctx, tt.args.msg, tt.args.urlPrefix, tt.args.metas), "RenderCommitBody(%v, %v, %v, %v)", tt.args.ctx, tt.args.msg, tt.args.urlPrefix, tt.args.metas)
54+
})
55+
}
56+
}

0 commit comments

Comments
 (0)