@@ -19,6 +19,7 @@ import (
19
19
"runtime"
20
20
"strings"
21
21
"time"
22
+ "unicode"
22
23
23
24
"code.gitea.io/gitea/models"
24
25
"code.gitea.io/gitea/modules/base"
@@ -338,16 +339,19 @@ func RenderCommitMessageLink(msg, urlPrefix, urlDefault string, metas map[string
338
339
// RenderCommitMessageLinkSubject renders commit message as a XXS-safe link to
339
340
// the provided default url, handling for special links without email to links.
340
341
func RenderCommitMessageLinkSubject (msg , urlPrefix , urlDefault string , metas map [string ]string ) template.HTML {
341
- cleanMsg := template .HTMLEscapeString (msg )
342
-
343
- msgLines := strings .Split (strings .TrimSpace (string (cleanMsg )), "\n " )
344
- if len (msgLines [0 ]) == 0 {
342
+ msgLine := strings .TrimLeftFunc (msg , unicode .IsSpace )
343
+ lineEnd := strings .IndexByte (msgLine , '\n' )
344
+ if lineEnd > 0 {
345
+ msgLine = msgLine [:lineEnd ]
346
+ }
347
+ msgLine = strings .TrimRightFunc (msgLine , unicode .IsSpace )
348
+ if len (msgLine ) == 0 {
345
349
return template .HTML ("" )
346
350
}
347
351
348
352
// we can safely assume that it will not return any error, since there
349
353
// shouldn't be any special HTML.
350
- renderedMessage , err := markup .RenderCommitMessageSubject ([]byte (msgLines [ 0 ] ), urlPrefix , urlDefault , metas )
354
+ renderedMessage , err := markup .RenderCommitMessageSubject ([]byte (template . HTMLEscapeString ( msgLine ) ), urlPrefix , urlDefault , metas )
351
355
if err != nil {
352
356
log .Error ("RenderCommitMessageSubject: %v" , err )
353
357
return template .HTML ("" )
@@ -357,14 +361,19 @@ func RenderCommitMessageLinkSubject(msg, urlPrefix, urlDefault string, metas map
357
361
358
362
// RenderCommitBody extracts the body of a commit message without its title.
359
363
func RenderCommitBody (msg , urlPrefix string , metas map [string ]string ) template.HTML {
360
- cleanMsg := template .HTMLEscapeString (msg )
361
-
362
- msgLines := strings .Split (strings .TrimSpace (string (cleanMsg )), "\n " )
363
- if len (msgLines ) <= 1 {
364
+ msgLine := strings .TrimRightFunc (msg , unicode .IsSpace )
365
+ lineEnd := strings .IndexByte (msgLine , '\n' )
366
+ if lineEnd > 0 {
367
+ msgLine = msgLine [lineEnd + 1 :]
368
+ } else {
369
+ return template .HTML ("" )
370
+ }
371
+ msgLine = strings .TrimLeftFunc (msgLine , unicode .IsSpace )
372
+ if len (msgLine ) == 0 {
364
373
return template .HTML ("" )
365
374
}
366
375
367
- renderedMessage , err := markup .RenderCommitMessage ([]byte (strings . Join ( msgLines [ 1 :], " \n " )), urlPrefix , "" , metas )
376
+ renderedMessage , err := markup .RenderCommitMessage ([]byte (template . HTMLEscapeString ( msgLine )), urlPrefix , "" , metas )
368
377
if err != nil {
369
378
log .Error ("RenderCommitMessage: %v" , err )
370
379
return ""
0 commit comments