refactor: hide git repo path details from more packages (#38601)

Remove `RepoPath` from "models/repo" package, remove `UserPath` from
"models/user" package, use `WithRepo` for more places, fine tune tests.
This commit is contained in:
wxiaoguang
2026-07-24 01:42:55 +08:00
committed by GitHub
parent 86a3048247
commit 7065637e61
22 changed files with 216 additions and 186 deletions
+7 -6
View File
@@ -11,6 +11,7 @@ import (
issues_model "gitea.dev/models/issues"
"gitea.dev/models/unittest"
"gitea.dev/modules/git/gitcmd"
"gitea.dev/modules/git/gitrepo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -51,7 +52,7 @@ func TestPullRequestMergeable(t *testing.T) {
})
pr.BaseBranch, pr.HeadBranch = "test-merge-tree-conflict-base", "test-merge-tree-conflict-head"
conflictFiles := createConflictBranches(t, pr.BaseRepo.RepoPath(), pr.BaseBranch, pr.HeadBranch)
conflictFiles := createConflictBranches(t, pr.BaseRepo, pr.BaseBranch, pr.HeadBranch)
t.Run("Conflict-MergeTree", func(t *testing.T) {
testPullRequestMergeCheck(t, checkPullRequestMergeableByMergeTree, pr, issues_model.PullRequestStatusConflict, conflictFiles, nil)
})
@@ -60,7 +61,7 @@ func TestPullRequestMergeable(t *testing.T) {
})
pr.BaseBranch, pr.HeadBranch = "test-merge-tree-empty-base", "test-merge-tree-empty-head"
createEmptyBranches(t, pr.BaseRepo.RepoPath(), pr.BaseBranch, pr.HeadBranch)
createEmptyBranches(t, pr.BaseRepo, pr.BaseBranch, pr.HeadBranch)
t.Run("Empty-MergeTree", func(t *testing.T) {
testPullRequestMergeCheck(t, checkPullRequestMergeableByMergeTree, pr, issues_model.PullRequestStatusEmpty, nil, nil)
})
@@ -69,7 +70,7 @@ func TestPullRequestMergeable(t *testing.T) {
})
}
func createConflictBranches(t *testing.T, repoPath, baseBranch, headBranch string) []string {
func createConflictBranches(t *testing.T, repo gitrepo.RepositoryFacade, baseBranch, headBranch string) []string {
conflictFile := "conflict.txt"
stdin := fmt.Sprintf(
`reset refs/heads/%[1]s
@@ -107,12 +108,12 @@ M 100644 inline %[3]s
data 11
head change
`, baseBranch, headBranch, conflictFile)
err := gitcmd.NewCommand("fast-import").WithDir(repoPath).WithStdinBytes([]byte(stdin)).RunWithStderr(t.Context())
err := gitcmd.NewCommand("fast-import").WithRepo(repo).WithStdinBytes([]byte(stdin)).RunWithStderr(t.Context())
require.NoError(t, err)
return []string{conflictFile}
}
func createEmptyBranches(t *testing.T, repoPath, baseBranch, headBranch string) {
func createEmptyBranches(t *testing.T, repo gitrepo.RepositoryFacade, baseBranch, headBranch string) {
emptyFile := "empty.txt"
stdin := fmt.Sprintf(`reset refs/heads/%[1]s
from refs/heads/master
@@ -149,6 +150,6 @@ M 100644 inline %[3]s
data 4
base
`, baseBranch, headBranch, emptyFile)
err := gitcmd.NewCommand("fast-import").WithDir(repoPath).WithStdinBytes([]byte(stdin)).RunWithStderr(t.Context())
err := gitcmd.NewCommand("fast-import").WithRepo(repo).WithStdinBytes([]byte(stdin)).RunWithStderr(t.Context())
require.NoError(t, err)
}
+3 -2
View File
@@ -16,6 +16,7 @@ import (
repo_model "gitea.dev/models/repo"
"gitea.dev/modules/git"
"gitea.dev/modules/git/gitcmd"
"gitea.dev/modules/git/gitrepo"
repo_module "gitea.dev/modules/repository"
)
@@ -80,8 +81,8 @@ func createTemporaryRepoForPR(ctx context.Context, pr *issues_model.PullRequest)
outbuf: &bytes.Buffer{},
}
baseRepoPath := pr.BaseRepo.RepoPath()
headRepoPath := pr.HeadRepo.RepoPath()
baseRepoPath := gitrepo.RepoLocalPath(pr.BaseRepo.CodeStorageRepo())
headRepoPath := gitrepo.RepoLocalPath(pr.HeadRepo.CodeStorageRepo())
if err := git.InitRepositoryLocal(ctx, tmpBasePath, false, pr.BaseRepo.ObjectFormatName); err != nil {
return nil, nil, fmt.Errorf("InitRepository[PR:%d]: %w", pr.ID, err)