Skip to content
Merged
Changes from 7 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
17 changes: 8 additions & 9 deletions services/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"bytes"
"context"
"fmt"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -516,6 +517,8 @@ func CloseRepoBranchesPulls(doer *models.User, repo *models.Repository) error {
return nil
}

var commitMessageTrailersPattern = regexp.MustCompile(`(?:^|\n\n)(?:[\w-]+: [^\n]+\n*)+$`)
Copy link
Contributor

@zeripath zeripath Jun 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK testing from git interpret-trailers I think this needs to be, at least:

Suggested change
var commitMessageTrailersPattern = regexp.MustCompile(`(?:^|\n\n)(?:[\w-]+: [^\n]+\n*)+$`)
var commitMessageTrailersPattern = regexp.MustCompile(`(?:^|\n\n)(?:[\w-]+[:=][^\n]+\n)*(?:[\w-]+: [^\n]+)\n*$`)

However trailers can be folded, see https://git-scm.com/docs/git-interpret-trailers

When reading trailers, there can be whitespaces after the token, the separator and the value. There can also be whitespaces inside the token and the value. The value may be split over multiple lines with each subsequent line starting with whitespace, like the "folding" in RFC 822.

Which would make this:

Suggested change
var commitMessageTrailersPattern = regexp.MustCompile(`(?:^|\n\n)(?:[\w-]+: [^\n]+\n*)+$`)
var commitMessageTrailersPattern = regexp.MustCompile(`(?:^|\n\n)(?:[\w-]+[:=][^\n]+\n(?: [^\n]*\n)*)*(?:[\w-]+: [^\n]+(?:\n [^\n]*)*)\n*$`)

(I think)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

echo -n 'Fix #1546                                                                         [19:26:27]

Signed-off-by: Not a trailer

Signed-off-by:Alice <[email protected]>
Signed-off-by:   Bob <[email protected]>
' | git interpret-trailers --parse
echo -n 'Fix #1546                                                                         [19:26:27]

Signed-off-by:Not a trailer

Signed-off-by:Alice <[email protected]>
Signed-off-by:   Bob <[email protected]>' | git interpret-trailers --parse
echo -n 'Fix #1546

Signed-off-by:Not a trailer

Signed-off-by:Alice <[email protected]>
Signed-off-by:   Bob <[email protected]>


' | git interpret-trailers --parse
echo -n 'Fix #1546

Signed-off-by:Not a trailer

Signed-off-by:Alice <[email protected]>
Folded: This is a folded value
 with folded results
   preceding spaces?
Signed-off-by:   Bob <[email protected]>' | git interpret-trailers --parse


// GetSquashMergeCommitMessages returns the commit messages between head and merge base (if there is one)
func GetSquashMergeCommitMessages(pr *models.PullRequest) string {
if err := pr.LoadIssue(); err != nil {
Expand Down Expand Up @@ -571,10 +574,13 @@ func GetSquashMergeCommitMessages(pr *models.PullRequest) string {
stringBuilder := strings.Builder{}

if !setting.Repository.PullRequest.PopulateSquashCommentWithCommitMessages {
stringBuilder.WriteString(pr.Issue.Content)
message := strings.TrimSpace(pr.Issue.Content)
stringBuilder.WriteString(message)
if stringBuilder.Len() > 0 {
stringBuilder.WriteRune('\n')
stringBuilder.WriteRune('\n')
if !commitMessageTrailersPattern.MatchString(message) {
stringBuilder.WriteRune('\n')
}
}
}

Expand Down Expand Up @@ -645,13 +651,6 @@ func GetSquashMergeCommitMessages(pr *models.PullRequest) string {
}
}

if len(authors) > 0 {
if _, err := stringBuilder.WriteRune('\n'); err != nil {
log.Error("Unable to write to string builder Error: %v", err)
return ""
}
}

for _, author := range authors {
if _, err := stringBuilder.Write([]byte("Co-authored-by: ")); err != nil {
log.Error("Unable to write to string builder Error: %v", err)
Expand Down