|
|
|
|
@ -6,18 +6,22 @@ package wiki
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
|
|
|
system_model "code.gitea.io/gitea/models/system"
|
|
|
|
|
"code.gitea.io/gitea/models/unit"
|
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
|
"code.gitea.io/gitea/modules/gitrepo"
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
|
|
|
|
"code.gitea.io/gitea/modules/sync"
|
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
|
asymkey_service "code.gitea.io/gitea/services/asymkey"
|
|
|
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
|
|
|
|
)
|
|
|
|
|
@ -25,10 +29,7 @@ import (
|
|
|
|
|
// TODO: use clustered lock (unique queue? or *abuse* cache)
|
|
|
|
|
var wikiWorkingPool = sync.NewExclusivePool()
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
DefaultRemote = "origin"
|
|
|
|
|
DefaultBranch = "master"
|
|
|
|
|
)
|
|
|
|
|
const DefaultRemote = "origin"
|
|
|
|
|
|
|
|
|
|
// InitWiki initializes a wiki for repository,
|
|
|
|
|
// it does nothing when repository already has wiki.
|
|
|
|
|
@ -41,25 +42,25 @@ func InitWiki(ctx context.Context, repo *repo_model.Repository) error {
|
|
|
|
|
return fmt.Errorf("InitRepository: %w", err)
|
|
|
|
|
} else if err = repo_module.CreateDelegateHooks(repo.WikiPath()); err != nil {
|
|
|
|
|
return fmt.Errorf("createDelegateHooks: %w", err)
|
|
|
|
|
} else if _, _, err = git.NewCommand(ctx, "symbolic-ref", "HEAD", git.BranchPrefix+DefaultBranch).RunStdString(&git.RunOpts{Dir: repo.WikiPath()}); err != nil {
|
|
|
|
|
return fmt.Errorf("unable to set default wiki branch to master: %w", err)
|
|
|
|
|
} else if _, _, err = git.NewCommand(ctx, "symbolic-ref", "HEAD").AddDynamicArguments(git.BranchPrefix + repo.DefaultWikiBranch).RunStdString(&git.RunOpts{Dir: repo.WikiPath()}); err != nil {
|
|
|
|
|
return fmt.Errorf("unable to set default wiki branch to %q: %w", repo.DefaultWikiBranch, err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// prepareGitPath 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 prepareGitPath(gitRepo *git.Repository, wikiPath WebPath) (bool, string, error) {
|
|
|
|
|
func prepareGitPath(gitRepo *git.Repository, defaultWikiBranch string, wikiPath WebPath) (bool, string, error) {
|
|
|
|
|
unescaped := string(wikiPath) + ".md"
|
|
|
|
|
gitPath := WebPathToGitPath(wikiPath)
|
|
|
|
|
|
|
|
|
|
// Look for both files
|
|
|
|
|
filesInIndex, err := gitRepo.LsTree(DefaultBranch, unescaped, gitPath)
|
|
|
|
|
filesInIndex, err := gitRepo.LsTree(defaultWikiBranch, unescaped, gitPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if strings.Contains(err.Error(), "Not a valid object name master") {
|
|
|
|
|
return false, gitPath, nil
|
|
|
|
|
if strings.Contains(err.Error(), "Not a valid object name") {
|
|
|
|
|
return false, gitPath, nil // branch doesn't exist
|
|
|
|
|
}
|
|
|
|
|
log.Error("%v", err)
|
|
|
|
|
log.Error("Wiki LsTree failed, err: %v", err)
|
|
|
|
|
return false, gitPath, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -95,7 +96,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
|
|
|
|
|
return fmt.Errorf("InitWiki: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hasMasterBranch := git.IsBranchExist(ctx, repo.WikiPath(), DefaultBranch)
|
|
|
|
|
hasDefaultBranch := git.IsBranchExist(ctx, repo.WikiPath(), repo.DefaultWikiBranch)
|
|
|
|
|
|
|
|
|
|
basePath, err := repo_module.CreateTemporaryPath("update-wiki")
|
|
|
|
|
if err != nil {
|
|
|
|
|
@ -112,8 +113,8 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
|
|
|
|
|
Shared: true,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if hasMasterBranch {
|
|
|
|
|
cloneOpts.Branch = DefaultBranch
|
|
|
|
|
if hasDefaultBranch {
|
|
|
|
|
cloneOpts.Branch = repo.DefaultWikiBranch
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := git.Clone(ctx, repo.WikiPath(), basePath, cloneOpts); err != nil {
|
|
|
|
|
@ -128,14 +129,14 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
|
|
|
|
|
}
|
|
|
|
|
defer gitRepo.Close()
|
|
|
|
|
|
|
|
|
|
if hasMasterBranch {
|
|
|
|
|
if hasDefaultBranch {
|
|
|
|
|
if err := gitRepo.ReadTreeToIndex("HEAD"); err != nil {
|
|
|
|
|
log.Error("Unable to read HEAD tree to index in: %s %v", basePath, err)
|
|
|
|
|
return fmt.Errorf("fnable to read HEAD tree to index in: %s %w", basePath, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isWikiExist, newWikiPath, err := prepareGitPath(gitRepo, newWikiName)
|
|
|
|
|
isWikiExist, newWikiPath, err := prepareGitPath(gitRepo, repo.DefaultWikiBranch, newWikiName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
@ -151,7 +152,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
|
|
|
|
|
isOldWikiExist := true
|
|
|
|
|
oldWikiPath := newWikiPath
|
|
|
|
|
if oldWikiName != newWikiName {
|
|
|
|
|
isOldWikiExist, oldWikiPath, err = prepareGitPath(gitRepo, oldWikiName)
|
|
|
|
|
isOldWikiExist, oldWikiPath, err = prepareGitPath(gitRepo, repo.DefaultWikiBranch, oldWikiName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
@ -200,7 +201,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
|
|
|
|
|
} else {
|
|
|
|
|
commitTreeOpts.NoGPGSign = true
|
|
|
|
|
}
|
|
|
|
|
if hasMasterBranch {
|
|
|
|
|
if hasDefaultBranch {
|
|
|
|
|
commitTreeOpts.Parents = []string{"HEAD"}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -212,7 +213,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
|
|
|
|
|
|
|
|
|
|
if err := git.Push(gitRepo.Ctx, basePath, git.PushOptions{
|
|
|
|
|
Remote: DefaultRemote,
|
|
|
|
|
Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, DefaultBranch),
|
|
|
|
|
Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, repo.DefaultWikiBranch),
|
|
|
|
|
Env: repo_module.FullPushingEnvironment(
|
|
|
|
|
doer,
|
|
|
|
|
doer,
|
|
|
|
|
@ -269,7 +270,7 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
|
|
|
|
|
if err := git.Clone(ctx, repo.WikiPath(), basePath, git.CloneRepoOptions{
|
|
|
|
|
Bare: true,
|
|
|
|
|
Shared: true,
|
|
|
|
|
Branch: DefaultBranch,
|
|
|
|
|
Branch: repo.DefaultWikiBranch,
|
|
|
|
|
}); err != nil {
|
|
|
|
|
log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err)
|
|
|
|
|
return fmt.Errorf("failed to clone repository: %s (%w)", repo.FullName(), err)
|
|
|
|
|
@ -287,7 +288,7 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
|
|
|
|
|
return fmt.Errorf("unable to read HEAD tree to index in: %s %w", basePath, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
found, wikiPath, err := prepareGitPath(gitRepo, wikiName)
|
|
|
|
|
found, wikiPath, err := prepareGitPath(gitRepo, repo.DefaultWikiBranch, wikiName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
@ -331,7 +332,7 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
|
|
|
|
|
|
|
|
|
|
if err := git.Push(gitRepo.Ctx, basePath, git.PushOptions{
|
|
|
|
|
Remote: DefaultRemote,
|
|
|
|
|
Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, DefaultBranch),
|
|
|
|
|
Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, repo.DefaultWikiBranch),
|
|
|
|
|
Env: repo_module.FullPushingEnvironment(
|
|
|
|
|
doer,
|
|
|
|
|
doer,
|
|
|
|
|
@ -358,3 +359,37 @@ func DeleteWiki(ctx context.Context, repo *repo_model.Repository) error {
|
|
|
|
|
system_model.RemoveAllWithNotice(ctx, "Delete repository wiki", repo.WikiPath())
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ChangeDefaultWikiBranch(ctx context.Context, repo *repo_model.Repository, newBranch string) error {
|
|
|
|
|
if !git.IsValidRefPattern(newBranch) {
|
|
|
|
|
return fmt.Errorf("invalid branch name: %s", newBranch)
|
|
|
|
|
}
|
|
|
|
|
return db.WithTx(ctx, func(ctx context.Context) error {
|
|
|
|
|
repo.DefaultWikiBranch = newBranch
|
|
|
|
|
if err := repo_model.UpdateRepositoryCols(ctx, repo, "default_wiki_branch"); err != nil {
|
|
|
|
|
return fmt.Errorf("unable to update database: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gitRepo, err := gitrepo.OpenWikiRepository(ctx, repo)
|
|
|
|
|
if errors.Is(err, util.ErrNotExist) {
|
|
|
|
|
return nil // no git repo on storage, no need to do anything else
|
|
|
|
|
} else if err != nil {
|
|
|
|
|
return fmt.Errorf("unable to open repository: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer gitRepo.Close()
|
|
|
|
|
|
|
|
|
|
oldDefBranch, err := gitRepo.GetDefaultBranch()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("unable to get default branch: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if oldDefBranch == newBranch {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = gitRepo.RenameBranch(oldDefBranch, newBranch)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("unable to rename default branch: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|