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 InitUncyclo(repo *models.Repository) error {
8181
return nil
8282
}
8383

84+
// prepareUncycloFileName 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 prepareUncycloFileName(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
// updateUncycloPage adds a new page to the repository wiki.
85113
func updateUncycloPage(doer *models.User, repo *models.Repository, oldUncycloName, newUncycloName, content, message string, isNew bool) (err error) {
86114
if err = nameAllowed(newUncycloName); err != nil {
@@ -133,27 +161,29 @@ func updateUncycloPage(doer *models.User, repo *models.Repository, oldUncycloName, new
133161
}
134162
}
135163

136-
newUncycloPath := NameToFilename(newUncycloName)
164+
isUncycloExist, newUncycloPath, err := prepareUncycloFileName(gitRepo, newUncycloName)
165+
if err != nil {
166+
return err
167+
}
168+
137169
if isNew {
138-
filesInIndex, err := gitRepo.LsFiles(newUncycloPath)
139-
if err != nil {
140-
log.Error("%v", err)
141-
return err
142-
}
143-
if util.IsStringInSlice(newUncycloPath, filesInIndex) {
170+
if isUncycloExist {
144171
return models.ErrUncycloAlreadyExist{
145172
Title: newUncycloPath,
146173
}
147174
}
148175
} else {
149-
oldUncycloPath := NameToFilename(oldUncycloName)
150-
filesInIndex, err := gitRepo.LsFiles(oldUncycloPath)
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+
isOldUncycloExist := true
178+
oldUncycloPath := newUncycloPath
179+
if oldUncycloName != newUncycloName {
180+
isOldUncycloExist, oldUncycloPath, err = prepareUncycloFileName(gitRepo, oldUncycloName)
181+
if err != nil {
182+
return err
183+
}
154184
}
155185

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

0 commit comments

Comments
 (0)