@@ -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,34 +339,46 @@ 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
+ 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 {
349
+ return template .HTML ("" )
350
+ }
351
+
342
352
// we can safely assume that it will not return any error, since there
343
353
// shouldn't be any special HTML.
344
- fullMessage , err := markup .RenderCommitMessageSubject ([]byte (cleanMsg ), urlPrefix , urlDefault , metas )
354
+ renderedMessage , err := markup .RenderCommitMessageSubject ([]byte (template . HTMLEscapeString ( msgLine ) ), urlPrefix , urlDefault , metas )
345
355
if err != nil {
346
356
log .Error ("RenderCommitMessageSubject: %v" , err )
347
- return ""
348
- }
349
- msgLines := strings .Split (strings .TrimSpace (string (fullMessage )), "\n " )
350
- if len (msgLines ) == 0 {
351
357
return template .HTML ("" )
352
358
}
353
- return template .HTML (msgLines [ 0 ] )
359
+ return template .HTML (renderedMessage )
354
360
}
355
361
356
362
// RenderCommitBody extracts the body of a commit message without its title.
357
363
func RenderCommitBody (msg , urlPrefix string , metas map [string ]string ) template.HTML {
358
- cleanMsg := template .HTMLEscapeString (msg )
359
- fullMessage , err := markup .RenderCommitMessage ([]byte (cleanMsg ), urlPrefix , "" , metas )
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 {
373
+ return template .HTML ("" )
374
+ }
375
+
376
+ renderedMessage , err := markup .RenderCommitMessage ([]byte (template .HTMLEscapeString (msgLine )), urlPrefix , "" , metas )
360
377
if err != nil {
361
378
log .Error ("RenderCommitMessage: %v" , err )
362
379
return ""
363
380
}
364
- body := strings .Split (strings .TrimSpace (string (fullMessage )), "\n " )
365
- if len (body ) == 0 {
366
- return template .HTML ("" )
367
- }
368
- return template .HTML (strings .Join (body [1 :], "\n " ))
381
+ return template .HTML (renderedMessage )
369
382
}
370
383
371
384
// RenderNote renders the contents of a git-notes file as a commit message.
0 commit comments