2020-01-08 02:27:36 +08:00
// Copyright 2015 The Gogs Authors. All rights reserved.
// Copyright 2019 The Gitea Authors. All rights reserved.
2022-11-27 13:20:29 -05:00
// SPDX-License-Identifier: MIT
2020-01-08 02:27:36 +08:00
package wiki
import (
2022-01-19 23:26:57 +00:00
"context"
2020-01-08 02:27:36 +08:00
"fmt"
"os"
"strings"
2021-12-10 09:27:50 +08:00
repo_model "code.gitea.io/gitea/models/repo"
2022-10-17 07:29:26 +08:00
system_model "code.gitea.io/gitea/models/system"
2021-11-10 03:57:58 +08:00
"code.gitea.io/gitea/models/unit"
2021-11-24 17:49:20 +08:00
user_model "code.gitea.io/gitea/models/user"
2020-01-08 02:27:36 +08:00
"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"
2021-12-10 16:14:24 +08:00
asymkey_service "code.gitea.io/gitea/services/asymkey"
2024-01-13 05:50:38 +08:00
repo_service "code.gitea.io/gitea/services/repository"
2020-01-08 02:27:36 +08:00
)
2023-04-20 01:50:10 +08:00
// TODO: use clustered lock (unique queue? or *abuse* cache)
var wikiWorkingPool = sync . NewExclusivePool ( )
2020-01-08 02:27:36 +08:00
2022-10-15 16:40:32 +02:00
const (
DefaultRemote = "origin"
DefaultBranch = "master"
)
2020-01-08 02:27:36 +08:00
// InitWiki initializes a wiki for repository,
// it does nothing when repository already has wiki.
2022-01-19 23:26:57 +00:00
func InitWiki ( ctx context . Context , repo * repo_model . Repository ) error {
2020-01-08 02:27:36 +08:00
if repo . HasWiki ( ) {
return nil
}
2023-12-17 19:56:08 +08:00
if err := git . InitRepository ( ctx , repo . WikiPath ( ) , true , repo . ObjectFormatName ) ; err != nil {
2022-10-24 21:29:17 +02:00
return fmt . Errorf ( "InitRepository: %w" , err )
2020-01-21 04:01:19 +08:00
} else if err = repo_module . CreateDelegateHooks ( repo . WikiPath ( ) ) ; err != nil {
2022-10-24 21:29:17 +02:00
return fmt . Errorf ( "createDelegateHooks: %w" , err )
2022-10-15 16:40:32 +02:00
} else if _ , _ , err = git . NewCommand ( ctx , "symbolic-ref" , "HEAD" , git . BranchPrefix + DefaultBranch ) . RunStdString ( & git . RunOpts { Dir : repo . WikiPath ( ) } ) ; err != nil {
2022-10-24 21:29:17 +02:00
return fmt . Errorf ( "unable to set default wiki branch to master: %w" , err )
2020-01-08 02:27:36 +08:00
}
return nil
}
2023-04-20 01:50:10 +08:00
// prepareGitPath try to find a suitable file path with file name by the given raw wiki name.
2021-07-08 07:23:09 +08:00
// return: existence, prepared file path with name, error
2023-04-20 01:50:10 +08:00
func prepareGitPath ( gitRepo * git . Repository , wikiPath WebPath ) ( bool , string , error ) {
unescaped := string ( wikiPath ) + ".md"
gitPath := WebPathToGitPath ( wikiPath )
2021-07-08 07:23:09 +08:00
// Look for both files
2023-04-20 01:50:10 +08:00
filesInIndex , err := gitRepo . LsTree ( DefaultBranch , unescaped , gitPath )
2021-07-08 07:23:09 +08:00
if err != nil {
2021-08-01 18:04:32 +01:00
if strings . Contains ( err . Error ( ) , "Not a valid object name master" ) {
2023-04-20 01:50:10 +08:00
return false , gitPath , nil
2021-08-01 18:04:32 +01:00
}
2021-07-08 07:23:09 +08:00
log . Error ( "%v" , err )
2023-04-20 01:50:10 +08:00
return false , gitPath , err
2021-07-08 07:23:09 +08:00
}
foundEscaped := false
for _ , filename := range filesInIndex {
switch filename {
case unescaped :
// if we find the unescaped file return it
return true , unescaped , nil
2023-04-20 01:50:10 +08:00
case gitPath :
2021-07-08 07:23:09 +08:00
foundEscaped = true
}
}
// If not return whether the escaped file exists, and the escaped filename to keep backwards compatibility.
2023-04-20 01:50:10 +08:00
return foundEscaped , gitPath , nil
2021-07-08 07:23:09 +08:00
}
2022-09-04 21:54:23 +02:00
// updateWikiPage adds a new page or edits an existing page in repository wiki.
2023-04-20 01:50:10 +08:00
func updateWikiPage ( ctx context . Context , doer * user_model . User , repo * repo_model . Repository , oldWikiName , newWikiName WebPath , content , message string , isNew bool ) ( err error ) {
2023-09-22 01:43:29 +02:00
err = repo . MustNotBeArchived ( )
if err != nil {
return err
}
2023-04-20 01:50:10 +08:00
if err = validateWebPath ( newWikiName ) ; err != nil {
2020-01-08 02:27:36 +08:00
return err
}
2020-12-25 09:59:32 +00:00
wikiWorkingPool . CheckIn ( fmt . Sprint ( repo . ID ) )
defer wikiWorkingPool . CheckOut ( fmt . Sprint ( repo . ID ) )
2020-01-08 02:27:36 +08:00
2022-01-19 23:26:57 +00:00
if err = InitWiki ( ctx , repo ) ; err != nil {
2022-10-24 21:29:17 +02:00
return fmt . Errorf ( "InitWiki: %w" , err )
2020-01-08 02:27:36 +08:00
}
2022-10-15 16:40:32 +02:00
hasMasterBranch := git . IsBranchExist ( ctx , repo . WikiPath ( ) , DefaultBranch )
2020-01-08 02:27:36 +08:00
2022-05-09 00:46:32 +08:00
basePath , err := repo_module . CreateTemporaryPath ( "update-wiki" )
2020-01-08 02:27:36 +08:00
if err != nil {
return err
}
defer func ( ) {
2022-05-09 00:46:32 +08:00
if err := repo_module . RemoveTemporaryPath ( basePath ) ; err != nil {
2020-01-08 02:27:36 +08:00
log . Error ( "Merge: RemoveTemporaryPath: %s" , err )
}
} ( )
cloneOpts := git . CloneRepoOptions {
Bare : true ,
Shared : true ,
}
if hasMasterBranch {
2022-10-15 16:40:32 +02:00
cloneOpts . Branch = DefaultBranch
2020-01-08 02:27:36 +08:00
}
2022-01-19 23:26:57 +00:00
if err := git . Clone ( ctx , repo . WikiPath ( ) , basePath , cloneOpts ) ; err != nil {
2020-01-08 02:27:36 +08:00
log . Error ( "Failed to clone repository: %s (%v)" , repo . FullName ( ) , err )
2023-04-20 01:50:10 +08:00
return fmt . Errorf ( "failed to clone repository: %s (%w)" , repo . FullName ( ) , err )
2020-01-08 02:27:36 +08:00
}
2022-03-29 21:13:41 +02:00
gitRepo , err := git . OpenRepository ( ctx , basePath )
2020-01-08 02:27:36 +08:00
if err != nil {
log . Error ( "Unable to open temporary repository: %s (%v)" , basePath , err )
2023-04-20 01:50:10 +08:00
return fmt . Errorf ( "failed to open new temporary repository in: %s %w" , basePath , err )
2020-01-08 02:27:36 +08:00
}
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 )
2023-04-20 01:50:10 +08:00
return fmt . Errorf ( "fnable to read HEAD tree to index in: %s %w" , basePath , err )
2020-01-08 02:27:36 +08:00
}
}
2023-04-20 01:50:10 +08:00
isWikiExist , newWikiPath , err := prepareGitPath ( gitRepo , newWikiName )
2021-07-08 07:23:09 +08:00
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 {
2022-08-25 10:31:57 +08:00
return repo_model . ErrWikiAlreadyExist {
2020-01-08 02:27:36 +08:00
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 {
2023-04-20 01:50:10 +08:00
isOldWikiExist , oldWikiPath , err = prepareGitPath ( gitRepo , oldWikiName )
2021-07-08 07:23:09 +08:00
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-19 17:44:55 +01:00
committer := doer . NewGitSig ( )
2022-01-19 23:26:57 +00:00
sign , signingKey , signer , _ := asymkey_service . SignWikiCommit ( ctx , repo . WikiPath ( ) , doer )
2020-01-08 02:27:36 +08:00
if sign {
commitTreeOpts . KeyID = signingKey
2021-12-10 09:27:50 +08:00
if repo . GetTrustModel ( ) == repo_model . CommitterTrustModel || repo . GetTrustModel ( ) == repo_model . CollaboratorCommitterTrustModel {
2020-09-19 17:44:55 +01:00
committer = signer
}
2020-01-08 02:27:36 +08:00
} else {
commitTreeOpts . NoGPGSign = true
}
if hasMasterBranch {
commitTreeOpts . Parents = [ ] string { "HEAD" }
}
2020-09-19 17:44:55 +01: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
}
2021-11-30 20:06:32 +00:00
if err := git . Push ( gitRepo . Ctx , basePath , git . PushOptions {
2022-10-15 16:40:32 +02:00
Remote : DefaultRemote ,
Branch : fmt . Sprintf ( "%s:%s%s" , commitHash . String ( ) , git . BranchPrefix , DefaultBranch ) ,
2022-05-09 00:46:32 +08:00
Env : repo_module . FullPushingEnvironment (
2020-01-08 02:27:36 +08:00
doer ,
doer ,
repo ,
repo . Name + ".wiki" ,
0 ,
) ,
} ) ; err != nil {
log . Error ( "%v" , err )
2020-03-28 04:13:18 +00:00
if git . IsErrPushOutOfDate ( err ) || git . IsErrPushRejected ( err ) {
return err
}
2022-10-24 21:29:17 +02:00
return fmt . Errorf ( "Push: %w" , err )
2020-01-08 02:27:36 +08:00
}
return nil
}
// AddWikiPage adds a new wiki page with a given wikiPath.
2023-04-20 01:50:10 +08:00
func AddWikiPage ( ctx context . Context , doer * user_model . User , repo * repo_model . Repository , wikiName WebPath , content , message string ) error {
2022-01-19 23:26:57 +00:00
return updateWikiPage ( ctx , doer , repo , "" , wikiName , content , message , true )
2020-01-08 02:27:36 +08:00
}
// EditWikiPage updates a wiki page identified by its wikiPath,
// optionally also changing wikiPath.
2023-04-20 01:50:10 +08:00
func EditWikiPage ( ctx context . Context , doer * user_model . User , repo * repo_model . Repository , oldWikiName , newWikiName WebPath , content , message string ) error {
2022-01-19 23:26:57 +00:00
return updateWikiPage ( ctx , doer , repo , oldWikiName , newWikiName , content , message , false )
2020-01-08 02:27:36 +08:00
}
// DeleteWikiPage deletes a wiki page identified by its path.
2023-04-20 01:50:10 +08:00
func DeleteWikiPage ( ctx context . Context , doer * user_model . User , repo * repo_model . Repository , wikiName WebPath ) ( err error ) {
2023-09-22 01:43:29 +02:00
err = repo . MustNotBeArchived ( )
if err != nil {
return err
}
2020-12-25 09:59:32 +00:00
wikiWorkingPool . CheckIn ( fmt . Sprint ( repo . ID ) )
defer wikiWorkingPool . CheckOut ( fmt . Sprint ( repo . ID ) )
2020-01-08 02:27:36 +08:00
2022-01-19 23:26:57 +00:00
if err = InitWiki ( ctx , repo ) ; err != nil {
2022-10-24 21:29:17 +02:00
return fmt . Errorf ( "InitWiki: %w" , err )
2020-01-08 02:27:36 +08:00
}
2022-05-09 00:46:32 +08:00
basePath , err := repo_module . CreateTemporaryPath ( "update-wiki" )
2020-01-08 02:27:36 +08:00
if err != nil {
return err
}
defer func ( ) {
2022-05-09 00:46:32 +08:00
if err := repo_module . RemoveTemporaryPath ( basePath ) ; err != nil {
2020-01-08 02:27:36 +08:00
log . Error ( "Merge: RemoveTemporaryPath: %s" , err )
}
} ( )
2022-01-19 23:26:57 +00:00
if err := git . Clone ( ctx , repo . WikiPath ( ) , basePath , git . CloneRepoOptions {
2020-01-08 02:27:36 +08:00
Bare : true ,
Shared : true ,
2022-10-15 16:40:32 +02:00
Branch : DefaultBranch ,
2020-01-08 02:27:36 +08:00
} ) ; err != nil {
log . Error ( "Failed to clone repository: %s (%v)" , repo . FullName ( ) , err )
2023-04-20 01:50:10 +08:00
return fmt . Errorf ( "failed to clone repository: %s (%w)" , repo . FullName ( ) , err )
2020-01-08 02:27:36 +08:00
}
2022-03-29 21:13:41 +02:00
gitRepo , err := git . OpenRepository ( ctx , basePath )
2020-01-08 02:27:36 +08:00
if err != nil {
log . Error ( "Unable to open temporary repository: %s (%v)" , basePath , err )
2023-04-20 01:50:10 +08:00
return fmt . Errorf ( "failed to open new temporary repository in: %s %w" , basePath , err )
2020-01-08 02:27:36 +08:00
}
defer gitRepo . Close ( )
if err := gitRepo . ReadTreeToIndex ( "HEAD" ) ; err != nil {
log . Error ( "Unable to read HEAD tree to index in: %s %v" , basePath , err )
2023-04-20 01:50:10 +08:00
return fmt . Errorf ( "unable to read HEAD tree to index in: %s %w" , basePath , err )
2020-01-08 02:27:36 +08:00
}
2023-04-20 01:50:10 +08:00
found , wikiPath , err := prepareGitPath ( gitRepo , wikiName )
2021-07-20 00:14:00 +08:00
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
}
2023-04-20 01:50:10 +08:00
message := fmt . Sprintf ( "Delete page %q" , wikiName )
2020-01-08 02:27:36 +08:00
commitTreeOpts := git . CommitTreeOpts {
Message : message ,
Parents : [ ] string { "HEAD" } ,
}
2020-09-19 17:44:55 +01:00
committer := doer . NewGitSig ( )
2022-01-19 23:26:57 +00:00
sign , signingKey , signer , _ := asymkey_service . SignWikiCommit ( ctx , repo . WikiPath ( ) , doer )
2020-01-08 02:27:36 +08:00
if sign {
commitTreeOpts . KeyID = signingKey
2021-12-10 09:27:50 +08:00
if repo . GetTrustModel ( ) == repo_model . CommitterTrustModel || repo . GetTrustModel ( ) == repo_model . CollaboratorCommitterTrustModel {
2020-09-19 17:44:55 +01:00
committer = signer
}
2020-01-08 02:27:36 +08:00
} else {
commitTreeOpts . NoGPGSign = true
}
2020-09-19 17:44:55 +01:00
commitHash , err := gitRepo . CommitTree ( doer . NewGitSig ( ) , committer , tree , commitTreeOpts )
2020-01-08 02:27:36 +08:00
if err != nil {
return err
}
2021-11-30 20:06:32 +00:00
if err := git . Push ( gitRepo . Ctx , basePath , git . PushOptions {
2022-10-15 16:40:32 +02:00
Remote : DefaultRemote ,
Branch : fmt . Sprintf ( "%s:%s%s" , commitHash . String ( ) , git . BranchPrefix , DefaultBranch ) ,
2023-04-24 05:22:16 +08:00
Env : repo_module . FullPushingEnvironment (
doer ,
doer ,
repo ,
repo . Name + ".wiki" ,
0 ,
) ,
2020-01-08 02:27:36 +08:00
} ) ; err != nil {
2020-03-28 04:13:18 +00:00
if git . IsErrPushOutOfDate ( err ) || git . IsErrPushRejected ( err ) {
return err
}
2022-10-24 21:29:17 +02:00
return fmt . Errorf ( "Push: %w" , err )
2020-01-08 02:27:36 +08:00
}
return nil
}
2021-09-08 23:19:30 +08:00
// DeleteWiki removes the actual and local copy of repository wiki.
2022-01-19 23:26:57 +00:00
func DeleteWiki ( ctx context . Context , repo * repo_model . Repository ) error {
2024-01-13 05:50:38 +08:00
if err := repo_service . UpdateRepositoryUnits ( ctx , repo , nil , [ ] unit . Type { unit . TypeWiki } ) ; err != nil {
2021-09-08 23:19:30 +08:00
return err
}
2022-10-17 07:29:26 +08:00
system_model . RemoveAllWithNotice ( ctx , "Delete repository wiki" , repo . WikiPath ( ) )
2021-09-08 23:19:30 +08:00
return nil
}