mirror of
1
Fork 0

[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
This commit is contained in:
6543 2022-10-15 16:40:32 +02:00 committed by GitHub
parent 677af6ac57
commit a33ff822ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 16 deletions

View File

@ -99,7 +99,7 @@ func findWikiRepoCommit(ctx *context.Context) (*git.Repository, *git.Commit, err
return nil, nil, err return nil, nil, err
} }
commit, err := wikiRepo.GetBranchCommit("master") commit, err := wikiRepo.GetBranchCommit(wiki_service.DefaultBranch)
if err != nil { if err != nil {
return wikiRepo, nil, err return wikiRepo, nil, err
} }
@ -302,7 +302,7 @@ func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
ctx.Data["toc"] = rctx.TableOfContents ctx.Data["toc"] = rctx.TableOfContents
// get commit count - wiki revisions // get commit count - wiki revisions
commitsCount, _ := wikiRepo.FileCommitsCount("master", pageFilename) commitsCount, _ := wikiRepo.FileCommitsCount(wiki_service.DefaultBranch, pageFilename)
ctx.Data["CommitCount"] = commitsCount ctx.Data["CommitCount"] = commitsCount
return wikiRepo, entry return wikiRepo, entry
@ -351,7 +351,7 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)
ctx.Data["footerContent"] = "" ctx.Data["footerContent"] = ""
// get commit count - wiki revisions // get commit count - wiki revisions
commitsCount, _ := wikiRepo.FileCommitsCount("master", pageFilename) commitsCount, _ := wikiRepo.FileCommitsCount(wiki_service.DefaultBranch, pageFilename)
ctx.Data["CommitCount"] = commitsCount ctx.Data["CommitCount"] = commitsCount
// get page // get page
@ -361,7 +361,7 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)
} }
// get Commit Count // get Commit Count
commitsHistory, err := wikiRepo.CommitsByFileAndRange("master", pageFilename, page) commitsHistory, err := wikiRepo.CommitsByFileAndRange(wiki_service.DefaultBranch, pageFilename, page)
if err != nil { if err != nil {
if wikiRepo != nil { if wikiRepo != nil {
wikiRepo.Close() wikiRepo.Close()

View File

@ -30,6 +30,11 @@ var (
wikiWorkingPool = sync.NewExclusivePool() wikiWorkingPool = sync.NewExclusivePool()
) )
const (
DefaultRemote = "origin"
DefaultBranch = "master"
)
func nameAllowed(name string) error { func nameAllowed(name string) error {
if util.IsStringInSlice(name, reservedWikiNames) { if util.IsStringInSlice(name, reservedWikiNames) {
return repo_model.ErrWikiReservedName{ return repo_model.ErrWikiReservedName{
@ -81,7 +86,7 @@ func InitWiki(ctx context.Context, repo *repo_model.Repository) error {
return fmt.Errorf("InitRepository: %v", err) return fmt.Errorf("InitRepository: %v", err)
} else if err = repo_module.CreateDelegateHooks(repo.WikiPath()); err != nil { } else if err = repo_module.CreateDelegateHooks(repo.WikiPath()); err != nil {
return fmt.Errorf("createDelegateHooks: %v", err) 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 fmt.Errorf("unable to set default wiki branch to master: %v", err)
} }
return nil return nil
@ -94,7 +99,7 @@ func prepareWikiFileName(gitRepo *git.Repository, wikiName string) (bool, string
escaped := NameToFilename(wikiName) escaped := NameToFilename(wikiName)
// Look for both files // Look for both files
filesInIndex, err := gitRepo.LsTree("master", unescaped, escaped) filesInIndex, err := gitRepo.LsTree(DefaultBranch, unescaped, escaped)
if err != nil { if err != nil {
if strings.Contains(err.Error(), "Not a valid object name master") { if strings.Contains(err.Error(), "Not a valid object name master") {
return false, escaped, nil 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) 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") basePath, err := repo_module.CreateTemporaryPath("update-wiki")
if err != nil { if err != nil {
@ -148,7 +153,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
} }
if hasMasterBranch { if hasMasterBranch {
cloneOpts.Branch = "master" cloneOpts.Branch = DefaultBranch
} }
if err := git.Clone(ctx, repo.WikiPath(), basePath, cloneOpts); err != nil { 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{ if err := git.Push(gitRepo.Ctx, basePath, git.PushOptions{
Remote: "origin", Remote: DefaultRemote,
Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, "master"), Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, DefaultBranch),
Env: repo_module.FullPushingEnvironment( Env: repo_module.FullPushingEnvironment(
doer, doer,
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{ if err := git.Clone(ctx, repo.WikiPath(), basePath, git.CloneRepoOptions{
Bare: true, Bare: true,
Shared: true, Shared: true,
Branch: "master", Branch: DefaultBranch,
}); err != nil { }); err != nil {
log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err) log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err)
return fmt.Errorf("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{ if err := git.Push(gitRepo.Ctx, basePath, git.PushOptions{
Remote: "origin", Remote: DefaultRemote,
Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, "master"), Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, DefaultBranch),
Env: repo_module.PushingEnvironment(doer, repo), Env: repo_module.PushingEnvironment(doer, repo),
}); err != nil { }); err != nil {
if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) { if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {

View File

@ -140,7 +140,7 @@ func TestRepository_AddWikiPage(t *testing.T) {
gitRepo, err := git.OpenRepository(git.DefaultContext, repo.WikiPath()) gitRepo, err := git.OpenRepository(git.DefaultContext, repo.WikiPath())
assert.NoError(t, err) assert.NoError(t, err)
defer gitRepo.Close() defer gitRepo.Close()
masterTree, err := gitRepo.GetTree("master") masterTree, err := gitRepo.GetTree(DefaultBranch)
assert.NoError(t, err) assert.NoError(t, err)
wikiPath := NameToFilename(wikiName) wikiPath := NameToFilename(wikiName)
entry, err := masterTree.GetTreeEntryByPath(wikiPath) 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: // Now need to show that the page has been added:
gitRepo, err := git.OpenRepository(git.DefaultContext, repo.WikiPath()) gitRepo, err := git.OpenRepository(git.DefaultContext, repo.WikiPath())
assert.NoError(t, err) assert.NoError(t, err)
masterTree, err := gitRepo.GetTree("master") masterTree, err := gitRepo.GetTree(DefaultBranch)
assert.NoError(t, err) assert.NoError(t, err)
wikiPath := NameToFilename(newWikiName) wikiPath := NameToFilename(newWikiName)
entry, err := masterTree.GetTreeEntryByPath(wikiPath) entry, err := masterTree.GetTreeEntryByPath(wikiPath)
@ -209,7 +209,7 @@ func TestRepository_DeleteWikiPage(t *testing.T) {
gitRepo, err := git.OpenRepository(git.DefaultContext, repo.WikiPath()) gitRepo, err := git.OpenRepository(git.DefaultContext, repo.WikiPath())
assert.NoError(t, err) assert.NoError(t, err)
defer gitRepo.Close() defer gitRepo.Close()
masterTree, err := gitRepo.GetTree("master") masterTree, err := gitRepo.GetTree(DefaultBranch)
assert.NoError(t, err) assert.NoError(t, err)
wikiPath := NameToFilename("Home") wikiPath := NameToFilename("Home")
_, err = masterTree.GetTreeEntryByPath(wikiPath) _, err = masterTree.GetTreeEntryByPath(wikiPath)