mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-11-03 21:16:26 +01:00 
			
		
		
		
	Always use an empty line to separate the commit message and trailer (#34512)
If the message from form.MergeMessageField is empty, we will miss a "\n" between the title and the "Co-authored-by:" line. The title and message should have a blank line between of them. --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
		
							parent
							
								
									74858dc5ae
								
							
						
					
					
						commit
						82ea2387e4
					
				@ -13,6 +13,7 @@ import (
 | 
			
		||||
	"regexp"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"unicode"
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/models/db"
 | 
			
		||||
	git_model "code.gitea.io/gitea/models/git"
 | 
			
		||||
@ -161,6 +162,41 @@ func GetDefaultMergeMessage(ctx context.Context, baseGitRepo *git.Repository, pr
 | 
			
		||||
	return getMergeMessage(ctx, baseGitRepo, pr, mergeStyle, nil)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func AddCommitMessageTailer(message, tailerKey, tailerValue string) string {
 | 
			
		||||
	tailerLine := tailerKey + ": " + tailerValue
 | 
			
		||||
	message = strings.ReplaceAll(message, "\r\n", "\n")
 | 
			
		||||
	message = strings.ReplaceAll(message, "\r", "\n")
 | 
			
		||||
	if strings.Contains(message, "\n"+tailerLine+"\n") || strings.HasSuffix(message, "\n"+tailerLine) {
 | 
			
		||||
		return message
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if !strings.HasSuffix(message, "\n") {
 | 
			
		||||
		message += "\n"
 | 
			
		||||
	}
 | 
			
		||||
	pos1 := strings.LastIndexByte(message[:len(message)-1], '\n')
 | 
			
		||||
	pos2 := -1
 | 
			
		||||
	if pos1 != -1 {
 | 
			
		||||
		pos2 = strings.IndexByte(message[pos1:], ':')
 | 
			
		||||
		if pos2 != -1 {
 | 
			
		||||
			pos2 += pos1
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	var lastLineKey string
 | 
			
		||||
	if pos1 != -1 && pos2 != -1 {
 | 
			
		||||
		lastLineKey = message[pos1+1 : pos2]
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	isLikelyTailerLine := lastLineKey != "" && unicode.IsUpper(rune(lastLineKey[0])) && strings.Contains(message, "-")
 | 
			
		||||
	for i := 0; isLikelyTailerLine && i < len(lastLineKey); i++ {
 | 
			
		||||
		r := rune(lastLineKey[i])
 | 
			
		||||
		isLikelyTailerLine = unicode.IsLetter(r) || unicode.IsDigit(r) || r == '-'
 | 
			
		||||
	}
 | 
			
		||||
	if !strings.HasSuffix(message, "\n\n") && !isLikelyTailerLine {
 | 
			
		||||
		message += "\n"
 | 
			
		||||
	}
 | 
			
		||||
	return message + tailerLine
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ErrInvalidMergeStyle represents an error if merging with disabled merge strategy
 | 
			
		||||
type ErrInvalidMergeStyle struct {
 | 
			
		||||
	ID    int64
 | 
			
		||||
 | 
			
		||||
@ -5,7 +5,6 @@ package pull
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	repo_model "code.gitea.io/gitea/models/repo"
 | 
			
		||||
	user_model "code.gitea.io/gitea/models/user"
 | 
			
		||||
@ -66,10 +65,8 @@ func doMergeStyleSquash(ctx *mergeContext, message string) error {
 | 
			
		||||
 | 
			
		||||
	if setting.Repository.PullRequest.AddCoCommitterTrailers && ctx.committer.String() != sig.String() {
 | 
			
		||||
		// add trailer
 | 
			
		||||
		if !strings.Contains(message, "Co-authored-by: "+sig.String()) {
 | 
			
		||||
			message += "\nCo-authored-by: " + sig.String()
 | 
			
		||||
		}
 | 
			
		||||
		message += fmt.Sprintf("\nCo-committed-by: %s\n", sig.String())
 | 
			
		||||
		message = AddCommitMessageTailer(message, "Co-authored-by", sig.String())
 | 
			
		||||
		message = AddCommitMessageTailer(message, "Co-committed-by", sig.String()) // FIXME: this one should be removed, it is not really used or widely used
 | 
			
		||||
	}
 | 
			
		||||
	cmdCommit := git.NewCommand("commit").
 | 
			
		||||
		AddOptionFormat("--author='%s <%s>'", sig.Name, sig.Email).
 | 
			
		||||
 | 
			
		||||
@ -65,3 +65,28 @@ func Test_expandDefaultMergeMessage(t *testing.T) {
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestAddCommitMessageTailer(t *testing.T) {
 | 
			
		||||
	// add tailer for empty message
 | 
			
		||||
	assert.Equal(t, "\n\nTest-tailer: TestValue", AddCommitMessageTailer("", "Test-tailer", "TestValue"))
 | 
			
		||||
 | 
			
		||||
	// add tailer for message without newlines
 | 
			
		||||
	assert.Equal(t, "title\n\nTest-tailer: TestValue", AddCommitMessageTailer("title", "Test-tailer", "TestValue"))
 | 
			
		||||
	assert.Equal(t, "title\n\nNot tailer: xxx\n\nTest-tailer: TestValue", AddCommitMessageTailer("title\n\nNot tailer: xxx", "Test-tailer", "TestValue"))
 | 
			
		||||
	assert.Equal(t, "title\n\nNotTailer: xxx\n\nTest-tailer: TestValue", AddCommitMessageTailer("title\n\nNotTailer: xxx", "Test-tailer", "TestValue"))
 | 
			
		||||
	assert.Equal(t, "title\n\nnot-tailer: xxx\n\nTest-tailer: TestValue", AddCommitMessageTailer("title\n\nnot-tailer: xxx", "Test-tailer", "TestValue"))
 | 
			
		||||
 | 
			
		||||
	// add tailer for message with one EOL
 | 
			
		||||
	assert.Equal(t, "title\n\nTest-tailer: TestValue", AddCommitMessageTailer("title\n", "Test-tailer", "TestValue"))
 | 
			
		||||
 | 
			
		||||
	// add tailer for message with two EOLs
 | 
			
		||||
	assert.Equal(t, "title\n\nTest-tailer: TestValue", AddCommitMessageTailer("title\n\n", "Test-tailer", "TestValue"))
 | 
			
		||||
 | 
			
		||||
	// add tailer for message with existing tailer (won't duplicate)
 | 
			
		||||
	assert.Equal(t, "title\n\nTest-tailer: TestValue", AddCommitMessageTailer("title\n\nTest-tailer: TestValue", "Test-tailer", "TestValue"))
 | 
			
		||||
	assert.Equal(t, "title\n\nTest-tailer: TestValue\n", AddCommitMessageTailer("title\n\nTest-tailer: TestValue\n", "Test-tailer", "TestValue"))
 | 
			
		||||
 | 
			
		||||
	// add tailer for message with existing tailer and different value (will append)
 | 
			
		||||
	assert.Equal(t, "title\n\nTest-tailer: v1\nTest-tailer: v2", AddCommitMessageTailer("title\n\nTest-tailer: v1", "Test-tailer", "v2"))
 | 
			
		||||
	assert.Equal(t, "title\n\nTest-tailer: v1\nTest-tailer: v2", AddCommitMessageTailer("title\n\nTest-tailer: v1\n", "Test-tailer", "v2"))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user