mirror of
https://github.com/go-gitea/gitea.git
synced 2026-09-21 20:23:41 +09:00
fix
This commit is contained in:
+10
-12
@@ -12,7 +12,6 @@ import (
|
|||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"unicode/utf8"
|
|
||||||
|
|
||||||
"gitea.dev/models/db"
|
"gitea.dev/models/db"
|
||||||
git_model "gitea.dev/models/git"
|
git_model "gitea.dev/models/git"
|
||||||
@@ -831,11 +830,13 @@ func GetSquashMergeCommitMessages(ctx context.Context, pr *issues_model.PullRequ
|
|||||||
authors := make([]string, 0, len(commits))
|
authors := make([]string, 0, len(commits))
|
||||||
stringBuilder := strings.Builder{}
|
stringBuilder := strings.Builder{}
|
||||||
|
|
||||||
messageHasTrailers := false
|
// trailerBlockAtEnd tracks whether the message currently ends with a Git trailer block.
|
||||||
|
// When true, we skip the "---------" separator so Co-authored-by lines stay contiguous with it.
|
||||||
|
trailerBlockAtEnd := false
|
||||||
if !setting.Repository.PullRequest.PopulateSquashCommentWithCommitMessages {
|
if !setting.Repository.PullRequest.PopulateSquashCommentWithCommitMessages {
|
||||||
// use PR's title and description as squash commit message
|
// use PR's title and description as squash commit message
|
||||||
message := strings.TrimSpace(pr.Issue.Content)
|
message := strings.TrimSpace(pr.Issue.Content)
|
||||||
messageHasTrailers = commitMessageTrailersPattern.MatchString(message)
|
messageHasTrailers := commitMessageTrailersPattern.MatchString(message)
|
||||||
stringBuilder.WriteString(message)
|
stringBuilder.WriteString(message)
|
||||||
additionalCommitMessages := formatSquashMergeCommitMessages(commits[:max(0, len(commits)-1)])
|
additionalCommitMessages := formatSquashMergeCommitMessages(commits[:max(0, len(commits)-1)])
|
||||||
if additionalCommitMessages != "" {
|
if additionalCommitMessages != "" {
|
||||||
@@ -843,11 +844,13 @@ func GetSquashMergeCommitMessages(ctx context.Context, pr *issues_model.PullRequ
|
|||||||
stringBuilder.WriteString("\n\n")
|
stringBuilder.WriteString("\n\n")
|
||||||
}
|
}
|
||||||
stringBuilder.WriteString(additionalCommitMessages)
|
stringBuilder.WriteString(additionalCommitMessages)
|
||||||
|
// appended bullets push the PR-description trailers (if any) out of the trailer-block position
|
||||||
} else if stringBuilder.Len() > 0 {
|
} else if stringBuilder.Len() > 0 {
|
||||||
stringBuilder.WriteRune('\n')
|
stringBuilder.WriteRune('\n')
|
||||||
if !messageHasTrailers {
|
if !messageHasTrailers {
|
||||||
stringBuilder.WriteRune('\n')
|
stringBuilder.WriteRune('\n')
|
||||||
}
|
}
|
||||||
|
trailerBlockAtEnd = messageHasTrailers
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// use PR's commit messages as squash commit message
|
// use PR's commit messages as squash commit message
|
||||||
@@ -893,7 +896,7 @@ func GetSquashMergeCommitMessages(ctx context.Context, pr *issues_model.PullRequ
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if stringBuilder.Len() > 0 && len(authors) > 0 && !messageHasTrailers {
|
if stringBuilder.Len() > 0 && len(authors) > 0 && !trailerBlockAtEnd {
|
||||||
stringBuilder.WriteString("---------\n\n")
|
stringBuilder.WriteString("---------\n\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -921,14 +924,9 @@ func formatSquashMergeCommitMessages(commits []*git.Commit) string {
|
|||||||
// Maybe, ideally, we should indent those lines too.
|
// Maybe, ideally, we should indent those lines too.
|
||||||
_, _ = fmt.Fprintf(&stringBuilder, "* %s\n\n", msg)
|
_, _ = fmt.Fprintf(&stringBuilder, "* %s\n\n", msg)
|
||||||
if maxMsgSize > 0 && stringBuilder.Len() >= maxMsgSize {
|
if maxMsgSize > 0 && stringBuilder.Len() >= maxMsgSize {
|
||||||
tmp := stringBuilder.String()
|
// MessageUTF8 already guarantees valid UTF-8, but the byte-offset truncation
|
||||||
wasValidUtf8 := utf8.ValidString(tmp)
|
// can split a multi-byte rune, so trim any resulting invalid trailing bytes.
|
||||||
tmp = tmp[:maxMsgSize] + "..."
|
tmp := strings.ToValidUTF8(stringBuilder.String()[:maxMsgSize], "") + "..."
|
||||||
if wasValidUtf8 {
|
|
||||||
// If the message was valid UTF-8 before truncation, ensure it remains valid after truncation
|
|
||||||
// For non-utf8 messages, we can't do much about it, end users should use utf-8 as much as possible
|
|
||||||
tmp = strings.ToValidUTF8(tmp, "")
|
|
||||||
}
|
|
||||||
stringBuilder.Reset()
|
stringBuilder.Reset()
|
||||||
stringBuilder.WriteString(tmp)
|
stringBuilder.WriteString(tmp)
|
||||||
break
|
break
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import (
|
|||||||
"gitea.dev/modules/git"
|
"gitea.dev/modules/git"
|
||||||
"gitea.dev/modules/gitrepo"
|
"gitea.dev/modules/gitrepo"
|
||||||
"gitea.dev/modules/setting"
|
"gitea.dev/modules/setting"
|
||||||
|
"gitea.dev/modules/test"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
@@ -41,10 +42,7 @@ func TestPullRequest_FormatSquashMergeCommitMessages(t *testing.T) {
|
|||||||
oldest := &git.Commit{CommitMessage: git.CommitMessage{MessageRaw: "commit msg 1"}}
|
oldest := &git.Commit{CommitMessage: git.CommitMessage{MessageRaw: "commit msg 1"}}
|
||||||
newest := &git.Commit{CommitMessage: git.CommitMessage{MessageRaw: "commit msg 2\n\nCommit description."}}
|
newest := &git.Commit{CommitMessage: git.CommitMessage{MessageRaw: "commit msg 2\n\nCommit description."}}
|
||||||
|
|
||||||
defer func(old int) { setting.Repository.PullRequest.DefaultMergeMessageSize = old }(
|
defer test.MockVariableValue(&setting.Repository.PullRequest.DefaultMergeMessageSize, 0)()
|
||||||
setting.Repository.PullRequest.DefaultMergeMessageSize,
|
|
||||||
)
|
|
||||||
setting.Repository.PullRequest.DefaultMergeMessageSize = 0
|
|
||||||
|
|
||||||
// all commits
|
// all commits
|
||||||
assert.Equal(t, "* commit msg 1\n\n* commit msg 2\n\nCommit description.\n\n",
|
assert.Equal(t, "* commit msg 1\n\n* commit msg 2\n\nCommit description.\n\n",
|
||||||
|
|||||||
Reference in New Issue
Block a user