@@ -19,6 +19,7 @@ import (
1919 "runtime"
2020 "strings"
2121 "time"
22+ "unicode"
2223
2324 "code.gitea.io/gitea/models"
2425 "code.gitea.io/gitea/modules/base"
@@ -331,34 +332,46 @@ func RenderCommitMessageLink(msg, urlPrefix, urlDefault string, metas map[string
331332// RenderCommitMessageLinkSubject renders commit message as a XXS-safe link to
332333// the provided default url, handling for special links without email to links.
333334func 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+
335345 // we can safely assume that it will not return any error, since there
336346 // 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 )
338348 if err != nil {
339349 log .Error ("RenderCommitMessageSubject: %v" , err )
340- return ""
341- }
342- msgLines := strings .Split (strings .TrimSpace (string (fullMessage )), "\n " )
343- if len (msgLines ) == 0 {
344350 return template .HTML ("" )
345351 }
346- return template .HTML (msgLines [ 0 ] )
352+ return template .HTML (renderedMessage )
347353}
348354
349355// RenderCommitBody extracts the body of a commit message without its title.
350356func 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 )
353370 if err != nil {
354371 log .Error ("RenderCommitMessage: %v" , err )
355372 return ""
356373 }
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 )
362375}
363376
364377// RenderNote renders the contents of a git-notes file as a commit message.
0 commit comments