2023-03-07 13:07:35 -07:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package pull
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
)
|
|
|
|
|
|
|
|
// doMergeStyleSquash squashes the tracking branch on the current HEAD (=base)
|
|
|
|
func doMergeStyleSquash(ctx *mergeContext, message string) error {
|
2023-03-09 08:48:52 -07:00
|
|
|
cmdMerge := git.NewCommand(ctx, "merge", "--squash").AddDynamicArguments(trackingBranch)
|
|
|
|
if err := runMergeCommand(ctx, repo_model.MergeStyleSquash, cmdMerge); err != nil {
|
2023-03-07 13:07:35 -07:00
|
|
|
log.Error("%-v Unable to merge --squash tracking into base: %v", ctx.pr, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ctx.pr.Issue.LoadPoster(ctx); err != nil {
|
|
|
|
log.Error("%-v Issue[%d].LoadPoster: %v", ctx.pr, ctx.pr.Issue.ID, err)
|
|
|
|
return fmt.Errorf("LoadPoster: %w", err)
|
|
|
|
}
|
|
|
|
sig := ctx.pr.Issue.Poster.NewGitSig()
|
2023-03-09 08:48:52 -07:00
|
|
|
if setting.Repository.PullRequest.AddCoCommitterTrailers && ctx.committer.String() != sig.String() {
|
|
|
|
// add trailer
|
|
|
|
message += fmt.Sprintf("\nCo-authored-by: %s\nCo-committed-by: %s\n", sig.String(), sig.String())
|
|
|
|
}
|
|
|
|
cmdCommit := git.NewCommand(ctx, "commit").
|
|
|
|
AddOptionFormat("--author='%s <%s>'", sig.Name, sig.Email).
|
|
|
|
AddOptionFormat("--message=%s", message)
|
|
|
|
if ctx.signKeyID == "" {
|
|
|
|
cmdCommit.AddArguments("--no-gpg-sign")
|
2023-03-07 13:07:35 -07:00
|
|
|
} else {
|
2023-03-09 08:48:52 -07:00
|
|
|
cmdCommit.AddOptionFormat("-S%s", ctx.signKeyID)
|
|
|
|
}
|
|
|
|
if err := cmdCommit.Run(ctx.RunOpts()); err != nil {
|
|
|
|
log.Error("git commit %-v: %v\n%s\n%s", ctx.pr, err, ctx.outbuf.String(), ctx.errbuf.String())
|
|
|
|
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(), ctx.errbuf.String())
|
2023-03-07 13:07:35 -07:00
|
|
|
}
|
|
|
|
ctx.outbuf.Reset()
|
|
|
|
ctx.errbuf.Reset()
|
|
|
|
return nil
|
|
|
|
}
|