Don't create duplicated functions for code repositories and wiki repositories (#33924)

Fix
https://github.com/go-gitea/gitea/pull/33910#pullrequestreview-2688913865

This PR changed the Repositroy interface in `gitrepo` package which
makes it only focus the relative path in the disk and abstract whether
it's a wiki repository or not.
This commit is contained in:
Lunny Xiao
2025-03-19 11:17:19 -07:00
committed by GitHub
parent e25f860735
commit a9e8ac0fe0
20 changed files with 56 additions and 77 deletions

View File

@ -8,7 +8,6 @@ import (
"fmt"
"io"
"path/filepath"
"strings"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/reqctx"
@ -16,21 +15,15 @@ import (
"code.gitea.io/gitea/modules/util"
)
// Repository represents a git repository which stored in a disk
type Repository interface {
GetName() string
GetOwnerName() string
}
func absPath(owner, name string) string {
return filepath.Join(setting.RepoRootPath, strings.ToLower(owner), strings.ToLower(name)+".git")
RelativePath() string // We don't assume how the directory structure of the repository is, so we only need the relative path
}
// RelativePath should be an unix style path like username/reponame.git
// This method should change it according to the current OS.
func repoPath(repo Repository) string {
return absPath(repo.GetOwnerName(), repo.GetName())
}
func wikiPath(repo Repository) string {
return filepath.Join(setting.RepoRootPath, strings.ToLower(repo.GetOwnerName()), strings.ToLower(repo.GetName())+".wiki.git")
return filepath.Join(setting.RepoRootPath, filepath.FromSlash(repo.RelativePath()))
}
// OpenRepository opens the repository at the given relative path with the provided context.
@ -38,10 +31,6 @@ func OpenRepository(ctx context.Context, repo Repository) (*git.Repository, erro
return git.OpenRepository(ctx, repoPath(repo))
}
func OpenWikiRepository(ctx context.Context, repo Repository) (*git.Repository, error) {
return git.OpenRepository(ctx, wikiPath(repo))
}
// contextKey is a value for use with context.WithValue.
type contextKey struct {
repoPath string
@ -86,9 +75,8 @@ func DeleteRepository(ctx context.Context, repo Repository) error {
}
// RenameRepository renames a repository's name on disk
func RenameRepository(ctx context.Context, repo Repository, newName string) error {
newRepoPath := absPath(repo.GetOwnerName(), newName)
if err := util.Rename(repoPath(repo), newRepoPath); err != nil {
func RenameRepository(ctx context.Context, repo, newRepo Repository) error {
if err := util.Rename(repoPath(repo), repoPath(newRepo)); err != nil {
return fmt.Errorf("rename repository directory: %w", err)
}
return nil