mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-27 22:07:33 +08:00
Merge and squash now run `git merge-tree` and `git commit-tree` in the base repository and push the commit to itself, instead of building a temporary repository per merge, which made merges take over 10 s on NFS (https://github.com/go-gitea/gitea/issues/39410). Rebase styles, fast-forward-only and git older than 2.40 are unchanged. Commit and tag messages are cleaned up in one place like `git commit` does, and merge-specific `[git.config]` overrides no longer apply to merge and squash. Assisted-by: Claude Code:claude-opus-5-5 --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
187 lines
6.6 KiB
Go
187 lines
6.6 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package git
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestCommitMessageSanitizesInvalidUTF8(t *testing.T) {
|
|
commit := &Commit{
|
|
CommitMessage: CommitMessage{MessageRaw: "title \xff\n\n\n\nbody \xff\n\n\n"},
|
|
}
|
|
assert.Equal(t, "title ÿ", commit.MessageTitle())
|
|
assert.Equal(t, "body ÿ", commit.MessageBody())
|
|
assert.Equal(t, "title ÿ\n\n\n\nbody ÿ\n\n\n", commit.MessageUTF8())
|
|
}
|
|
|
|
func TestCommitMessageTrailer(t *testing.T) {
|
|
cases := []struct {
|
|
msg, body, sep, trailer string
|
|
}{
|
|
{"", "", "", ""},
|
|
{"a", "a", "", ""},
|
|
{"a\n\nk", "a\n\nk", "", ""},
|
|
{"a\n\nk:v", "a", "\n\n", "k:v"},
|
|
{"a\n\nk:v\n next-line", "a", "\n\n", "k:v\n next-line"},
|
|
{"a\n\nk:v\n next-line\nother: v", "a", "\n\n", "k:v\n next-line\nother: v"},
|
|
{"a\n\nk:v\n\n", "a", "\n\n", "k:v\n\n"},
|
|
{"a\n--\nk:v", "a\n--\nk:v", "", ""},
|
|
{"a\n---\nk:v", "a", "\n---\n", "k:v"}, // TODO: should we support such case? No empty line between "---" and the trailer
|
|
{"a\n\n---\n\nk:v", "a", "\n\n---\n\n", "k:v"},
|
|
|
|
{"k: v", "", "", "k: v"},
|
|
{"\nk:v", "", "\n", "k:v"},
|
|
{"\n\nk:v", "", "\n\n", "k:v"},
|
|
|
|
{"---\nk:v", "", "---\n", "k:v"},
|
|
{"\n---\nk:v", "", "\n---\n", "k:v"},
|
|
{"a:b\n---\nk:v", "a:b", "\n---\n", "k:v"},
|
|
}
|
|
for _, c := range cases {
|
|
body, sep, trailer := CommitMessageSplitTrailer(c.msg)
|
|
assert.Equal(t, c.body, body, "input=%q", c.msg)
|
|
assert.Equal(t, c.sep, sep, "input=%q", c.msg)
|
|
assert.Equal(t, c.trailer, trailer, "input=%q", c.msg)
|
|
}
|
|
}
|
|
|
|
func TestCommitMessageParticipants(t *testing.T) {
|
|
sig := func(n, e string) *Signature { return &Signature{Name: n, Email: e} }
|
|
idt := func(n, e string, r int) *CommitIdentity { return &CommitIdentity{n, e, r} }
|
|
roleAuthor, _, roleCoAuthor := commitIdentityRoleAuthor, commitIdentityRoleCommitter, commitIdentityRoleCoAuthor
|
|
type testCase struct {
|
|
name string
|
|
commit *Commit
|
|
identities []*CommitIdentity
|
|
}
|
|
|
|
t.Run("AllAuthors", func(t *testing.T) {
|
|
cases := []testCase{
|
|
{
|
|
"CommitterExcluded",
|
|
&Commit{
|
|
Author: sig("a", "a@m.com"), Committer: sig("c", "c@m.com"),
|
|
CommitMessage: CommitMessage{MessageRaw: "CO-Authored-BY: Full Name <x@m.com>"},
|
|
},
|
|
[]*CommitIdentity{idt("a", "a@m.com", roleAuthor), idt("Full Name", "x@m.com", roleCoAuthor)},
|
|
},
|
|
{
|
|
"AuthorIsCoAuthor",
|
|
&Commit{
|
|
Author: sig("a", "a@m.com"), Committer: sig("c", "c@m.com"),
|
|
CommitMessage: CommitMessage{MessageRaw: "CO-Authored-BY: other-name <a@m.com>"},
|
|
},
|
|
[]*CommitIdentity{idt("a", "a@m.com", roleAuthor)},
|
|
},
|
|
{
|
|
"EmptyAuthor", // synthesized commits (push feed) may have no author signature at all
|
|
&Commit{
|
|
Author: sig("", ""), Committer: sig("", ""),
|
|
CommitMessage: CommitMessage{MessageRaw: "Co-authored-by: c <c@m.com>"},
|
|
},
|
|
// but if the commit message contains co-authors, the co-authors are still parsed for "all authors"
|
|
// if it is a problem, the caller should fix the problem (provide correct "author")
|
|
[]*CommitIdentity{idt("c", "c@m.com", roleCoAuthor)},
|
|
},
|
|
{
|
|
"CoAuthorIsCommitter",
|
|
&Commit{
|
|
Author: sig("a", "a@m.com"), Committer: sig("c", "c@m.com"),
|
|
CommitMessage: CommitMessage{MessageRaw: "Co-authored-by: c <c@m.com>"},
|
|
},
|
|
[]*CommitIdentity{idt("a", "a@m.com", roleAuthor), idt("c", "c@m.com", roleCoAuthor)},
|
|
},
|
|
{
|
|
"CoAuthorNameOnlyAndDuplicate",
|
|
&Commit{
|
|
Author: sig("a", "a@m.com"), Committer: sig("c", "c@m.com"),
|
|
CommitMessage: CommitMessage{MessageRaw: "Co-authored-by: b\nCo-authored-by: b\nCo-authored-by: c"},
|
|
},
|
|
[]*CommitIdentity{idt("a", "a@m.com", roleAuthor), idt("b", "", roleCoAuthor), idt("c", "", roleCoAuthor)},
|
|
},
|
|
{
|
|
"CoAuthorNameNotAnEmailAddress", // names net/mail rejects, e.g. bots and names with a comma
|
|
&Commit{
|
|
Author: sig("a", "a@m.com"), Committer: sig("c", "c@m.com"),
|
|
CommitMessage: CommitMessage{MessageRaw: "Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>\nCo-authored-by: Smith, John <j@m.com>"},
|
|
},
|
|
[]*CommitIdentity{
|
|
idt("a", "a@m.com", roleAuthor),
|
|
idt("dependabot[bot]", "49699333+dependabot[bot]@users.noreply.github.com", roleCoAuthor),
|
|
idt("Smith, John", "j@m.com", roleCoAuthor),
|
|
},
|
|
},
|
|
}
|
|
for _, c := range cases {
|
|
assert.Equal(t, c.identities, c.commit.AllAuthorIdentities(), "case: %s", c.name)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestCommitMessageMerge(t *testing.T) {
|
|
cases := []struct {
|
|
m1, m2 string
|
|
out string
|
|
}{
|
|
{"", "", ""},
|
|
{"msg1", "", "msg1"},
|
|
{"", "msg2", "msg2"},
|
|
{"msg1", "msg2", "msg1\n\nmsg2"},
|
|
{"k1: a", "", "k1: a"},
|
|
{"", "k2: b", "k2: b"},
|
|
{"k1: a", "k2: b", "k1: a\nk2: b"},
|
|
{"msg1", "k2: b", "msg1\n\nk2: b"},
|
|
{"k1: a", "msg2", "msg2\n\nk1: a"},
|
|
{"msg1\n\nk1: a", "msg2", "msg1\n\nmsg2\n\nk1: a"},
|
|
{"msg1\n----\nk1: a", "msg2", "msg1\n\nmsg2\n----\nk1: a"},
|
|
{"msg1\n\n----\n\nk1: a", "msg2", "msg1\n\nmsg2\n\n----\n\nk1: a"},
|
|
{"msg1", "msg2\n----\nk2: b", "msg1\n\nmsg2\n----\nk2: b"},
|
|
{"msg1", "msg2\n\nk2: b", "msg1\n\nmsg2\n\nk2: b"},
|
|
{"msg1\n\nk1: a", "msg2\n\nk2: b", "msg1\n\nmsg2\n\nk1: a\nk2: b"},
|
|
}
|
|
|
|
for i, c := range cases {
|
|
out := CommitMessageMerge(c.m1, c.m2)
|
|
assert.Equal(t, c.out, out, "idx=%d, m1=%q m2=%q", i, c.m1, c.m2)
|
|
}
|
|
}
|
|
|
|
func TestCommitMessageAddTrailer(t *testing.T) {
|
|
cases := []struct{ message, out string }{
|
|
{"", "Key: v"},
|
|
{"title", "title\n\nKey: v"},
|
|
{"title\n", "title\n\nKey: v"},
|
|
{"title\n\n", "title\n\nKey: v"},
|
|
{"feat: subject", "feat: subject\n\nKey: v"},
|
|
{"title\n\nNot trailer: xxx", "title\n\nNot trailer: xxx\n\nKey: v"},
|
|
{"title\n\nOther: xxx", "title\n\nOther: xxx\nKey: v"},
|
|
{"title\n\nKey: v", "title\n\nKey: v"},
|
|
{"title\n\nKey: v\n", "title\n\nKey: v\n"},
|
|
{"title\n\nKey: v1", "title\n\nKey: v1\nKey: v"},
|
|
{"title\n\nKey: v\n\nbody", "title\n\nKey: v\n\nbody\n\nKey: v"},
|
|
{"fix-up\n\nbody\nOther: xxx", "fix-up\n\nbody\nOther: xxx\n\nKey: v"},
|
|
{"title\n\nSigned-off-by: a\n folded\nKey: v1", "title\n\nSigned-off-by: a\n folded\nKey: v1\nKey: v"},
|
|
}
|
|
for _, tc := range cases {
|
|
assert.Equal(t, tc.out, CommitMessageAddTrailer(tc.message, "Key", "v"), "message=%q", tc.message)
|
|
}
|
|
}
|
|
|
|
func TestCleanupObjectMessage(t *testing.T) {
|
|
cases := []struct{ message, out string }{
|
|
{"", ""},
|
|
{" \r\n\n", ""},
|
|
{"title", "title\n"},
|
|
{"\n\ntitle \r\n\r\n\r\n# heading\r\nbody\t \n\n", "title\n\n# heading\nbody\n"},
|
|
{"title\rbody", "title\nbody\n"},
|
|
{" indented\v\n", " indented\v\n"},
|
|
}
|
|
for _, tc := range cases {
|
|
assert.Equal(t, tc.out, CleanupObjectMessage(tc.message), "message=%q", tc.message)
|
|
}
|
|
}
|