Skip to content

Commit 915173a

Browse files
committed
make the code simpler
1 parent 67e7a1e commit 915173a

File tree

12 files changed

+41
-81
lines changed

12 files changed

+41
-81
lines changed

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 & 30 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,12 +475,7 @@ func Wiki(ctx *context.Context) {
474475
return
475476
}
476477

477-
hasWiki, err := gitrepo.IsRepositoryExist(ctx, ctx.Repo.Repository.WikiStorageRepo())
478-
if err != nil {
479-
ctx.ServerError("IsWikiRepositoryExist", err)
480-
return
481-
}
482-
if !hasWiki {
478+
if !repo_service.HasWiki(ctx, ctx.Repo.Repository) {
483479
ctx.Data["Title"] = ctx.Tr("repo.wiki")
484480
ctx.HTML(http.StatusOK, tplWikiStart)
485481
return
@@ -515,12 +511,7 @@ func Wiki(ctx *context.Context) {
515511
func WikiRevision(ctx *context.Context) {
516512
ctx.Data["CanWriteWiki"] = ctx.Repo.CanWrite(unit.TypeWiki) && !ctx.Repo.Repository.IsArchived
517513

518-
hasWiki, err := gitrepo.IsRepositoryExist(ctx, ctx.Repo.Repository.WikiStorageRepo())
519-
if err != nil {
520-
ctx.ServerError("IsWikiRepositoryExist", err)
521-
return
522-
}
523-
if !hasWiki {
514+
if !repo_service.HasWiki(ctx, ctx.Repo.Repository) {
524515
ctx.Data["Title"] = ctx.Tr("repo.wiki")
525516
ctx.HTML(http.StatusOK, tplWikiStart)
526517
return
@@ -550,12 +541,7 @@ func WikiRevision(ctx *context.Context) {
550541

551542
// WikiPages render wiki pages list page
552543
func WikiPages(ctx *context.Context) {
553-
hasWiki, err := gitrepo.IsRepositoryExist(ctx, ctx.Repo.Repository.WikiStorageRepo())
554-
if err != nil {
555-
ctx.ServerError("IsWikiRepositoryExist", err)
556-
return
557-
}
558-
if !hasWiki {
544+
if !repo_service.HasWiki(ctx, ctx.Repo.Repository) {
559545
ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
560546
return
561547
}
@@ -663,12 +649,7 @@ func WikiRaw(ctx *context.Context) {
663649
func NewWiki(ctx *context.Context) {
664650
ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
665651

666-
hasWiki, err := gitrepo.IsRepositoryExist(ctx, ctx.Repo.Repository.WikiStorageRepo())
667-
if err != nil {
668-
ctx.ServerError("IsWikiRepositoryExist", err)
669-
return
670-
}
671-
if !hasWiki {
652+
if !repo_service.HasWiki(ctx, ctx.Repo.Repository) {
672653
ctx.Data["title"] = "Home"
673654
}
674655
if ctx.FormString("title") != "" {
@@ -721,12 +702,7 @@ func NewWikiPost(ctx *context.Context) {
721702
func EditWiki(ctx *context.Context) {
722703
ctx.Data["PageIsWikiEdit"] = true
723704

724-
hasWiki, err := gitrepo.IsRepositoryExist(ctx, ctx.Repo.Repository.WikiStorageRepo())
725-
if err != nil {
726-
ctx.ServerError("IsWikiRepositoryExist", err)
727-
return
728-
}
729-
if !hasWiki {
705+
if !repo_service.HasWiki(ctx, ctx.Repo.Repository) {
730706
ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
731707
return
732708
}

services/migrations/gitea_uploader_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +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-
exist, err := gitrepo.IsRepositoryExist(ctx, repo)
67-
assert.NoError(t, err)
68-
assert.True(t, exist)
66+
assert.True(t, repo_service.HasWiki(ctx, repo))
6967
assert.Equal(t, repo_model.RepositoryReady, repo.Status)
7068

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

services/mirror/mirror_pull.go

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

55-
hasWiki, err := gitrepo.IsRepositoryExist(ctx, m.Repo.WikiStorageRepo())
56-
if err != nil {
57-
return err
58-
}
59-
if hasWiki {
55+
if repo_service.HasWiki(ctx, m.Repo) {
6056
wikiPath := m.Repo.WikiPath()
6157
wikiRemotePath := repo_module.WikiRemoteURL(ctx, addr)
6258
// Remove old remote of wiki
@@ -371,11 +367,7 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo
371367
log.Error("SyncMirrors [repo: %-v]: failed to update size for mirror repository: %v", m.Repo.FullName(), err)
372368
}
373369

374-
hasWiki, err := gitrepo.IsRepositoryExist(ctx, m.Repo.WikiStorageRepo())
375-
if err != nil {
376-
log.Error("SyncMirrors [repo: %-v]: failed to check if wiki repository exists: %v", m.Repo.FullName(), err)
377-
}
378-
if hasWiki {
370+
if repo_service.HasWiki(ctx, m.Repo) {
379371
log.Trace("SyncMirrors [repo: %-v Wiki]: running git remote update...", m.Repo)
380372
stderrBuilder.Reset()
381373
stdoutBuilder.Reset()

services/mirror/mirror_push.go

Lines changed: 4 additions & 15 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,11 +48,7 @@ func AddPushMirrorRemote(ctx context.Context, m *repo_model.PushMirror, addr str
4748
return err
4849
}
4950

50-
hasWiki, err := gitrepo.IsRepositoryExist(ctx, m.Repo.WikiStorageRepo())
51-
if err != nil {
52-
return err
53-
}
54-
if hasWiki {
51+
if repo_service.HasWiki(ctx, m.Repo) {
5552
wikiRemoteURL := repository.WikiRemoteURL(ctx, addr)
5653
if len(wikiRemoteURL) > 0 {
5754
if err := addRemoteAndConfig(wikiRemoteURL, m.Repo.WikiPath()); err != nil {
@@ -72,11 +69,7 @@ func RemovePushMirrorRemote(ctx context.Context, m *repo_model.PushMirror) error
7269
return err
7370
}
7471

75-
hasWiki, err := gitrepo.IsRepositoryExist(ctx, m.Repo.WikiStorageRepo())
76-
if err != nil {
77-
return err
78-
}
79-
if hasWiki {
72+
if repo_service.HasWiki(ctx, m.Repo) {
8073
if _, _, err := cmd.RunStdString(ctx, &git.RunOpts{Dir: m.Repo.WikiPath()}); err != nil {
8174
// The wiki remote may not exist
8275
log.Warn("Wiki Remote[%d] could not be removed: %v", m.ID, err)
@@ -191,11 +184,7 @@ func runPushSync(ctx context.Context, m *repo_model.PushMirror) error {
191184
return err
192185
}
193186

194-
hasWiki, err := gitrepo.IsRepositoryExist(ctx, m.Repo.WikiStorageRepo())
195-
if err != nil {
196-
return err
197-
}
198-
if hasWiki {
187+
if repo_service.HasWiki(ctx, m.Repo) {
199188
_, err := git.GetRemoteAddress(ctx, m.Repo.WikiPath(), m.RemoteName)
200189
if err == nil {
201190
err := performPush(m.Repo, true)

services/repository/hooks.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,8 @@ func SyncRepositoryHooks(ctx context.Context) error {
3434
if err := gitrepo.CreateDelegateHooks(ctx, repo); err != nil {
3535
return fmt.Errorf("CreateDelegateHooks: %w", err)
3636
}
37-
exist, err := gitrepo.IsRepositoryExist(ctx, repo.WikiStorageRepo())
38-
if err != nil {
39-
return fmt.Errorf("IsRepositoryExist: %w", err)
40-
}
41-
if exist {
37+
38+
if HasWiki(ctx, repo) {
4239
if err := gitrepo.CreateDelegateHooks(ctx, repo.WikiStorageRepo()); err != nil {
4340
return fmt.Errorf("CreateDelegateHooks: %w", err)
4441
}

services/repository/migrate.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,17 +281,14 @@ func CleanUpMigrateInfo(ctx context.Context, repo *repo_model.Repository) (*repo
281281
return repo, fmt.Errorf("createDelegateHooks: %w", err)
282282
}
283283

284-
hasWiki, err := gitrepo.IsRepositoryExist(ctx, repo.WikiStorageRepo())
285-
if err != nil {
286-
return repo, fmt.Errorf("IsWikiRepositoryExist: %w", err)
287-
}
284+
hasWiki := HasWiki(ctx, repo)
288285
if hasWiki {
289286
if err := gitrepo.CreateDelegateHooks(ctx, repo.WikiStorageRepo()); err != nil {
290287
return repo, fmt.Errorf("createDelegateHooks.(wiki): %w", err)
291288
}
292289
}
293290

294-
_, _, err = git.NewCommand("remote", "rm", "origin").RunStdString(ctx, &git.RunOpts{Dir: repo.RepoPath()})
291+
_, _, err := git.NewCommand("remote", "rm", "origin").RunStdString(ctx, &git.RunOpts{Dir: repo.RepoPath()})
295292
if err != nil && !git.IsRemoteNotExistError(err) {
296293
return repo, fmt.Errorf("CleanUpMigrateInfo: %w", err)
297294
}

services/repository/repository.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
repo_model "code.gitea.io/gitea/models/repo"
2121
"code.gitea.io/gitea/models/unit"
2222
user_model "code.gitea.io/gitea/models/user"
23+
"code.gitea.io/gitea/modules/gitrepo"
2324
"code.gitea.io/gitea/modules/graceful"
2425
issue_indexer "code.gitea.io/gitea/modules/indexer/issues"
2526
"code.gitea.io/gitea/modules/log"
@@ -336,3 +337,11 @@ func updateRepository(ctx context.Context, repo *repo_model.Repository, visibili
336337

337338
return nil
338339
}
340+
341+
func HasWiki(ctx context.Context, repo *repo_model.Repository) bool {
342+
hasWiki, err := gitrepo.IsRepositoryExist(ctx, repo.WikiStorageRepo())
343+
if err != nil {
344+
log.Error("gitrepo.IsRepositoryExist: %v", err)
345+
}
346+
return hasWiki && err == nil
347+
}

services/repository/repository_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,12 @@ func TestUpdateRepositoryVisibilityChanged(t *testing.T) {
6161
assert.NoError(t, err)
6262
assert.True(t, act.IsPrivate)
6363
}
64+
65+
func TestRepository_HasWiki(t *testing.T) {
66+
unittest.PrepareTestEnv(t)
67+
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
68+
assert.True(t, HasWiki(t.Context(), repo1))
69+
70+
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
71+
assert.False(t, HasWiki(t.Context(), repo2))
72+
}

services/repository/transfer.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -358,12 +358,7 @@ func changeRepositoryName(ctx context.Context, repo *repo_model.Repository, newR
358358
return fmt.Errorf("rename repository directory: %w", err)
359359
}
360360

361-
isExist, err := gitrepo.IsRepositoryExist(ctx, repo.WikiStorageRepo())
362-
if err != nil {
363-
log.Error("Unable to check if the wiki of %s exists. Error: %v", repo.FullName(), err)
364-
return err
365-
}
366-
if isExist {
361+
if HasWiki(ctx, repo) {
367362
if err = gitrepo.RenameRepository(ctx, repo.WikiStorageRepo(), repo_model.StorageRepo(
368363
repo_model.RelativeWikiPath(repo.OwnerName, newRepoName))); err != nil {
369364
return fmt.Errorf("rename repository wiki: %w", err)

0 commit comments

Comments
 (0)