wiki: auto-detect default branch (#8094)

This commit is contained in:
ᴊᴏᴇ ᴄʜᴇɴ
2026-01-20 23:38:10 -05:00
committed by Joe Chen
parent 5b5793bb4a
commit 4167a4d568
2 changed files with 25 additions and 10 deletions

View File

@@ -24,6 +24,15 @@ import (
var wikiWorkingPool = sync.NewExclusivePool()
// WikiBranch returns the branch name used by the wiki repository. It checks if
// "main" branch exists, otherwise falls back to "master".
func WikiBranch(repoPath string) string {
if git.RepoHasBranch(repoPath, "main") {
return "main"
}
return "master"
}
// ToWikiPageURL formats a string to corresponding wiki URL name.
func ToWikiPageURL(name string) string {
return url.QueryEscape(name)
@@ -79,11 +88,12 @@ func (repo *Repository) LocalWikiPath() string {
// UpdateLocalWiki makes sure the local copy of repository wiki is up-to-date.
func (repo *Repository) UpdateLocalWiki() error {
return UpdateLocalCopyBranch(repo.WikiPath(), repo.LocalWikiPath(), "master", true)
wikiPath := repo.WikiPath()
return UpdateLocalCopyBranch(wikiPath, repo.LocalWikiPath(), WikiBranch(wikiPath), true)
}
func discardLocalWikiChanges(localPath string) error {
return discardLocalRepoBranchChanges(localPath, "master")
return discardLocalRepoBranchChanges(localPath, WikiBranch(localPath))
}
// updateWikiPage adds new page to repository wiki.
@@ -143,7 +153,7 @@ func (repo *Repository) updateWikiPage(doer *User, oldTitle, title, content, mes
)
if err != nil {
return fmt.Errorf("commit changes: %v", err)
} else if err = git.Push(localPath, "origin", "master"); err != nil {
} else if err = git.Push(localPath, "origin", WikiBranch(localPath)); err != nil {
return fmt.Errorf("push: %v", err)
}
@@ -190,7 +200,7 @@ func (repo *Repository) DeleteWikiPage(doer *User, title string) (err error) {
)
if err != nil {
return fmt.Errorf("commit changes: %v", err)
} else if err = git.Push(localPath, "origin", "master"); err != nil {
} else if err = git.Push(localPath, "origin", WikiBranch(localPath)); err != nil {
return fmt.Errorf("push: %v", err)
}

View File

@@ -43,12 +43,13 @@ type PageMeta struct {
}
func renderWikiPage(c *context.Context, isViewPage bool) (*git.Repository, string) {
wikiRepo, err := git.Open(c.Repo.Repository.WikiPath())
wikiPath := c.Repo.Repository.WikiPath()
wikiRepo, err := git.Open(wikiPath)
if err != nil {
c.Error(err, "open repository")
return nil, ""
}
commit, err := wikiRepo.BranchCommit("master")
commit, err := wikiRepo.BranchCommit(database.WikiBranch(wikiPath))
if err != nil {
c.Error(err, "get branch commit")
return nil, ""
@@ -124,7 +125,8 @@ func Wiki(c *context.Context) {
}
// Get last change information.
commits, err := wikiRepo.Log(git.RefsHeads+"master", git.LogOptions{Path: pageName + ".md"})
branch := database.WikiBranch(c.Repo.Repository.WikiPath())
commits, err := wikiRepo.Log(git.RefsHeads+branch, git.LogOptions{Path: pageName + ".md"})
if err != nil {
c.Error(err, "get commits by path")
return
@@ -143,12 +145,15 @@ func WikiPages(c *context.Context) {
return
}
wikiRepo, err := git.Open(c.Repo.Repository.WikiPath())
wikiPath := c.Repo.Repository.WikiPath()
wikiRepo, err := git.Open(wikiPath)
if err != nil {
c.Error(err, "open repository")
return
}
commit, err := wikiRepo.BranchCommit("master")
branch := database.WikiBranch(wikiPath)
commit, err := wikiRepo.BranchCommit(branch)
if err != nil {
c.Error(err, "get branch commit")
return
@@ -162,7 +167,7 @@ func WikiPages(c *context.Context) {
pages := make([]PageMeta, 0, len(entries))
for i := range entries {
if entries[i].Type() == git.ObjectBlob && strings.HasSuffix(entries[i].Name(), ".md") {
commits, err := wikiRepo.Log(git.RefsHeads+"master", git.LogOptions{Path: entries[i].Name()})
commits, err := wikiRepo.Log(git.RefsHeads+branch, git.LogOptions{Path: entries[i].Name()})
if err != nil {
c.Error(err, "get commits by path")
return