Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: not able to update local created non-urlencoded wiki pages #16139

Merged
merged 9 commits into from
Jul 7, 2021
56 changes: 43 additions & 13 deletions services/wiki/wiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,34 @@ func InitWiki(repo *models.Repository) error {
return nil
}

// prepareWikiFileName try to find a suitable file path with file name by the given raw wiki name.
// return: existence, prepared file path with name, error
func prepareWikiFileName(gitRepo *git.Repository, wikiName string) (bool, string, error) {
6543 marked this conversation as resolved.
Show resolved Hide resolved
unescaped := wikiName + ".md"
escaped := NameToFilename(wikiName)

// Look for both files
filesInIndex, err := gitRepo.LsFiles(unescaped, escaped)
if err != nil {
log.Error("%v", err)
return false, escaped, err
}

foundEscaped := false
for _, filename := range filesInIndex {
switch filename {
case unescaped:
// if we find the unescaped file return it
return true, unescaped, nil
case escaped:
foundEscaped = true
}
}

// If not return whether the escaped file exists, and the escaped filename to keep backwards compatibility.
return foundEscaped, escaped, nil
}

// updateWikiPage adds a new page to the repository wiki.
func updateWikiPage(doer *models.User, repo *models.Repository, oldWikiName, newWikiName, content, message string, isNew bool) (err error) {
if err = nameAllowed(newWikiName); err != nil {
Expand Down Expand Up @@ -133,27 +161,29 @@ func updateWikiPage(doer *models.User, repo *models.Repository, oldWikiName, new
}
}

newWikiPath := NameToFilename(newWikiName)
isWikiExist, newWikiPath, err := prepareWikiFileName(gitRepo, newWikiName)
if err != nil {
return err
}

if isNew {
filesInIndex, err := gitRepo.LsFiles(newWikiPath)
if err != nil {
log.Error("%v", err)
return err
}
if util.IsStringInSlice(newWikiPath, filesInIndex) {
if isWikiExist {
return models.ErrWikiAlreadyExist{
Title: newWikiPath,
}
}
} else {
oldWikiPath := NameToFilename(oldWikiName)
filesInIndex, err := gitRepo.LsFiles(oldWikiPath)
if err != nil {
log.Error("%v", err)
return err
// avoid check existence again if wiki name is not changed since gitRepo.LsFiles(...) is not free.
isOldWikiExist := true
oldWikiPath := newWikiPath
if oldWikiName != newWikiName {
isOldWikiExist, oldWikiPath, err = prepareWikiFileName(gitRepo, oldWikiName)
if err != nil {
return err
}
}

if util.IsStringInSlice(oldWikiPath, filesInIndex) {
if isOldWikiExist {
err := gitRepo.RemoveFilesFromIndex(oldWikiPath)
if err != nil {
log.Error("%v", err)
Expand Down