diff options
author | 6543 <6543@obermui.de> | 2022-10-15 16:40:32 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-15 16:40:32 +0200 |
commit | a33ff822ffc84b0d99d122ca9dabfedc583e9f8c (patch) | |
tree | 4b5b2af3c3d19e745aea52767ef05b07ad1d46e4 /services/wiki | |
parent | 677af6ac57295a9e8795a8ab3f29e284f2247fb6 (diff) | |
download | gitea-a33ff822ffc84b0d99d122ca9dabfedc583e9f8c.tar.gz gitea-a33ff822ffc84b0d99d122ca9dabfedc583e9f8c.zip |
[refactor] Use const for wiki DefaultBranch (#21466)
just a nit, that will make it easier to change things and we now have a
single source of truth
Diffstat (limited to 'services/wiki')
-rw-r--r-- | services/wiki/wiki.go | 23 | ||||
-rw-r--r-- | services/wiki/wiki_test.go | 6 |
2 files changed, 17 insertions, 12 deletions
diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go index b341b048cc..d2374ade5a 100644 --- a/services/wiki/wiki.go +++ b/services/wiki/wiki.go @@ -30,6 +30,11 @@ var ( wikiWorkingPool = sync.NewExclusivePool() ) +const ( + DefaultRemote = "origin" + DefaultBranch = "master" +) + func nameAllowed(name string) error { if util.IsStringInSlice(name, reservedWikiNames) { return repo_model.ErrWikiReservedName{ @@ -81,7 +86,7 @@ func InitWiki(ctx context.Context, repo *repo_model.Repository) error { return fmt.Errorf("InitRepository: %v", err) } else if err = repo_module.CreateDelegateHooks(repo.WikiPath()); err != nil { return fmt.Errorf("createDelegateHooks: %v", err) - } else if _, _, err = git.NewCommand(ctx, "symbolic-ref", "HEAD", git.BranchPrefix+"master").RunStdString(&git.RunOpts{Dir: repo.WikiPath()}); err != nil { + } else if _, _, err = git.NewCommand(ctx, "symbolic-ref", "HEAD", git.BranchPrefix+DefaultBranch).RunStdString(&git.RunOpts{Dir: repo.WikiPath()}); err != nil { return fmt.Errorf("unable to set default wiki branch to master: %v", err) } return nil @@ -94,7 +99,7 @@ func prepareWikiFileName(gitRepo *git.Repository, wikiName string) (bool, string escaped := NameToFilename(wikiName) // Look for both files - filesInIndex, err := gitRepo.LsTree("master", unescaped, escaped) + filesInIndex, err := gitRepo.LsTree(DefaultBranch, unescaped, escaped) if err != nil { if strings.Contains(err.Error(), "Not a valid object name master") { return false, escaped, nil @@ -130,7 +135,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model return fmt.Errorf("InitWiki: %v", err) } - hasMasterBranch := git.IsBranchExist(ctx, repo.WikiPath(), "master") + hasMasterBranch := git.IsBranchExist(ctx, repo.WikiPath(), DefaultBranch) basePath, err := repo_module.CreateTemporaryPath("update-wiki") if err != nil { @@ -148,7 +153,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model } if hasMasterBranch { - cloneOpts.Branch = "master" + cloneOpts.Branch = DefaultBranch } if err := git.Clone(ctx, repo.WikiPath(), basePath, cloneOpts); err != nil { @@ -246,8 +251,8 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model } if err := git.Push(gitRepo.Ctx, basePath, git.PushOptions{ - Remote: "origin", - Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, "master"), + Remote: DefaultRemote, + Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, DefaultBranch), Env: repo_module.FullPushingEnvironment( doer, doer, @@ -299,7 +304,7 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model if err := git.Clone(ctx, repo.WikiPath(), basePath, git.CloneRepoOptions{ Bare: true, Shared: true, - Branch: "master", + Branch: DefaultBranch, }); err != nil { log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err) return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err) @@ -360,8 +365,8 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model } if err := git.Push(gitRepo.Ctx, basePath, git.PushOptions{ - Remote: "origin", - Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, "master"), + Remote: DefaultRemote, + Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, DefaultBranch), Env: repo_module.PushingEnvironment(doer, repo), }); err != nil { if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) { diff --git a/services/wiki/wiki_test.go b/services/wiki/wiki_test.go index cabeecb60a..774ef6c283 100644 --- a/services/wiki/wiki_test.go +++ b/services/wiki/wiki_test.go @@ -140,7 +140,7 @@ func TestRepository_AddWikiPage(t *testing.T) { gitRepo, err := git.OpenRepository(git.DefaultContext, repo.WikiPath()) assert.NoError(t, err) defer gitRepo.Close() - masterTree, err := gitRepo.GetTree("master") + masterTree, err := gitRepo.GetTree(DefaultBranch) assert.NoError(t, err) wikiPath := NameToFilename(wikiName) entry, err := masterTree.GetTreeEntryByPath(wikiPath) @@ -184,7 +184,7 @@ func TestRepository_EditWikiPage(t *testing.T) { // Now need to show that the page has been added: gitRepo, err := git.OpenRepository(git.DefaultContext, repo.WikiPath()) assert.NoError(t, err) - masterTree, err := gitRepo.GetTree("master") + masterTree, err := gitRepo.GetTree(DefaultBranch) assert.NoError(t, err) wikiPath := NameToFilename(newWikiName) entry, err := masterTree.GetTreeEntryByPath(wikiPath) @@ -209,7 +209,7 @@ func TestRepository_DeleteWikiPage(t *testing.T) { gitRepo, err := git.OpenRepository(git.DefaultContext, repo.WikiPath()) assert.NoError(t, err) defer gitRepo.Close() - masterTree, err := gitRepo.GetTree("master") + masterTree, err := gitRepo.GetTree(DefaultBranch) assert.NoError(t, err) wikiPath := NameToFilename("Home") _, err = masterTree.GetTreeEntryByPath(wikiPath) |