[gitea] Always use an empty line to separate the commit message and trailer (#8041)

This is a port of a gitea PR: https://github.com/go-gitea/gitea/pull/34512.

I have added some copy-editing commits on top for cleanliness.
I haven't tested the changes manually and only relied on the existing automated test.

## Checklist
### Tests

- I added test coverage for Go changes...
  - [x] in their respective `*_test.go` for unit tests.
  - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server.

### Documentation

- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it.

### Release notes

- [x] I do not want this change to show in the release notes.
- [ ] I want the title to show in the release notes with a link to this pull request.
- [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title.

Co-authored-by: Jim Lin <jim@andestech.com>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8041
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Antonin Delpeuch <antonin@delpeuch.eu>
Co-committed-by: Antonin Delpeuch <antonin@delpeuch.eu>
This commit is contained in:
Antonin Delpeuch 2025-06-02 22:10:59 +02:00 committed by Earl Warren
parent fc35915a28
commit 0ed7237b12
3 changed files with 63 additions and 5 deletions

View file

@ -14,6 +14,7 @@ import (
"regexp"
"strconv"
"strings"
"unicode"
"forgejo.org/models"
"forgejo.org/models/db"
@ -169,6 +170,41 @@ func GetDefaultMergeMessage(ctx context.Context, baseGitRepo *git.Repository, pr
return getMergeMessage(ctx, baseGitRepo, pr, mergeStyle, nil)
}
func AddCommitMessageTrailer(message, tailerKey, tailerValue string) string {
trailerLine := tailerKey + ": " + tailerValue
message = strings.ReplaceAll(message, "\r\n", "\n")
message = strings.ReplaceAll(message, "\r", "\n")
if strings.Contains(message, "\n"+trailerLine+"\n") || strings.HasSuffix(message, "\n"+trailerLine) {
return message
}
if !strings.HasSuffix(message, "\n") {
message += "\n"
}
lastNewLine := strings.LastIndexByte(message[:len(message)-1], '\n')
keyEnd := -1
if lastNewLine != -1 {
keyEnd = strings.IndexByte(message[lastNewLine:], ':')
if keyEnd != -1 {
keyEnd += lastNewLine
}
}
var lastLineKey string
if lastNewLine != -1 && keyEnd != -1 {
lastLineKey = message[lastNewLine+1 : keyEnd]
}
isLikelyTrailerLine := lastLineKey != "" && unicode.IsUpper(rune(lastLineKey[0])) && strings.Contains(message, "-")
for i := 0; isLikelyTrailerLine && i < len(lastLineKey); i++ {
r := rune(lastLineKey[i])
isLikelyTrailerLine = unicode.IsLetter(r) || unicode.IsDigit(r) || r == '-'
}
if !strings.HasSuffix(message, "\n\n") && !isLikelyTrailerLine {
message += "\n"
}
return message + trailerLine
}
// Merge merges pull request to base repository.
// Caller should check PR is ready to be merged (review and status checks)
func Merge(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.User, baseGitRepo *git.Repository, mergeStyle repo_model.MergeStyle, expectedHeadCommitID, message string, wasAutoMerged bool) error {