Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions services/pull/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"regexp"
"strconv"
"strings"
"unicode"

"code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
Expand Down Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions services/pull/merge_squash.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package pull

import (
"fmt"
"strings"

repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
Expand Down Expand Up @@ -66,10 +65,8 @@ func doMergeStyleSquash(ctx *mergeContext, message string) error {

if setting.Repository.PullRequest.AddCoCommitterTrailers && ctx.committer.String() != sig.String() {
// add trailer
if !strings.Contains(message, "Co-authored-by: "+sig.String()) {
message += "\nCo-authored-by: " + sig.String()
}
message += fmt.Sprintf("\nCo-committed-by: %s\n", sig.String())
message = AddCommitMessageTailer(message, "Co-authored-by", sig.String())
message = AddCommitMessageTailer(message, "Co-committed-by", sig.String()) // FIXME: this one should be removed, it is not really used or widely used
}
cmdCommit := git.NewCommand("commit").
AddOptionFormat("--author='%s <%s>'", sig.Name, sig.Email).
Expand Down
25 changes: 25 additions & 0 deletions services/pull/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,28 @@ func Test_expandDefaultMergeMessage(t *testing.T) {
})
}
}

func TestAddCommitMessageTailer(t *testing.T) {
// add tailer for empty message
assert.Equal(t, "\n\nTest-tailer: TestValue", AddCommitMessageTailer("", "Test-tailer", "TestValue"))

// add tailer for message without newlines
assert.Equal(t, "title\n\nTest-tailer: TestValue", AddCommitMessageTailer("title", "Test-tailer", "TestValue"))
assert.Equal(t, "title\n\nNot tailer: xxx\n\nTest-tailer: TestValue", AddCommitMessageTailer("title\n\nNot tailer: xxx", "Test-tailer", "TestValue"))
assert.Equal(t, "title\n\nNotTailer: xxx\n\nTest-tailer: TestValue", AddCommitMessageTailer("title\n\nNotTailer: xxx", "Test-tailer", "TestValue"))
assert.Equal(t, "title\n\nnot-tailer: xxx\n\nTest-tailer: TestValue", AddCommitMessageTailer("title\n\nnot-tailer: xxx", "Test-tailer", "TestValue"))

// add tailer for message with one EOL
assert.Equal(t, "title\n\nTest-tailer: TestValue", AddCommitMessageTailer("title\n", "Test-tailer", "TestValue"))

// add tailer for message with two EOLs
assert.Equal(t, "title\n\nTest-tailer: TestValue", AddCommitMessageTailer("title\n\n", "Test-tailer", "TestValue"))

// add tailer for message with existing tailer (won't duplicate)
assert.Equal(t, "title\n\nTest-tailer: TestValue", AddCommitMessageTailer("title\n\nTest-tailer: TestValue", "Test-tailer", "TestValue"))
assert.Equal(t, "title\n\nTest-tailer: TestValue\n", AddCommitMessageTailer("title\n\nTest-tailer: TestValue\n", "Test-tailer", "TestValue"))

// add tailer for message with existing tailer and different value (will append)
assert.Equal(t, "title\n\nTest-tailer: v1\nTest-tailer: v2", AddCommitMessageTailer("title\n\nTest-tailer: v1", "Test-tailer", "v2"))
assert.Equal(t, "title\n\nTest-tailer: v1\nTest-tailer: v2", AddCommitMessageTailer("title\n\nTest-tailer: v1\n", "Test-tailer", "v2"))
}