From 477851157061ce1744a6ae98125a3298aff60168 Mon Sep 17 00:00:00 2001 From: Aly Sewelam Date: Thu, 27 Nov 2025 18:16:41 +0200 Subject: [PATCH] Include defaultWikiFormat in WebPathToGitPath Updated all calls of function to include the new signature The new signature calls for the repo setting for wiki formats --- routers/api/v1/repo/wiki.go | 4 +++- routers/web/repo/wiki.go | 6 ++++-- routers/web/repo/wiki_test.go | 2 +- services/wiki/wiki.go | 2 +- services/wiki/wiki_path.go | 9 ++++++--- services/wiki/wiki_test.go | 12 ++++++------ 6 files changed, 21 insertions(+), 14 deletions(-) diff --git a/routers/api/v1/repo/wiki.go b/routers/api/v1/repo/wiki.go index 5435274eaf..49263a4994 100644 --- a/routers/api/v1/repo/wiki.go +++ b/routers/api/v1/repo/wiki.go @@ -8,6 +8,7 @@ import ( "fmt" "net/http" "net/url" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/gitrepo" @@ -509,7 +510,8 @@ func wikiContentsByEntry(ctx *context.APIContext, entry *git.TreeEntry) string { // wikiContentsByName returns the contents of a wiki page, along with a boolean // indicating whether the page exists. Writes to ctx if an error occurs. func wikiContentsByName(ctx *context.APIContext, commit *git.Commit, wikiName wiki_service.WebPath, isSidebarOrFooter bool) (string, string) { - gitFilename := wiki_service.WebPathToGitPath(wikiName) + repoDefaultWikiFormat := ctx.Repo.Repository.DefaultWikiFormat + gitFilename := wiki_service.WebPathToGitPath(wikiName, repoDefaultWikiFormat) entry, err := findEntryForFile(commit, gitFilename) if err != nil { if git.IsErrNotExist(err) { diff --git a/routers/web/repo/wiki.go b/routers/web/repo/wiki.go index fbc677de80..d847a322c1 100644 --- a/routers/web/repo/wiki.go +++ b/routers/web/repo/wiki.go @@ -146,7 +146,8 @@ func wikiContentsByEntry(ctx *context.Context, entry *git.TreeEntry) []byte { // The last return value indicates whether the file should be returned as a raw file func wikiEntryByName(ctx *context.Context, commit *git.Commit, wikiName wiki_service.WebPath) (*git.TreeEntry, string, bool, bool) { isRaw := false - gitFilename := wiki_service.WebPathToGitPath(wikiName) + repoDefaultWikiFormat := ctx.Repo.Repository.DefaultWikiFormat + gitFilename := wiki_service.WebPathToGitPath(wikiName, repoDefaultWikiFormat) entry, err := findEntryForFile(commit, gitFilename) if err != nil && !git.IsErrNotExist(err) { ctx.ServerError("findEntryForFile", err) @@ -719,7 +720,8 @@ func WikiRaw(ctx *context.Context) { } providedWebPath := wiki_service.WebPathFromRequest(ctx.PathParamRaw("*")) - providedGitPath := wiki_service.WebPathToGitPath(providedWebPath) + repoDefaultWikiFormat := ctx.Repo.Repository.DefaultWikiFormat + providedGitPath := wiki_service.WebPathToGitPath(providedWebPath, repoDefaultWikiFormat) var entry *git.TreeEntry if commit != nil { // Try to find a file with that name diff --git a/routers/web/repo/wiki_test.go b/routers/web/repo/wiki_test.go index 409d7c9a05..5426ce749f 100644 --- a/routers/web/repo/wiki_test.go +++ b/routers/web/repo/wiki_test.go @@ -37,7 +37,7 @@ func wikiEntry(t *testing.T, repo *repo_model.Repository, wikiName wiki_service. entries, err := commit.ListEntries() assert.NoError(t, err) for _, entry := range entries { - if entry.Name() == wiki_service.WebPathToGitPath(wikiName) { + if entry.Name() == wiki_service.WebPathToGitPath(wikiName, repo.DefaultWikiFormat) { return entry } } diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go index fdba8d870f..899c3d48f9 100644 --- a/services/wiki/wiki.go +++ b/services/wiki/wiki.go @@ -58,7 +58,7 @@ func InitWiki(ctx context.Context, repo *repo_model.Repository) error { func prepareGitPath(gitRepo *git.Repository, defaultWikiBranch string, wikiPath WebPath, defaultWikiFormat string) (bool, string, error) { unescapedMd := string(wikiPath) + ".md" unescapedOrg := string(wikiPath) + ".org" - gitPath := WebPathToGitPath(wikiPath) + gitPath := WebPathToGitPath(wikiPath, defaultWikiFormat) // Build list of files to look for based on defaultWikiFormat var filesToCheck []string diff --git a/services/wiki/wiki_path.go b/services/wiki/wiki_path.go index 2fd057640c..2b05586595 100644 --- a/services/wiki/wiki_path.go +++ b/services/wiki/wiki_path.go @@ -94,7 +94,7 @@ func WebPathSegments(s WebPath) []string { return a } -func WebPathToGitPath(s WebPath) string { +func WebPathToGitPath(s WebPath, repoDefaultWikiFormat string) string { str := string(s) // Accept only .md or .org directly if strings.HasSuffix(str, ".md") || strings.HasSuffix(str, ".org") { @@ -102,8 +102,11 @@ func WebPathToGitPath(s WebPath) string { return util.PathJoinRelX(ret) } - // Get default wiki format from global setting - defaultWikiFormat := setting.Repository.DefaultWikiFormat + // Prioritize repository's DefaultWikiFormat, fallback to global setting if empty + defaultWikiFormat := repoDefaultWikiFormat + if defaultWikiFormat == "" { + defaultWikiFormat = setting.Repository.DefaultWikiFormat + } a := strings.Split(string(s), "/") for i := range a { diff --git a/services/wiki/wiki_test.go b/services/wiki/wiki_test.go index 26c877b4d9..23bc65f790 100644 --- a/services/wiki/wiki_test.go +++ b/services/wiki/wiki_test.go @@ -81,7 +81,7 @@ func TestWebPathToGitPath(t *testing.T) { {"2000-01-02-meeting.md", "2000-01-02+meeting"}, {"2000-01-02 meeting.-.md", "2000-01-02%20meeting.-"}, } { - assert.Equal(t, test.Expected, WebPathToGitPath(test.WikiName)) + assert.Equal(t, test.Expected, WebPathToGitPath(test.WikiName, "markdown")) } } @@ -129,11 +129,11 @@ func TestUserWebGitPathConsistency(t *testing.T) { continue } webPath := UserTitleToWebPath("", userTitle) - gitPath := WebPathToGitPath(webPath) + gitPath := WebPathToGitPath(webPath, "markdown") webPath1, _ := GitPathToWebPath(gitPath) _, userTitle1 := WebPathToUserTitle(webPath1) - gitPath1 := WebPathToGitPath(webPath1) + gitPath1 := WebPathToGitPath(webPath1, "markdown") assert.Equal(t, userTitle, userTitle1, "UserTitle for userTitle: %q", userTitle) assert.Equal(t, webPath, webPath1, "WebPath for userTitle: %q", userTitle) @@ -173,7 +173,7 @@ func TestRepository_AddWikiPage(t *testing.T) { defer gitRepo.Close() masterTree, err := gitRepo.GetTree(repo.DefaultWikiBranch) assert.NoError(t, err) - gitPath := WebPathToGitPath(webPath) + gitPath := WebPathToGitPath(webPath, repo.DefaultWikiFormat) entry, err := masterTree.GetTreeEntryByPath(gitPath) assert.NoError(t, err) assert.Equal(t, gitPath, entry.Name(), "%s not added correctly", userTitle) @@ -218,7 +218,7 @@ func TestRepository_EditWikiPage(t *testing.T) { assert.NoError(t, err) masterTree, err := gitRepo.GetTree(repo.DefaultWikiBranch) assert.NoError(t, err) - gitPath := WebPathToGitPath(webPath) + gitPath := WebPathToGitPath(webPath, repo.DefaultWikiFormat) entry, err := masterTree.GetTreeEntryByPath(gitPath) assert.NoError(t, err) assert.Equal(t, gitPath, entry.Name(), "%s not edited correctly", newWikiName) @@ -244,7 +244,7 @@ func TestRepository_DeleteWikiPage(t *testing.T) { defer gitRepo.Close() masterTree, err := gitRepo.GetTree(repo.DefaultWikiBranch) assert.NoError(t, err) - gitPath := WebPathToGitPath("Home") + gitPath := WebPathToGitPath("Home", repo.DefaultWikiFormat) _, err = masterTree.GetTreeEntryByPath(gitPath) assert.Error(t, err) }