From a33ff822ffc84b0d99d122ca9dabfedc583e9f8c Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sat, 15 Oct 2022 16:40:32 +0200 Subject: [PATCH] [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 --- routers/web/repo/wiki.go | 8 ++++---- services/wiki/wiki.go | 23 ++++++++++++++--------- services/wiki/wiki_test.go | 6 +++--- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/routers/web/repo/wiki.go b/routers/web/repo/wiki.go index 0f349547cd..a10e12ee63 100644 --- a/routers/web/repo/wiki.go +++ b/routers/web/repo/wiki.go @@ -99,7 +99,7 @@ func findWikiRepoCommit(ctx *context.Context) (*git.Repository, *git.Commit, err return nil, nil, err } - commit, err := wikiRepo.GetBranchCommit("master") + commit, err := wikiRepo.GetBranchCommit(wiki_service.DefaultBranch) if err != nil { return wikiRepo, nil, err } @@ -302,7 +302,7 @@ func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) { ctx.Data["toc"] = rctx.TableOfContents // get commit count - wiki revisions - commitsCount, _ := wikiRepo.FileCommitsCount("master", pageFilename) + commitsCount, _ := wikiRepo.FileCommitsCount(wiki_service.DefaultBranch, pageFilename) ctx.Data["CommitCount"] = commitsCount return wikiRepo, entry @@ -351,7 +351,7 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) ctx.Data["footerContent"] = "" // get commit count - wiki revisions - commitsCount, _ := wikiRepo.FileCommitsCount("master", pageFilename) + commitsCount, _ := wikiRepo.FileCommitsCount(wiki_service.DefaultBranch, pageFilename) ctx.Data["CommitCount"] = commitsCount // get page @@ -361,7 +361,7 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) } // get Commit Count - commitsHistory, err := wikiRepo.CommitsByFileAndRange("master", pageFilename, page) + commitsHistory, err := wikiRepo.CommitsByFileAndRange(wiki_service.DefaultBranch, pageFilename, page) if err != nil { if wikiRepo != nil { wikiRepo.Close() 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)