Skip to content

Commit c5798e1

Browse files
parnicStu Wehrly
authored and
Stu Wehrly
committed
Reintroduce squash merge default comment as a config setting (go-gitea#16134)
* Reinstate most of commit 09304db * Move the behaviour behind a config setting * Also fix the initial go-gitea#12365
1 parent d0b8e3c commit c5798e1

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,11 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
9393
- `REOPEN_KEYWORDS`: **reopen**, **reopens**, **reopened**: List of keywords used in Pull Request comments to automatically reopen
9494
a related issue
9595
- `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
96-
- `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.
96+
- `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`.
9797
- `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
9898
- `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.
9999
- `DEFAULT_MERGE_MESSAGE_OFFICIAL_APPROVERS_ONLY`: **true**: In default merge messages only include approvers who are officially allowed to review.
100+
- `POPULATE_SQUASH_COMMENT_WITH_COMMIT_MESSAGES`: **false**: In default squash-merge messages include the commit message of all commits comprising the pull request.
100101

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

modules/setting/repository.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ var (
7777
DefaultMergeMessageAllAuthors bool
7878
DefaultMergeMessageMaxApprovers int
7979
DefaultMergeMessageOfficialApproversOnly bool
80+
PopulateSquashCommentWithCommitMessages bool
8081
} `ini:"repository.pull-request"`
8182

8283
// Issue Setting
@@ -197,6 +198,7 @@ var (
197198
DefaultMergeMessageAllAuthors bool
198199
DefaultMergeMessageMaxApprovers int
199200
DefaultMergeMessageOfficialApproversOnly bool
201+
PopulateSquashCommentWithCommitMessages bool
200202
}{
201203
WorkInProgressPrefixes: []string{"WIP:", "[WIP]"},
202204
// Same as GitHub. See
@@ -208,6 +210,7 @@ var (
208210
DefaultMergeMessageAllAuthors: false,
209211
DefaultMergeMessageMaxApprovers: 10,
210212
DefaultMergeMessageOfficialApproversOnly: true,
213+
PopulateSquashCommentWithCommitMessages: false,
211214
},
212215

213216
// Issue settings

services/pull/pull.go

Lines changed: 32 additions & 4 deletions
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)