Skip to content

Commit a9e8ac0

Browse files
authored
Don't create duplicated functions for code repositories and wiki repositories (#33924)
Fix #33910 (review) 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.
1 parent e25f860 commit a9e8ac0

File tree

20 files changed

+56
-77
lines changed

20 files changed

+56
-77
lines changed

models/repo/repo.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,24 @@ func init() {
215215
db.RegisterModel(new(Repository))
216216
}
217217

218-
func (repo *Repository) GetName() string {
219-
return repo.Name
218+
func RelativePath(ownerName, repoName string) string {
219+
return strings.ToLower(ownerName) + "/" + strings.ToLower(repoName) + ".git"
220220
}
221221

222-
func (repo *Repository) GetOwnerName() string {
223-
return repo.OwnerName
222+
// RelativePath should be an unix style path like username/reponame.git
223+
func (repo *Repository) RelativePath() string {
224+
return RelativePath(repo.OwnerName, repo.Name)
225+
}
226+
227+
type StorageRepo string
228+
229+
// RelativePath should be an unix style path like username/reponame.git
230+
func (sr StorageRepo) RelativePath() string {
231+
return string(sr)
232+
}
233+
234+
func (repo *Repository) WikiStorageRepo() StorageRepo {
235+
return StorageRepo(strings.ToLower(repo.OwnerName) + "/" + strings.ToLower(repo.Name) + ".wiki.git")
224236
}
225237

226238
// SanitizedOriginalURL returns a sanitized OriginalURL

modules/gitrepo/branch.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,12 @@ func GetDefaultBranch(ctx context.Context, repo Repository) (string, error) {
4444
return git.GetDefaultBranch(ctx, repoPath(repo))
4545
}
4646

47-
func GetWikiDefaultBranch(ctx context.Context, repo Repository) (string, error) {
48-
return git.GetDefaultBranch(ctx, wikiPath(repo))
49-
}
50-
5147
// IsReferenceExist returns true if given reference exists in the repository.
5248
func IsReferenceExist(ctx context.Context, repo Repository, name string) bool {
5349
return git.IsReferenceExist(ctx, repoPath(repo), name)
5450
}
5551

56-
func IsWikiReferenceExist(ctx context.Context, repo Repository, name string) bool {
57-
return git.IsReferenceExist(ctx, wikiPath(repo), name)
58-
}
59-
6052
// IsBranchExist returns true if given branch exists in the repository.
6153
func IsBranchExist(ctx context.Context, repo Repository, name string) bool {
6254
return IsReferenceExist(ctx, repo, git.BranchPrefix+name)
6355
}
64-
65-
func IsWikiBranchExist(ctx context.Context, repo Repository, name string) bool {
66-
return IsWikiReferenceExist(ctx, repo, git.BranchPrefix+name)
67-
}

modules/gitrepo/gitrepo.go

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,29 @@ import (
88
"fmt"
99
"io"
1010
"path/filepath"
11-
"strings"
1211

1312
"code.gitea.io/gitea/modules/git"
1413
"code.gitea.io/gitea/modules/reqctx"
1514
"code.gitea.io/gitea/modules/setting"
1615
"code.gitea.io/gitea/modules/util"
1716
)
1817

18+
// Repository represents a git repository which stored in a disk
1919
type Repository interface {
20-
GetName() string
21-
GetOwnerName() string
22-
}
23-
24-
func absPath(owner, name string) string {
25-
return filepath.Join(setting.RepoRootPath, strings.ToLower(owner), strings.ToLower(name)+".git")
20+
RelativePath() string // We don't assume how the directory structure of the repository is, so we only need the relative path
2621
}
2722

23+
// RelativePath should be an unix style path like username/reponame.git
24+
// This method should change it according to the current OS.
2825
func repoPath(repo Repository) string {
29-
return absPath(repo.GetOwnerName(), repo.GetName())
30-
}
31-
32-
func wikiPath(repo Repository) string {
33-
return filepath.Join(setting.RepoRootPath, strings.ToLower(repo.GetOwnerName()), strings.ToLower(repo.GetName())+".wiki.git")
26+
return filepath.Join(setting.RepoRootPath, filepath.FromSlash(repo.RelativePath()))
3427
}
3528

3629
// OpenRepository opens the repository at the given relative path with the provided context.
3730
func OpenRepository(ctx context.Context, repo Repository) (*git.Repository, error) {
3831
return git.OpenRepository(ctx, repoPath(repo))
3932
}
4033

41-
func OpenWikiRepository(ctx context.Context, repo Repository) (*git.Repository, error) {
42-
return git.OpenRepository(ctx, wikiPath(repo))
43-
}
44-
4534
// contextKey is a value for use with context.WithValue.
4635
type contextKey struct {
4736
repoPath string
@@ -86,9 +75,8 @@ func DeleteRepository(ctx context.Context, repo Repository) error {
8675
}
8776

8877
// RenameRepository renames a repository's name on disk
89-
func RenameRepository(ctx context.Context, repo Repository, newName string) error {
90-
newRepoPath := absPath(repo.GetOwnerName(), newName)
91-
if err := util.Rename(repoPath(repo), newRepoPath); err != nil {
78+
func RenameRepository(ctx context.Context, repo, newRepo Repository) error {
79+
if err := util.Rename(repoPath(repo), repoPath(newRepo)); err != nil {
9280
return fmt.Errorf("rename repository directory: %w", err)
9381
}
9482
return nil

modules/gitrepo/hooks.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,11 @@ done
106106
return hookNames, hookTpls, giteaHookTpls
107107
}
108108

109-
// CreateDelegateHooksForRepo creates all the hooks scripts for the repo
110-
func CreateDelegateHooksForRepo(_ context.Context, repo Repository) (err error) {
109+
// CreateDelegateHooks creates all the hooks scripts for the repo
110+
func CreateDelegateHooks(_ context.Context, repo Repository) (err error) {
111111
return createDelegateHooks(filepath.Join(repoPath(repo), "hooks"))
112112
}
113113

114-
// CreateDelegateHooksForWiki creates all the hooks scripts for the wiki repo
115-
func CreateDelegateHooksForWiki(_ context.Context, repo Repository) (err error) {
116-
return createDelegateHooks(filepath.Join(wikiPath(repo), "hooks"))
117-
}
118-
119114
func createDelegateHooks(hookDir string) (err error) {
120115
hookNames, hookTpls, giteaHookTpls := getHookTemplates()
121116

@@ -178,16 +173,11 @@ func ensureExecutable(filename string) error {
178173
return os.Chmod(filename, mode)
179174
}
180175

181-
// CheckDelegateHooksForRepo checks the hooks scripts for the repo
182-
func CheckDelegateHooksForRepo(_ context.Context, repo Repository) ([]string, error) {
176+
// CheckDelegateHooks checks the hooks scripts for the repo
177+
func CheckDelegateHooks(_ context.Context, repo Repository) ([]string, error) {
183178
return checkDelegateHooks(filepath.Join(repoPath(repo), "hooks"))
184179
}
185180

186-
// CheckDelegateHooksForWiki checks the hooks scripts for the repo
187-
func CheckDelegateHooksForWiki(_ context.Context, repo Repository) ([]string, error) {
188-
return checkDelegateHooks(filepath.Join(wikiPath(repo), "hooks"))
189-
}
190-
191181
func checkDelegateHooks(hookDir string) ([]string, error) {
192182
hookNames, hookTpls, giteaHookTpls := getHookTemplates()
193183

modules/repository/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func CheckInitRepository(ctx context.Context, repo *repo_model.Repository) (err
138138
// Init git bare new repository.
139139
if err = git.InitRepository(ctx, repo.RepoPath(), true, repo.ObjectFormatName); err != nil {
140140
return fmt.Errorf("git.InitRepository: %w", err)
141-
} else if err = gitrepo.CreateDelegateHooksForRepo(ctx, repo); err != nil {
141+
} else if err = gitrepo.CreateDelegateHooks(ctx, repo); err != nil {
142142
return fmt.Errorf("createDelegateHooks: %w", err)
143143
}
144144
return nil

routers/api/v1/repo/wiki.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ func findEntryForFile(commit *git.Commit, target string) (*git.TreeEntry, error)
476476
// findWikiRepoCommit opens the wiki repo and returns the latest commit, writing to context on error.
477477
// The caller is responsible for closing the returned repo again
478478
func findWikiRepoCommit(ctx *context.APIContext) (*git.Repository, *git.Commit) {
479-
wikiRepo, err := gitrepo.OpenWikiRepository(ctx, ctx.Repo.Repository)
479+
wikiRepo, err := gitrepo.OpenRepository(ctx, ctx.Repo.Repository.WikiStorageRepo())
480480
if err != nil {
481481
if git.IsErrNotExist(err) || err.Error() == "no such file or directory" {
482482
ctx.APIErrorNotFound(err)

routers/web/repo/commit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ func Diff(ctx *context.Context) {
284284
)
285285

286286
if ctx.Data["PageIsWiki"] != nil {
287-
gitRepo, err = gitrepo.OpenWikiRepository(ctx, ctx.Repo.Repository)
287+
gitRepo, err = gitrepo.OpenRepository(ctx, ctx.Repo.Repository.WikiStorageRepo())
288288
if err != nil {
289289
ctx.ServerError("Repo.GitRepo.GetCommit", err)
290290
return
@@ -417,7 +417,7 @@ func Diff(ctx *context.Context) {
417417
func RawDiff(ctx *context.Context) {
418418
var gitRepo *git.Repository
419419
if ctx.Data["PageIsWiki"] != nil {
420-
wikiRepo, err := gitrepo.OpenWikiRepository(ctx, ctx.Repo.Repository)
420+
wikiRepo, err := gitrepo.OpenRepository(ctx, ctx.Repo.Repository.WikiStorageRepo())
421421
if err != nil {
422422
ctx.ServerError("OpenRepository", err)
423423
return

routers/web/repo/compare.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ func ExcerptBlob(ctx *context.Context) {
885885
gitRepo := ctx.Repo.GitRepo
886886
if ctx.Data["PageIsWiki"] == true {
887887
var err error
888-
gitRepo, err = gitrepo.OpenWikiRepository(ctx, ctx.Repo.Repository)
888+
gitRepo, err = gitrepo.OpenRepository(ctx, ctx.Repo.Repository.WikiStorageRepo())
889889
if err != nil {
890890
ctx.ServerError("OpenRepository", err)
891891
return

routers/web/repo/wiki.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func findEntryForFile(commit *git.Commit, target string) (*git.TreeEntry, error)
9696
}
9797

9898
func findWikiRepoCommit(ctx *context.Context) (*git.Repository, *git.Commit, error) {
99-
wikiGitRepo, errGitRepo := gitrepo.OpenWikiRepository(ctx, ctx.Repo.Repository)
99+
wikiGitRepo, errGitRepo := gitrepo.OpenRepository(ctx, ctx.Repo.Repository.WikiStorageRepo())
100100
if errGitRepo != nil {
101101
ctx.ServerError("OpenRepository", errGitRepo)
102102
return nil, nil, errGitRepo
@@ -105,7 +105,7 @@ func findWikiRepoCommit(ctx *context.Context) (*git.Repository, *git.Commit, err
105105
commit, errCommit := wikiGitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultWikiBranch)
106106
if git.IsErrNotExist(errCommit) {
107107
// if the default branch recorded in database is out of sync, then re-sync it
108-
gitRepoDefaultBranch, errBranch := gitrepo.GetWikiDefaultBranch(ctx, ctx.Repo.Repository)
108+
gitRepoDefaultBranch, errBranch := gitrepo.GetDefaultBranch(ctx, ctx.Repo.Repository.WikiStorageRepo())
109109
if errBranch != nil {
110110
return wikiGitRepo, nil, errBranch
111111
}

routers/web/repo/wiki_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const (
2929
)
3030

3131
func wikiEntry(t *testing.T, repo *repo_model.Repository, wikiName wiki_service.WebPath) *git.TreeEntry {
32-
wikiRepo, err := gitrepo.OpenWikiRepository(git.DefaultContext, repo)
32+
wikiRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo.WikiStorageRepo())
3333
assert.NoError(t, err)
3434
defer wikiRepo.Close()
3535
commit, err := wikiRepo.GetBranchCommit("master")

0 commit comments

Comments
 (0)