Skip to content

Commit b06342f

Browse files
BLumiazeripathtechknowlogick
authored
fix: not able to update local created non-urlencoded wiki pages (#16139)
* fix: not able to update local created non-urlencoded wiki pages * tidy code * as per suggestion Signed-off-by: Andrew Thornton <[email protected]> * Don't replace space to dash for unescaped wiki filename Co-authored-by: zeripath <[email protected]> * Remove incorrect comment * Remove NameToUnescapedFilename() Co-authored-by: Andrew Thornton <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent 061a8e7 commit b06342f

File tree

1 file changed

+43
-13
lines changed

1 file changed

+43
-13
lines changed

services/wiki/wiki.go

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,34 @@ func InitWiki(repo *models.Repository) error {
8181
return nil
8282
}
8383

84+
// prepareWikiFileName try to find a suitable file path with file name by the given raw wiki name.
85+
// return: existence, prepared file path with name, error
86+
func prepareWikiFileName(gitRepo *git.Repository, wikiName string) (bool, string, error) {
87+
unescaped := wikiName + ".md"
88+
escaped := NameToFilename(wikiName)
89+
90+
// Look for both files
91+
filesInIndex, err := gitRepo.LsFiles(unescaped, escaped)
92+
if err != nil {
93+
log.Error("%v", err)
94+
return false, escaped, err
95+
}
96+
97+
foundEscaped := false
98+
for _, filename := range filesInIndex {
99+
switch filename {
100+
case unescaped:
101+
// if we find the unescaped file return it
102+
return true, unescaped, nil
103+
case escaped:
104+
foundEscaped = true
105+
}
106+
}
107+
108+
// If not return whether the escaped file exists, and the escaped filename to keep backwards compatibility.
109+
return foundEscaped, escaped, nil
110+
}
111+
84112
// updateWikiPage adds a new page to the repository wiki.
85113
func updateWikiPage(doer *models.User, repo *models.Repository, oldWikiName, newWikiName, content, message string, isNew bool) (err error) {
86114
if err = nameAllowed(newWikiName); err != nil {
@@ -133,27 +161,29 @@ func updateWikiPage(doer *models.User, repo *models.Repository, oldWikiName, new
133161
}
134162
}
135163

136-
newWikiPath := NameToFilename(newWikiName)
164+
isWikiExist, newWikiPath, err := prepareWikiFileName(gitRepo, newWikiName)
165+
if err != nil {
166+
return err
167+
}
168+
137169
if isNew {
138-
filesInIndex, err := gitRepo.LsFiles(newWikiPath)
139-
if err != nil {
140-
log.Error("%v", err)
141-
return err
142-
}
143-
if util.IsStringInSlice(newWikiPath, filesInIndex) {
170+
if isWikiExist {
144171
return models.ErrWikiAlreadyExist{
145172
Title: newWikiPath,
146173
}
147174
}
148175
} else {
149-
oldWikiPath := NameToFilename(oldWikiName)
150-
filesInIndex, err := gitRepo.LsFiles(oldWikiPath)
151-
if err != nil {
152-
log.Error("%v", err)
153-
return err
176+
// avoid check existence again if wiki name is not changed since gitRepo.LsFiles(...) is not free.
177+
isOldWikiExist := true
178+
oldWikiPath := newWikiPath
179+
if oldWikiName != newWikiName {
180+
isOldWikiExist, oldWikiPath, err = prepareWikiFileName(gitRepo, oldWikiName)
181+
if err != nil {
182+
return err
183+
}
154184
}
155185

156-
if util.IsStringInSlice(oldWikiPath, filesInIndex) {
186+
if isOldWikiExist {
157187
err := gitRepo.RemoveFilesFromIndex(oldWikiPath)
158188
if err != nil {
159189
log.Error("%v", err)

0 commit comments

Comments
 (0)