2020-01-08 02:27:36 +08:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package wiki
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-01-21 04:01:19 +08:00
|
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
2020-01-08 02:27:36 +08:00
|
|
|
"code.gitea.io/gitea/modules/sync"
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
reservedWikiNames = []string{"_pages", "_new", "_edit", "raw"}
|
|
|
|
wikiWorkingPool = sync.NewExclusivePool()
|
|
|
|
)
|
|
|
|
|
|
|
|
func nameAllowed(name string) error {
|
|
|
|
if util.IsStringInSlice(name, reservedWikiNames) {
|
|
|
|
return models.ErrWikiReservedName{
|
|
|
|
Title: name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NameToSubURL converts a wiki name to its corresponding sub-URL.
|
|
|
|
func NameToSubURL(name string) string {
|
2020-10-12 04:27:20 +08:00
|
|
|
return url.QueryEscape(strings.ReplaceAll(name, " ", "-"))
|
2020-01-08 02:27:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NormalizeWikiName normalizes a wiki name
|
|
|
|
func NormalizeWikiName(name string) string {
|
2020-10-12 04:27:20 +08:00
|
|
|
return strings.ReplaceAll(name, "-", " ")
|
2020-01-08 02:27:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NameToFilename converts a wiki name to its corresponding filename.
|
|
|
|
func NameToFilename(name string) string {
|
2020-10-12 04:27:20 +08:00
|
|
|
name = strings.ReplaceAll(name, " ", "-")
|
2020-01-08 02:27:36 +08:00
|
|
|
return url.QueryEscape(name) + ".md"
|
|
|
|
}
|
|
|
|
|
|
|
|
// FilenameToName converts a wiki filename to its corresponding page name.
|
|
|
|
func FilenameToName(filename string) (string, error) {
|
|
|
|
if !strings.HasSuffix(filename, ".md") {
|
|
|
|
return "", models.ErrWikiInvalidFileName{
|
|
|
|
FileName: filename,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
basename := filename[:len(filename)-3]
|
|
|
|
unescaped, err := url.QueryUnescape(basename)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return NormalizeWikiName(unescaped), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitWiki initializes a wiki for repository,
|
|
|
|
// it does nothing when repository already has wiki.
|
|
|
|
func InitWiki(repo *models.Repository) error {
|
|
|
|
if repo.HasWiki() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := git.InitRepository(repo.WikiPath(), true); err != nil {
|
|
|
|
return fmt.Errorf("InitRepository: %v", err)
|
2020-01-21 04:01:19 +08:00
|
|
|
} else if err = repo_module.CreateDelegateHooks(repo.WikiPath()); err != nil {
|
2020-01-08 02:27:36 +08:00
|
|
|
return fmt.Errorf("createDelegateHooks: %v", err)
|
2020-12-12 19:20:14 +08:00
|
|
|
} else if _, err = git.NewCommand("symbolic-ref", "HEAD", git.BranchPrefix+"master").RunInDir(repo.WikiPath()); err != nil {
|
|
|
|
return fmt.Errorf("unable to set default wiki branch to master: %v", err)
|
2020-01-08 02:27:36 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-08 07:23:09 +08:00
|
|
|
// 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) {
|
|
|
|
unescaped := wikiName + ".md"
|
|
|
|
escaped := NameToFilename(wikiName)
|
|
|
|
|
|
|
|
// Look for both files
|
2021-07-20 21:16:20 +08:00
|
|
|
filesInIndex, err := gitRepo.LsTree("master", unescaped, escaped)
|
2021-07-08 07:23:09 +08:00
|
|
|
if err != nil {
|
2021-08-02 01:04:32 +08:00
|
|
|
if strings.Contains(err.Error(), "Not a valid object name master") {
|
|
|
|
return false, escaped, nil
|
|
|
|
}
|
2021-07-08 07:23:09 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-08 02:27:36 +08:00
|
|
|
// 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 {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-25 17:59:32 +08:00
|
|
|
wikiWorkingPool.CheckIn(fmt.Sprint(repo.ID))
|
|
|
|
defer wikiWorkingPool.CheckOut(fmt.Sprint(repo.ID))
|
2020-01-08 02:27:36 +08:00
|
|
|
|
|
|
|
if err = InitWiki(repo); err != nil {
|
|
|
|
return fmt.Errorf("InitWiki: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
hasMasterBranch := git.IsBranchExist(repo.WikiPath(), "master")
|
|
|
|
|
|
|
|
basePath, err := models.CreateTemporaryPath("update-wiki")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := models.RemoveTemporaryPath(basePath); err != nil {
|
|
|
|
log.Error("Merge: RemoveTemporaryPath: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
cloneOpts := git.CloneRepoOptions{
|
|
|
|
Bare: true,
|
|
|
|
Shared: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasMasterBranch {
|
|
|
|
cloneOpts.Branch = "master"
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := git.Clone(repo.WikiPath(), basePath, cloneOpts); err != nil {
|
|
|
|
log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err)
|
|
|
|
return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
gitRepo, err := git.OpenRepository(basePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to open temporary repository: %s (%v)", basePath, err)
|
|
|
|
return fmt.Errorf("Failed to open new temporary repository in: %s %v", basePath, err)
|
|
|
|
}
|
|
|
|
defer gitRepo.Close()
|
|
|
|
|
|
|
|
if hasMasterBranch {
|
|
|
|
if err := gitRepo.ReadTreeToIndex("HEAD"); err != nil {
|
|
|
|
log.Error("Unable to read HEAD tree to index in: %s %v", basePath, err)
|
|
|
|
return fmt.Errorf("Unable to read HEAD tree to index in: %s %v", basePath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-08 07:23:09 +08:00
|
|
|
isWikiExist, newWikiPath, err := prepareWikiFileName(gitRepo, newWikiName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-08 02:27:36 +08:00
|
|
|
if isNew {
|
2021-07-08 07:23:09 +08:00
|
|
|
if isWikiExist {
|
2020-01-08 02:27:36 +08:00
|
|
|
return models.ErrWikiAlreadyExist{
|
|
|
|
Title: newWikiPath,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-07-08 07:23:09 +08:00
|
|
|
// 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
|
|
|
|
}
|
2020-01-08 02:27:36 +08:00
|
|
|
}
|
|
|
|
|
2021-07-08 07:23:09 +08:00
|
|
|
if isOldWikiExist {
|
2020-01-08 02:27:36 +08:00
|
|
|
err := gitRepo.RemoveFilesFromIndex(oldWikiPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("%v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: The wiki doesn't have lfs support at present - if this changes need to check attributes here
|
|
|
|
|
|
|
|
objectHash, err := gitRepo.HashObject(strings.NewReader(content))
|
|
|
|
if err != nil {
|
|
|
|
log.Error("%v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := gitRepo.AddObjectToIndex("100644", objectHash, newWikiPath); err != nil {
|
|
|
|
log.Error("%v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tree, err := gitRepo.WriteTree()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("%v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
commitTreeOpts := git.CommitTreeOpts{
|
|
|
|
Message: message,
|
|
|
|
}
|
|
|
|
|
2020-09-20 00:44:55 +08:00
|
|
|
committer := doer.NewGitSig()
|
|
|
|
|
|
|
|
sign, signingKey, signer, _ := repo.SignWikiCommit(doer)
|
2020-01-08 02:27:36 +08:00
|
|
|
if sign {
|
|
|
|
commitTreeOpts.KeyID = signingKey
|
2020-09-20 00:44:55 +08:00
|
|
|
if repo.GetTrustModel() == models.CommitterTrustModel || repo.GetTrustModel() == models.CollaboratorCommitterTrustModel {
|
|
|
|
committer = signer
|
|
|
|
}
|
2020-01-08 02:27:36 +08:00
|
|
|
} else {
|
|
|
|
commitTreeOpts.NoGPGSign = true
|
|
|
|
}
|
|
|
|
if hasMasterBranch {
|
|
|
|
commitTreeOpts.Parents = []string{"HEAD"}
|
|
|
|
}
|
2020-09-20 00:44:55 +08:00
|
|
|
|
|
|
|
commitHash, err := gitRepo.CommitTree(doer.NewGitSig(), committer, tree, commitTreeOpts)
|
2020-01-08 02:27:36 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Error("%v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := git.Push(basePath, git.PushOptions{
|
|
|
|
Remote: "origin",
|
|
|
|
Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, "master"),
|
|
|
|
Env: models.FullPushingEnvironment(
|
|
|
|
doer,
|
|
|
|
doer,
|
|
|
|
repo,
|
|
|
|
repo.Name+".wiki",
|
|
|
|
0,
|
|
|
|
),
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("%v", err)
|
2020-03-28 12:13:18 +08:00
|
|
|
if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-08 02:27:36 +08:00
|
|
|
return fmt.Errorf("Push: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddWikiPage adds a new wiki page with a given wikiPath.
|
|
|
|
func AddWikiPage(doer *models.User, repo *models.Repository, wikiName, content, message string) error {
|
|
|
|
return updateWikiPage(doer, repo, "", wikiName, content, message, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EditWikiPage updates a wiki page identified by its wikiPath,
|
|
|
|
// optionally also changing wikiPath.
|
|
|
|
func EditWikiPage(doer *models.User, repo *models.Repository, oldWikiName, newWikiName, content, message string) error {
|
|
|
|
return updateWikiPage(doer, repo, oldWikiName, newWikiName, content, message, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteWikiPage deletes a wiki page identified by its path.
|
|
|
|
func DeleteWikiPage(doer *models.User, repo *models.Repository, wikiName string) (err error) {
|
2020-12-25 17:59:32 +08:00
|
|
|
wikiWorkingPool.CheckIn(fmt.Sprint(repo.ID))
|
|
|
|
defer wikiWorkingPool.CheckOut(fmt.Sprint(repo.ID))
|
2020-01-08 02:27:36 +08:00
|
|
|
|
|
|
|
if err = InitWiki(repo); err != nil {
|
|
|
|
return fmt.Errorf("InitWiki: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
basePath, err := models.CreateTemporaryPath("update-wiki")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := models.RemoveTemporaryPath(basePath); err != nil {
|
|
|
|
log.Error("Merge: RemoveTemporaryPath: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := git.Clone(repo.WikiPath(), basePath, git.CloneRepoOptions{
|
|
|
|
Bare: true,
|
|
|
|
Shared: true,
|
|
|
|
Branch: "master",
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err)
|
|
|
|
return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
gitRepo, err := git.OpenRepository(basePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to open temporary repository: %s (%v)", basePath, err)
|
|
|
|
return fmt.Errorf("Failed to open new temporary repository in: %s %v", basePath, err)
|
|
|
|
}
|
|
|
|
defer gitRepo.Close()
|
|
|
|
|
|
|
|
if err := gitRepo.ReadTreeToIndex("HEAD"); err != nil {
|
|
|
|
log.Error("Unable to read HEAD tree to index in: %s %v", basePath, err)
|
|
|
|
return fmt.Errorf("Unable to read HEAD tree to index in: %s %v", basePath, err)
|
|
|
|
}
|
|
|
|
|
2021-07-20 00:14:00 +08:00
|
|
|
found, wikiPath, err := prepareWikiFileName(gitRepo, wikiName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-01-08 02:27:36 +08:00
|
|
|
}
|
|
|
|
if found {
|
|
|
|
err := gitRepo.RemoveFilesFromIndex(wikiPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return os.ErrNotExist
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: The wiki doesn't have lfs support at present - if this changes need to check attributes here
|
|
|
|
|
|
|
|
tree, err := gitRepo.WriteTree()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
message := "Delete page '" + wikiName + "'"
|
|
|
|
commitTreeOpts := git.CommitTreeOpts{
|
|
|
|
Message: message,
|
|
|
|
Parents: []string{"HEAD"},
|
|
|
|
}
|
|
|
|
|
2020-09-20 00:44:55 +08:00
|
|
|
committer := doer.NewGitSig()
|
|
|
|
|
|
|
|
sign, signingKey, signer, _ := repo.SignWikiCommit(doer)
|
2020-01-08 02:27:36 +08:00
|
|
|
if sign {
|
|
|
|
commitTreeOpts.KeyID = signingKey
|
2020-09-20 00:44:55 +08:00
|
|
|
if repo.GetTrustModel() == models.CommitterTrustModel || repo.GetTrustModel() == models.CollaboratorCommitterTrustModel {
|
|
|
|
committer = signer
|
|
|
|
}
|
2020-01-08 02:27:36 +08:00
|
|
|
} else {
|
|
|
|
commitTreeOpts.NoGPGSign = true
|
|
|
|
}
|
|
|
|
|
2020-09-20 00:44:55 +08:00
|
|
|
commitHash, err := gitRepo.CommitTree(doer.NewGitSig(), committer, tree, commitTreeOpts)
|
2020-01-08 02:27:36 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := git.Push(basePath, git.PushOptions{
|
|
|
|
Remote: "origin",
|
|
|
|
Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, "master"),
|
|
|
|
Env: models.PushingEnvironment(doer, repo),
|
|
|
|
}); err != nil {
|
2020-03-28 12:13:18 +08:00
|
|
|
if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-08 02:27:36 +08:00
|
|
|
return fmt.Errorf("Push: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-08 23:19:30 +08:00
|
|
|
|
|
|
|
// DeleteWiki removes the actual and local copy of repository wiki.
|
|
|
|
func DeleteWiki(repo *models.Repository) error {
|
|
|
|
if err := models.UpdateRepositoryUnits(repo, nil, []models.UnitType{models.UnitTypeWiki}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
models.RemoveAllWithNotice("Delete repository wiki", repo.WikiPath())
|
|
|
|
return nil
|
|
|
|
}
|