mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-31 21:28:11 +09:00 
			
		
		
		
	Always use an empty line to separate the commit message and trailer (#34512)
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: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
		| @@ -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 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user