From e2ca53029e0c4c28c0796d3d55bfecf28c132d2e Mon Sep 17 00:00:00 2001 From: Adam Strzelecki Date: Mon, 7 Dec 2015 00:18:12 +0100 Subject: Render commit msg as header + verbatim description Most commit in Git are expected to follow standard of single header line, followed by description paragraphs, separated by empty line from previous block. Previously Gogs were treating everything as single header. Now we are trying to render only first line as header, but following lines (description chunks) as a verbatim. --- modules/template/template.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'modules/template/template.go') diff --git a/modules/template/template.go b/modules/template/template.go index d0d77b017b..9d63452dd3 100644 --- a/modules/template/template.go +++ b/modules/template/template.go @@ -183,15 +183,27 @@ func ReplaceLeft(s, old, new string) string { } // RenderCommitMessage renders commit message with XSS-safe and special links. -func RenderCommitMessage(msg, urlPrefix string, metas map[string]string) template.HTML { +func RenderCommitMessage(full bool, msg, urlPrefix string, metas map[string]string) template.HTML { cleanMsg := template.HTMLEscapeString(msg) fullMessage := string(base.RenderIssueIndexPattern([]byte(cleanMsg), urlPrefix, metas)) msgLines := strings.Split(strings.TrimSpace(fullMessage), "\n") - for i := range msgLines { - msgLines[i] = ReplaceLeft(msgLines[i], " ", " ") + numLines := len(msgLines) + if numLines == 0 { + return template.HTML("") + } else if !full { + return template.HTML(msgLines[0]) + } else if numLines == 1 || (numLines >= 2 && len(msgLines[1]) == 0) { + // First line is a header, standalone or followed by empty line + header := fmt.Sprintf("

%s

", msgLines[0]) + if numLines >= 2 { + fullMessage = header + fmt.Sprintf("\n
%s
", strings.Join(msgLines[2:], "\n")) + } else { + fullMessage = header + } + } else { + // Non-standard git message, there is no header line + fullMessage = fmt.Sprintf("

%s

", strings.Join(msgLines, "
")) } - - fullMessage = strings.Join(msgLines, "
") return template.HTML(fullMessage) } -- cgit v1.2.3