Skip to content

Commit de6634f

Browse files
committed
fix corrupted utf8
1 parent 57ffa78 commit de6634f

File tree

1 file changed

+27
-38
lines changed

1 file changed

+27
-38
lines changed

services/pull/pull.go

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -837,8 +837,8 @@ func GetSquashMergeCommitMessages(ctx context.Context, pr *issues_model.PullRequ
837837
authors := make([]string, 0, len(commits))
838838
stringBuilder := strings.Builder{}
839839

840-
populateWithCommits := setting.Repository.PullRequest.PopulateSquashCommentWithCommitMessages
841-
if !populateWithCommits {
840+
if !setting.Repository.PullRequest.PopulateSquashCommentWithCommitMessages {
841+
//use PR's title and description as squash commit message
842842
message := strings.TrimSpace(pr.Issue.Content)
843843
stringBuilder.WriteString(message)
844844
if stringBuilder.Len() > 0 {
@@ -847,48 +847,46 @@ func GetSquashMergeCommitMessages(ctx context.Context, pr *issues_model.PullRequ
847847
stringBuilder.WriteRune('\n')
848848
}
849849
}
850-
}
851-
852-
// commits list is in reverse chronological order
853-
for i := len(commits) - 1; i >= 0; i-- {
854-
commit := commits[i]
855-
856-
if populateWithCommits {
857-
maxSize := setting.Repository.PullRequest.DefaultMergeMessageSize
858-
if maxSize < 0 || stringBuilder.Len() < maxSize {
859-
if strings.TrimSpace(commit.CommitMessage) == "" {
860-
continue
861-
}
862-
863-
toWrite := fmt.Appendf(nil, "* %s\n", commit.CommitMessage)
850+
} else {
851+
// use PR's commit messages as squash commit message
852+
// commits list is in reverse chronological order
853+
maxSize := setting.Repository.PullRequest.DefaultMergeMessageSize
854+
for i := len(commits) - 1; i >= 0; i-- {
855+
commit := commits[i]
856+
msg := strings.TrimSpace(commit.CommitMessage)
857+
if msg == "" {
858+
continue
859+
}
864860

865-
if len(toWrite) > maxSize-stringBuilder.Len() && maxSize > -1 {
866-
toWrite = append(toWrite[:maxSize-stringBuilder.Len()], "..."...)
867-
}
868-
if _, err := stringBuilder.Write(toWrite); err != nil {
869-
log.Error("Unable to write commit message Error: %v", err)
870-
return ""
871-
}
861+
_, _ = fmt.Fprintf(&stringBuilder, "* %s\n\n", msg)
862+
if maxSize > 0 && stringBuilder.Len() >= maxSize {
863+
tmp := strings.ToValidUTF8(stringBuilder.String()[:maxSize]+"...", "?")
864+
stringBuilder.Reset()
865+
stringBuilder.WriteString(tmp)
866+
break
872867
}
873868
}
869+
}
874870

871+
// collect co-authors
872+
for _, commit := range commits {
875873
authorString := commit.Author.String()
876874
if uniqueAuthors.Add(authorString) && authorString != posterSig {
877875
// Compare use account as well to avoid adding the same author multiple times
878-
// times when email addresses are private or multiple emails are used.
876+
// when email addresses are private or multiple emails are used.
879877
commitUser, _ := user_model.GetUserByEmail(ctx, commit.Author.Email)
880878
if commitUser == nil || commitUser.ID != pr.Issue.Poster.ID {
881879
authors = append(authors, authorString)
882880
}
883881
}
884882
}
885883

886-
// Consider collecting the remaining authors
884+
// collect the remaining authors
887885
if limit >= 0 && setting.Repository.PullRequest.DefaultMergeMessageAllAuthors {
888886
skip := limit
889887
limit = 30
890888
for {
891-
commits, err := gitRepo.CommitsBetweenLimit(headCommit, mergeBase, limit, skip)
889+
commits, err = gitRepo.CommitsBetweenLimit(headCommit, mergeBase, limit, skip)
892890
if err != nil {
893891
log.Error("Unable to get commits between: %s %s Error: %v", pr.HeadBranch, pr.MergeBase, err)
894892
return ""
@@ -914,18 +912,9 @@ func GetSquashMergeCommitMessages(ctx context.Context, pr *issues_model.PullRequ
914912
}
915913

916914
for _, author := range authors {
917-
if _, err := stringBuilder.WriteString("Co-authored-by: "); err != nil {
918-
log.Error("Unable to write to string builder Error: %v", err)
919-
return ""
920-
}
921-
if _, err := stringBuilder.WriteString(author); err != nil {
922-
log.Error("Unable to write to string builder Error: %v", err)
923-
return ""
924-
}
925-
if _, err := stringBuilder.WriteRune('\n'); err != nil {
926-
log.Error("Unable to write to string builder Error: %v", err)
927-
return ""
928-
}
915+
stringBuilder.WriteString("Co-authored-by: ")
916+
stringBuilder.WriteString(author)
917+
stringBuilder.WriteRune('\n')
929918
}
930919

931920
return stringBuilder.String()

0 commit comments

Comments
 (0)