@@ -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"
@@ -331,34 +332,46 @@ func RenderCommitMessageLink(msg, urlPrefix, urlDefault string, metas map[string
331
332
// RenderCommitMessageLinkSubject renders commit message as a XXS-safe link to
332
333
// the provided default url, handling for special links without email to links.
333
334
func RenderCommitMessageLinkSubject (msg , urlPrefix , urlDefault string , metas map [string ]string ) template.HTML {
334
- cleanMsg := template .HTMLEscapeString (msg )
335
+ msgLine := strings .TrimLeftFunc (msg , unicode .IsSpace )
336
+ lineEnd := strings .IndexByte (msgLine , '\n' )
337
+ if lineEnd > 0 {
338
+ msgLine = msgLine [:lineEnd ]
339
+ }
340
+ msgLine = strings .TrimRightFunc (msgLine , unicode .IsSpace )
341
+ if len (msgLine ) == 0 {
342
+ return template .HTML ("" )
343
+ }
344
+
335
345
// we can safely assume that it will not return any error, since there
336
346
// shouldn't be any special HTML.
337
- fullMessage , err := markup .RenderCommitMessageSubject ([]byte (cleanMsg ), urlPrefix , urlDefault , metas )
347
+ renderedMessage , err := markup .RenderCommitMessageSubject ([]byte (template . HTMLEscapeString ( msgLine ) ), urlPrefix , urlDefault , metas )
338
348
if err != nil {
339
349
log .Error ("RenderCommitMessageSubject: %v" , err )
340
- return ""
341
- }
342
- msgLines := strings .Split (strings .TrimSpace (string (fullMessage )), "\n " )
343
- if len (msgLines ) == 0 {
344
350
return template .HTML ("" )
345
351
}
346
- return template .HTML (msgLines [ 0 ] )
352
+ return template .HTML (renderedMessage )
347
353
}
348
354
349
355
// RenderCommitBody extracts the body of a commit message without its title.
350
356
func RenderCommitBody (msg , urlPrefix string , metas map [string ]string ) template.HTML {
351
- cleanMsg := template .HTMLEscapeString (msg )
352
- fullMessage , err := markup .RenderCommitMessage ([]byte (cleanMsg ), urlPrefix , "" , metas )
357
+ msgLine := strings .TrimRightFunc (msg , unicode .IsSpace )
358
+ lineEnd := strings .IndexByte (msgLine , '\n' )
359
+ if lineEnd > 0 {
360
+ msgLine = msgLine [lineEnd + 1 :]
361
+ } else {
362
+ return template .HTML ("" )
363
+ }
364
+ msgLine = strings .TrimLeftFunc (msgLine , unicode .IsSpace )
365
+ if len (msgLine ) == 0 {
366
+ return template .HTML ("" )
367
+ }
368
+
369
+ renderedMessage , err := markup .RenderCommitMessage ([]byte (template .HTMLEscapeString (msgLine )), urlPrefix , "" , metas )
353
370
if err != nil {
354
371
log .Error ("RenderCommitMessage: %v" , err )
355
372
return ""
356
373
}
357
- body := strings .Split (strings .TrimSpace (string (fullMessage )), "\n " )
358
- if len (body ) == 0 {
359
- return template .HTML ("" )
360
- }
361
- return template .HTML (strings .Join (body [1 :], "\n " ))
374
+ return template .HTML (renderedMessage )
362
375
}
363
376
364
377
// RenderNote renders the contents of a git-notes file as a commit message.
0 commit comments