mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-30 18:22:14 +01:00
5f82ead13c
## Purpose This is a refactor toward building an abstraction over managing git repositories. Afterwards, it does not matter anymore if they are stored on the local disk or somewhere remote. ## What this PR changes We used `git.OpenRepository` everywhere previously. Now, we should split them into two distinct functions: Firstly, there are temporary repositories which do not change: ```go git.OpenRepository(ctx, diskPath) ``` Gitea managed repositories having a record in the database in the `repository` table are moved into the new package `gitrepo`: ```go gitrepo.OpenRepository(ctx, repo_model.Repo) ``` Why is `repo_model.Repository` the second parameter instead of file path? Because then we can easily adapt our repository storage strategy. The repositories can be stored locally, however, they could just as well be stored on a remote server. ## Further changes in other PRs - A Git Command wrapper on package `gitrepo` could be created. i.e. `NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir: repo.RepoPath()}`, the directory should be empty before invoking this method and it can be filled in the function only. #28940 - Remove the `RepoPath()`/`WikiPath()` functions to reduce the possibility of mistakes. --------- Co-authored-by: delvh <dev.lh@web.de>
121 lines
4 KiB
Go
121 lines
4 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package pull
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
"code.gitea.io/gitea/modules/git"
|
|
"code.gitea.io/gitea/modules/gitrepo"
|
|
"code.gitea.io/gitea/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 := git.NewCommand(ctx, "show", "--format=%B", "-s").RunStdString(&git.RunOpts{Dir: ctx.tmpBasePath})
|
|
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 := git.NewCommand(ctx, "merge", "--ff-only").AddDynamicArguments(stagingBranch)
|
|
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 != "" {
|
|
if err := git.NewCommand(ctx, "commit", "--amend").AddOptionFormat("--message=%s", newMessage).Run(&git.RunOpts{Dir: ctx.tmpBasePath}); 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 := git.NewCommand(ctx, "merge").AddArguments("--no-ff", "--no-commit").AddDynamicArguments(stagingBranch)
|
|
|
|
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 := git.NewCommand(ctx, "checkout").AddDynamicArguments(baseBranch).
|
|
Run(ctx.RunOpts()); 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(), ctx.errbuf.String())
|
|
return fmt.Errorf("git checkout base prior to merge post staging rebase %v: %w\n%s\n%s", ctx.pr, err, ctx.outbuf.String(), ctx.errbuf.String())
|
|
}
|
|
ctx.outbuf.Reset()
|
|
ctx.errbuf.Reset()
|
|
|
|
if mergeStyle == repo_model.MergeStyleRebase {
|
|
return doMergeRebaseFastForward(ctx)
|
|
}
|
|
|
|
return doMergeRebaseMergeCommit(ctx, message)
|
|
}
|