Skip to content

Commit ce4f8f4

Browse files
committed
Remove HasWiki method
1 parent 18bd700 commit ce4f8f4

File tree

15 files changed

+148
-47
lines changed

15 files changed

+148
-47
lines changed

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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
repo_model "code.gitea.io/gitea/models/repo"
1111
"code.gitea.io/gitea/models/unittest"
12+
"code.gitea.io/gitea/modules/gitrepo"
1213
"code.gitea.io/gitea/modules/setting"
1314

1415
"github.com/stretchr/testify/assert"
@@ -39,7 +40,12 @@ func TestRepository_WikiPath(t *testing.T) {
3940
func TestRepository_HasWiki(t *testing.T) {
4041
unittest.PrepareTestEnv(t)
4142
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
42-
assert.True(t, repo1.HasWiki())
43+
exist, err := gitrepo.IsWikiRepositoryExist(t.Context(), repo1)
44+
assert.NoError(t, err)
45+
assert.True(t, exist)
46+
4347
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
44-
assert.False(t, repo2.HasWiki())
48+
exist, err = gitrepo.IsWikiRepositoryExist(t.Context(), repo2)
49+
assert.NoError(t, err)
50+
assert.False(t, exist)
4551
}

modules/gitrepo/gitrepo.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,11 @@ func repoPath(repo Repository) string {
2929
return absPath(repo.GetOwnerName(), repo.GetName())
3030
}
3131

32-
func wikiPath(repo Repository) string {
33-
return filepath.Join(setting.RepoRootPath, strings.ToLower(repo.GetOwnerName()), strings.ToLower(repo.GetName())+".wiki.git")
34-
}
35-
3632
// OpenRepository opens the repository at the given relative path with the provided context.
3733
func OpenRepository(ctx context.Context, repo Repository) (*git.Repository, error) {
3834
return git.OpenRepository(ctx, repoPath(repo))
3935
}
4036

41-
func OpenWikiRepository(ctx context.Context, repo Repository) (*git.Repository, error) {
42-
return git.OpenRepository(ctx, wikiPath(repo))
43-
}
44-
4537
// contextKey is a value for use with context.WithValue.
4638
type contextKey struct {
4739
repoPath string

modules/gitrepo/wiki.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package gitrepo
5+
6+
import (
7+
"context"
8+
"fmt"
9+
"path/filepath"
10+
"strings"
11+
12+
"code.gitea.io/gitea/modules/git"
13+
"code.gitea.io/gitea/modules/setting"
14+
"code.gitea.io/gitea/modules/util"
15+
)
16+
17+
func absWikiPath(owner, name string) string {
18+
return filepath.Join(setting.RepoRootPath, strings.ToLower(owner), strings.ToLower(name)+".wiki.git")
19+
}
20+
21+
func wikiPath(repo Repository) string {
22+
return absWikiPath(repo.GetOwnerName(), repo.GetName())
23+
}
24+
25+
func OpenWikiRepository(ctx context.Context, repo Repository) (*git.Repository, error) {
26+
return git.OpenRepository(ctx, wikiPath(repo))
27+
}
28+
29+
// IsWikiRepositoryExist returns true if the repository directory exists in the disk
30+
func IsWikiRepositoryExist(ctx context.Context, repo Repository) (bool, error) {
31+
return util.IsExist(wikiPath(repo))
32+
}
33+
34+
// RenameRepository renames a repository's name on disk
35+
func RenameWikiRepository(ctx context.Context, repo Repository, newName string) error {
36+
newRepoPath := absWikiPath(repo.GetOwnerName(), newName)
37+
if err := util.Rename(wikiPath(repo), newRepoPath); err != nil {
38+
return fmt.Errorf("rename repository directory: %w", err)
39+
}
40+
return nil
41+
}
42+
43+
// DeleteWikiRepository deletes the repository directory from the disk
44+
func DeleteWikiRepository(ctx context.Context, repo Repository) error {
45+
return util.RemoveAll(wikiPath(repo))
46+
}

routers/web/repo/wiki.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,12 @@ func Wiki(ctx *context.Context) {
557557
return
558558
}
559559

560-
if !ctx.Repo.Repository.HasWiki() {
560+
hasWiki, err := gitrepo.IsWikiRepositoryExist(ctx, ctx.Repo.Repository)
561+
if err != nil {
562+
ctx.ServerError("IsWikiRepositoryExist", err)
563+
return
564+
}
565+
if !hasWiki {
561566
ctx.Data["Title"] = ctx.Tr("repo.wiki")
562567
ctx.HTML(http.StatusOK, tplWikiStart)
563568
return
@@ -598,7 +603,12 @@ func Wiki(ctx *context.Context) {
598603
func WikiRevision(ctx *context.Context) {
599604
ctx.Data["CanWriteWiki"] = ctx.Repo.CanWrite(unit.TypeWiki) && !ctx.Repo.Repository.IsArchived
600605

601-
if !ctx.Repo.Repository.HasWiki() {
606+
hasWiki, err := gitrepo.IsWikiRepositoryExist(ctx, ctx.Repo.Repository)
607+
if err != nil {
608+
ctx.ServerError("IsWikiRepositoryExist", err)
609+
return
610+
}
611+
if !hasWiki {
602612
ctx.Data["Title"] = ctx.Tr("repo.wiki")
603613
ctx.HTML(http.StatusOK, tplWikiStart)
604614
return
@@ -634,7 +644,12 @@ func WikiRevision(ctx *context.Context) {
634644

635645
// WikiPages render wiki pages list page
636646
func WikiPages(ctx *context.Context) {
637-
if !ctx.Repo.Repository.HasWiki() {
647+
hasWiki, err := gitrepo.IsWikiRepositoryExist(ctx, ctx.Repo.Repository)
648+
if err != nil {
649+
ctx.ServerError("IsWikiRepositoryExist", err)
650+
return
651+
}
652+
if !hasWiki {
638653
ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
639654
return
640655
}
@@ -753,7 +768,12 @@ func WikiRaw(ctx *context.Context) {
753768
func NewWiki(ctx *context.Context) {
754769
ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
755770

756-
if !ctx.Repo.Repository.HasWiki() {
771+
hasWiki, err := gitrepo.IsWikiRepositoryExist(ctx, ctx.Repo.Repository)
772+
if err != nil {
773+
ctx.ServerError("IsWikiRepositoryExist", err)
774+
return
775+
}
776+
if !hasWiki {
757777
ctx.Data["title"] = "Home"
758778
}
759779
if ctx.FormString("title") != "" {
@@ -806,7 +826,12 @@ func NewWikiPost(ctx *context.Context) {
806826
func EditWiki(ctx *context.Context) {
807827
ctx.Data["PageIsWikiEdit"] = true
808828

809-
if !ctx.Repo.Repository.HasWiki() {
829+
hasWiki, err := gitrepo.IsWikiRepositoryExist(ctx, ctx.Repo.Repository)
830+
if err != nil {
831+
ctx.ServerError("IsWikiRepositoryExist", err)
832+
return
833+
}
834+
if !hasWiki {
810835
ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
811836
return
812837
}

routers/web/repo/wiki_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,9 @@ func TestDefaultWikiBranch(t *testing.T) {
241241

242242
// repo with no wiki
243243
repoWithNoWiki := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
244-
assert.False(t, repoWithNoWiki.HasWiki())
244+
exist, err := gitrepo.IsWikiRepositoryExist(db.DefaultContext, repoWithNoWiki)
245+
assert.NoError(t, err)
246+
assert.False(t, exist)
245247
assert.NoError(t, wiki_service.ChangeDefaultWikiBranch(db.DefaultContext, repoWithNoWiki, "main"))
246248

247249
// repo with wiki

services/migrations/gitea_uploader_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ 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+
exist, err := gitrepo.IsRepositoryExist(ctx, repo)
67+
assert.NoError(t, err)
68+
assert.True(t, exist)
6769
assert.EqualValues(t, repo_model.RepositoryReady, repo.Status)
6870

6971
milestones, err := db.Find[issues_model.Milestone](db.DefaultContext, issues_model.FindMilestoneOptions{

services/mirror/mirror_pull.go

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

55-
if m.Repo.HasWiki() {
55+
hasWiki, err := gitrepo.IsWikiRepositoryExist(ctx, m.Repo)
56+
if err != nil {
57+
return err
58+
}
59+
if hasWiki {
5660
wikiPath := m.Repo.WikiPath()
5761
wikiRemotePath := repo_module.WikiRemoteURL(ctx, addr)
5862
// Remove old remote of wiki
@@ -339,17 +343,21 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo
339343
endpoint := lfs.DetermineEndpoint(remoteURL.String(), m.LFSEndpoint)
340344
lfsClient := lfs.NewClient(endpoint, nil)
341345
if err = repo_module.StoreMissingLfsObjectsInRepository(ctx, m.Repo, gitRepo, lfsClient); err != nil {
342-
log.Error("SyncMirrors [repo: %-v]: failed to synchronize LFS objects for repository: %v", m.Repo, err)
346+
log.Error("SyncMirrors [repo: %-v]: failed to synchronize LFS objects for repository: %v", m.Repo.FullName(), err)
343347
}
344348
}
345349
gitRepo.Close()
346350

347351
log.Trace("SyncMirrors [repo: %-v]: updating size of repository", m.Repo)
348352
if err := repo_module.UpdateRepoSize(ctx, m.Repo); err != nil {
349-
log.Error("SyncMirrors [repo: %-v]: failed to update size for mirror repository: %v", m.Repo, err)
353+
log.Error("SyncMirrors [repo: %-v]: failed to update size for mirror repository: %v", m.Repo.FullName(), err)
350354
}
351355

352-
if m.Repo.HasWiki() {
356+
hasWiki, err := gitrepo.IsWikiRepositoryExist(ctx, m.Repo)
357+
if err != nil {
358+
log.Error("SyncMirrors [repo: %-v]: failed to check if wiki repository exists: %v", m.Repo.FullName(), err)
359+
}
360+
if hasWiki {
353361
log.Trace("SyncMirrors [repo: %-v Wiki]: running git remote update...", m.Repo)
354362
stderrBuilder.Reset()
355363
stdoutBuilder.Reset()

services/mirror/mirror_push.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ func AddPushMirrorRemote(ctx context.Context, m *repo_model.PushMirror, addr str
4747
return err
4848
}
4949

50-
if m.Repo.HasWiki() {
50+
hasWiki, err := gitrepo.IsWikiRepositoryExist(ctx, m.Repo)
51+
if err != nil {
52+
return err
53+
}
54+
if hasWiki {
5155
wikiRemoteURL := repository.WikiRemoteURL(ctx, addr)
5256
if len(wikiRemoteURL) > 0 {
5357
if err := addRemoteAndConfig(wikiRemoteURL, m.Repo.WikiPath()); err != nil {
@@ -68,7 +72,11 @@ func RemovePushMirrorRemote(ctx context.Context, m *repo_model.PushMirror) error
6872
return err
6973
}
7074

71-
if m.Repo.HasWiki() {
75+
hasWiki, err := gitrepo.IsWikiRepositoryExist(ctx, m.Repo)
76+
if err != nil {
77+
return err
78+
}
79+
if hasWiki {
7280
if _, _, err := cmd.RunStdString(ctx, &git.RunOpts{Dir: m.Repo.WikiPath()}); err != nil {
7381
// The wiki remote may not exist
7482
log.Warn("Wiki Remote[%d] could not be removed: %v", m.ID, err)
@@ -183,7 +191,11 @@ func runPushSync(ctx context.Context, m *repo_model.PushMirror) error {
183191
return err
184192
}
185193

186-
if m.Repo.HasWiki() {
194+
hasWiki, err := gitrepo.IsWikiRepositoryExist(ctx, m.Repo)
195+
if err != nil {
196+
return err
197+
}
198+
if hasWiki {
187199
_, err := git.GetRemoteAddress(ctx, m.Repo.WikiPath(), m.RemoteName)
188200
if err == nil {
189201
err := performPush(m.Repo, true)

services/repository/delete.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,12 @@ func DeleteRepositoryDirectly(ctx context.Context, doer *user_model.User, repoID
299299
}
300300

301301
// Remove wiki files
302-
if repo.HasWiki() {
303-
system_model.RemoveAllWithNotice(ctx, "Delete repository wiki", repo.WikiPath())
302+
if err := gitrepo.DeleteWikiRepository(ctx, repo); err != nil {
303+
desc := fmt.Sprintf("Delete wiki repository files [%s]: %v", repo.FullName(), err)
304+
// Note we use the db.DefaultContext here rather than passing in a context as the context may be cancelled
305+
if err = system_model.CreateNotice(db.DefaultContext, system_model.NoticeRepository, desc); err != nil {
306+
log.Error("CreateRepositoryNotice: %v", err)
307+
}
304308
}
305309

306310
// Remove archives

0 commit comments

Comments
 (0)