@@ -6,6 +6,7 @@ package pull
66import (
77 "fmt"
88 "strings"
9+ "unicode"
910
1011 repo_model "code.gitea.io/gitea/models/repo"
1112 user_model "code.gitea.io/gitea/models/user"
@@ -51,6 +52,34 @@ func getAuthorSignatureSquash(ctx *mergeContext) (*git.Signature, error) {
5152 return ctx .pr .Issue .Poster .NewGitSig (), nil
5253}
5354
55+ func AddCommitMessageTailer (message , tailerKey , tailerValue string ) string {
56+ tailerLine := tailerKey + ": " + tailerValue
57+ if strings .Contains (message , "\n " + tailerLine + "\n " ) || strings .HasSuffix (message , "\n " + tailerLine ) {
58+ return message
59+ }
60+
61+ if ! strings .HasSuffix (message , "\n " ) {
62+ message += "\n "
63+ }
64+ pos1 := strings .LastIndexByte (message [:len (message )- 1 ], '\n' )
65+ pos2 := - 1
66+ if pos1 != - 1 {
67+ pos2 = strings .IndexByte (message [pos1 :], ':' )
68+ if pos2 != - 1 {
69+ pos2 += pos1
70+ }
71+ }
72+ var lastLine string
73+ if pos1 != - 1 && pos2 != - 1 {
74+ lastLine = message [pos1 + 1 : pos2 + 1 ]
75+ }
76+ lastLineIsTailerLine := lastLine != "" && unicode .IsUpper (rune (lastLine [0 ])) && strings .Contains (lastLine , "-" )
77+ if ! strings .HasSuffix (message , "\n \n " ) && ! lastLineIsTailerLine {
78+ message += "\n "
79+ }
80+ return message + tailerLine
81+ }
82+
5483// doMergeStyleSquash squashes the tracking branch on the current HEAD (=base)
5584func doMergeStyleSquash (ctx * mergeContext , message string ) error {
5685 sig , err := getAuthorSignatureSquash (ctx )
@@ -66,13 +95,8 @@ func doMergeStyleSquash(ctx *mergeContext, message string) error {
6695
6796 if setting .Repository .PullRequest .AddCoCommitterTrailers && ctx .committer .String () != sig .String () {
6897 // add trailer
69- if ! strings .HasSuffix (message , "\n " ) {
70- message += "\n "
71- }
72- if ! strings .Contains (message , "Co-authored-by: " + sig .String ()) {
73- message += "\n Co-authored-by: " + sig .String ()
74- }
75- message += fmt .Sprintf ("\n Co-committed-by: %s\n " , sig .String ())
98+ message = AddCommitMessageTailer (message , "Co-authored-by" , sig .String ())
99+ message = AddCommitMessageTailer (message , "Co-committed-by" , sig .String ()) // FIXME: this one should be removed, it is not really used or widely used
76100 }
77101 cmdCommit := git .NewCommand ("commit" ).
78102 AddOptionFormat ("--author='%s <%s>'" , sig .Name , sig .Email ).
0 commit comments