0
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-12-13 10:22:17 +01:00

use config option for default wiki format paths

- listing of wiki pages now depends on option and defaults to markdown
only
This commit is contained in:
Aly Sewelam 2025-11-26 17:25:30 +02:00
parent f0c005be7d
commit a4ccf65011
4 changed files with 48 additions and 9 deletions

View File

@ -286,6 +286,7 @@ func loadRepositoryFrom(rootCfg ConfigProvider) {
Repository.GoGetCloneURLProtocol = sec.Key("GO_GET_CLONE_URL_PROTOCOL").MustString("https") Repository.GoGetCloneURLProtocol = sec.Key("GO_GET_CLONE_URL_PROTOCOL").MustString("https")
Repository.MaxCreationLimit = sec.Key("MAX_CREATION_LIMIT").MustInt(-1) Repository.MaxCreationLimit = sec.Key("MAX_CREATION_LIMIT").MustInt(-1)
Repository.DefaultBranch = sec.Key("DEFAULT_BRANCH").MustString(Repository.DefaultBranch) Repository.DefaultBranch = sec.Key("DEFAULT_BRANCH").MustString(Repository.DefaultBranch)
Repository.DefaultWikiFormat = sec.Key("WIKI_FORMAT").MustString(Repository.DefaultWikiFormat)
RepoRootPath = sec.Key("ROOT").MustString(filepath.Join(AppDataPath, "gitea-repositories")) RepoRootPath = sec.Key("ROOT").MustString(filepath.Join(AppDataPath, "gitea-repositories"))
if !filepath.IsAbs(RepoRootPath) { if !filepath.IsAbs(RepoRootPath) {
RepoRootPath = filepath.Join(AppWorkPath, RepoRootPath) RepoRootPath = filepath.Join(AppWorkPath, RepoRootPath)

View File

@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"net/url" "net/url"
"strings"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"

View File

@ -21,6 +21,7 @@ import (
"code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
repo_module "code.gitea.io/gitea/modules/repository" repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting"
asymkey_service "code.gitea.io/gitea/services/asymkey" asymkey_service "code.gitea.io/gitea/services/asymkey"
repo_service "code.gitea.io/gitea/services/repository" repo_service "code.gitea.io/gitea/services/repository"
) )
@ -54,13 +55,26 @@ func InitWiki(ctx context.Context, repo *repo_model.Repository) error {
// prepareGitPath try to find a suitable file path with file name by the given raw wiki name. // prepareGitPath try to find a suitable file path with file name by the given raw wiki name.
// return: existence, prepared file path with name, error // return: existence, prepared file path with name, error
func prepareGitPath(gitRepo *git.Repository, defaultWikiBranch string, wikiPath WebPath) (bool, string, error) { func prepareGitPath(gitRepo *git.Repository, defaultWikiBranch string, wikiPath WebPath, defaultWikiFormat string) (bool, string, error) {
unescapedMd := string(wikiPath) + ".md" unescapedMd := string(wikiPath) + ".md"
unescapedOrg := string(wikiPath) + ".org" unescapedOrg := string(wikiPath) + ".org"
gitPath := WebPathToGitPath(wikiPath) gitPath := WebPathToGitPath(wikiPath)
// Look for .md, .org, and escaped file // Build list of files to look for based on defaultWikiFormat
filesInIndex, err := gitRepo.LsTree(defaultWikiBranch, unescapedMd, unescapedOrg, gitPath) var filesToCheck []string
checkMarkdown := defaultWikiFormat == "markdown" || defaultWikiFormat == "both"
checkOrg := defaultWikiFormat == "org" || defaultWikiFormat == "both"
if checkMarkdown {
filesToCheck = append(filesToCheck, unescapedMd)
}
if checkOrg {
filesToCheck = append(filesToCheck, unescapedOrg)
}
filesToCheck = append(filesToCheck, gitPath)
// Look for files based on format setting
filesInIndex, err := gitRepo.LsTree(defaultWikiBranch, filesToCheck...)
if err != nil { if err != nil {
if strings.Contains(err.Error(), "Not a valid object name") { if strings.Contains(err.Error(), "Not a valid object name") {
return false, gitPath, nil // branch doesn't exist return false, gitPath, nil // branch doesn't exist
@ -74,9 +88,13 @@ func prepareGitPath(gitRepo *git.Repository, defaultWikiBranch string, wikiPath
switch filename { switch filename {
// if we find unescaped file (.md or .org) return it // if we find unescaped file (.md or .org) return it
case unescapedMd: case unescapedMd:
return true, unescapedMd, nil if checkMarkdown {
return true, unescapedMd, nil
}
case unescapedOrg: case unescapedOrg:
return true, unescapedOrg, nil if checkOrg {
return true, unescapedOrg, nil
}
case gitPath: case gitPath:
foundEscaped = true foundEscaped = true
} }
@ -142,7 +160,11 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
} }
} }
isWikiExist, newWikiPath, err := prepareGitPath(gitRepo, repo.DefaultWikiBranch, newWikiName) defaultWikiFormat := repo.DefaultWikiFormat
if defaultWikiFormat == "" {
defaultWikiFormat = setting.Repository.DefaultWikiFormat
}
isWikiExist, newWikiPath, err := prepareGitPath(gitRepo, repo.DefaultWikiBranch, newWikiName, defaultWikiFormat)
if err != nil { if err != nil {
return err return err
} }
@ -158,7 +180,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
isOldWikiExist := true isOldWikiExist := true
oldWikiPath := newWikiPath oldWikiPath := newWikiPath
if oldWikiName != newWikiName { if oldWikiName != newWikiName {
isOldWikiExist, oldWikiPath, err = prepareGitPath(gitRepo, repo.DefaultWikiBranch, oldWikiName) isOldWikiExist, oldWikiPath, err = prepareGitPath(gitRepo, repo.DefaultWikiBranch, oldWikiName, defaultWikiFormat)
if err != nil { if err != nil {
return err return err
} }
@ -294,7 +316,11 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
return fmt.Errorf("unable to read HEAD tree to index in: %s %w", basePath, err) return fmt.Errorf("unable to read HEAD tree to index in: %s %w", basePath, err)
} }
found, wikiPath, err := prepareGitPath(gitRepo, repo.DefaultWikiBranch, wikiName) defaultWikiFormat := repo.DefaultWikiFormat
if defaultWikiFormat == "" {
defaultWikiFormat = setting.Repository.DefaultWikiFormat
}
found, wikiPath, err := prepareGitPath(gitRepo, repo.DefaultWikiBranch, wikiName, defaultWikiFormat)
if err != nil { if err != nil {
return err return err
} }

View File

@ -10,6 +10,7 @@ import (
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/services/convert" "code.gitea.io/gitea/services/convert"
@ -101,6 +102,9 @@ func WebPathToGitPath(s WebPath) string {
return util.PathJoinRelX(ret) return util.PathJoinRelX(ret)
} }
// Get default wiki format from global setting
defaultWikiFormat := setting.Repository.DefaultWikiFormat
a := strings.Split(string(s), "/") a := strings.Split(string(s), "/")
for i := range a { for i := range a {
shouldAddDashMarker := hasDashMarker(a[i]) shouldAddDashMarker := hasDashMarker(a[i])
@ -109,7 +113,14 @@ func WebPathToGitPath(s WebPath) string {
a[i] = strings.ReplaceAll(a[i], "%20", " ") // space is safe to be kept in git path a[i] = strings.ReplaceAll(a[i], "%20", " ") // space is safe to be kept in git path
a[i] = strings.ReplaceAll(a[i], "+", " ") a[i] = strings.ReplaceAll(a[i], "+", " ")
} }
return strings.Join(a, "/") + ".md" basePath := strings.Join(a, "/")
// Determine extension based on format setting
if defaultWikiFormat == "org" {
return basePath + ".org"
}
// For "both" or "markdown", default to .md
return basePath + ".md"
} }
func GitPathToWebPath(s string) (wp WebPath, err error) { func GitPathToWebPath(s string) (wp WebPath, err error) {