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

+16-4
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

-12
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

+7-19
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

+4-14
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

+1-1
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

+1-1
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

+2-2
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

+1-1
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

+2-2
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

+1-1
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")

services/asymkey/sign.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ Loop:
204204
return false, "", nil, &ErrWontSign{twofa}
205205
}
206206
case parentSigned:
207-
gitRepo, err := gitrepo.OpenWikiRepository(ctx, repo)
207+
gitRepo, err := gitrepo.OpenRepository(ctx, repo.WikiStorageRepo())
208208
if err != nil {
209209
return false, "", nil, err
210210
}

services/doctor/misc.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ func checkScriptType(ctx context.Context, logger log.Logger, autofix bool) error
4949

5050
func checkHooks(ctx context.Context, logger log.Logger, autofix bool) error {
5151
if err := iterateRepositories(ctx, func(repo *repo_model.Repository) error {
52-
results, err := gitrepo.CheckDelegateHooksForRepo(ctx, repo)
52+
results, err := gitrepo.CheckDelegateHooks(ctx, repo)
5353
if err != nil {
5454
logger.Critical("Unable to check delegate hooks for repo %-v. ERROR: %v", repo, err)
5555
return fmt.Errorf("Unable to check delegate hooks for repo %-v. ERROR: %w", repo, err)
5656
}
5757
if len(results) > 0 && autofix {
5858
logger.Warn("Regenerated hooks for %s", repo.FullName())
59-
if err := gitrepo.CreateDelegateHooksForRepo(ctx, repo); err != nil {
59+
if err := gitrepo.CreateDelegateHooks(ctx, repo); err != nil {
6060
logger.Critical("Unable to recreate delegate hooks for %-v. ERROR: %v", repo, err)
6161
return fmt.Errorf("Unable to recreate delegate hooks for %-v. ERROR: %w", repo, err)
6262
}

services/mirror/mirror_push.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func runPushSync(ctx context.Context, m *repo_model.PushMirror) error {
143143

144144
var gitRepo *git.Repository
145145
if isWiki {
146-
gitRepo, err = gitrepo.OpenWikiRepository(ctx, repo)
146+
gitRepo, err = gitrepo.OpenRepository(ctx, repo.WikiStorageRepo())
147147
} else {
148148
gitRepo, err = gitrepo.OpenRepository(ctx, repo)
149149
}

services/repository/adopt.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func adoptRepository(ctx context.Context, repo *repo_model.Repository, defaultBr
115115
return fmt.Errorf("adoptRepository: path does not already exist: %s", repo.FullName())
116116
}
117117

118-
if err := gitrepo.CreateDelegateHooksForRepo(ctx, repo); err != nil {
118+
if err := gitrepo.CreateDelegateHooks(ctx, repo); err != nil {
119119
return fmt.Errorf("createDelegateHooks: %w", err)
120120
}
121121

services/repository/fork.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts Fork
170170
return fmt.Errorf("git update-server-info: %w", err)
171171
}
172172

173-
if err = gitrepo.CreateDelegateHooksForRepo(ctx, repo); err != nil {
173+
if err = gitrepo.CreateDelegateHooks(ctx, repo); err != nil {
174174
return fmt.Errorf("createDelegateHooks: %w", err)
175175
}
176176

services/repository/hooks.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ func SyncRepositoryHooks(ctx context.Context) error {
3131
default:
3232
}
3333

34-
if err := gitrepo.CreateDelegateHooksForRepo(ctx, repo); err != nil {
34+
if err := gitrepo.CreateDelegateHooks(ctx, repo); err != nil {
3535
return fmt.Errorf("SyncRepositoryHook: %w", err)
3636
}
3737
if repo.HasWiki() {
38-
if err := gitrepo.CreateDelegateHooksForWiki(ctx, repo); err != nil {
38+
if err := gitrepo.CreateDelegateHooks(ctx, repo.WikiStorageRepo()); err != nil {
3939
return fmt.Errorf("SyncRepositoryHook: %w", err)
4040
}
4141
}

services/repository/migrate.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,11 @@ func cleanUpMigrateGitConfig(ctx context.Context, repoPath string) error {
265265

266266
// CleanUpMigrateInfo finishes migrating repository and/or wiki with things that don't need to be done for mirrors.
267267
func CleanUpMigrateInfo(ctx context.Context, repo *repo_model.Repository) (*repo_model.Repository, error) {
268-
if err := gitrepo.CreateDelegateHooksForRepo(ctx, repo); err != nil {
268+
if err := gitrepo.CreateDelegateHooks(ctx, repo); err != nil {
269269
return repo, fmt.Errorf("createDelegateHooks: %w", err)
270270
}
271271
if repo.HasWiki() {
272-
if err := gitrepo.CreateDelegateHooksForWiki(ctx, repo); err != nil {
272+
if err := gitrepo.CreateDelegateHooks(ctx, repo.WikiStorageRepo()); err != nil {
273273
return repo, fmt.Errorf("createDelegateHooks.(wiki): %w", err)
274274
}
275275
}

services/repository/transfer.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -331,12 +331,13 @@ func changeRepositoryName(ctx context.Context, repo *repo_model.Repository, newR
331331
return fmt.Errorf("IsRepositoryExist: %w", err)
332332
} else if has {
333333
return repo_model.ErrRepoAlreadyExist{
334-
Uname: repo.Owner.Name,
334+
Uname: repo.OwnerName,
335335
Name: newRepoName,
336336
}
337337
}
338338

339-
if err = gitrepo.RenameRepository(ctx, repo, newRepoName); err != nil {
339+
if err = gitrepo.RenameRepository(ctx, repo,
340+
repo_model.StorageRepo(repo_model.RelativePath(repo.OwnerName, newRepoName))); err != nil {
340341
return fmt.Errorf("rename repository directory: %w", err)
341342
}
342343

services/wiki/wiki.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func InitWiki(ctx context.Context, repo *repo_model.Repository) error {
4141

4242
if err := git.InitRepository(ctx, repo.WikiPath(), true, repo.ObjectFormatName); err != nil {
4343
return fmt.Errorf("InitRepository: %w", err)
44-
} else if err = gitrepo.CreateDelegateHooksForWiki(ctx, repo); err != nil {
44+
} else if err = gitrepo.CreateDelegateHooks(ctx, repo.WikiStorageRepo()); err != nil {
4545
return fmt.Errorf("createDelegateHooks: %w", err)
4646
} else if _, _, err = git.NewCommand("symbolic-ref", "HEAD").AddDynamicArguments(git.BranchPrefix+repo.DefaultWikiBranch).RunStdString(ctx, &git.RunOpts{Dir: repo.WikiPath()}); err != nil {
4747
return fmt.Errorf("unable to set default wiki branch to %q: %w", repo.DefaultWikiBranch, err)
@@ -100,7 +100,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
100100
return fmt.Errorf("InitWiki: %w", err)
101101
}
102102

103-
hasDefaultBranch := gitrepo.IsWikiBranchExist(ctx, repo, repo.DefaultWikiBranch)
103+
hasDefaultBranch := gitrepo.IsBranchExist(ctx, repo.WikiStorageRepo(), repo.DefaultWikiBranch)
104104

105105
basePath, err := repo_module.CreateTemporaryPath("update-wiki")
106106
if err != nil {
@@ -381,15 +381,15 @@ func ChangeDefaultWikiBranch(ctx context.Context, repo *repo_model.Repository, n
381381
return nil
382382
}
383383

384-
oldDefBranch, err := gitrepo.GetWikiDefaultBranch(ctx, repo)
384+
oldDefBranch, err := gitrepo.GetDefaultBranch(ctx, repo.WikiStorageRepo())
385385
if err != nil {
386386
return fmt.Errorf("unable to get default branch: %w", err)
387387
}
388388
if oldDefBranch == newBranch {
389389
return nil
390390
}
391391

392-
gitRepo, err := gitrepo.OpenWikiRepository(ctx, repo)
392+
gitRepo, err := gitrepo.OpenRepository(ctx, repo.WikiStorageRepo())
393393
if errors.Is(err, util.ErrNotExist) {
394394
return nil // no git repo on storage, no need to do anything else
395395
} else if err != nil {

services/wiki/wiki_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func TestRepository_AddWikiPage(t *testing.T) {
166166
webPath := UserTitleToWebPath("", userTitle)
167167
assert.NoError(t, AddWikiPage(git.DefaultContext, doer, repo, webPath, wikiContent, commitMsg))
168168
// Now need to show that the page has been added:
169-
gitRepo, err := gitrepo.OpenWikiRepository(git.DefaultContext, repo)
169+
gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo.WikiStorageRepo())
170170
require.NoError(t, err)
171171

172172
defer gitRepo.Close()
@@ -213,7 +213,7 @@ func TestRepository_EditWikiPage(t *testing.T) {
213213
assert.NoError(t, EditWikiPage(git.DefaultContext, doer, repo, "Home", webPath, newWikiContent, commitMsg))
214214

215215
// Now need to show that the page has been added:
216-
gitRepo, err := gitrepo.OpenWikiRepository(git.DefaultContext, repo)
216+
gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo.WikiStorageRepo())
217217
assert.NoError(t, err)
218218
masterTree, err := gitRepo.GetTree(repo.DefaultWikiBranch)
219219
assert.NoError(t, err)
@@ -237,7 +237,7 @@ func TestRepository_DeleteWikiPage(t *testing.T) {
237237
assert.NoError(t, DeleteWikiPage(git.DefaultContext, doer, repo, "Home"))
238238

239239
// Now need to show that the page has been added:
240-
gitRepo, err := gitrepo.OpenWikiRepository(git.DefaultContext, repo)
240+
gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo.WikiStorageRepo())
241241
require.NoError(t, err)
242242

243243
defer gitRepo.Close()
@@ -251,7 +251,7 @@ func TestRepository_DeleteWikiPage(t *testing.T) {
251251
func TestPrepareWikiFileName(t *testing.T) {
252252
unittest.PrepareTestEnv(t)
253253
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
254-
gitRepo, err := gitrepo.OpenWikiRepository(git.DefaultContext, repo)
254+
gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo.WikiStorageRepo())
255255
require.NoError(t, err)
256256

257257
defer gitRepo.Close()

0 commit comments

Comments
 (0)