mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in NOTICE, alongside LICENSE. The header lint accepts any copyright line above the SPDX line, so it no longer demands an upstream one. Co-authored-by: Hanzo Dev <[email protected]>
82 lines
3.0 KiB
Go
82 lines
3.0 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package pull
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
repo_model "github.com/hanzoai/git/models/repo"
|
|
user_model "github.com/hanzoai/git/models/user"
|
|
"github.com/hanzoai/git/modules/container"
|
|
"github.com/hanzoai/git/modules/git"
|
|
"github.com/hanzoai/git/modules/git/gitcmd"
|
|
"github.com/hanzoai/git/modules/log"
|
|
"github.com/hanzoai/git/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.OpenRepository(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(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").
|
|
AddOptionFormat("--author='%s <%s>'", sig.Name, sig.Email).
|
|
AddOptionFormat("--message=%s", message).
|
|
AddArguments("--allow-empty")
|
|
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
|
|
}
|