diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2025-03-19 11:17:19 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-19 11:17:19 -0700 |
commit | a9e8ac0fe0084af33a8e2bb0b3d8d3971755276e (patch) | |
tree | 4630eb7fe5eff7d3c7c36bd6e6158cc2da152eaa /modules | |
parent | e25f860735e27ff8f62c6a256bfbda34e8491acb (diff) | |
download | gitea-a9e8ac0fe0084af33a8e2bb0b3d8d3971755276e.tar.gz gitea-a9e8ac0fe0084af33a8e2bb0b3d8d3971755276e.zip |
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.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/gitrepo/branch.go | 12 | ||||
-rw-r--r-- | modules/gitrepo/gitrepo.go | 26 | ||||
-rw-r--r-- | modules/gitrepo/hooks.go | 18 | ||||
-rw-r--r-- | modules/repository/init.go | 2 |
4 files changed, 12 insertions, 46 deletions
diff --git a/modules/gitrepo/branch.go b/modules/gitrepo/branch.go index 9c4bdc5bdf..25ea5abfca 100644 --- a/modules/gitrepo/branch.go +++ b/modules/gitrepo/branch.go @@ -44,24 +44,12 @@ func GetDefaultBranch(ctx context.Context, repo Repository) (string, error) { return git.GetDefaultBranch(ctx, repoPath(repo)) } -func GetWikiDefaultBranch(ctx context.Context, repo Repository) (string, error) { - return git.GetDefaultBranch(ctx, wikiPath(repo)) -} - // IsReferenceExist returns true if given reference exists in the repository. func IsReferenceExist(ctx context.Context, repo Repository, name string) bool { return git.IsReferenceExist(ctx, repoPath(repo), name) } -func IsWikiReferenceExist(ctx context.Context, repo Repository, name string) bool { - return git.IsReferenceExist(ctx, wikiPath(repo), name) -} - // IsBranchExist returns true if given branch exists in the repository. func IsBranchExist(ctx context.Context, repo Repository, name string) bool { return IsReferenceExist(ctx, repo, git.BranchPrefix+name) } - -func IsWikiBranchExist(ctx context.Context, repo Repository, name string) bool { - return IsWikiReferenceExist(ctx, repo, git.BranchPrefix+name) -} diff --git a/modules/gitrepo/gitrepo.go b/modules/gitrepo/gitrepo.go index 5e2ec9ed1e..5da65e2452 100644 --- a/modules/gitrepo/gitrepo.go +++ b/modules/gitrepo/gitrepo.go @@ -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 diff --git a/modules/gitrepo/hooks.go b/modules/gitrepo/hooks.go index cf44f792c6..d9d4a88ff1 100644 --- a/modules/gitrepo/hooks.go +++ b/modules/gitrepo/hooks.go @@ -106,16 +106,11 @@ done return hookNames, hookTpls, giteaHookTpls } -// CreateDelegateHooksForRepo creates all the hooks scripts for the repo -func CreateDelegateHooksForRepo(_ context.Context, repo Repository) (err error) { +// CreateDelegateHooks creates all the hooks scripts for the repo +func CreateDelegateHooks(_ context.Context, repo Repository) (err error) { return createDelegateHooks(filepath.Join(repoPath(repo), "hooks")) } -// CreateDelegateHooksForWiki creates all the hooks scripts for the wiki repo -func CreateDelegateHooksForWiki(_ context.Context, repo Repository) (err error) { - return createDelegateHooks(filepath.Join(wikiPath(repo), "hooks")) -} - func createDelegateHooks(hookDir string) (err error) { hookNames, hookTpls, giteaHookTpls := getHookTemplates() @@ -178,16 +173,11 @@ func ensureExecutable(filename string) error { return os.Chmod(filename, mode) } -// CheckDelegateHooksForRepo checks the hooks scripts for the repo -func CheckDelegateHooksForRepo(_ context.Context, repo Repository) ([]string, error) { +// CheckDelegateHooks checks the hooks scripts for the repo +func CheckDelegateHooks(_ context.Context, repo Repository) ([]string, error) { return checkDelegateHooks(filepath.Join(repoPath(repo), "hooks")) } -// CheckDelegateHooksForWiki checks the hooks scripts for the repo -func CheckDelegateHooksForWiki(_ context.Context, repo Repository) ([]string, error) { - return checkDelegateHooks(filepath.Join(wikiPath(repo), "hooks")) -} - func checkDelegateHooks(hookDir string) ([]string, error) { hookNames, hookTpls, giteaHookTpls := getHookTemplates() diff --git a/modules/repository/init.go b/modules/repository/init.go index 3fc1261baa..ace21254ba 100644 --- a/modules/repository/init.go +++ b/modules/repository/init.go @@ -138,7 +138,7 @@ func CheckInitRepository(ctx context.Context, repo *repo_model.Repository) (err // Init git bare new repository. if err = git.InitRepository(ctx, repo.RepoPath(), true, repo.ObjectFormatName); err != nil { return fmt.Errorf("git.InitRepository: %w", err) - } else if err = gitrepo.CreateDelegateHooksForRepo(ctx, repo); err != nil { + } else if err = gitrepo.CreateDelegateHooks(ctx, repo); err != nil { return fmt.Errorf("createDelegateHooks: %w", err) } return nil |