Skip to content

[refactor] Use const for wiki DefaultBranch #21466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions routers/web/repo/wiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down
23 changes: 14 additions & 9 deletions services/wiki/wiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions services/wiki/wiki_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down