Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Append to existing trailers in generated squash commit message #15980

Merged
Merged
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 <alice@example.com>
Signed-off-by:   Bob <bob@example.com>
' | git interpret-trailers --parse
echo -n 'Fix #1546                                                                         [19:26:27]

Signed-off-by:Not a trailer

Signed-off-by:Alice <alice@example.com>
Signed-off-by:   Bob <bob@example.com>' | git interpret-trailers --parse
echo -n 'Fix #1546

Signed-off-by:Not a trailer

Signed-off-by:Alice <alice@example.com>
Signed-off-by:   Bob <bob@example.com>


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

Signed-off-by:Not a trailer

Signed-off-by:Alice <alice@example.com>
Folded: This is a folded value
 with folded results
   preceding spaces?
Signed-off-by:   Bob <bob@example.com>' | 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