mirror of
https://github.com/go-gitea/gitea.git
synced 2026-09-21 12:13:38 +09:00
`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>
83 lines
3.0 KiB
Go
83 lines
3.0 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package pull
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
repo_model "gitea.dev/models/repo"
|
|
user_model "gitea.dev/models/user"
|
|
"gitea.dev/modules/container"
|
|
"gitea.dev/modules/git"
|
|
"gitea.dev/modules/git/gitcmd"
|
|
"gitea.dev/modules/log"
|
|
"gitea.dev/modules/setting"
|
|
)
|
|
|
|
// doMergeStyleSquash gets a commit author signature for squash commits
|
|
func getAuthorSignatureSquash(ctx *mergeContext) (*git.Signature, error) {
|
|
if err := ctx.pr.Issue.LoadPoster(ctx); err != nil {
|
|
log.Error("%-v Issue[%d].LoadPoster: %v", ctx.pr, ctx.pr.Issue.ID, err)
|
|
return nil, err
|
|
}
|
|
|
|
// Try to get a signature from the same user in one of the commits, as the
|
|
// poster email might be private or commits might have a different signature
|
|
// than the primary email address of the poster.
|
|
gitRepo, err := git.OpenRepositoryLocal(ctx, ctx.tmpBasePath)
|
|
if err != nil {
|
|
log.Error("%-v Unable to open base repository: %v", ctx.pr, err)
|
|
return nil, err
|
|
}
|
|
defer gitRepo.Close()
|
|
|
|
commits, err := gitRepo.CommitsBetween(ctx, git.RefNameFromBranch(tmpRepoTrackingBranch), git.RefNameHead, -1)
|
|
if err != nil {
|
|
log.Error("%-v Unable to get commits between: head and tracking branch: %v", ctx.pr, err)
|
|
return nil, err
|
|
}
|
|
|
|
uniqueEmails := make(container.Set[string])
|
|
for _, commit := range commits {
|
|
if commit.Author != nil && uniqueEmails.Add(commit.Author.Email) {
|
|
commitUser, _ := user_model.GetUserByEmail(ctx, commit.Author.Email)
|
|
if commitUser != nil && commitUser.ID == ctx.pr.Issue.Poster.ID {
|
|
return commit.Author, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
return ctx.pr.Issue.Poster.NewGitSig(), nil
|
|
}
|
|
|
|
// doMergeStyleSquash squashes the tracking branch on the current HEAD (=base)
|
|
func doMergeStyleSquash(ctx *mergeContext, message string) error {
|
|
sig, err := getAuthorSignatureSquash(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("getAuthorSignatureSquash: %w", err)
|
|
}
|
|
|
|
cmdMerge := gitcmd.NewCommand("merge", "--squash").AddDynamicArguments(tmpRepoTrackingBranch)
|
|
if err := runMergeCommand(ctx, repo_model.MergeStyleSquash, cmdMerge); err != nil {
|
|
log.Error("%-v Unable to merge --squash tracking into base: %v", ctx.pr, err)
|
|
return err
|
|
}
|
|
|
|
if setting.Repository.PullRequest.AddCoCommitterTrailers && ctx.committer.String() != sig.String() {
|
|
message = AddCommitMessageTailer(message, git.CoAuthoredByTrailer, sig.String())
|
|
}
|
|
cmdCommit := gitcmd.NewCommand("commit", "--allow-empty").
|
|
AddOptionFormat("--author='%s <%s>'", sig.Name, sig.Email)
|
|
if err = git.AddObjectMessageArgument(cmdCommit, git.ObjectCommit, message); err != nil {
|
|
return err
|
|
}
|
|
addCommitSigningOptions(cmdCommit, ctx.signKey)
|
|
if err := ctx.PrepareGitCmd(cmdCommit).RunWithStderr(ctx); err != nil {
|
|
log.Error("git commit %-v: %v\n%s\n%s", ctx.pr, err, ctx.outbuf.String(), err.Stderr())
|
|
return fmt.Errorf("git commit [%s:%s -> %s:%s]: %w\n%s\n%s", ctx.pr.HeadRepo.FullName(), ctx.pr.HeadBranch, ctx.pr.BaseRepo.FullName(), ctx.pr.BaseBranch, err, ctx.outbuf.String(), err.Stderr())
|
|
}
|
|
ctx.outbuf.Reset()
|
|
return nil
|
|
}
|