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

View File

@@ -3,14 +3,18 @@
package gitrepo
import "strings"
import (
"strings"
"gitea.dev/modules/util"
)
func RepoCodeGitRepoRelativePath(ownerName, repoName string) string {
return strings.ToLower(ownerName) + "/" + strings.ToLower(repoName) + ".git"
return util.PathJoinRelX(strings.ToLower(ownerName), strings.ToLower(repoName)+".git")
}
func RepoWikiGitRepoRelativePath(ownerName, repoName string) string {
return strings.ToLower(ownerName) + "/" + strings.ToLower(repoName) + ".wiki.git"
return util.PathJoinRelX(strings.ToLower(ownerName), strings.ToLower(repoName)+".wiki.git")
}
// CodeRepoByName returns an unmanaged repository facade for the code repository of the given owner and repository name.

View File

@@ -4,7 +4,10 @@
package gitrepo
import (
"io/fs"
"os"
"path/filepath"
"strings"
"sync/atomic"
"gitea.dev/modules/setting"
@@ -39,6 +42,13 @@ func RepoLocalPath(repo RepositoryFacade) string {
return setting.RepoRootPath + string(filepath.Separator) + filepath.FromSlash(repoLoc)
}
func UserLocalPath(userName string) string {
if setting.RepoRootPath == "" {
panic("repo root path is not initialized")
}
return filepath.Join(setting.RepoRootPath, filepath.Clean(strings.ToLower(userName)))
}
func repoLogNameByLocation(loc string) string {
t := filepath.FromSlash(loc)
// hide the parent paths, then the name should be safe for end users
@@ -100,3 +110,7 @@ func (r *repositoryManaged) GitRepoLocation() string {
func RepositoryManaged(id, loc string) RepositoryFacade {
return &repositoryManaged{id: id, loc: filepath.Clean(loc)}
}
func RepoLocalFS(repo RepositoryFacade) fs.FS {
return os.DirFS(RepoLocalPath(repo))
}

View File

@@ -7,7 +7,6 @@ import (
"context"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
@@ -43,10 +42,6 @@ func InitRepository(ctx context.Context, repo RepositoryFacade, objectFormatName
return InitRepositoryLocal(ctx, gitrepo.RepoLocalPath(repo), true, objectFormatName)
}
func GetRepoFS(repo RepositoryFacade) fs.FS {
return os.DirFS(gitrepo.RepoLocalPath(repo))
}
func IsRepoFileExist(ctx context.Context, repo RepositoryFacade, relativeFilePath string) (bool, error) {
absoluteFilePath := filepath.Join(gitrepo.RepoLocalPath(repo), relativeFilePath)
return util.IsExist(absoluteFilePath)

View File

@@ -15,6 +15,7 @@ import (
"strings"
"gitea.dev/modules/git/gitcmd"
"gitea.dev/modules/git/gitrepo"
)
type lineCountWriter struct {
@@ -113,7 +114,7 @@ func (repo *Repository) GetFilesChangedBetween(ctx context.Context, base, head s
// ReadPatchCommit will check if a diff patch exists and return stats
func (repo *Repository) ReadPatchCommit(prID int64) (commitSHA string, err error) {
// Migrated repositories download patches to "pulls" location
repoFS := GetRepoFS(repo)
repoFS := gitrepo.RepoLocalFS(repo)
loadPatch, err := repoFS.Open(fmt.Sprintf("pulls/%d.patch", prID))
if err != nil {
return "", err