diff options
author | Adam Strzelecki <ono@java.pl> | 2015-12-07 00:18:12 +0100 |
---|---|---|
committer | Adam Strzelecki <ono@java.pl> | 2015-12-07 01:50:45 +0100 |
commit | e2ca53029e0c4c28c0796d3d55bfecf28c132d2e (patch) | |
tree | f3815745e988911a34edb7524b69048ee4f9c17c /modules/template/template.go | |
parent | b5f6206a659eaa76382774675f2e3f2664d0f501 (diff) | |
download | gitea-e2ca53029e0c4c28c0796d3d55bfecf28c132d2e.tar.gz gitea-e2ca53029e0c4c28c0796d3d55bfecf28c132d2e.zip |
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.
Diffstat (limited to 'modules/template/template.go')
-rw-r--r-- | modules/template/template.go | 22 |
1 files changed, 17 insertions, 5 deletions
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("<h3>%s</h3>", msgLines[0]) + if numLines >= 2 { + fullMessage = header + fmt.Sprintf("\n<pre>%s</pre>", strings.Join(msgLines[2:], "\n")) + } else { + fullMessage = header + } + } else { + // Non-standard git message, there is no header line + fullMessage = fmt.Sprintf("<h4>%s</h4>", strings.Join(msgLines, "<br>")) } - - fullMessage = strings.Join(msgLines, "<br>") return template.HTML(fullMessage) } |