Skip to content

Commit 6364056

Browse files
committed
improve
1 parent 26e1f78 commit 6364056

File tree

3 files changed

+47
-30
lines changed

3 files changed

+47
-30
lines changed

services/pull/merge.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"regexp"
1414
"strconv"
1515
"strings"
16+
"unicode"
1617

1718
"code.gitea.io/gitea/models/db"
1819
git_model "code.gitea.io/gitea/models/git"
@@ -161,6 +162,41 @@ func GetDefaultMergeMessage(ctx context.Context, baseGitRepo *git.Repository, pr
161162
return getMergeMessage(ctx, baseGitRepo, pr, mergeStyle, nil)
162163
}
163164

165+
func AddCommitMessageTailer(message, tailerKey, tailerValue string) string {
166+
tailerLine := tailerKey + ": " + tailerValue
167+
message = strings.ReplaceAll(message, "\r\n", "\n")
168+
message = strings.ReplaceAll(message, "\r", "\n")
169+
if strings.Contains(message, "\n"+tailerLine+"\n") || strings.HasSuffix(message, "\n"+tailerLine) {
170+
return message
171+
}
172+
173+
if !strings.HasSuffix(message, "\n") {
174+
message += "\n"
175+
}
176+
pos1 := strings.LastIndexByte(message[:len(message)-1], '\n')
177+
pos2 := -1
178+
if pos1 != -1 {
179+
pos2 = strings.IndexByte(message[pos1:], ':')
180+
if pos2 != -1 {
181+
pos2 += pos1
182+
}
183+
}
184+
var lastLineKey string
185+
if pos1 != -1 && pos2 != -1 {
186+
lastLineKey = message[pos1+1 : pos2]
187+
}
188+
189+
isLikelyTailerLine := lastLineKey != "" && unicode.IsUpper(rune(lastLineKey[0]))
190+
for i := 0; isLikelyTailerLine && i < len(lastLineKey); i++ {
191+
r := rune(lastLineKey[i])
192+
isLikelyTailerLine = unicode.IsLetter(r) || unicode.IsDigit(r) || r == '-'
193+
}
194+
if !strings.HasSuffix(message, "\n\n") && !isLikelyTailerLine {
195+
message += "\n"
196+
}
197+
return message + tailerLine
198+
}
199+
164200
// ErrInvalidMergeStyle represents an error if merging with disabled merge strategy
165201
type ErrInvalidMergeStyle struct {
166202
ID int64

services/pull/merge_squash.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ package pull
55

66
import (
77
"fmt"
8-
"strings"
9-
"unicode"
108

119
repo_model "code.gitea.io/gitea/models/repo"
1210
user_model "code.gitea.io/gitea/models/user"
@@ -52,34 +50,6 @@ func getAuthorSignatureSquash(ctx *mergeContext) (*git.Signature, error) {
5250
return ctx.pr.Issue.Poster.NewGitSig(), nil
5351
}
5452

55-
func AddCommitMessageTailer(message, tailerKey, tailerValue string) string {
56-
tailerLine := tailerKey + ": " + tailerValue
57-
if strings.Contains(message, "\n"+tailerLine+"\n") || strings.HasSuffix(message, "\n"+tailerLine) {
58-
return message
59-
}
60-
61-
if !strings.HasSuffix(message, "\n") {
62-
message += "\n"
63-
}
64-
pos1 := strings.LastIndexByte(message[:len(message)-1], '\n')
65-
pos2 := -1
66-
if pos1 != -1 {
67-
pos2 = strings.IndexByte(message[pos1:], ':')
68-
if pos2 != -1 {
69-
pos2 += pos1
70-
}
71-
}
72-
var lastLine string
73-
if pos1 != -1 && pos2 != -1 {
74-
lastLine = message[pos1+1 : pos2+1]
75-
}
76-
lastLineIsTailerLine := lastLine != "" && unicode.IsUpper(rune(lastLine[0])) && strings.Contains(lastLine, "-")
77-
if !strings.HasSuffix(message, "\n\n") && !lastLineIsTailerLine {
78-
message += "\n"
79-
}
80-
return message + tailerLine
81-
}
82-
8353
// doMergeStyleSquash squashes the tracking branch on the current HEAD (=base)
8454
func doMergeStyleSquash(ctx *mergeContext, message string) error {
8555
sig, err := getAuthorSignatureSquash(ctx)

services/pull/merge_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,24 @@ func Test_expandDefaultMergeMessage(t *testing.T) {
6767
}
6868

6969
func TestAddCommitMessageTailer(t *testing.T) {
70+
// add tailer for empty message
7071
assert.Equal(t, "\n\nTest-tailer: TestValue", AddCommitMessageTailer("", "Test-tailer", "TestValue"))
72+
73+
// add tailer for message without newlines
7174
assert.Equal(t, "title\n\nTest-tailer: TestValue", AddCommitMessageTailer("title", "Test-tailer", "TestValue"))
75+
assert.Equal(t, "title\n\nnot tailer: xxx\n\nTest-tailer: TestValue", AddCommitMessageTailer("title\n\nnot tailer: xxx", "Test-tailer", "TestValue"))
76+
77+
// add tailer for message with one EOL
7278
assert.Equal(t, "title\n\nTest-tailer: TestValue", AddCommitMessageTailer("title\n", "Test-tailer", "TestValue"))
79+
80+
// add tailer for message with two EOLs
7381
assert.Equal(t, "title\n\nTest-tailer: TestValue", AddCommitMessageTailer("title\n\n", "Test-tailer", "TestValue"))
82+
83+
// add tailer for message with existing tailer (won't duplicate)
7484
assert.Equal(t, "title\n\nTest-tailer: TestValue", AddCommitMessageTailer("title\n\nTest-tailer: TestValue", "Test-tailer", "TestValue"))
7585
assert.Equal(t, "title\n\nTest-tailer: TestValue\n", AddCommitMessageTailer("title\n\nTest-tailer: TestValue\n", "Test-tailer", "TestValue"))
7686

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

0 commit comments

Comments
 (0)