Skip to content

Commit 35742d4

Browse files
authored
Reintroduce squash merge default comment as a config setting (#16134)
* Reinstate most of commit 09304db * Move the behaviour behind a config setting * Also fix the initial #12365
1 parent 889dea8 commit 35742d4

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

docs/content/doc/advanced/config-cheat-sheet.en-us.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,11 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
9494
- `REOPEN_KEYWORDS`: **reopen**, **reopens**, **reopened**: List of keywords used in Pull Request comments to automatically reopen
9595
a related issue
9696
- `DEFAULT_MERGE_MESSAGE_COMMITS_LIMIT`: **50**: In the default merge message for squash commits include at most this many commits. Set to `-1` to include all commits
97-
- `DEFAULT_MERGE_MESSAGE_SIZE`: **5120**: In the default merge message for squash commits limit the size of the commit messages. Set to `-1` to have no limit.
97+
- `DEFAULT_MERGE_MESSAGE_SIZE`: **5120**: In the default merge message for squash commits limit the size of the commit messages. Set to `-1` to have no limit. Only used if `POPULATE_SQUASH_COMMENT_WITH_COMMIT_MESSAGES` is `true`.
9898
- `DEFAULT_MERGE_MESSAGE_ALL_AUTHORS`: **false**: In the default merge message for squash commits walk all commits to include all authors in the Co-authored-by otherwise just use those in the limited list
9999
- `DEFAULT_MERGE_MESSAGE_MAX_APPROVERS`: **10**: In default merge messages limit the number of approvers listed as `Reviewed-by:`. Set to `-1` to include all.
100100
- `DEFAULT_MERGE_MESSAGE_OFFICIAL_APPROVERS_ONLY`: **true**: In default merge messages only include approvers who are officially allowed to review.
101+
- `POPULATE_SQUASH_COMMENT_WITH_COMMIT_MESSAGES`: **false**: In default squash-merge messages include the commit message of all commits comprising the pull request.
101102

102103
### Repository - Issue (`repository.issue`)
103104

modules/setting/repository.go

+3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ var (
7878
DefaultMergeMessageAllAuthors bool
7979
DefaultMergeMessageMaxApprovers int
8080
DefaultMergeMessageOfficialApproversOnly bool
81+
PopulateSquashCommentWithCommitMessages bool
8182
} `ini:"repository.pull-request"`
8283

8384
// Issue Setting
@@ -199,6 +200,7 @@ var (
199200
DefaultMergeMessageAllAuthors bool
200201
DefaultMergeMessageMaxApprovers int
201202
DefaultMergeMessageOfficialApproversOnly bool
203+
PopulateSquashCommentWithCommitMessages bool
202204
}{
203205
WorkInProgressPrefixes: []string{"WIP:", "[WIP]"},
204206
// Same as GitHub. See
@@ -210,6 +212,7 @@ var (
210212
DefaultMergeMessageAllAuthors: false,
211213
DefaultMergeMessageMaxApprovers: 10,
212214
DefaultMergeMessageOfficialApproversOnly: true,
215+
PopulateSquashCommentWithCommitMessages: false,
213216
},
214217

215218
// Issue settings

services/pull/pull.go

+32-4
Original file line numberDiff line numberDiff line change
@@ -570,16 +570,44 @@ func GetSquashMergeCommitMessages(pr *models.PullRequest) string {
570570
authors := make([]string, 0, list.Len())
571571
stringBuilder := strings.Builder{}
572572

573-
stringBuilder.WriteString(pr.Issue.Content)
574-
if stringBuilder.Len() > 0 {
575-
stringBuilder.WriteRune('\n')
576-
stringBuilder.WriteRune('\n')
573+
if !setting.Repository.PullRequest.PopulateSquashCommentWithCommitMessages {
574+
stringBuilder.WriteString(pr.Issue.Content)
575+
if stringBuilder.Len() > 0 {
576+
stringBuilder.WriteRune('\n')
577+
stringBuilder.WriteRune('\n')
578+
}
577579
}
578580

579581
// commits list is in reverse chronological order
580582
element := list.Back()
581583
for element != nil {
582584
commit := element.Value.(*git.Commit)
585+
586+
if setting.Repository.PullRequest.PopulateSquashCommentWithCommitMessages {
587+
maxSize := setting.Repository.PullRequest.DefaultMergeMessageSize
588+
if maxSize < 0 || stringBuilder.Len() < maxSize {
589+
var toWrite []byte
590+
if element == list.Back() {
591+
toWrite = []byte(strings.TrimPrefix(commit.CommitMessage, pr.Issue.Title))
592+
} else {
593+
toWrite = []byte(commit.CommitMessage)
594+
}
595+
596+
if len(toWrite) > maxSize-stringBuilder.Len() && maxSize > -1 {
597+
toWrite = append(toWrite[:maxSize-stringBuilder.Len()], "..."...)
598+
}
599+
if _, err := stringBuilder.Write(toWrite); err != nil {
600+
log.Error("Unable to write commit message Error: %v", err)
601+
return ""
602+
}
603+
604+
if _, err := stringBuilder.WriteRune('\n'); err != nil {
605+
log.Error("Unable to write commit message Error: %v", err)
606+
return ""
607+
}
608+
}
609+
}
610+
583611
authorString := commit.Author.String()
584612
if !authorsMap[authorString] && authorString != posterSig {
585613
authors = append(authors, authorString)

0 commit comments

Comments
 (0)