mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
2978 files. The fork is 2026-07-15 (rebrand: gitea.dev -> github.com/hanzoai/git), so 2026 is the year, and "Copyright 2026 Hanzo AI, Inc." was already the form used by the handful of files that had ours. Shape is now: // Copyright 2026 Hanzo AI, Inc. All rights reserved. // Copyright 2015 The Gogs Authors. All rights reserved. // Copyright 2019 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT Ours on top, theirs retained verbatim. Retained because MIT requires it, and separately because they are true — those authors did write that code. Not renamed to "The Git Authors": Git is a different project under a different license, and putting its name here would attribute this code to people who have nothing to do with it. Adding our line is the fix; overwriting theirs would have been both a licence violation and a false statement. gofmt still reports import-order drift in some of these files. It predates this change (verified against a stash) and is left alone rather than burying a one-line-per-file legal change under 3000 files of unrelated reordering. Build clean. Co-authored-by: Hanzo Dev <[email protected]>
83 lines
3.1 KiB
Go
83 lines
3.1 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// Copyright 2023 The Gitea Authors. 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
|
|
}
|