@@ -6,6 +6,7 @@ package pull
6
6
import (
7
7
"fmt"
8
8
"strings"
9
+ "unicode"
9
10
10
11
repo_model "code.gitea.io/gitea/models/repo"
11
12
user_model "code.gitea.io/gitea/models/user"
@@ -51,6 +52,34 @@ func getAuthorSignatureSquash(ctx *mergeContext) (*git.Signature, error) {
51
52
return ctx .pr .Issue .Poster .NewGitSig (), nil
52
53
}
53
54
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
+
54
83
// doMergeStyleSquash squashes the tracking branch on the current HEAD (=base)
55
84
func doMergeStyleSquash (ctx * mergeContext , message string ) error {
56
85
sig , err := getAuthorSignatureSquash (ctx )
@@ -66,13 +95,8 @@ func doMergeStyleSquash(ctx *mergeContext, message string) error {
66
95
67
96
if setting .Repository .PullRequest .AddCoCommitterTrailers && ctx .committer .String () != sig .String () {
68
97
// 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
76
100
}
77
101
cmdCommit := git .NewCommand ("commit" ).
78
102
AddOptionFormat ("--author='%s <%s>'" , sig .Name , sig .Email ).
0 commit comments