Some refactors to reduce RepoPath

This commit is contained in:
Lunny Xiao
2025-12-17 20:29:01 -08:00
parent 3e566172f5
commit 47f6f1dca9
9 changed files with 87 additions and 96 deletions
+19
View File
@@ -0,0 +1,19 @@
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package gitrepo
import (
"context"
"code.gitea.io/gitea/modules/git/gitcmd"
)
// FetchRemoteCommit fetches a specific commit and related commits from a remote repository into the managed repository
// it will be checked in 2 weeks by default from git if the pull request created failure.
// It's enough for a temporary fetch to get the merge base.
func FetchRemoteCommit(ctx context.Context, repo, remoteRepo Repository, commitID string) error {
return RunCmd(ctx, repo, gitcmd.NewCommand("fetch", "--no-tags").
AddDynamicArguments(repoPath(remoteRepo)).
AddDynamicArguments(commitID))
}
+5
View File
@@ -118,3 +118,8 @@ func CreateRepoFile(ctx context.Context, repo Repository, relativeFilePath strin
absoluteFilePath := filepath.Join(repoPath(repo), relativeFilePath)
return os.Create(absoluteFilePath)
}
func MkRepoDir(ctx context.Context, repo Repository, relativeDirPath string) error {
absoluteDirPath := filepath.Join(repoPath(repo), relativeDirPath)
return os.MkdirAll(absoluteDirPath, os.ModePerm)
}
+34
View File
@@ -0,0 +1,34 @@
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package gitrepo
import (
"context"
"fmt"
"strings"
"code.gitea.io/gitea/modules/git/gitcmd"
)
// MergeBase checks and returns merge base of two commits.
func MergeBase(ctx context.Context, repo Repository, commit1, commit2 string) (string, error) {
mergeBase, err := RunCmdString(ctx, repo, gitcmd.NewCommand("merge-base").
AddDashesAndList(commit1, commit2))
if err != nil {
return "", fmt.Errorf("get merge-base of %s and %s failed: %w", commit1, commit2, err)
}
return strings.TrimSpace(mergeBase), nil
}
// MergeBaseFromRemote checks and returns merge base of two commits from different repositories.
func MergeBaseFromRemote(ctx context.Context, repo, remoteRepo Repository, commit1, commit2 string) (string, error) {
// fetch head commit id into the current repository if the repositories are different
if repo.RelativePath() != remoteRepo.RelativePath() {
if err := FetchRemoteCommit(ctx, repo, remoteRepo, commit2); err != nil {
return "", fmt.Errorf("FetchRemoteCommit: %w", err)
}
}
return MergeBase(ctx, repo, commit1, commit2)
}