summaryrefslogtreecommitdiffstats
path: root/services/wiki
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2022-01-19 23:26:57 +0000
committerGitHub <noreply@github.com>2022-01-19 23:26:57 +0000
commit5cb0c9aa0d7eed087055b1efca79628957207d36 (patch)
treed117a514e1f17e5f6bfcda1be273f6a971112663 /services/wiki
parent4563148a61ba892e8f2bb66342f00a950bcd5315 (diff)
downloadgitea-5cb0c9aa0d7eed087055b1efca79628957207d36.tar.gz
gitea-5cb0c9aa0d7eed087055b1efca79628957207d36.zip
Propagate context and ensure git commands run in request context (#17868)
This PR continues the work in #17125 by progressively ensuring that git commands run within the request context. This now means that the if there is a git repo already open in the context it will be used instead of reopening it. Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'services/wiki')
-rw-r--r--services/wiki/wiki.go42
-rw-r--r--services/wiki/wiki_test.go24
2 files changed, 33 insertions, 33 deletions
diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go
index 4b679a4ad2..938aea96ca 100644
--- a/services/wiki/wiki.go
+++ b/services/wiki/wiki.go
@@ -6,6 +6,7 @@
package wiki
import (
+ "context"
"fmt"
"net/url"
"os"
@@ -13,7 +14,6 @@ import (
"code.gitea.io/gitea/models"
admin_model "code.gitea.io/gitea/models/admin"
- "code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
@@ -72,16 +72,16 @@ func FilenameToName(filename string) (string, error) {
// InitWiki initializes a wiki for repository,
// it does nothing when repository already has wiki.
-func InitWiki(repo *repo_model.Repository) error {
+func InitWiki(ctx context.Context, repo *repo_model.Repository) error {
if repo.HasWiki() {
return nil
}
- if err := git.InitRepository(repo.WikiPath(), true); err != nil {
+ if err := git.InitRepository(ctx, repo.WikiPath(), true); err != nil {
return fmt.Errorf("InitRepository: %v", err)
} else if err = repo_module.CreateDelegateHooks(repo.WikiPath()); err != nil {
return fmt.Errorf("createDelegateHooks: %v", err)
- } else if _, err = git.NewCommand("symbolic-ref", "HEAD", git.BranchPrefix+"master").RunInDir(repo.WikiPath()); err != nil {
+ } else if _, err = git.NewCommandContext(ctx, "symbolic-ref", "HEAD", git.BranchPrefix+"master").RunInDir(repo.WikiPath()); err != nil {
return fmt.Errorf("unable to set default wiki branch to master: %v", err)
}
return nil
@@ -119,18 +119,18 @@ func prepareWikiFileName(gitRepo *git.Repository, wikiName string) (bool, string
}
// updateWikiPage adds a new page to the repository wiki.
-func updateWikiPage(doer *user_model.User, repo *repo_model.Repository, oldWikiName, newWikiName, content, message string, isNew bool) (err error) {
+func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, oldWikiName, newWikiName, content, message string, isNew bool) (err error) {
if err = nameAllowed(newWikiName); err != nil {
return err
}
wikiWorkingPool.CheckIn(fmt.Sprint(repo.ID))
defer wikiWorkingPool.CheckOut(fmt.Sprint(repo.ID))
- if err = InitWiki(repo); err != nil {
+ if err = InitWiki(ctx, repo); err != nil {
return fmt.Errorf("InitWiki: %v", err)
}
- hasMasterBranch := git.IsBranchExist(git.DefaultContext, repo.WikiPath(), "master")
+ hasMasterBranch := git.IsBranchExist(ctx, repo.WikiPath(), "master")
basePath, err := models.CreateTemporaryPath("update-wiki")
if err != nil {
@@ -151,12 +151,12 @@ func updateWikiPage(doer *user_model.User, repo *repo_model.Repository, oldWikiN
cloneOpts.Branch = "master"
}
- if err := git.Clone(repo.WikiPath(), basePath, cloneOpts); err != nil {
+ if err := git.Clone(ctx, 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)
+ gitRepo, err := git.OpenRepositoryCtx(ctx, 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)
@@ -226,7 +226,7 @@ func updateWikiPage(doer *user_model.User, repo *repo_model.Repository, oldWikiN
committer := doer.NewGitSig()
- sign, signingKey, signer, _ := asymkey_service.SignWikiCommit(repo.WikiPath(), doer)
+ sign, signingKey, signer, _ := asymkey_service.SignWikiCommit(ctx, repo.WikiPath(), doer)
if sign {
commitTreeOpts.KeyID = signingKey
if repo.GetTrustModel() == repo_model.CommitterTrustModel || repo.GetTrustModel() == repo_model.CollaboratorCommitterTrustModel {
@@ -267,22 +267,22 @@ func updateWikiPage(doer *user_model.User, repo *repo_model.Repository, oldWikiN
}
// AddWikiPage adds a new wiki page with a given wikiPath.
-func AddWikiPage(doer *user_model.User, repo *repo_model.Repository, wikiName, content, message string) error {
- return updateWikiPage(doer, repo, "", wikiName, content, message, true)
+func AddWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, wikiName, content, message string) error {
+ return updateWikiPage(ctx, doer, repo, "", wikiName, content, message, true)
}
// EditWikiPage updates a wiki page identified by its wikiPath,
// optionally also changing wikiPath.
-func EditWikiPage(doer *user_model.User, repo *repo_model.Repository, oldWikiName, newWikiName, content, message string) error {
- return updateWikiPage(doer, repo, oldWikiName, newWikiName, content, message, false)
+func EditWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, oldWikiName, newWikiName, content, message string) error {
+ return updateWikiPage(ctx, doer, repo, oldWikiName, newWikiName, content, message, false)
}
// DeleteWikiPage deletes a wiki page identified by its path.
-func DeleteWikiPage(doer *user_model.User, repo *repo_model.Repository, wikiName string) (err error) {
+func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, wikiName string) (err error) {
wikiWorkingPool.CheckIn(fmt.Sprint(repo.ID))
defer wikiWorkingPool.CheckOut(fmt.Sprint(repo.ID))
- if err = InitWiki(repo); err != nil {
+ if err = InitWiki(ctx, repo); err != nil {
return fmt.Errorf("InitWiki: %v", err)
}
@@ -296,7 +296,7 @@ func DeleteWikiPage(doer *user_model.User, repo *repo_model.Repository, wikiName
}
}()
- if err := git.Clone(repo.WikiPath(), basePath, git.CloneRepoOptions{
+ if err := git.Clone(ctx, repo.WikiPath(), basePath, git.CloneRepoOptions{
Bare: true,
Shared: true,
Branch: "master",
@@ -305,7 +305,7 @@ func DeleteWikiPage(doer *user_model.User, repo *repo_model.Repository, wikiName
return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err)
}
- gitRepo, err := git.OpenRepository(basePath)
+ gitRepo, err := git.OpenRepositoryCtx(ctx, 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)
@@ -344,7 +344,7 @@ func DeleteWikiPage(doer *user_model.User, repo *repo_model.Repository, wikiName
committer := doer.NewGitSig()
- sign, signingKey, signer, _ := asymkey_service.SignWikiCommit(repo.WikiPath(), doer)
+ sign, signingKey, signer, _ := asymkey_service.SignWikiCommit(ctx, repo.WikiPath(), doer)
if sign {
commitTreeOpts.KeyID = signingKey
if repo.GetTrustModel() == repo_model.CommitterTrustModel || repo.GetTrustModel() == repo_model.CollaboratorCommitterTrustModel {
@@ -374,11 +374,11 @@ func DeleteWikiPage(doer *user_model.User, repo *repo_model.Repository, wikiName
}
// DeleteWiki removes the actual and local copy of repository wiki.
-func DeleteWiki(repo *repo_model.Repository) error {
+func DeleteWiki(ctx context.Context, repo *repo_model.Repository) error {
if err := repo_model.UpdateRepositoryUnits(repo, nil, []unit.Type{unit.TypeWiki}); err != nil {
return err
}
- admin_model.RemoveAllWithNotice(db.DefaultContext, "Delete repository wiki", repo.WikiPath())
+ admin_model.RemoveAllWithNotice(ctx, "Delete repository wiki", repo.WikiPath())
return nil
}
diff --git a/services/wiki/wiki_test.go b/services/wiki/wiki_test.go
index f5ee76109c..928d08c828 100644
--- a/services/wiki/wiki_test.go
+++ b/services/wiki/wiki_test.go
@@ -115,11 +115,11 @@ func TestRepository_InitWiki(t *testing.T) {
unittest.PrepareTestEnv(t)
// repo1 already has a wiki
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
- assert.NoError(t, InitWiki(repo1))
+ assert.NoError(t, InitWiki(git.DefaultContext, repo1))
// repo2 does not already have a wiki
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}).(*repo_model.Repository)
- assert.NoError(t, InitWiki(repo2))
+ assert.NoError(t, InitWiki(git.DefaultContext, repo2))
assert.True(t, repo2.HasWiki())
}
@@ -136,9 +136,9 @@ func TestRepository_AddWikiPage(t *testing.T) {
wikiName := wikiName
t.Run("test wiki exist: "+wikiName, func(t *testing.T) {
t.Parallel()
- assert.NoError(t, AddWikiPage(doer, repo, wikiName, wikiContent, commitMsg))
+ assert.NoError(t, AddWikiPage(git.DefaultContext, doer, repo, wikiName, wikiContent, commitMsg))
// Now need to show that the page has been added:
- gitRepo, err := git.OpenRepository(repo.WikiPath())
+ gitRepo, err := git.OpenRepositoryCtx(git.DefaultContext, repo.WikiPath())
assert.NoError(t, err)
defer gitRepo.Close()
masterTree, err := gitRepo.GetTree("master")
@@ -153,7 +153,7 @@ func TestRepository_AddWikiPage(t *testing.T) {
t.Run("check wiki already exist", func(t *testing.T) {
t.Parallel()
// test for already-existing wiki name
- err := AddWikiPage(doer, repo, "Home", wikiContent, commitMsg)
+ err := AddWikiPage(git.DefaultContext, doer, repo, "Home", wikiContent, commitMsg)
assert.Error(t, err)
assert.True(t, models.IsErrWikiAlreadyExist(err))
})
@@ -161,7 +161,7 @@ func TestRepository_AddWikiPage(t *testing.T) {
t.Run("check wiki reserved name", func(t *testing.T) {
t.Parallel()
// test for reserved wiki name
- err := AddWikiPage(doer, repo, "_edit", wikiContent, commitMsg)
+ err := AddWikiPage(git.DefaultContext, doer, repo, "_edit", wikiContent, commitMsg)
assert.Error(t, err)
assert.True(t, models.IsErrWikiReservedName(err))
})
@@ -180,10 +180,10 @@ func TestRepository_EditWikiPage(t *testing.T) {
"New/name/with/slashes",
} {
unittest.PrepareTestEnv(t)
- assert.NoError(t, EditWikiPage(doer, repo, "Home", newWikiName, newWikiContent, commitMsg))
+ assert.NoError(t, EditWikiPage(git.DefaultContext, doer, repo, "Home", newWikiName, newWikiContent, commitMsg))
// Now need to show that the page has been added:
- gitRepo, err := git.OpenRepository(repo.WikiPath())
+ gitRepo, err := git.OpenRepositoryCtx(git.DefaultContext, repo.WikiPath())
assert.NoError(t, err)
masterTree, err := gitRepo.GetTree("master")
assert.NoError(t, err)
@@ -204,10 +204,10 @@ func TestRepository_DeleteWikiPage(t *testing.T) {
unittest.PrepareTestEnv(t)
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
- assert.NoError(t, DeleteWikiPage(doer, repo, "Home"))
+ assert.NoError(t, DeleteWikiPage(git.DefaultContext, doer, repo, "Home"))
// Now need to show that the page has been added:
- gitRepo, err := git.OpenRepository(repo.WikiPath())
+ gitRepo, err := git.OpenRepositoryCtx(git.DefaultContext, repo.WikiPath())
assert.NoError(t, err)
defer gitRepo.Close()
masterTree, err := gitRepo.GetTree("master")
@@ -220,7 +220,7 @@ func TestRepository_DeleteWikiPage(t *testing.T) {
func TestPrepareWikiFileName(t *testing.T) {
unittest.PrepareTestEnv(t)
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
- gitRepo, err := git.OpenRepository(repo.WikiPath())
+ gitRepo, err := git.OpenRepositoryCtx(git.DefaultContext, repo.WikiPath())
defer gitRepo.Close()
assert.NoError(t, err)
@@ -280,7 +280,7 @@ func TestPrepareWikiFileName_FirstPage(t *testing.T) {
}
}()
- err = git.InitRepository(tmpDir, true)
+ err = git.InitRepository(git.DefaultContext, tmpDir, true)
assert.NoError(t, err)
gitRepo, err := git.OpenRepository(tmpDir)