Skip to content

Commit 4e1b8db

Browse files
authored
Move HasWiki to repository service package (#33912)
Move HasWiki out of the models package to avoid referencing the absolute wiki path directly.
1 parent ea96ff6 commit 4e1b8db

File tree

17 files changed

+76
-54
lines changed

17 files changed

+76
-54
lines changed

models/repo/repo.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ func RelativePath(ownerName, repoName string) string {
229229
return strings.ToLower(ownerName) + "/" + strings.ToLower(repoName) + ".git"
230230
}
231231

232+
func RelativeWikiPath(ownerName, repoName string) string {
233+
return strings.ToLower(ownerName) + "/" + strings.ToLower(repoName) + ".wiki.git"
234+
}
235+
232236
// RelativePath should be an unix style path like username/reponame.git
233237
func (repo *Repository) RelativePath() string {
234238
return RelativePath(repo.OwnerName, repo.Name)
@@ -242,7 +246,7 @@ func (sr StorageRepo) RelativePath() string {
242246
}
243247

244248
func (repo *Repository) WikiStorageRepo() StorageRepo {
245-
return StorageRepo(strings.ToLower(repo.OwnerName) + "/" + strings.ToLower(repo.Name) + ".wiki.git")
249+
return StorageRepo(RelativeWikiPath(repo.OwnerName, repo.Name))
246250
}
247251

248252
// SanitizedOriginalURL returns a sanitized OriginalURL

models/repo/wiki.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"strings"
1212

1313
user_model "code.gitea.io/gitea/models/user"
14-
"code.gitea.io/gitea/modules/log"
1514
"code.gitea.io/gitea/modules/util"
1615
)
1716

@@ -86,12 +85,3 @@ func WikiPath(userName, repoName string) string {
8685
func (repo *Repository) WikiPath() string {
8786
return WikiPath(repo.OwnerName, repo.Name)
8887
}
89-
90-
// HasWiki returns true if repository has wiki.
91-
func (repo *Repository) HasWiki() bool {
92-
isDir, err := util.IsDir(repo.WikiPath())
93-
if err != nil {
94-
log.Error("Unable to check if %s is a directory: %v", repo.WikiPath(), err)
95-
}
96-
return isDir
97-
}

models/repo/wiki_test.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,3 @@ func TestRepository_WikiPath(t *testing.T) {
3535
expected := filepath.Join(setting.RepoRootPath, "user2/repo1.wiki.git")
3636
assert.Equal(t, expected, repo.WikiPath())
3737
}
38-
39-
func TestRepository_HasWiki(t *testing.T) {
40-
unittest.PrepareTestEnv(t)
41-
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
42-
assert.True(t, repo1.HasWiki())
43-
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
44-
assert.False(t, repo2.HasWiki())
45-
}

modules/gitrepo/gitrepo.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ func IsRepositoryExist(ctx context.Context, repo Repository) (bool, error) {
6969
return util.IsExist(repoPath(repo))
7070
}
7171

72-
// DeleteRepository deletes the repository directory from the disk
72+
// DeleteRepository deletes the repository directory from the disk, it will return
73+
// nil if the repository does not exist.
7374
func DeleteRepository(ctx context.Context, repo Repository) error {
7475
return util.RemoveAll(repoPath(repo))
7576
}

routers/web/repo/wiki.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"code.gitea.io/gitea/services/forms"
3434
git_service "code.gitea.io/gitea/services/git"
3535
notify_service "code.gitea.io/gitea/services/notify"
36+
repo_service "code.gitea.io/gitea/services/repository"
3637
wiki_service "code.gitea.io/gitea/services/wiki"
3738
)
3839

@@ -474,7 +475,7 @@ func Wiki(ctx *context.Context) {
474475
return
475476
}
476477

477-
if !ctx.Repo.Repository.HasWiki() {
478+
if !repo_service.HasWiki(ctx, ctx.Repo.Repository) {
478479
ctx.Data["Title"] = ctx.Tr("repo.wiki")
479480
ctx.HTML(http.StatusOK, tplWikiStart)
480481
return
@@ -510,7 +511,7 @@ func Wiki(ctx *context.Context) {
510511
func WikiRevision(ctx *context.Context) {
511512
ctx.Data["CanWriteWiki"] = ctx.Repo.CanWrite(unit.TypeWiki) && !ctx.Repo.Repository.IsArchived
512513

513-
if !ctx.Repo.Repository.HasWiki() {
514+
if !repo_service.HasWiki(ctx, ctx.Repo.Repository) {
514515
ctx.Data["Title"] = ctx.Tr("repo.wiki")
515516
ctx.HTML(http.StatusOK, tplWikiStart)
516517
return
@@ -540,7 +541,7 @@ func WikiRevision(ctx *context.Context) {
540541

541542
// WikiPages render wiki pages list page
542543
func WikiPages(ctx *context.Context) {
543-
if !ctx.Repo.Repository.HasWiki() {
544+
if !repo_service.HasWiki(ctx, ctx.Repo.Repository) {
544545
ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
545546
return
546547
}
@@ -648,7 +649,7 @@ func WikiRaw(ctx *context.Context) {
648649
func NewWiki(ctx *context.Context) {
649650
ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
650651

651-
if !ctx.Repo.Repository.HasWiki() {
652+
if !repo_service.HasWiki(ctx, ctx.Repo.Repository) {
652653
ctx.Data["title"] = "Home"
653654
}
654655
if ctx.FormString("title") != "" {
@@ -701,7 +702,7 @@ func NewWikiPost(ctx *context.Context) {
701702
func EditWiki(ctx *context.Context) {
702703
ctx.Data["PageIsWikiEdit"] = true
703704

704-
if !ctx.Repo.Repository.HasWiki() {
705+
if !repo_service.HasWiki(ctx, ctx.Repo.Repository) {
705706
ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
706707
return
707708
}

routers/web/repo/wiki_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"code.gitea.io/gitea/modules/web"
1717
"code.gitea.io/gitea/services/contexttest"
1818
"code.gitea.io/gitea/services/forms"
19+
repo_service "code.gitea.io/gitea/services/repository"
1920
wiki_service "code.gitea.io/gitea/services/wiki"
2021

2122
"github.com/stretchr/testify/assert"
@@ -240,7 +241,7 @@ func TestDefaultWikiBranch(t *testing.T) {
240241

241242
// repo with no wiki
242243
repoWithNoWiki := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
243-
assert.False(t, repoWithNoWiki.HasWiki())
244+
assert.False(t, repo_service.HasWiki(t.Context(), repoWithNoWiki))
244245
assert.NoError(t, wiki_service.ChangeDefaultWikiBranch(t.Context(), repoWithNoWiki, "main"))
245246

246247
// repo with wiki

services/migrations/gitea_uploader_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func TestGiteaUploadRepo(t *testing.T) {
6363
assert.NoError(t, err)
6464

6565
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: user.ID, Name: repoName})
66-
assert.True(t, repo.HasWiki())
66+
assert.True(t, repo_service.HasWiki(ctx, repo))
6767
assert.Equal(t, repo_model.RepositoryReady, repo.Status)
6868

6969
milestones, err := db.Find[issues_model.Milestone](t.Context(), issues_model.FindMilestoneOptions{

services/mirror/mirror_pull.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func UpdateAddress(ctx context.Context, m *repo_model.Mirror, addr string) error
5252
return err
5353
}
5454

55-
if m.Repo.HasWiki() {
55+
if repo_service.HasWiki(ctx, m.Repo) {
5656
wikiPath := m.Repo.WikiPath()
5757
wikiRemotePath := repo_module.WikiRemoteURL(ctx, addr)
5858
// Remove old remote of wiki
@@ -347,7 +347,7 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo
347347
endpoint := lfs.DetermineEndpoint(remoteURL.String(), m.LFSEndpoint)
348348
lfsClient := lfs.NewClient(endpoint, nil)
349349
if err = repo_module.StoreMissingLfsObjectsInRepository(ctx, m.Repo, gitRepo, lfsClient); err != nil {
350-
log.Error("SyncMirrors [repo: %-v]: failed to synchronize LFS objects for repository: %v", m.Repo, err)
350+
log.Error("SyncMirrors [repo: %-v]: failed to synchronize LFS objects for repository: %v", m.Repo.FullName(), err)
351351
}
352352
}
353353

@@ -364,10 +364,10 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo
364364

365365
log.Trace("SyncMirrors [repo: %-v]: updating size of repository", m.Repo)
366366
if err := repo_module.UpdateRepoSize(ctx, m.Repo); err != nil {
367-
log.Error("SyncMirrors [repo: %-v]: failed to update size for mirror repository: %v", m.Repo, err)
367+
log.Error("SyncMirrors [repo: %-v]: failed to update size for mirror repository: %v", m.Repo.FullName(), err)
368368
}
369369

370-
if m.Repo.HasWiki() {
370+
if repo_service.HasWiki(ctx, m.Repo) {
371371
log.Trace("SyncMirrors [repo: %-v Wiki]: running git remote update...", m.Repo)
372372
stderrBuilder.Reset()
373373
stdoutBuilder.Reset()

services/mirror/mirror_push.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"code.gitea.io/gitea/modules/setting"
2424
"code.gitea.io/gitea/modules/timeutil"
2525
"code.gitea.io/gitea/modules/util"
26+
repo_service "code.gitea.io/gitea/services/repository"
2627
)
2728

2829
var stripExitStatus = regexp.MustCompile(`exit status \d+ - `)
@@ -47,7 +48,7 @@ func AddPushMirrorRemote(ctx context.Context, m *repo_model.PushMirror, addr str
4748
return err
4849
}
4950

50-
if m.Repo.HasWiki() {
51+
if repo_service.HasWiki(ctx, m.Repo) {
5152
wikiRemoteURL := repository.WikiRemoteURL(ctx, addr)
5253
if len(wikiRemoteURL) > 0 {
5354
if err := addRemoteAndConfig(wikiRemoteURL, m.Repo.WikiPath()); err != nil {
@@ -68,7 +69,7 @@ func RemovePushMirrorRemote(ctx context.Context, m *repo_model.PushMirror) error
6869
return err
6970
}
7071

71-
if m.Repo.HasWiki() {
72+
if repo_service.HasWiki(ctx, m.Repo) {
7273
if _, _, err := cmd.RunStdString(ctx, &git.RunOpts{Dir: m.Repo.WikiPath()}); err != nil {
7374
// The wiki remote may not exist
7475
log.Warn("Wiki Remote[%d] could not be removed: %v", m.ID, err)
@@ -183,7 +184,7 @@ func runPushSync(ctx context.Context, m *repo_model.PushMirror) error {
183184
return err
184185
}
185186

186-
if m.Repo.HasWiki() {
187+
if repo_service.HasWiki(ctx, m.Repo) {
187188
_, err := git.GetRemoteAddress(ctx, m.Repo.WikiPath(), m.RemoteName)
188189
if err == nil {
189190
err := performPush(m.Repo, true)

services/repository/delete.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,13 @@ func DeleteRepositoryDirectly(ctx context.Context, repoID int64, ignoreOrgTeams
315315
}
316316
}
317317

318-
// Remove wiki files
319-
if repo.HasWiki() {
320-
system_model.RemoveAllWithNotice(ctx, "Delete repository wiki", repo.WikiPath())
318+
// Remove wiki files if it exists.
319+
if err := gitrepo.DeleteRepository(ctx, repo.WikiStorageRepo()); err != nil {
320+
desc := fmt.Sprintf("Delete wiki repository files [%s]: %v", repo.FullName(), err)
321+
// Note we use the db.DefaultContext here rather than passing in a context as the context may be cancelled
322+
if err = system_model.CreateNotice(graceful.GetManager().ShutdownContext(), system_model.NoticeRepository, desc); err != nil {
323+
log.Error("CreateRepositoryNotice: %v", err)
324+
}
321325
}
322326

323327
// Remove archives

0 commit comments

Comments
 (0)