1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-06 10:37:20 +00:00

Always use an empty line to separate the commit message and trailer (#34512) (#34578)

Backport #34512 by tclin914

If the message from form.MergeMessageField is empty, we will miss a "\n"
between the title and the "Co-authored-by:" line. The title and message
should have a blank line between of them.

Co-authored-by: Jim Lin <jim@andestech.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
Giteabot
2025-06-02 15:59:10 +08:00
committed by GitHub
parent e9481e1da3
commit d3317ebabe
3 changed files with 63 additions and 5 deletions

View File

@ -13,6 +13,7 @@ import (
"regexp"
"strconv"
"strings"
"unicode"
"code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
@ -161,6 +162,41 @@ func GetDefaultMergeMessage(ctx context.Context, baseGitRepo *git.Repository, pr
return getMergeMessage(ctx, baseGitRepo, pr, mergeStyle, nil)
}
func AddCommitMessageTailer(message, tailerKey, tailerValue string) string {
tailerLine := tailerKey + ": " + tailerValue
message = strings.ReplaceAll(message, "\r\n", "\n")
message = strings.ReplaceAll(message, "\r", "\n")
if strings.Contains(message, "\n"+tailerLine+"\n") || strings.HasSuffix(message, "\n"+tailerLine) {
return message
}
if !strings.HasSuffix(message, "\n") {
message += "\n"
}
pos1 := strings.LastIndexByte(message[:len(message)-1], '\n')
pos2 := -1
if pos1 != -1 {
pos2 = strings.IndexByte(message[pos1:], ':')
if pos2 != -1 {
pos2 += pos1
}
}
var lastLineKey string
if pos1 != -1 && pos2 != -1 {
lastLineKey = message[pos1+1 : pos2]
}
isLikelyTailerLine := lastLineKey != "" && unicode.IsUpper(rune(lastLineKey[0])) && strings.Contains(message, "-")
for i := 0; isLikelyTailerLine && i < len(lastLineKey); i++ {
r := rune(lastLineKey[i])
isLikelyTailerLine = unicode.IsLetter(r) || unicode.IsDigit(r) || r == '-'
}
if !strings.HasSuffix(message, "\n\n") && !isLikelyTailerLine {
message += "\n"
}
return message + tailerLine
}
// ErrInvalidMergeStyle represents an error if merging with disabled merge strategy
type ErrInvalidMergeStyle struct {
ID int64