فهرست منبع

wiki: auto-detect default branch (#8094)

ᴊᴏᴇ ᴄʜᴇɴ 2 هفته پیش
والد
کامیت
5544212adb
3فایلهای تغییر یافته به همراه26 افزوده شده و 10 حذف شده
  1. 1 0
      CHANGELOG.md
  2. 14 4
      internal/database/wiki.go
  3. 11 6
      internal/route/repo/wiki.go

+ 1 - 0
CHANGELOG.md

@@ -23,6 +23,7 @@ All notable changes to Gogs are documented in this file.
 - Submodules using `ssh://` protocol and a port number are not rendered correctly. [#4941](https://github.com/gogs/gogs/issues/4941)
 - Missing link to user profile on the first commit in commits history page. [#7404](https://github.com/gogs/gogs/issues/7404)
 - Route `GET /api/v1/user/repos` responses 500 when accessible repositories contain forks. [#8069](https://github.com/gogs/gogs/pull/8069)
+- Newer Git versions that uses default branch `main` cause wiki initialization to fail. [#8094](https://github.com/gogs/gogs/pull/8094)
 
 ## 0.13.3
 

+ 14 - 4
internal/database/wiki.go

@@ -20,6 +20,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)
@@ -75,11 +84,12 @@ func (r *Repository) LocalWikiPath() string {
 
 // UpdateLocalWiki makes sure the local copy of repository wiki is up-to-date.
 func (r *Repository) UpdateLocalWiki() error {
-	return UpdateLocalCopyBranch(r.WikiPath(), r.LocalWikiPath(), "master", true)
+	wikiPath := r.WikiPath()
+	return UpdateLocalCopyBranch(wikiPath, r.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.
@@ -139,7 +149,7 @@ func (r *Repository) updateWikiPage(doer *User, oldTitle, title, content, messag
 	)
 	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)
 	}
 
@@ -186,7 +196,7 @@ func (r *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)
 	}
 

+ 11 - 6
internal/route/repo/wiki.go

@@ -39,12 +39,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, ""
@@ -120,7 +121,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
@@ -139,12 +141,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
@@ -158,7 +163,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