fix: pass merge commit messages to git via stdin (#39269)

`git commit --message=` passes the merge message as a single argument,
which Linux caps at 128 KiB and Windows at 32 KiB for the whole command
line. Long messages failed with `argument list too long` and the merge
box toast showed the raw HTML 500 page.

Pass the message via `--file=-` on stdin instead, and answer
fetch-action requests with JSON on server errors so the toast shows the
error text. Limits merge commit messages to 512KB which could be
extended or made configurable later.

Fixes: https://github.com/go-gitea/gitea/issues/39261
Fixes: https://github.com/go-gitea/gitea/issues/30276
Signed-off-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
silverwind
2026-09-19 13:04:53 +00:00
committed by GitHub
co-authored by wxiaoguang
parent cc34c26172
commit cdf786ce92
21 changed files with 175 additions and 93 deletions
+18 -9
View File
@@ -6,6 +6,7 @@ package integration
import (
"fmt"
"net/http"
"strings"
"testing"
auth_model "gitea.dev/models/auth"
@@ -14,6 +15,7 @@ import (
user_model "gitea.dev/models/user"
"gitea.dev/modules/git"
api "gitea.dev/modules/structs"
"gitea.dev/modules/test"
"gitea.dev/tests"
"github.com/stretchr/testify/assert"
@@ -36,20 +38,17 @@ func TestAPIGitTags(t *testing.T) {
commit, _ := gitRepo.GetBranchCommit(t.Context(), "master")
lTagName := "lightweightTag"
gitRepo.CreateTag(t.Context(), lTagName, commit.ID.String())
_ = gitRepo.CreateTag(t.Context(), lTagName, commit.ID.String())
aTagName := "annotatedTag"
aTagMessage := "my annotated message"
gitRepo.CreateAnnotatedTag(t.Context(), aTagName, aTagMessage, commit.ID.String())
aTagMessage := strings.Repeat("very long " /*10bytes*/, 20*1024) + "annotated message"
_ = gitRepo.CreateAnnotatedTag(t.Context(), aTagName, aTagMessage, commit.ID.String())
aTag, _ := gitRepo.GetTag(t.Context(), aTagName)
// SHOULD work for annotated tags
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/git/tags/%s", user.Name, repo.Name, aTag.ID.String()).
AddTokenAuth(token)
res := MakeRequest(t, req, http.StatusOK)
tag := DecodeJSON(t, res, &api.AnnotatedTag{})
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/git/tags/%s", user.Name, repo.Name, aTag.ID.String()).AddTokenAuth(token)
resp := MakeRequest(t, req, http.StatusOK)
tag := DecodeJSON(t, resp, &api.AnnotatedTag{})
assert.Equal(t, aTagName, tag.Tag)
assert.Equal(t, aTag.ID.String(), tag.SHA)
assert.Equal(t, commit.ID.String(), tag.Object.SHA)
@@ -58,6 +57,16 @@ func TestAPIGitTags(t *testing.T) {
assert.Equal(t, user.Email, tag.Tagger.Email)
assert.Equal(t, repo.APIURL()+"/git/tags/"+aTag.ID.String(), tag.URL)
if !git.DefaultFeatures().UsingGogit {
defer test.MockVariableValue(&git.MaxGitObjectSize, 1024)()
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/git/tags/%s", user.Name, repo.Name, aTag.ID.String()).AddTokenAuth(token)
resp = MakeRequest(t, req, http.StatusOK)
tag = DecodeJSON(t, resp, &api.AnnotatedTag{})
assert.True(t, strings.HasPrefix(aTagMessage, tag.Message))
assert.Less(t, len(tag.Message), len(aTagMessage))
assert.Less(t, len(tag.Message), 1024)
}
// Should NOT work for lightweight tags
badReq := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/git/tags/%s", user.Name, repo.Name, commit.ID.String()).
AddTokenAuth(token)
+13 -2
View File
@@ -51,6 +51,7 @@ type MergeOptions struct {
Style repo_model.MergeStyle
HeadCommitID string
DeleteBranch bool
Message string
}
func testPullMerge(t *testing.T, session *TestSession, user, repo, pullNum string, mergeOptions MergeOptions) *httptest.ResponseRecorder {
@@ -58,6 +59,7 @@ func testPullMerge(t *testing.T, session *TestSession, user, repo, pullNum strin
"do": string(mergeOptions.Style),
"head_commit_id": mergeOptions.HeadCommitID,
"delete_branch_after_merge": util.Iif(mergeOptions.DeleteBranch, "on", ""),
"merge_message_field": mergeOptions.Message,
}
var resp *httptest.ResponseRecorder
require.Eventually(t, func() bool {
@@ -77,6 +79,15 @@ func testPullMerge(t *testing.T, session *TestSession, user, repo, pullNum strin
assert.NoError(t, err)
assert.True(t, pull.HasMerged)
if mergeOptions.Message != "" {
gitRepo, err := git.OpenRepository(t.Context(), repository)
require.NoError(t, err)
defer gitRepo.Close()
commit, err := gitRepo.GetCommit(t.Context(), pull.MergedCommitID)
require.NoError(t, err)
assert.Contains(t, commit.CommitMessage.MessageRaw, mergeOptions.Message)
}
return resp
}
@@ -116,8 +127,8 @@ func TestPullMerge(t *testing.T) {
elem := strings.Split(test.RedirectURL(resp), "/")
assert.Equal(t, "pulls", elem[3])
testPullMerge(t, session, elem[1], elem[2], elem[4], MergeOptions{
Style: repo_model.MergeStyleMerge,
DeleteBranch: false,
Style: repo_model.MergeStyleMerge,
Message: strings.Repeat("x", 200*1024),
})
repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repo.ID})