Fix missing commit message body when the message has leading newlines (#25418)
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">
This commit is contained in:
parent
6a8ebaf22b
commit
59d2189875
2 changed files with 59 additions and 3 deletions
|
@ -81,16 +81,16 @@ func RenderCommitMessageLinkSubject(ctx context.Context, msg, urlPrefix, urlDefa
|
||||||
|
|
||||||
// RenderCommitBody extracts the body of a commit message without its title.
|
// RenderCommitBody extracts the body of a commit message without its title.
|
||||||
func RenderCommitBody(ctx context.Context, msg, urlPrefix string, metas map[string]string) template.HTML {
|
func RenderCommitBody(ctx context.Context, msg, urlPrefix string, metas map[string]string) template.HTML {
|
||||||
msgLine := strings.TrimRightFunc(msg, unicode.IsSpace)
|
msgLine := strings.TrimSpace(msg)
|
||||||
lineEnd := strings.IndexByte(msgLine, '\n')
|
lineEnd := strings.IndexByte(msgLine, '\n')
|
||||||
if lineEnd > 0 {
|
if lineEnd > 0 {
|
||||||
msgLine = msgLine[lineEnd+1:]
|
msgLine = msgLine[lineEnd+1:]
|
||||||
} else {
|
} else {
|
||||||
return template.HTML("")
|
return ""
|
||||||
}
|
}
|
||||||
msgLine = strings.TrimLeftFunc(msgLine, unicode.IsSpace)
|
msgLine = strings.TrimLeftFunc(msgLine, unicode.IsSpace)
|
||||||
if len(msgLine) == 0 {
|
if len(msgLine) == 0 {
|
||||||
return template.HTML("")
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
renderedMessage, err := markup.RenderCommitMessage(&markup.RenderContext{
|
renderedMessage, err := markup.RenderCommitMessage(&markup.RenderContext{
|
||||||
|
|
56
modules/templates/util_render_test.go
Normal file
56
modules/templates/util_render_test.go
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package templates
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"html/template"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRenderCommitBody(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
ctx context.Context
|
||||||
|
msg string
|
||||||
|
urlPrefix string
|
||||||
|
metas map[string]string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want template.HTML
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "multiple lines",
|
||||||
|
args: args{
|
||||||
|
ctx: context.Background(),
|
||||||
|
msg: "first line\nsecond line",
|
||||||
|
},
|
||||||
|
want: "second line",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "multiple lines with leading newlines",
|
||||||
|
args: args{
|
||||||
|
ctx: context.Background(),
|
||||||
|
msg: "\n\n\n\nfirst line\nsecond line",
|
||||||
|
},
|
||||||
|
want: "second line",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "multiple lines with trailing newlines",
|
||||||
|
args: args{
|
||||||
|
ctx: context.Background(),
|
||||||
|
msg: "first line\nsecond line\n\n\n",
|
||||||
|
},
|
||||||
|
want: "second line",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue