diff options
author | Giteabot <teabot@gitea.io> | 2023-06-21 07:55:08 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-21 13:55:08 +0200 |
commit | ffe089432f8dd8ab4e37bc5765ad07196608aba4 (patch) | |
tree | d2c86ebba1d5723036f6a8e1ba0d2ffb4a2c2230 | |
parent | 8302b95d6bfdbc40083a72e5d1b727fcb1e00940 (diff) | |
download | gitea-ffe089432f8dd8ab4e37bc5765ad07196608aba4.tar.gz gitea-ffe089432f8dd8ab4e37bc5765ad07196608aba4.zip |
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 <i@wolfogre.com>
-rw-r--r-- | modules/templates/util_render.go | 6 | ||||
-rw-r--r-- | modules/templates/util_render_test.go | 56 |
2 files changed, 59 insertions, 3 deletions
diff --git a/modules/templates/util_render.go b/modules/templates/util_render.go index a26c0531f8..d23103ce1b 100644 --- a/modules/templates/util_render.go +++ b/modules/templates/util_render.go @@ -81,16 +81,16 @@ func RenderCommitMessageLinkSubject(ctx context.Context, msg, urlPrefix, urlDefa // 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 { - msgLine := strings.TrimRightFunc(msg, unicode.IsSpace) + msgLine := strings.TrimSpace(msg) lineEnd := strings.IndexByte(msgLine, '\n') if lineEnd > 0 { msgLine = msgLine[lineEnd+1:] } else { - return template.HTML("") + return "" } msgLine = strings.TrimLeftFunc(msgLine, unicode.IsSpace) if len(msgLine) == 0 { - return template.HTML("") + return "" } renderedMessage, err := markup.RenderCommitMessage(&markup.RenderContext{ diff --git a/modules/templates/util_render_test.go b/modules/templates/util_render_test.go new file mode 100644 index 0000000000..29d3ed3a56 --- /dev/null +++ b/modules/templates/util_render_test.go @@ -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) + }) + } +} |