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]>
125 lines
4.2 KiB
Go
125 lines
4.2 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"
|
|
"strings"
|
|
|
|
repo_model "github.com/hanzoai/git/models/repo"
|
|
"github.com/hanzoai/git/modules/git"
|
|
"github.com/hanzoai/git/modules/git/gitcmd"
|
|
"github.com/hanzoai/git/modules/gitrepo"
|
|
"github.com/hanzoai/git/modules/log"
|
|
)
|
|
|
|
// getRebaseAmendMessage composes the message to amend commits in rebase merge of a pull request.
|
|
func getRebaseAmendMessage(ctx *mergeContext, baseGitRepo *git.Repository) (message string, err error) {
|
|
// Get existing commit message.
|
|
commitMessage, _, err := gitcmd.NewCommand("show", "--format=%B", "-s").WithDir(ctx.tmpBasePath).RunStdString(ctx)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
commitTitle, commitBody, _ := strings.Cut(commitMessage, "\n")
|
|
extraVars := map[string]string{"CommitTitle": strings.TrimSpace(commitTitle), "CommitBody": strings.TrimSpace(commitBody)}
|
|
|
|
message, body, err := getMergeMessage(ctx, baseGitRepo, ctx.pr, repo_model.MergeStyleRebase, extraVars)
|
|
if err != nil || message == "" {
|
|
return "", err
|
|
}
|
|
|
|
if len(body) > 0 {
|
|
message = message + "\n\n" + body
|
|
}
|
|
return message, err
|
|
}
|
|
|
|
// Perform rebase merge without merge commit.
|
|
func doMergeRebaseFastForward(ctx *mergeContext) error {
|
|
baseHeadSHA, err := git.GetFullCommitID(ctx, ctx.tmpBasePath, "HEAD")
|
|
if err != nil {
|
|
return fmt.Errorf("Failed to get full commit id for HEAD: %w", err)
|
|
}
|
|
|
|
cmd := gitcmd.NewCommand("merge", "--ff-only").AddDynamicArguments(tmpRepoStagingBranch)
|
|
if err := runMergeCommand(ctx, repo_model.MergeStyleRebase, cmd); err != nil {
|
|
log.Error("Unable to merge staging into base: %v", err)
|
|
return err
|
|
}
|
|
|
|
// Check if anything actually changed before we amend the message, fast forward can skip commits.
|
|
newMergeHeadSHA, err := git.GetFullCommitID(ctx, ctx.tmpBasePath, "HEAD")
|
|
if err != nil {
|
|
return fmt.Errorf("Failed to get full commit id for HEAD: %w", err)
|
|
}
|
|
if baseHeadSHA == newMergeHeadSHA {
|
|
return nil
|
|
}
|
|
|
|
// Original repo to read template from.
|
|
baseGitRepo, err := gitrepo.OpenRepository(ctx, ctx.pr.BaseRepo)
|
|
if err != nil {
|
|
log.Error("Unable to get Git repo for rebase: %v", err)
|
|
return err
|
|
}
|
|
defer baseGitRepo.Close()
|
|
|
|
// Amend last commit message based on template, if one exists
|
|
newMessage, err := getRebaseAmendMessage(ctx, baseGitRepo)
|
|
if err != nil {
|
|
log.Error("Unable to get commit message for amend: %v", err)
|
|
return err
|
|
}
|
|
|
|
if newMessage != "" {
|
|
cmdCommit := gitcmd.NewCommand("commit", "--amend").
|
|
AddOptionFormat("--message=%s", newMessage)
|
|
addCommitSigningOptions(cmdCommit, ctx.signKey)
|
|
if err := cmdCommit.WithDir(ctx.tmpBasePath).Run(ctx); err != nil {
|
|
log.Error("Unable to amend commit message: %v", err)
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Perform rebase merge with merge commit.
|
|
func doMergeRebaseMergeCommit(ctx *mergeContext, message string) error {
|
|
cmd := gitcmd.NewCommand("merge").AddArguments("--no-ff", "--no-commit").AddDynamicArguments(tmpRepoStagingBranch)
|
|
|
|
if err := runMergeCommand(ctx, repo_model.MergeStyleRebaseMerge, cmd); err != nil {
|
|
log.Error("Unable to merge staging into base: %v", err)
|
|
return err
|
|
}
|
|
if err := commitAndSignNoAuthor(ctx, message); err != nil {
|
|
log.Error("Unable to make final commit: %v", err)
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// doMergeStyleRebase rebases the tracking branch on the base branch as the current HEAD with or with a merge commit to the original pr branch
|
|
func doMergeStyleRebase(ctx *mergeContext, mergeStyle repo_model.MergeStyle, message string) error {
|
|
if err := rebaseTrackingOnToBase(ctx, mergeStyle); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Checkout base branch again
|
|
if err := ctx.PrepareGitCmd(gitcmd.NewCommand("checkout").AddDynamicArguments(tmpRepoBaseBranch)).
|
|
RunWithStderr(ctx); err != nil {
|
|
log.Error("git checkout base prior to merge post staging rebase %-v: %v\n%s\n%s", ctx.pr, err, ctx.outbuf.String(), err.Stderr())
|
|
return fmt.Errorf("git checkout base prior to merge post staging rebase %v: %w\n%s\n%s", ctx.pr, err, ctx.outbuf.String(), err.Stderr())
|
|
}
|
|
ctx.outbuf.Reset()
|
|
if mergeStyle == repo_model.MergeStyleRebase {
|
|
return doMergeRebaseFastForward(ctx)
|
|
}
|
|
|
|
return doMergeRebaseMergeCommit(ctx, message)
|
|
}
|