aboutsummaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
Diffstat (limited to 'modules')
-rw-r--r--modules/context/api.go48
-rw-r--r--modules/context/context.go6
-rw-r--r--modules/context/private.go41
-rw-r--r--modules/context/repo.go10
-rw-r--r--modules/convert/git_commit.go5
-rw-r--r--modules/convert/pull.go11
-rw-r--r--modules/convert/pull_review.go13
-rw-r--r--modules/convert/pull_test.go5
-rw-r--r--modules/doctor/authorizedkeys.go3
-rw-r--r--modules/doctor/checkOldArchives.go3
-rw-r--r--modules/doctor/dbconsistency.go8
-rw-r--r--modules/doctor/dbversion.go6
-rw-r--r--modules/doctor/doctor.go4
-rw-r--r--modules/doctor/fix16961.go3
-rw-r--r--modules/doctor/mergebase.go11
-rw-r--r--modules/doctor/misc.go17
-rw-r--r--modules/doctor/paths.go3
-rw-r--r--modules/doctor/storage.go4
-rw-r--r--modules/doctor/usertype.go4
-rw-r--r--modules/git/blame.go6
-rw-r--r--modules/git/commit.go31
-rw-r--r--modules/git/commit_info_test.go2
-rw-r--r--modules/git/commit_test.go6
-rw-r--r--modules/git/diff.go14
-rw-r--r--modules/git/pipeline/catfile.go13
-rw-r--r--modules/git/pipeline/lfs.go2
-rw-r--r--modules/git/pipeline/lfs_nogogit.go4
-rw-r--r--modules/git/pipeline/namerev.go5
-rw-r--r--modules/git/pipeline/revlist.go9
-rw-r--r--modules/git/repo.go169
-rw-r--r--modules/git/repo_base.go49
-rw-r--r--modules/git/repo_base_gogit.go3
-rw-r--r--modules/git/repo_base_nogogit.go3
-rw-r--r--modules/git/repo_branch.go4
-rw-r--r--modules/git/repo_branch_gogit.go12
-rw-r--r--modules/git/repo_commit.go6
-rw-r--r--modules/git/repo_compare.go15
-rw-r--r--modules/git/repo_test.go2
-rw-r--r--modules/git/tree.go2
-rw-r--r--modules/git/tree_entry_nogogit.go16
-rw-r--r--modules/git/tree_nogogit.go4
-rw-r--r--modules/gitgraph/graph.go2
-rw-r--r--modules/indexer/code/bleve.go13
-rw-r--r--modules/indexer/code/elastic_search.go10
-rw-r--r--modules/indexer/code/git.go23
-rw-r--r--modules/indexer/code/indexer.go12
-rw-r--r--modules/indexer/code/indexer_test.go3
-rw-r--r--modules/indexer/code/wrapped.go5
-rw-r--r--modules/lfs/pointer_scanner_nogogit.go8
-rw-r--r--modules/markup/html.go2
-rw-r--r--modules/markup/html_test.go2
-rw-r--r--modules/markup/markdown/markdown_test.go3
-rw-r--r--modules/notification/action/action.go11
-rw-r--r--modules/notification/base/notifier.go4
-rw-r--r--modules/notification/base/null.go4
-rw-r--r--modules/notification/mail/mail.go32
-rw-r--r--modules/notification/notification.go8
-rw-r--r--modules/notification/webhook/webhook.go98
-rw-r--r--modules/process/manager.go16
-rw-r--r--modules/repository/commits.go11
-rw-r--r--modules/repository/commits_test.go2
-rw-r--r--modules/repository/generate.go16
-rw-r--r--modules/repository/init.go22
-rw-r--r--modules/repository/push.go5
-rw-r--r--modules/repository/repo.go18
-rw-r--r--modules/templates/helper.go24
-rw-r--r--modules/test/context_tests.go4
-rw-r--r--modules/web/route.go10
68 files changed, 504 insertions, 446 deletions
diff --git a/modules/context/api.go b/modules/context/api.go
index b079385aff..dae6d23989 100644
--- a/modules/context/api.go
+++ b/modules/context/api.go
@@ -303,6 +303,7 @@ func APIContexter() func(http.Handler) http.Handler {
ctx.Resp.Header().Set(`X-Frame-Options`, setting.CORSConfig.XFrameOptions)
ctx.Data["CsrfToken"] = html.EscapeString(ctx.csrf.GetToken())
+ ctx.Data["Context"] = &ctx
next.ServeHTTP(ctx.Resp, ctx.Req)
@@ -321,35 +322,32 @@ func APIContexter() func(http.Handler) http.Handler {
}
// ReferencesGitRepo injects the GitRepo into the Context
-func ReferencesGitRepo(allowEmpty bool) func(http.Handler) http.Handler {
- return func(next http.Handler) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
- ctx := GetAPIContext(req)
- // Empty repository does not have reference information.
- if !allowEmpty && ctx.Repo.Repository.IsEmpty {
+func ReferencesGitRepo(allowEmpty bool) func(ctx *APIContext) (cancel context.CancelFunc) {
+ return func(ctx *APIContext) (cancel context.CancelFunc) {
+ // Empty repository does not have reference information.
+ if !allowEmpty && ctx.Repo.Repository.IsEmpty {
+ return
+ }
+
+ // For API calls.
+ if ctx.Repo.GitRepo == nil {
+ repoPath := repo_model.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
+ gitRepo, err := git.OpenRepositoryCtx(ctx, repoPath)
+ if err != nil {
+ ctx.Error(http.StatusInternalServerError, "RepoRef Invalid repo "+repoPath, err)
return
}
-
- // For API calls.
- if ctx.Repo.GitRepo == nil {
- repoPath := repo_model.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
- gitRepo, err := git.OpenRepository(repoPath)
- if err != nil {
- ctx.Error(http.StatusInternalServerError, "RepoRef Invalid repo "+repoPath, err)
- return
+ ctx.Repo.GitRepo = gitRepo
+ // We opened it, we should close it
+ return func() {
+ // If it's been set to nil then assume someone else has closed it.
+ if ctx.Repo.GitRepo != nil {
+ ctx.Repo.GitRepo.Close()
}
- ctx.Repo.GitRepo = gitRepo
- // We opened it, we should close it
- defer func() {
- // If it's been set to nil then assume someone else has closed it.
- if ctx.Repo.GitRepo != nil {
- ctx.Repo.GitRepo.Close()
- }
- }()
}
+ }
- next.ServeHTTP(w, req)
- })
+ return
}
}
@@ -391,7 +389,7 @@ func RepoRefForAPI(next http.Handler) http.Handler {
if ctx.Repo.GitRepo == nil {
repoPath := repo_model.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
- ctx.Repo.GitRepo, err = git.OpenRepository(repoPath)
+ ctx.Repo.GitRepo, err = git.OpenRepositoryCtx(ctx, repoPath)
if err != nil {
ctx.InternalServerError(err)
return
diff --git a/modules/context/context.go b/modules/context/context.go
index 5038850649..ab83ae4eb5 100644
--- a/modules/context/context.go
+++ b/modules/context/context.go
@@ -23,6 +23,7 @@ import (
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/base"
mc "code.gitea.io/gitea/modules/cache"
+ "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
@@ -529,6 +530,10 @@ func (ctx *Context) Err() error {
// Value is part of the interface for context.Context and we pass this to the request context
func (ctx *Context) Value(key interface{}) interface{} {
+ if key == git.RepositoryContextKey && ctx.Repo != nil {
+ return ctx.Repo.GitRepo
+ }
+
return ctx.Req.Context().Value(key)
}
@@ -631,6 +636,7 @@ func Contexter() func(next http.Handler) http.Handler {
// PageData is passed by reference, and it will be rendered to `window.config.pageData` in `head.tmpl` for JavaScript modules
ctx.PageData = map[string]interface{}{}
ctx.Data["PageData"] = ctx.PageData
+ ctx.Data["Context"] = &ctx
ctx.Req = WithContext(req, &ctx)
ctx.csrf = Csrfer(csrfOpts, &ctx)
diff --git a/modules/context/private.go b/modules/context/private.go
index dfefa1d2f0..3e31a7e7d8 100644
--- a/modules/context/private.go
+++ b/modules/context/private.go
@@ -6,12 +6,42 @@ package context
import (
"context"
+ "fmt"
"net/http"
+ "time"
+
+ "code.gitea.io/gitea/modules/graceful"
+ "code.gitea.io/gitea/modules/process"
)
// PrivateContext represents a context for private routes
type PrivateContext struct {
*Context
+ Override context.Context
+}
+
+// Deadline is part of the interface for context.Context and we pass this to the request context
+func (ctx *PrivateContext) Deadline() (deadline time.Time, ok bool) {
+ if ctx.Override != nil {
+ return ctx.Override.Deadline()
+ }
+ return ctx.Req.Context().Deadline()
+}
+
+// Done is part of the interface for context.Context and we pass this to the request context
+func (ctx *PrivateContext) Done() <-chan struct{} {
+ if ctx.Override != nil {
+ return ctx.Override.Done()
+ }
+ return ctx.Req.Context().Done()
+}
+
+// Err is part of the interface for context.Context and we pass this to the request context
+func (ctx *PrivateContext) Err() error {
+ if ctx.Override != nil {
+ return ctx.Override.Err()
+ }
+ return ctx.Req.Context().Err()
}
var (
@@ -39,7 +69,18 @@ func PrivateContexter() func(http.Handler) http.Handler {
},
}
ctx.Req = WithPrivateContext(req, ctx)
+ ctx.Data["Context"] = ctx
next.ServeHTTP(ctx.Resp, ctx.Req)
})
}
}
+
+// OverrideContext overrides the underlying request context for Done() etc.
+// This function should be used when there is a need for work to continue even if the request has been cancelled.
+// Primarily this affects hook/post-receive and hook/proc-receive both of which need to continue working even if
+// the underlying request has timed out from the ssh/http push
+func OverrideContext(ctx *PrivateContext) (cancel context.CancelFunc) {
+ // We now need to override the request context as the base for our work because even if the request is cancelled we have to continue this work
+ ctx.Override, _, cancel = process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("PrivateContext: %s", ctx.Req.RequestURI))
+ return
+}
diff --git a/modules/context/repo.go b/modules/context/repo.go
index bf782383b5..4acb800b64 100644
--- a/modules/context/repo.go
+++ b/modules/context/repo.go
@@ -109,7 +109,7 @@ type CanCommitToBranchResults struct {
// CanCommitToBranch returns true if repository is editable and user has proper access level
// and branch is not protected for push
-func (r *Repository) CanCommitToBranch(doer *user_model.User) (CanCommitToBranchResults, error) {
+func (r *Repository) CanCommitToBranch(ctx context.Context, doer *user_model.User) (CanCommitToBranchResults, error) {
protectedBranch, err := models.GetProtectedBranchBy(r.Repository.ID, r.BranchName)
if err != nil {
@@ -122,7 +122,7 @@ func (r *Repository) CanCommitToBranch(doer *user_model.User) (CanCommitToBranch
requireSigned = protectedBranch.RequireSignedCommits
}
- sign, keyID, _, err := asymkey_service.SignCRUDAction(r.Repository.RepoPath(), doer, r.Repository.RepoPath(), git.BranchPrefix+r.BranchName)
+ sign, keyID, _, err := asymkey_service.SignCRUDAction(ctx, r.Repository.RepoPath(), doer, r.Repository.RepoPath(), git.BranchPrefix+r.BranchName)
canCommit := r.CanEnableEditor() && userCanPush
if requireSigned {
@@ -180,14 +180,14 @@ func (r *Repository) GetCommitsCount() (int64, error) {
}
// GetCommitGraphsCount returns cached commit count for current view
-func (r *Repository) GetCommitGraphsCount(hidePRRefs bool, branches, files []string) (int64, error) {
+func (r *Repository) GetCommitGraphsCount(ctx context.Context, hidePRRefs bool, branches, files []string) (int64, error) {
cacheKey := fmt.Sprintf("commits-count-%d-graph-%t-%s-%s", r.Repository.ID, hidePRRefs, branches, files)
return cache.GetInt64(cacheKey, func() (int64, error) {
if len(branches) == 0 {
- return git.AllCommitsCount(r.Repository.RepoPath(), hidePRRefs, files...)
+ return git.AllCommitsCount(ctx, r.Repository.RepoPath(), hidePRRefs, files...)
}
- return git.CommitsCountFiles(r.Repository.RepoPath(), branches, files)
+ return git.CommitsCountFiles(ctx, r.Repository.RepoPath(), branches, files)
})
}
diff --git a/modules/convert/git_commit.go b/modules/convert/git_commit.go
index 55cac5fd58..a5c3112f13 100644
--- a/modules/convert/git_commit.go
+++ b/modules/convert/git_commit.go
@@ -72,8 +72,7 @@ func ToPayloadCommit(repo *repo_model.Repository, c *git.Commit) *api.PayloadCom
}
// ToCommit convert a git.Commit to api.Commit
-func ToCommit(repo *repo_model.Repository, commit *git.Commit, userCache map[string]*user_model.User) (*api.Commit, error) {
-
+func ToCommit(repo *repo_model.Repository, gitRepo *git.Repository, commit *git.Commit, userCache map[string]*user_model.User) (*api.Commit, error) {
var apiAuthor, apiCommitter *api.User
// Retrieve author and committer information
@@ -134,7 +133,7 @@ func ToCommit(repo *repo_model.Repository, commit *git.Commit, userCache map[str
}
// Retrieve files affected by the commit
- fileStatus, err := git.GetCommitFileStatus(repo.RepoPath(), commit.ID.String())
+ fileStatus, err := git.GetCommitFileStatus(gitRepo.Ctx, repo.RepoPath(), commit.ID.String())
if err != nil {
return nil, err
}
diff --git a/modules/convert/pull.go b/modules/convert/pull.go
index 2e2c1b85bc..1551645a51 100644
--- a/modules/convert/pull.go
+++ b/modules/convert/pull.go
@@ -5,6 +5,7 @@
package convert
import (
+ "context"
"fmt"
"code.gitea.io/gitea/models"
@@ -18,7 +19,7 @@ import (
// ToAPIPullRequest assumes following fields have been assigned with valid values:
// Required - Issue
// Optional - Merger
-func ToAPIPullRequest(pr *models.PullRequest, doer *user_model.User) *api.PullRequest {
+func ToAPIPullRequest(ctx context.Context, pr *models.PullRequest, doer *user_model.User) *api.PullRequest {
var (
baseBranch *git.Branch
headBranch *git.Branch
@@ -84,7 +85,7 @@ func ToAPIPullRequest(pr *models.PullRequest, doer *user_model.User) *api.PullRe
},
}
- gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
+ gitRepo, err := git.OpenRepositoryCtx(ctx, pr.BaseRepo.RepoPath())
if err != nil {
log.Error("OpenRepository[%s]: %v", pr.BaseRepo.RepoPath(), err)
return nil
@@ -110,7 +111,7 @@ func ToAPIPullRequest(pr *models.PullRequest, doer *user_model.User) *api.PullRe
}
if pr.Flow == models.PullRequestFlowAGit {
- gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
+ gitRepo, err := git.OpenRepositoryCtx(ctx, pr.BaseRepo.RepoPath())
if err != nil {
log.Error("OpenRepository[%s]: %v", pr.GetGitRefName(), err)
return nil
@@ -137,7 +138,7 @@ func ToAPIPullRequest(pr *models.PullRequest, doer *user_model.User) *api.PullRe
apiPullRequest.Head.RepoID = pr.HeadRepo.ID
apiPullRequest.Head.Repository = ToRepo(pr.HeadRepo, p.AccessMode)
- headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
+ headGitRepo, err := git.OpenRepositoryCtx(ctx, pr.HeadRepo.RepoPath())
if err != nil {
log.Error("OpenRepository[%s]: %v", pr.HeadRepo.RepoPath(), err)
return nil
@@ -173,7 +174,7 @@ func ToAPIPullRequest(pr *models.PullRequest, doer *user_model.User) *api.PullRe
}
if len(apiPullRequest.Head.Sha) == 0 && len(apiPullRequest.Head.Ref) != 0 {
- baseGitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
+ baseGitRepo, err := git.OpenRepositoryCtx(ctx, pr.BaseRepo.RepoPath())
if err != nil {
log.Error("OpenRepository[%s]: %v", pr.BaseRepo.RepoPath(), err)
return nil
diff --git a/modules/convert/pull_review.go b/modules/convert/pull_review.go
index b99d7bead6..962aae58bb 100644
--- a/modules/convert/pull_review.go
+++ b/modules/convert/pull_review.go
@@ -5,6 +5,7 @@
package convert
import (
+ "context"
"strings"
"code.gitea.io/gitea/models"
@@ -13,8 +14,8 @@ import (
)
// ToPullReview convert a review to api format
-func ToPullReview(r *models.Review, doer *user_model.User) (*api.PullReview, error) {
- if err := r.LoadAttributes(); err != nil {
+func ToPullReview(ctx context.Context, r *models.Review, doer *user_model.User) (*api.PullReview, error) {
+ if err := r.LoadAttributes(ctx); err != nil {
if !user_model.IsErrUserNotExist(err) {
return nil, err
}
@@ -54,14 +55,14 @@ func ToPullReview(r *models.Review, doer *user_model.User) (*api.PullReview, err
}
// ToPullReviewList convert a list of review to it's api format
-func ToPullReviewList(rl []*models.Review, doer *user_model.User) ([]*api.PullReview, error) {
+func ToPullReviewList(ctx context.Context, rl []*models.Review, doer *user_model.User) ([]*api.PullReview, error) {
result := make([]*api.PullReview, 0, len(rl))
for i := range rl {
// show pending reviews only for the user who created them
if rl[i].Type == models.ReviewTypePending && !(doer.IsAdmin || doer.ID == rl[i].ReviewerID) {
continue
}
- r, err := ToPullReview(rl[i], doer)
+ r, err := ToPullReview(ctx, rl[i], doer)
if err != nil {
return nil, err
}
@@ -71,8 +72,8 @@ func ToPullReviewList(rl []*models.Review, doer *user_model.User) ([]*api.PullRe
}
// ToPullReviewCommentList convert the CodeComments of an review to it's api format
-func ToPullReviewCommentList(review *models.Review, doer *user_model.User) ([]*api.PullReviewComment, error) {
- if err := review.LoadAttributes(); err != nil {
+func ToPullReviewCommentList(ctx context.Context, review *models.Review, doer *user_model.User) ([]*api.PullReviewComment, error) {
+ if err := review.LoadAttributes(ctx); err != nil {
if !user_model.IsErrUserNotExist(err) {
return nil, err
}
diff --git a/modules/convert/pull_test.go b/modules/convert/pull_test.go
index 11789b1200..5351b5e172 100644
--- a/modules/convert/pull_test.go
+++ b/modules/convert/pull_test.go
@@ -11,6 +11,7 @@ import (
"code.gitea.io/gitea/models/perm"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
+ "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/structs"
"github.com/stretchr/testify/assert"
@@ -23,7 +24,7 @@ func TestPullRequest_APIFormat(t *testing.T) {
pr := unittest.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 1}).(*models.PullRequest)
assert.NoError(t, pr.LoadAttributes())
assert.NoError(t, pr.LoadIssue())
- apiPullRequest := ToAPIPullRequest(pr, nil)
+ apiPullRequest := ToAPIPullRequest(git.DefaultContext, pr, nil)
assert.NotNil(t, apiPullRequest)
assert.EqualValues(t, &structs.PRBranchInfo{
Name: "branch1",
@@ -40,7 +41,7 @@ func TestPullRequest_APIFormat(t *testing.T) {
// simulate fork deletion
pr.HeadRepo = nil
pr.HeadRepoID = 100000
- apiPullRequest = ToAPIPullRequest(pr, nil)
+ apiPullRequest = ToAPIPullRequest(git.DefaultContext, pr, nil)
assert.NotNil(t, apiPullRequest)
assert.Nil(t, apiPullRequest.Head.Repository)
assert.EqualValues(t, -1, apiPullRequest.Head.RepoID)
diff --git a/modules/doctor/authorizedkeys.go b/modules/doctor/authorizedkeys.go
index 1a9b60e248..3eb931e6f6 100644
--- a/modules/doctor/authorizedkeys.go
+++ b/modules/doctor/authorizedkeys.go
@@ -7,6 +7,7 @@ package doctor
import (
"bufio"
"bytes"
+ "context"
"fmt"
"os"
"path/filepath"
@@ -19,7 +20,7 @@ import (
const tplCommentPrefix = `# gitea public key`
-func checkAuthorizedKeys(logger log.Logger, autofix bool) error {
+func checkAuthorizedKeys(ctx context.Context, logger log.Logger, autofix bool) error {
if setting.SSH.StartBuiltinServer || !setting.SSH.CreateAuthorizedKeysFile {
return nil
}
diff --git a/modules/doctor/checkOldArchives.go b/modules/doctor/checkOldArchives.go
index 0db8794080..6353eaddd2 100644
--- a/modules/doctor/checkOldArchives.go
+++ b/modules/doctor/checkOldArchives.go
@@ -5,6 +5,7 @@
package doctor
import (
+ "context"
"os"
"path/filepath"
@@ -13,7 +14,7 @@ import (
"code.gitea.io/gitea/modules/util"
)
-func checkOldArchives(logger log.Logger, autofix bool) error {
+func checkOldArchives(ctx context.Context, logger log.Logger, autofix bool) error {
numRepos := 0
numReposUpdated := 0
err := iterateRepositories(func(repo *repo_model.Repository) error {
diff --git a/modules/doctor/dbconsistency.go b/modules/doctor/dbconsistency.go
index cd34994e1a..953a05fcd1 100644
--- a/modules/doctor/dbconsistency.go
+++ b/modules/doctor/dbconsistency.go
@@ -22,7 +22,7 @@ type consistencyCheck struct {
FixedMessage string
}
-func (c *consistencyCheck) Run(logger log.Logger, autofix bool) error {
+func (c *consistencyCheck) Run(ctx context.Context, logger log.Logger, autofix bool) error {
count, err := c.Counter()
if err != nil {
logger.Critical("Error: %v whilst counting %s", err, c.Name)
@@ -73,9 +73,9 @@ func genericOrphanCheck(name, subject, refobject, joincond string) consistencyCh
}
}
-func checkDBConsistency(logger log.Logger, autofix bool) error {
+func checkDBConsistency(ctx context.Context, logger log.Logger, autofix bool) error {
// make sure DB version is uptodate
- if err := db.InitEngineWithMigration(context.Background(), migrations.EnsureUpToDate); err != nil {
+ if err := db.InitEngineWithMigration(ctx, migrations.EnsureUpToDate); err != nil {
logger.Critical("Model version on the database does not match the current Gitea version. Model consistency will not be checked until the database is upgraded")
return err
}
@@ -180,7 +180,7 @@ func checkDBConsistency(logger log.Logger, autofix bool) error {
)
for _, c := range consistencyChecks {
- if err := c.Run(logger, autofix); err != nil {
+ if err := c.Run(ctx, logger, autofix); err != nil {
return err
}
}
diff --git a/modules/doctor/dbversion.go b/modules/doctor/dbversion.go
index 0fc6762e47..8afd9f44fd 100644
--- a/modules/doctor/dbversion.go
+++ b/modules/doctor/dbversion.go
@@ -12,8 +12,8 @@ import (
"code.gitea.io/gitea/modules/log"
)
-func checkDBVersion(logger log.Logger, autofix bool) error {
- if err := db.InitEngineWithMigration(context.Background(), migrations.EnsureUpToDate); err != nil {
+func checkDBVersion(ctx context.Context, logger log.Logger, autofix bool) error {
+ if err := db.InitEngineWithMigration(ctx, migrations.EnsureUpToDate); err != nil {
if !autofix {
logger.Critical("Error: %v during ensure up to date", err)
return err
@@ -21,7 +21,7 @@ func checkDBVersion(logger log.Logger, autofix bool) error {
logger.Warn("Got Error: %v during ensure up to date", err)
logger.Warn("Attempting to migrate to the latest DB version to fix this.")
- err = db.InitEngineWithMigration(context.Background(), migrations.Migrate)
+ err = db.InitEngineWithMigration(ctx, migrations.Migrate)
if err != nil {
logger.Critical("Error: %v during migration", err)
}
diff --git a/modules/doctor/doctor.go b/modules/doctor/doctor.go
index 20a32f1865..c8975a788e 100644
--- a/modules/doctor/doctor.go
+++ b/modules/doctor/doctor.go
@@ -20,7 +20,7 @@ type Check struct {
Title string
Name string
IsDefault bool
- Run func(logger log.Logger, autofix bool) error
+ Run func(ctx context.Context, logger log.Logger, autofix bool) error
AbortIfFailed bool
SkipDatabaseInitialization bool
Priority int
@@ -77,7 +77,7 @@ func RunChecks(ctx context.Context, logger log.Logger, autofix bool, checks []*C
}
logger.Info("[%d] %s", log.NewColoredIDValue(i+1), check.Title)
logger.Flush()
- if err := check.Run(&wrappedLogger, autofix); err != nil {
+ if err := check.Run(ctx, &wrappedLogger, autofix); err != nil {
if check.AbortIfFailed {
logger.Critical("FAIL")
return err
diff --git a/modules/doctor/fix16961.go b/modules/doctor/fix16961.go
index 56d02ae92e..b929616b38 100644
--- a/modules/doctor/fix16961.go
+++ b/modules/doctor/fix16961.go
@@ -6,6 +6,7 @@ package doctor
import (
"bytes"
+ "context"
"fmt"
"code.gitea.io/gitea/models/db"
@@ -254,7 +255,7 @@ func fixBrokenRepoUnit16961(repoUnit *repo_model.RepoUnit, bs []byte) (fixed boo
return true, nil
}
-func fixBrokenRepoUnits16961(logger log.Logger, autofix bool) error {
+func fixBrokenRepoUnits16961(ctx context.Context, logger log.Logger, autofix bool) error {
// RepoUnit describes all units of a repository
type RepoUnit struct {
ID int64
diff --git a/modules/doctor/mergebase.go b/modules/doctor/mergebase.go
index ef78cc49d1..e2ebc41aa5 100644
--- a/modules/doctor/mergebase.go
+++ b/modules/doctor/mergebase.go
@@ -5,6 +5,7 @@
package doctor
import (
+ "context"
"fmt"
"strings"
@@ -28,7 +29,7 @@ func iteratePRs(repo *repo_model.Repository, each func(*repo_model.Repository, *
)
}
-func checkPRMergeBase(logger log.Logger, autofix bool) error {
+func checkPRMergeBase(ctx context.Context, logger log.Logger, autofix bool) error {
numRepos := 0
numPRs := 0
numPRsUpdated := 0
@@ -43,17 +44,17 @@ func checkPRMergeBase(logger log.Logger, autofix bool) error {
if !pr.HasMerged {
var err error
- pr.MergeBase, err = git.NewCommand("merge-base", "--", pr.BaseBranch, pr.GetGitRefName()).RunInDir(repoPath)
+ pr.MergeBase, err = git.NewCommandContext(ctx, "merge-base", "--", pr.BaseBranch, pr.GetGitRefName()).RunInDir(repoPath)
if err != nil {
var err2 error
- pr.MergeBase, err2 = git.NewCommand("rev-parse", git.BranchPrefix+pr.BaseBranch).RunInDir(repoPath)
+ pr.MergeBase, err2 = git.NewCommandContext(ctx, "rev-parse", git.BranchPrefix+pr.BaseBranch).RunInDir(repoPath)
if err2 != nil {
logger.Warn("Unable to get merge base for PR ID %d, #%d onto %s in %s/%s. Error: %v & %v", pr.ID, pr.Index, pr.BaseBranch, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, err, err2)
return nil
}
}
} else {
- parentsString, err := git.NewCommand("rev-list", "--parents", "-n", "1", pr.MergedCommitID).RunInDir(repoPath)
+ parentsString, err := git.NewCommandContext(ctx, "rev-list", "--parents", "-n", "1", pr.MergedCommitID).RunInDir(repoPath)
if err != nil {
logger.Warn("Unable to get parents for merged PR ID %d, #%d onto %s in %s/%s. Error: %v", pr.ID, pr.Index, pr.BaseBranch, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, err)
return nil
@@ -66,7 +67,7 @@ func checkPRMergeBase(logger log.Logger, autofix bool) error {
args := append([]string{"merge-base", "--"}, parents[1:]...)
args = append(args, pr.GetGitRefName())
- pr.MergeBase, err = git.NewCommand(args...).RunInDir(repoPath)
+ pr.MergeBase, err = git.NewCommandContext(ctx, args...).RunInDir(repoPath)
if err != nil {
logger.Warn("Unable to get merge base for merged PR ID %d, #%d onto %s in %s/%s. Error: %v", pr.ID, pr.Index, pr.BaseBranch, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, err)
return nil
diff --git a/modules/doctor/misc.go b/modules/doctor/misc.go
index a788e5f6a9..cb302c9c5b 100644
--- a/modules/doctor/misc.go
+++ b/modules/doctor/misc.go
@@ -5,6 +5,7 @@
package doctor
import (
+ "context"
"fmt"
"os"
"os/exec"
@@ -38,7 +39,7 @@ func iterateRepositories(each func(*repo_model.Repository) error) error {
return err
}
-func checkScriptType(logger log.Logger, autofix bool) error {
+func checkScriptType(ctx context.Context, logger log.Logger, autofix bool) error {
path, err := exec.LookPath(setting.ScriptType)
if err != nil {
logger.Critical("ScriptType \"%q\" is not on the current PATH. Error: %v", setting.ScriptType, err)
@@ -48,7 +49,7 @@ func checkScriptType(logger log.Logger, autofix bool) error {
return nil
}
-func checkHooks(logger log.Logger, autofix bool) error {
+func checkHooks(ctx context.Context, logger log.Logger, autofix bool) error {
if err := iterateRepositories(func(repo *repo_model.Repository) error {
results, err := repository.CheckDelegateHooks(repo.RepoPath())
if err != nil {
@@ -73,7 +74,7 @@ func checkHooks(logger log.Logger, autofix bool) error {
return nil
}
-func checkUserStarNum(logger log.Logger, autofix bool) error {
+func checkUserStarNum(ctx context.Context, logger log.Logger, autofix bool) error {
if err := models.DoctorUserStarNum(); err != nil {
logger.Critical("Unable update User Stars numbers")
return err
@@ -81,24 +82,24 @@ func checkUserStarNum(logger log.Logger, autofix bool) error {
return nil
}
-func checkEnablePushOptions(logger log.Logger, autofix bool) error {
+func checkEnablePushOptions(ctx context.Context, logger log.Logger, autofix bool) error {
numRepos := 0
numNeedUpdate := 0
if err := iterateRepositories(func(repo *repo_model.Repository) error {
numRepos++
- r, err := git.OpenRepository(repo.RepoPath())
+ r, err := git.OpenRepositoryCtx(git.DefaultContext, repo.RepoPath())
if err != nil {
return err
}
defer r.Close()
if autofix {
- _, err := git.NewCommand("config", "receive.advertisePushOptions", "true").RunInDir(r.Path)
+ _, err := git.NewCommandContext(ctx, "config", "receive.advertisePushOptions", "true").RunInDir(r.Path)
return err
}
- value, err := git.NewCommand("config", "receive.advertisePushOptions").RunInDir(r.Path)
+ value, err := git.NewCommandContext(ctx, "config", "receive.advertisePushOptions").RunInDir(r.Path)
if err != nil {
return err
}
@@ -124,7 +125,7 @@ func checkEnablePushOptions(logger log.Logger, autofix bool) error {
return nil
}
-func checkDaemonExport(logger log.Logger, autofix bool) error {
+func checkDaemonExport(ctx context.Context, logger log.Logger, autofix bool) error {
numRepos := 0
numNeedUpdate := 0
cache, err := lru.New(512)
diff --git a/modules/doctor/paths.go b/modules/doctor/paths.go
index b4eab631ba..623df863cb 100644
--- a/modules/doctor/paths.go
+++ b/modules/doctor/paths.go
@@ -5,6 +5,7 @@
package doctor
import (
+ "context"
"fmt"
"os"
@@ -58,7 +59,7 @@ func checkConfigurationFile(logger log.Logger, autofix bool, fileOpts configurat
return nil
}
-func checkConfigurationFiles(logger log.Logger, autofix bool) error {
+func checkConfigurationFiles(ctx context.Context, logger log.Logger, autofix bool) error {
if fi, err := os.Stat(setting.CustomConf); err != nil || !fi.Mode().IsRegular() {
logger.Error("Failed to find configuration file at '%s'.", setting.CustomConf)
logger.Error("If you've never ran Gitea yet, this is normal and '%s' will be created for you on first run.", setting.CustomConf)
diff --git a/modules/doctor/storage.go b/modules/doctor/storage.go
index d2b7f5ee13..dafd989cf0 100644
--- a/modules/doctor/storage.go
+++ b/modules/doctor/storage.go
@@ -5,6 +5,8 @@
package doctor
import (
+ "context"
+
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/storage"
@@ -55,7 +57,7 @@ func checkAttachmentStorageFiles(logger log.Logger, autofix bool) error {
return nil
}
-func checkStorageFiles(logger log.Logger, autofix bool) error {
+func checkStorageFiles(ctx context.Context, logger log.Logger, autofix bool) error {
if err := storage.Init(); err != nil {
logger.Error("storage.Init failed: %v", err)
return err
diff --git a/modules/doctor/usertype.go b/modules/doctor/usertype.go
index 26c0d34cda..34e12afe72 100644
--- a/modules/doctor/usertype.go
+++ b/modules/doctor/usertype.go
@@ -5,11 +5,13 @@
package doctor
import (
+ "context"
+
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
)
-func checkUserType(logger log.Logger, autofix bool) error {
+func checkUserType(ctx context.Context, logger log.Logger, autofix bool) error {
count, err := models.CountWrongUserType()
if err != nil {
logger.Critical("Error: %v whilst counting wrong user types")
diff --git a/modules/git/blame.go b/modules/git/blame.go
index b30124594d..40e3d4e885 100644
--- a/modules/git/blame.go
+++ b/modules/git/blame.go
@@ -114,12 +114,6 @@ func (r *BlameReader) Close() error {
// CreateBlameReader creates reader for given repository, commit and file
func CreateBlameReader(ctx context.Context, repoPath, commitID, file string) (*BlameReader, error) {
- gitRepo, err := OpenRepositoryCtx(ctx, repoPath)
- if err != nil {
- return nil, err
- }
- gitRepo.Close()
-
return createBlameReader(ctx, repoPath, GitExecutable, "blame", commitID, "--porcelain", "--", file)
}
diff --git a/modules/git/commit.go b/modules/git/commit.go
index 3e3641b096..8a78653553 100644
--- a/modules/git/commit.go
+++ b/modules/git/commit.go
@@ -8,6 +8,7 @@ package git
import (
"bufio"
"bytes"
+ "context"
"errors"
"fmt"
"io"
@@ -138,12 +139,12 @@ func CommitChangesWithArgs(repoPath string, args []string, opts CommitChangesOpt
}
// AllCommitsCount returns count of all commits in repository
-func AllCommitsCount(repoPath string, hidePRRefs bool, files ...string) (int64, error) {
+func AllCommitsCount(ctx context.Context, repoPath string, hidePRRefs bool, files ...string) (int64, error) {
args := []string{"--all", "--count"}
if hidePRRefs {
args = append([]string{"--exclude=" + PullPrefix + "*"}, args...)
}
- cmd := NewCommand("rev-list")
+ cmd := NewCommandContext(ctx, "rev-list")
cmd.AddArguments(args...)
if len(files) > 0 {
cmd.AddArguments("--")
@@ -159,8 +160,8 @@ func AllCommitsCount(repoPath string, hidePRRefs bool, files ...string) (int64,
}
// CommitsCountFiles returns number of total commits of until given revision.
-func CommitsCountFiles(repoPath string, revision, relpath []string) (int64, error) {
- cmd := NewCommand("rev-list", "--count")
+func CommitsCountFiles(ctx context.Context, repoPath string, revision, relpath []string) (int64, error) {
+ cmd := NewCommandContext(ctx, "rev-list", "--count")
cmd.AddArguments(revision...)
if len(relpath) > 0 {
cmd.AddArguments("--")
@@ -176,13 +177,13 @@ func CommitsCountFiles(repoPath string, revision, relpath []string) (int64, erro
}
// CommitsCount returns number of total commits of until given revision.
-func CommitsCount(repoPath string, revision ...string) (int64, error) {
- return CommitsCountFiles(repoPath, revision, []string{})
+func CommitsCount(ctx context.Context, repoPath string, revision ...string) (int64, error) {
+ return CommitsCountFiles(ctx, repoPath, revision, []string{})
}
// CommitsCount returns number of total commits of until current revision.
func (c *Commit) CommitsCount() (int64, error) {
- return CommitsCount(c.repo.Path, c.ID.String())
+ return CommitsCount(c.repo.Ctx, c.repo.Path, c.ID.String())
}
// CommitsByRange returns the specific page commits before current revision, every page's number default by CommitsRangeSize
@@ -205,7 +206,7 @@ func (c *Commit) HasPreviousCommit(commitHash SHA1) (bool, error) {
}
if err := CheckGitVersionAtLeast("1.8"); err == nil {
- _, err := NewCommand("merge-base", "--is-ancestor", that, this).RunInDir(c.repo.Path)
+ _, err := NewCommandContext(c.repo.Ctx, "merge-base", "--is-ancestor", that, this).RunInDir(c.repo.Path)
if err == nil {
return true, nil
}
@@ -218,7 +219,7 @@ func (c *Commit) HasPreviousCommit(commitHash SHA1) (bool, error) {
return false, err
}
- result, err := NewCommand("rev-list", "--ancestry-path", "-n1", that+".."+this, "--").RunInDir(c.repo.Path)
+ result, err := NewCommandContext(c.repo.Ctx, "rev-list", "--ancestry-path", "-n1", that+".."+this, "--").RunInDir(c.repo.Path)
if err != nil {
return false, err
}
@@ -380,7 +381,7 @@ func (c *Commit) GetBranchName() (string, error) {
}
args = append(args, "--name-only", "--no-undefined", c.ID.String())
- data, err := NewCommand(args...).RunInDir(c.repo.Path)
+ data, err := NewCommandContext(c.repo.Ctx, args...).RunInDir(c.repo.Path)
if err != nil {
// handle special case where git can not describe commit
if strings.Contains(err.Error(), "cannot describe") {
@@ -406,7 +407,7 @@ func (c *Commit) LoadBranchName() (err error) {
// GetTagName gets the current tag name for given commit
func (c *Commit) GetTagName() (string, error) {
- data, err := NewCommand("describe", "--exact-match", "--tags", "--always", c.ID.String()).RunInDir(c.repo.Path)
+ data, err := NewCommandContext(c.repo.Ctx, "describe", "--exact-match", "--tags", "--always", c.ID.String()).RunInDir(c.repo.Path)
if err != nil {
// handle special case where there is no tag for this commit
if strings.Contains(err.Error(), "no tag exactly matches") {
@@ -473,7 +474,7 @@ func parseCommitFileStatus(fileStatus *CommitFileStatus, stdout io.Reader) {
}
// GetCommitFileStatus returns file status of commit in given repository.
-func GetCommitFileStatus(repoPath, commitID string) (*CommitFileStatus, error) {
+func GetCommitFileStatus(ctx context.Context, repoPath, commitID string) (*CommitFileStatus, error) {
stdout, w := io.Pipe()
done := make(chan struct{})
fileStatus := NewCommitFileStatus()
@@ -485,7 +486,7 @@ func GetCommitFileStatus(repoPath, commitID string) (*CommitFileStatus, error) {
stderr := new(bytes.Buffer)
args := []string{"log", "--name-status", "-c", "--pretty=format:", "--parents", "--no-renames", "-z", "-1", commitID}
- err := NewCommand(args...).RunInDirPipeline(repoPath, w, stderr)
+ err := NewCommandContext(ctx, args...).RunInDirPipeline(repoPath, w, stderr)
w.Close() // Close writer to exit parsing goroutine
if err != nil {
return nil, ConcatenateError(err, stderr.String())
@@ -496,8 +497,8 @@ func GetCommitFileStatus(repoPath, commitID string) (*CommitFileStatus, error) {
}
// GetFullCommitID returns full length (40) of commit ID by given short SHA in a repository.
-func GetFullCommitID(repoPath, shortID string) (string, error) {
- commitID, err := NewCommand("rev-parse", shortID).RunInDir(repoPath)
+func GetFullCommitID(ctx context.Context, repoPath, shortID string) (string, error) {
+ commitID, err := NewCommandContext(ctx, "rev-parse", shortID).RunInDir(repoPath)
if err != nil {
if strings.Contains(err.Error(), "exit status 128") {
return "", ErrNotExist{shortID, ""}
diff --git a/modules/git/commit_info_test.go b/modules/git/commit_info_test.go
index 8467bdaa90..a4c0483004 100644
--- a/modules/git/commit_info_test.go
+++ b/modules/git/commit_info_test.go
@@ -24,7 +24,7 @@ func cloneRepo(url, dir, name string) (string, error) {
if _, err := os.Stat(repoDir); err == nil {
return repoDir, nil
}
- return repoDir, Clone(url, repoDir, CloneRepoOptions{
+ return repoDir, Clone(DefaultContext, url, repoDir, CloneRepoOptions{
Mirror: false,
Bare: false,
Quiet: true,
diff --git a/modules/git/commit_test.go b/modules/git/commit_test.go
index 57132c00dc..2b3365342d 100644
--- a/modules/git/commit_test.go
+++ b/modules/git/commit_test.go
@@ -15,7 +15,7 @@ import (
func TestCommitsCount(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
- commitsCount, err := CommitsCount(bareRepo1Path, "8006ff9adbf0cb94da7dad9e537e53817f9fa5c0")
+ commitsCount, err := CommitsCount(DefaultContext, bareRepo1Path, "8006ff9adbf0cb94da7dad9e537e53817f9fa5c0")
assert.NoError(t, err)
assert.Equal(t, int64(3), commitsCount)
}
@@ -23,7 +23,7 @@ func TestCommitsCount(t *testing.T) {
func TestGetFullCommitID(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
- id, err := GetFullCommitID(bareRepo1Path, "8006ff9a")
+ id, err := GetFullCommitID(DefaultContext, bareRepo1Path, "8006ff9a")
assert.NoError(t, err)
assert.Equal(t, "8006ff9adbf0cb94da7dad9e537e53817f9fa5c0", id)
}
@@ -31,7 +31,7 @@ func TestGetFullCommitID(t *testing.T) {
func TestGetFullCommitIDError(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
- id, err := GetFullCommitID(bareRepo1Path, "unknown")
+ id, err := GetFullCommitID(DefaultContext, bareRepo1Path, "unknown")
assert.Empty(t, id)
if assert.Error(t, err) {
assert.EqualError(t, err, "object does not exist [id: unknown, rel_path: ]")
diff --git a/modules/git/diff.go b/modules/git/diff.go
index f90f911be0..02ed2c60c4 100644
--- a/modules/git/diff.go
+++ b/modules/git/diff.go
@@ -30,17 +30,17 @@ const (
)
// GetRawDiff dumps diff results of repository in given commit ID to io.Writer.
-func GetRawDiff(repoPath, commitID string, diffType RawDiffType, writer io.Writer) error {
- return GetRawDiffForFile(repoPath, "", commitID, diffType, "", writer)
+func GetRawDiff(ctx context.Context, repoPath, commitID string, diffType RawDiffType, writer io.Writer) error {
+ return GetRawDiffForFile(ctx, repoPath, "", commitID, diffType, "", writer)
}
// GetRawDiffForFile dumps diff results of file in given commit ID to io.Writer.
-func GetRawDiffForFile(repoPath, startCommit, endCommit string, diffType RawDiffType, file string, writer io.Writer) error {
- repo, err := OpenRepository(repoPath)
+func GetRawDiffForFile(ctx context.Context, repoPath, startCommit, endCommit string, diffType RawDiffType, file string, writer io.Writer) error {
+ repo, closer, err := RepositoryFromContextOrOpen(ctx, repoPath)
if err != nil {
return fmt.Errorf("OpenRepository: %v", err)
}
- defer repo.Close()
+ defer closer.Close()
return GetRepoRawDiffForFile(repo, startCommit, endCommit, diffType, file, writer)
}
@@ -276,7 +276,7 @@ func CutDiffAroundLine(originalDiff io.Reader, line int64, old bool, numbersOfLi
}
// GetAffectedFiles returns the affected files between two commits
-func GetAffectedFiles(oldCommitID, newCommitID string, env []string, repo *Repository) ([]string, error) {
+func GetAffectedFiles(repo *Repository, oldCommitID, newCommitID string, env []string) ([]string, error) {
stdoutReader, stdoutWriter, err := os.Pipe()
if err != nil {
log.Error("Unable to create os.Pipe for %s", repo.Path)
@@ -290,7 +290,7 @@ func GetAffectedFiles(oldCommitID, newCommitID string, env []string, repo *Repos
affectedFiles := make([]string, 0, 32)
// Run `git diff --name-only` to get the names of the changed files
- err = NewCommand("diff", "--name-only", oldCommitID, newCommitID).
+ err = NewCommandContext(repo.Ctx, "diff", "--name-only", oldCommitID, newCommitID).
RunInDirTimeoutEnvFullPipelineFunc(env, -1, repo.Path,
stdoutWriter, nil, nil,
func(ctx context.Context, cancel context.CancelFunc) error {
diff --git a/modules/git/pipeline/catfile.go b/modules/git/pipeline/catfile.go
index 7293cf9d7f..0ce5b5e06e 100644
--- a/modules/git/pipeline/catfile.go
+++ b/modules/git/pipeline/catfile.go
@@ -7,6 +7,7 @@ package pipeline
import (
"bufio"
"bytes"
+ "context"
"fmt"
"io"
"strconv"
@@ -18,27 +19,27 @@ import (
)
// CatFileBatchCheck runs cat-file with --batch-check
-func CatFileBatchCheck(shasToCheckReader *io.PipeReader, catFileCheckWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath string) {
+func CatFileBatchCheck(ctx context.Context, shasToCheckReader *io.PipeReader, catFileCheckWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath string) {
defer wg.Done()
defer shasToCheckReader.Close()
defer catFileCheckWriter.Close()
stderr := new(bytes.Buffer)
var errbuf strings.Builder
- cmd := git.NewCommand("cat-file", "--batch-check")
+ cmd := git.NewCommandContext(ctx, "cat-file", "--batch-check")
if err := cmd.RunInDirFullPipeline(tmpBasePath, catFileCheckWriter, stderr, shasToCheckReader); err != nil {
_ = catFileCheckWriter.CloseWithError(fmt.Errorf("git cat-file --batch-check [%s]: %v - %s", tmpBasePath, err, errbuf.String()))
}
}
// CatFileBatchCheckAllObjects runs cat-file with --batch-check --batch-all
-func CatFileBatchCheckAllObjects(catFileCheckWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath string, errChan chan<- error) {
+func CatFileBatchCheckAllObjects(ctx context.Context, catFileCheckWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath string, errChan chan<- error) {
defer wg.Done()
defer catFileCheckWriter.Close()
stderr := new(bytes.Buffer)
var errbuf strings.Builder
- cmd := git.NewCommand("cat-file", "--batch-check", "--batch-all-objects")
+ cmd := git.NewCommandContext(ctx, "cat-file", "--batch-check", "--batch-all-objects")
if err := cmd.RunInDirPipeline(tmpBasePath, catFileCheckWriter, stderr); err != nil {
log.Error("git cat-file --batch-check --batch-all-object [%s]: %v - %s", tmpBasePath, err, errbuf.String())
err = fmt.Errorf("git cat-file --batch-check --batch-all-object [%s]: %v - %s", tmpBasePath, err, errbuf.String())
@@ -48,14 +49,14 @@ func CatFileBatchCheckAllObjects(catFileCheckWriter *io.PipeWriter, wg *sync.Wai
}
// CatFileBatch runs cat-file --batch
-func CatFileBatch(shasToBatchReader *io.PipeReader, catFileBatchWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath string) {
+func CatFileBatch(ctx context.Context, shasToBatchReader *io.PipeReader, catFileBatchWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath string) {
defer wg.Done()
defer shasToBatchReader.Close()
defer catFileBatchWriter.Close()
stderr := new(bytes.Buffer)
var errbuf strings.Builder
- if err := git.NewCommand("cat-file", "--batch").RunInDirFullPipeline(tmpBasePath, catFileBatchWriter, stderr, shasToBatchReader); err != nil {
+ if err := git.NewCommandContext(ctx, "cat-file", "--batch").RunInDirFullPipeline(tmpBasePath, catFileBatchWriter, stderr, shasToBatchReader); err != nil {
_ = shasToBatchReader.CloseWithError(fmt.Errorf("git rev-list [%s]: %v - %s", tmpBasePath, err, errbuf.String()))
}
}
diff --git a/modules/git/pipeline/lfs.go b/modules/git/pipeline/lfs.go
index 4551ccd3f4..1b64b672e4 100644
--- a/modules/git/pipeline/lfs.go
+++ b/modules/git/pipeline/lfs.go
@@ -120,7 +120,7 @@ func FindLFSFile(repo *git.Repository, hash git.SHA1) ([]*LFSResult, error) {
i++
}
}()
- go NameRevStdin(shasToNameReader, nameRevStdinWriter, &wg, basePath)
+ go NameRevStdin(repo.Ctx, shasToNameReader, nameRevStdinWriter, &wg, basePath)
go func() {
defer wg.Done()
defer shasToNameWriter.Close()
diff --git a/modules/git/pipeline/lfs_nogogit.go b/modules/git/pipeline/lfs_nogogit.go
index 1352aa7662..3b993606ac 100644
--- a/modules/git/pipeline/lfs_nogogit.go
+++ b/modules/git/pipeline/lfs_nogogit.go
@@ -53,7 +53,7 @@ func FindLFSFile(repo *git.Repository, hash git.SHA1) ([]*LFSResult, error) {
go func() {
stderr := strings.Builder{}
- err := git.NewCommand("rev-list", "--all").RunInDirPipeline(repo.Path, revListWriter, &stderr)
+ err := git.NewCommandContext(repo.Ctx, "rev-list", "--all").RunInDirPipeline(repo.Path, revListWriter, &stderr)
if err != nil {
_ = revListWriter.CloseWithError(git.ConcatenateError(err, (&stderr).String()))
} else {
@@ -212,7 +212,7 @@ func FindLFSFile(repo *git.Repository, hash git.SHA1) ([]*LFSResult, error) {
i++
}
}()
- go NameRevStdin(shasToNameReader, nameRevStdinWriter, &wg, basePath)
+ go NameRevStdin(repo.Ctx, shasToNameReader, nameRevStdinWriter, &wg, basePath)
go func() {
defer wg.Done()
defer shasToNameWriter.Close()
diff --git a/modules/git/pipeline/namerev.go b/modules/git/pipeline/namerev.go
index eebb53b0ca..43f93f8f7e 100644
--- a/modules/git/pipeline/namerev.go
+++ b/modules/git/pipeline/namerev.go
@@ -6,6 +6,7 @@ package pipeline
import (
"bytes"
+ "context"
"fmt"
"io"
"strings"
@@ -15,14 +16,14 @@ import (
)
// NameRevStdin runs name-rev --stdin
-func NameRevStdin(shasToNameReader *io.PipeReader, nameRevStdinWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath string) {
+func NameRevStdin(ctx context.Context, shasToNameReader *io.PipeReader, nameRevStdinWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath string) {
defer wg.Done()
defer shasToNameReader.Close()
defer nameRevStdinWriter.Close()
stderr := new(bytes.Buffer)
var errbuf strings.Builder
- if err := git.NewCommand("name-rev", "--stdin", "--name-only", "--always").RunInDirFullPipeline(tmpBasePath, nameRevStdinWriter, stderr, shasToNameReader); err != nil {
+ if err := git.NewCommandContext(ctx, "name-rev", "--stdin", "--name-only", "--always").RunInDirFullPipeline(tmpBasePath, nameRevStdinWriter, stderr, shasToNameReader); err != nil {
_ = shasToNameReader.CloseWithError(fmt.Errorf("git name-rev [%s]: %v - %s", tmpBasePath, err, errbuf.String()))
}
}
diff --git a/modules/git/pipeline/revlist.go b/modules/git/pipeline/revlist.go
index 4e13e19444..373ceea1fb 100644
--- a/modules/git/pipeline/revlist.go
+++ b/modules/git/pipeline/revlist.go
@@ -7,6 +7,7 @@ package pipeline
import (
"bufio"
"bytes"
+ "context"
"fmt"
"io"
"strings"
@@ -17,13 +18,13 @@ import (
)
// RevListAllObjects runs rev-list --objects --all and writes to a pipewriter
-func RevListAllObjects(revListWriter *io.PipeWriter, wg *sync.WaitGroup, basePath string, errChan chan<- error) {
+func RevListAllObjects(ctx context.Context, revListWriter *io.PipeWriter, wg *sync.WaitGroup, basePath string, errChan chan<- error) {
defer wg.Done()
defer revListWriter.Close()
stderr := new(bytes.Buffer)
var errbuf strings.Builder
- cmd := git.NewCommand("rev-list", "--objects", "--all")
+ cmd := git.NewCommandContext(ctx, "rev-list", "--objects", "--all")
if err := cmd.RunInDirPipeline(basePath, revListWriter, stderr); err != nil {
log.Error("git rev-list --objects --all [%s]: %v - %s", basePath, err, errbuf.String())
err = fmt.Errorf("git rev-list --objects --all [%s]: %v - %s", basePath, err, errbuf.String())
@@ -33,12 +34,12 @@ func RevListAllObjects(revListWriter *io.PipeWriter, wg *sync.WaitGroup, basePat
}
// RevListObjects run rev-list --objects from headSHA to baseSHA
-func RevListObjects(revListWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath, headSHA, baseSHA string, errChan chan<- error) {
+func RevListObjects(ctx context.Context, revListWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath, headSHA, baseSHA string, errChan chan<- error) {
defer wg.Done()
defer revListWriter.Close()
stderr := new(bytes.Buffer)
var errbuf strings.Builder
- cmd := git.NewCommand("rev-list", "--objects", headSHA, "--not", baseSHA)
+ cmd := git.NewCommandContext(ctx, "rev-list", "--objects", headSHA, "--not", baseSHA)
if err := cmd.RunInDirPipeline(tmpBasePath, revListWriter, stderr); err != nil {
log.Error("git rev-list [%s]: %v - %s", tmpBasePath, err, errbuf.String())
errChan <- fmt.Errorf("git rev-list [%s]: %v - %s", tmpBasePath, err, errbuf.String())
diff --git a/modules/git/repo.go b/modules/git/repo.go
index b19e80cd44..591ef7362b 100644
--- a/modules/git/repo.go
+++ b/modules/git/repo.go
@@ -34,7 +34,7 @@ const prettyLogFormat = `--pretty=format:%H`
// GetAllCommitsCount returns count of all commits in repository
func (repo *Repository) GetAllCommitsCount() (int64, error) {
- return AllCommitsCount(repo.Path, false)
+ return AllCommitsCount(repo.Ctx, repo.Path, false)
}
func (repo *Repository) parsePrettyFormatLogToList(logs []byte) ([]*Commit, error) {
@@ -57,19 +57,19 @@ func (repo *Repository) parsePrettyFormatLogToList(logs []byte) ([]*Commit, erro
}
// IsRepoURLAccessible checks if given repository URL is accessible.
-func IsRepoURLAccessible(url string) bool {
- _, err := NewCommand("ls-remote", "-q", "-h", url, "HEAD").Run()
+func IsRepoURLAccessible(ctx context.Context, url string) bool {
+ _, err := NewCommandContext(ctx, "ls-remote", "-q", "-h", url, "HEAD").Run()
return err == nil
}
// InitRepository initializes a new Git repository.
-func InitRepository(repoPath string, bare bool) error {
+func InitRepository(ctx context.Context, repoPath string, bare bool) error {
err := os.MkdirAll(repoPath, os.ModePerm)
if err != nil {
return err
}
- cmd := NewCommand("init")
+ cmd := NewCommandContext(ctx, "init")
if bare {
cmd.AddArguments("--bare")
}
@@ -80,7 +80,7 @@ func InitRepository(repoPath string, bare bool) error {
// IsEmpty Check if repository is empty.
func (repo *Repository) IsEmpty() (bool, error) {
var errbuf strings.Builder
- if err := NewCommand("log", "-1").RunInDirPipeline(repo.Path, nil, &errbuf); err != nil {
+ if err := NewCommandContext(repo.Ctx, "log", "-1").RunInDirPipeline(repo.Path, nil, &errbuf); err != nil {
if strings.Contains(errbuf.String(), "fatal: bad default revision 'HEAD'") ||
strings.Contains(errbuf.String(), "fatal: your current branch 'master' does not have any commits yet") {
return true, nil
@@ -104,12 +104,7 @@ type CloneRepoOptions struct {
}
// Clone clones original repository to target path.
-func Clone(from, to string, opts CloneRepoOptions) error {
- return CloneWithContext(DefaultContext, from, to, opts)
-}
-
-// CloneWithContext clones original repository to target path.
-func CloneWithContext(ctx context.Context, from, to string, opts CloneRepoOptions) error {
+func Clone(ctx context.Context, from, to string, opts CloneRepoOptions) error {
cargs := make([]string, len(GlobalCommandArgs))
copy(cargs, GlobalCommandArgs)
return CloneWithArgs(ctx, from, to, cargs, opts)
@@ -171,35 +166,6 @@ func CloneWithArgs(ctx context.Context, from, to string, args []string, opts Clo
return nil
}
-// PullRemoteOptions options when pull from remote
-type PullRemoteOptions struct {
- Timeout time.Duration
- All bool
- Rebase bool
- Remote string
- Branch string
-}
-
-// Pull pulls changes from remotes.
-func Pull(repoPath string, opts PullRemoteOptions) error {
- cmd := NewCommand("pull")
- if opts.Rebase {
- cmd.AddArguments("--rebase")
- }
- if opts.All {
- cmd.AddArguments("--all")
- } else {
- cmd.AddArguments("--", opts.Remote, opts.Branch)
- }
-
- if opts.Timeout <= 0 {
- opts.Timeout = -1
- }
-
- _, err := cmd.RunInDirTimeout(opts.Timeout, repoPath)
- return err
-}
-
// PushOptions options when push to remote
type PushOptions struct {
Remote string
@@ -262,116 +228,9 @@ func Push(ctx context.Context, repoPath string, opts PushOptions) error {
return err
}
-// CheckoutOptions options when heck out some branch
-type CheckoutOptions struct {
- Timeout time.Duration
- Branch string
- OldBranch string
-}
-
-// Checkout checkouts a branch
-func Checkout(repoPath string, opts CheckoutOptions) error {
- cmd := NewCommand("checkout")
- if len(opts.OldBranch) > 0 {
- cmd.AddArguments("-b")
- }
-
- if opts.Timeout <= 0 {
- opts.Timeout = -1
- }
-
- cmd.AddArguments(opts.Branch)
-
- if len(opts.OldBranch) > 0 {
- cmd.AddArguments(opts.OldBranch)
- }
-
- _, err := cmd.RunInDirTimeout(opts.Timeout, repoPath)
- return err
-}
-
-// ResetHEAD resets HEAD to given revision or head of branch.
-func ResetHEAD(repoPath string, hard bool, revision string) error {
- cmd := NewCommand("reset")
- if hard {
- cmd.AddArguments("--hard")
- }
- _, err := cmd.AddArguments(revision).RunInDir(repoPath)
- return err
-}
-
-// MoveFile moves a file to another file or directory.
-func MoveFile(repoPath, oldTreeName, newTreeName string) error {
- _, err := NewCommand("mv").AddArguments(oldTreeName, newTreeName).RunInDir(repoPath)
- return err
-}
-
-// CountObject represents repository count objects report
-type CountObject struct {
- Count int64
- Size int64
- InPack int64
- Packs int64
- SizePack int64
- PrunePack int64
- Garbage int64
- SizeGarbage int64
-}
-
-const (
- statCount = "count: "
- statSize = "size: "
- statInpack = "in-pack: "
- statPacks = "packs: "
- statSizePack = "size-pack: "
- statPrunePackage = "prune-package: "
- statGarbage = "garbage: "
- statSizeGarbage = "size-garbage: "
-)
-
-// CountObjects returns the results of git count-objects on the repoPath
-func CountObjects(repoPath string) (*CountObject, error) {
- cmd := NewCommand("count-objects", "-v")
- stdout, err := cmd.RunInDir(repoPath)
- if err != nil {
- return nil, err
- }
-
- return parseSize(stdout), nil
-}
-
-// parseSize parses the output from count-objects and return a CountObject
-func parseSize(objects string) *CountObject {
- repoSize := new(CountObject)
- for _, line := range strings.Split(objects, "\n") {
- switch {
- case strings.HasPrefix(line, statCount):
- repoSize.Count, _ = strconv.ParseInt(line[7:], 10, 64)
- case strings.HasPrefix(line, statSize):
- repoSize.Size, _ = strconv.ParseInt(line[6:], 10, 64)
- repoSize.Size *= 1024
- case strings.HasPrefix(line, statInpack):
- repoSize.InPack, _ = strconv.ParseInt(line[9:], 10, 64)
- case strings.HasPrefix(line, statPacks):
- repoSize.Packs, _ = strconv.ParseInt(line[7:], 10, 64)
- case strings.HasPrefix(line, statSizePack):
- repoSize.Count, _ = strconv.ParseInt(line[11:], 10, 64)
- repoSize.Count *= 1024
- case strings.HasPrefix(line, statPrunePackage):
- repoSize.PrunePack, _ = strconv.ParseInt(line[16:], 10, 64)
- case strings.HasPrefix(line, statGarbage):
- repoSize.Garbage, _ = strconv.ParseInt(line[9:], 10, 64)
- case strings.HasPrefix(line, statSizeGarbage):
- repoSize.SizeGarbage, _ = strconv.ParseInt(line[14:], 10, 64)
- repoSize.SizeGarbage *= 1024
- }
- }
- return repoSize
-}
-
// GetLatestCommitTime returns time for latest commit in repository (across all branches)
-func GetLatestCommitTime(repoPath string) (time.Time, error) {
- cmd := NewCommand("for-each-ref", "--sort=-committerdate", BranchPrefix, "--count", "1", "--format=%(committerdate)")
+func GetLatestCommitTime(ctx context.Context, repoPath string) (time.Time, error) {
+ cmd := NewCommandContext(ctx, "for-each-ref", "--sort=-committerdate", BranchPrefix, "--count", "1", "--format=%(committerdate)")
stdout, err := cmd.RunInDir(repoPath)
if err != nil {
return time.Time{}, err
@@ -386,9 +245,9 @@ type DivergeObject struct {
Behind int
}
-func checkDivergence(repoPath, baseBranch, targetBranch string) (int, error) {
+func checkDivergence(ctx context.Context, repoPath, baseBranch, targetBranch string) (int, error) {
branches := fmt.Sprintf("%s..%s", baseBranch, targetBranch)
- cmd := NewCommand("rev-list", "--count", branches)
+ cmd := NewCommandContext(ctx, "rev-list", "--count", branches)
stdout, err := cmd.RunInDir(repoPath)
if err != nil {
return -1, err
@@ -401,15 +260,15 @@ func checkDivergence(repoPath, baseBranch, targetBranch string) (int, error) {
}
// GetDivergingCommits returns the number of commits a targetBranch is ahead or behind a baseBranch
-func GetDivergingCommits(repoPath, baseBranch, targetBranch string) (DivergeObject, error) {
+func GetDivergingCommits(ctx context.Context, repoPath, baseBranch, targetBranch string) (DivergeObject, error) {
// $(git rev-list --count master..feature) commits ahead of master
- ahead, errorAhead := checkDivergence(repoPath, baseBranch, targetBranch)
+ ahead, errorAhead := checkDivergence(ctx, repoPath, baseBranch, targetBranch)
if errorAhead != nil {
return DivergeObject{}, errorAhead
}
// $(git rev-list --count feature..master) commits behind master
- behind, errorBehind := checkDivergence(repoPath, targetBranch, baseBranch)
+ behind, errorBehind := checkDivergence(ctx, repoPath, targetBranch, baseBranch)
if errorBehind != nil {
return DivergeObject{}, errorBehind
}
diff --git a/modules/git/repo_base.go b/modules/git/repo_base.go
new file mode 100644
index 0000000000..8c2f19f6d7
--- /dev/null
+++ b/modules/git/repo_base.go
@@ -0,0 +1,49 @@
+// Copyright 2021 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 git
+
+import (
+ "context"
+ "io"
+)
+
+// contextKey is a value for use with context.WithValue.
+type contextKey struct {
+ name string
+}
+
+// RepositoryContextKey is a context key. It is used with context.Value() to get the current Repository for the context
+var RepositoryContextKey = &contextKey{"repository"}
+
+// RepositoryFromContext attempts to get the repository from the context
+func RepositoryFromContext(ctx context.Context, path string) *Repository {
+ value := ctx.Value(RepositoryContextKey)
+ if value == nil {
+ return nil
+ }
+
+ if repo, ok := value.(*Repository); ok && repo != nil {
+ if repo.Path == path {
+ return repo
+ }
+ }
+
+ return nil
+}
+
+type nopCloser func()
+
+func (nopCloser) Close() error { return nil }
+
+// RepositoryFromContextOrOpen attempts to get the repository from the context or just opens it
+func RepositoryFromContextOrOpen(ctx context.Context, path string) (*Repository, io.Closer, error) {
+ gitRepo := RepositoryFromContext(ctx, path)
+ if gitRepo != nil {
+ return gitRepo, nopCloser(nil), nil
+ }
+
+ gitRepo, err := OpenRepositoryCtx(ctx, path)
+ return gitRepo, gitRepo, err
+}
diff --git a/modules/git/repo_base_gogit.go b/modules/git/repo_base_gogit.go
index 7299526562..23009d80f7 100644
--- a/modules/git/repo_base_gogit.go
+++ b/modules/git/repo_base_gogit.go
@@ -73,13 +73,14 @@ func OpenRepositoryCtx(ctx context.Context, repoPath string) (*Repository, error
}
// Close this repository, in particular close the underlying gogitStorage if this is not nil
-func (repo *Repository) Close() {
+func (repo *Repository) Close() (err error) {
if repo == nil || repo.gogitStorage == nil {
return
}
if err := repo.gogitStorage.Close(); err != nil {
gitealog.Error("Error closing storage: %v", err)
}
+ return
}
// GoGitRepo gets the go-git repo representation
diff --git a/modules/git/repo_base_nogogit.go b/modules/git/repo_base_nogogit.go
index e264fd4a14..ad4a00be1f 100644
--- a/modules/git/repo_base_nogogit.go
+++ b/modules/git/repo_base_nogogit.go
@@ -86,7 +86,7 @@ func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError
}
// Close this repository, in particular close the underlying gogitStorage if this is not nil
-func (repo *Repository) Close() {
+func (repo *Repository) Close() (err error) {
if repo == nil {
return
}
@@ -102,4 +102,5 @@ func (repo *Repository) Close() {
repo.checkReader = nil
repo.checkWriter = nil
}
+ return
}
diff --git a/modules/git/repo_branch.go b/modules/git/repo_branch.go
index 71a81935d6..38c0a180cd 100644
--- a/modules/git/repo_branch.go
+++ b/modules/git/repo_branch.go
@@ -88,8 +88,8 @@ func (repo *Repository) GetBranch(branch string) (*Branch, error) {
// GetBranchesByPath returns a branch by it's path
// if limit = 0 it will not limit
-func GetBranchesByPath(path string, skip, limit int) ([]*Branch, int, error) {
- gitRepo, err := OpenRepository(path)
+func GetBranchesByPath(ctx context.Context, path string, skip, limit int) ([]*Branch, int, error) {
+ gitRepo, err := OpenRepositoryCtx(ctx, path)
if err != nil {
return nil, 0, err
}
diff --git a/modules/git/repo_branch_gogit.go b/modules/git/repo_branch_gogit.go
index d159aafd6f..57952bcc64 100644
--- a/modules/git/repo_branch_gogit.go
+++ b/modules/git/repo_branch_gogit.go
@@ -83,11 +83,15 @@ func (repo *Repository) GetBranchNames(skip, limit int) ([]string, int, error) {
// WalkReferences walks all the references from the repository
func WalkReferences(ctx context.Context, repoPath string, walkfn func(string) error) (int, error) {
- repo, err := OpenRepositoryCtx(ctx, repoPath)
- if err != nil {
- return 0, err
+ repo := RepositoryFromContext(ctx, repoPath)
+ if repo == nil {
+ var err error
+ repo, err = OpenRepositoryCtx(ctx, repoPath)
+ if err != nil {
+ return 0, err
+ }
+ defer repo.Close()
}
- defer repo.Close()
i := 0
iter, err := repo.gogitRepo.References()
diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go
index 896ad34518..ed04ee2f67 100644
--- a/modules/git/repo_commit.go
+++ b/modules/git/repo_commit.go
@@ -195,7 +195,7 @@ func (repo *Repository) FileChangedBetweenCommits(filename, id1, id2 string) (bo
// FileCommitsCount return the number of files at a revision
func (repo *Repository) FileCommitsCount(revision, file string) (int64, error) {
- return CommitsCountFiles(repo.Path, []string{revision}, []string{file})
+ return CommitsCountFiles(repo.Ctx, repo.Path, []string{revision}, []string{file})
}
// CommitsByFileAndRange return the commits according revision file and the page
@@ -321,11 +321,11 @@ func (repo *Repository) CommitsBetweenIDs(last, before string) ([]*Commit, error
// CommitsCountBetween return numbers of commits between two commits
func (repo *Repository) CommitsCountBetween(start, end string) (int64, error) {
- count, err := CommitsCountFiles(repo.Path, []string{start + ".." + end}, []string{})
+ count, err := CommitsCountFiles(repo.Ctx, repo.Path, []string{start + ".." + end}, []string{})
if err != nil && strings.Contains(err.Error(), "no merge base") {
// future versions of git >= 2.28 are likely to return an error if before and last have become unrelated.
// previously it would return the results of git rev-list before last so let's try that...
- return CommitsCountFiles(repo.Path, []string{start, end}, []string{})
+ return CommitsCountFiles(repo.Ctx, repo.Path, []string{start, end}, []string{})
}
return count, err
diff --git a/modules/git/repo_compare.go b/modules/git/repo_compare.go
index fb0ba91265..ffede62ede 100644
--- a/modules/git/repo_compare.go
+++ b/modules/git/repo_compare.go
@@ -8,6 +8,7 @@ package git
import (
"bufio"
"bytes"
+ "context"
"errors"
"fmt"
"io"
@@ -72,14 +73,14 @@ func (repo *Repository) GetCompareInfo(basePath, baseBranch, headBranch string,
compareInfo := new(CompareInfo)
- compareInfo.HeadCommitID, err = GetFullCommitID(repo.Path, headBranch)
+ compareInfo.HeadCommitID, err = GetFullCommitID(repo.Ctx, repo.Path, headBranch)
if err != nil {
compareInfo.HeadCommitID = headBranch
}
compareInfo.MergeBase, remoteBranch, err = repo.GetMergeBase(tmpRemote, baseBranch, headBranch)
if err == nil {
- compareInfo.BaseCommitID, err = GetFullCommitID(repo.Path, remoteBranch)
+ compareInfo.BaseCommitID, err = GetFullCommitID(repo.Ctx, repo.Path, remoteBranch)
if err != nil {
compareInfo.BaseCommitID = remoteBranch
}
@@ -105,7 +106,7 @@ func (repo *Repository) GetCompareInfo(basePath, baseBranch, headBranch string,
}
} else {
compareInfo.Commits = []*Commit{}
- compareInfo.MergeBase, err = GetFullCommitID(repo.Path, remoteBranch)
+ compareInfo.MergeBase, err = GetFullCommitID(repo.Ctx, repo.Path, remoteBranch)
if err != nil {
compareInfo.MergeBase = remoteBranch
}
@@ -163,15 +164,15 @@ func (repo *Repository) GetDiffNumChangedFiles(base, head string, directComparis
// GetDiffShortStat counts number of changed files, number of additions and deletions
func (repo *Repository) GetDiffShortStat(base, head string) (numFiles, totalAdditions, totalDeletions int, err error) {
- numFiles, totalAdditions, totalDeletions, err = GetDiffShortStat(repo.Path, base+"..."+head)
+ numFiles, totalAdditions, totalDeletions, err = GetDiffShortStat(repo.Ctx, repo.Path, base+"..."+head)
if err != nil && strings.Contains(err.Error(), "no merge base") {
- return GetDiffShortStat(repo.Path, base, head)
+ return GetDiffShortStat(repo.Ctx, repo.Path, base, head)
}
return
}
// GetDiffShortStat counts number of changed files, number of additions and deletions
-func GetDiffShortStat(repoPath string, args ...string) (numFiles, totalAdditions, totalDeletions int, err error) {
+func GetDiffShortStat(ctx context.Context, repoPath string, args ...string) (numFiles, totalAdditions, totalDeletions int, err error) {
// Now if we call:
// $ git diff --shortstat 1ebb35b98889ff77299f24d82da426b434b0cca0...788b8b1440462d477f45b0088875
// we get:
@@ -181,7 +182,7 @@ func GetDiffShortStat(repoPath string, args ...string) (numFiles, totalAdditions
"--shortstat",
}, args...)
- stdout, err := NewCommand(args...).RunInDir(repoPath)
+ stdout, err := NewCommandContext(ctx, args...).RunInDir(repoPath)
if err != nil {
return 0, 0, 0, err
}
diff --git a/modules/git/repo_test.go b/modules/git/repo_test.go
index 9517783e6b..e143c1b39d 100644
--- a/modules/git/repo_test.go
+++ b/modules/git/repo_test.go
@@ -13,7 +13,7 @@ import (
func TestGetLatestCommitTime(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
- lct, err := GetLatestCommitTime(bareRepo1Path)
+ lct, err := GetLatestCommitTime(DefaultContext, bareRepo1Path)
assert.NoError(t, err)
// Time is Sun Jul 21 22:43:13 2019 +0200
// which is the time of commit
diff --git a/modules/git/tree.go b/modules/git/tree.go
index 3671f421e9..dd73afadcd 100644
--- a/modules/git/tree.go
+++ b/modules/git/tree.go
@@ -49,7 +49,7 @@ func (t *Tree) SubTree(rpath string) (*Tree, error) {
// LsTree checks if the given filenames are in the tree
func (repo *Repository) LsTree(ref string, filenames ...string) ([]string, error) {
- cmd := NewCommand("ls-tree", "-z", "--name-only", "--", ref)
+ cmd := NewCommandContext(repo.Ctx, "ls-tree", "-z", "--name-only", "--", ref)
for _, arg := range filenames {
if arg != "" {
cmd.AddArguments(arg)
diff --git a/modules/git/tree_entry_nogogit.go b/modules/git/tree_entry_nogogit.go
index 288ec4db6e..076062e1d6 100644
--- a/modules/git/tree_entry_nogogit.go
+++ b/modules/git/tree_entry_nogogit.go
@@ -7,10 +7,7 @@
package git
-import (
- "strconv"
- "strings"
-)
+import "code.gitea.io/gitea/modules/log"
// TreeEntry the leaf in the git tree
type TreeEntry struct {
@@ -47,13 +44,20 @@ func (te *TreeEntry) Size() int64 {
return te.size
}
- stdout, err := NewCommand("cat-file", "-s", te.ID.String()).RunInDir(te.ptree.repo.Path)
+ wr, rd, cancel := te.ptree.repo.CatFileBatchCheck(te.ptree.repo.Ctx)
+ defer cancel()
+ _, err := wr.Write([]byte(te.ID.String() + "\n"))
if err != nil {
+ log.Debug("error whilst reading size for %s in %s. Error: %v", te.ID.String(), te.ptree.repo.Path, err)
+ return 0
+ }
+ _, _, te.size, err = ReadBatchLine(rd)
+ if err != nil {
+ log.Debug("error whilst reading size for %s in %s. Error: %v", te.ID.String(), te.ptree.repo.Path, err)
return 0
}
te.sized = true
- te.size, _ = strconv.ParseInt(strings.TrimSpace(stdout), 10, 64)
return te.size
}
diff --git a/modules/git/tree_nogogit.go b/modules/git/tree_nogogit.go
index cfa2291e8f..f71e02f742 100644
--- a/modules/git/tree_nogogit.go
+++ b/modules/git/tree_nogogit.go
@@ -81,7 +81,7 @@ func (t *Tree) ListEntries() (Entries, error) {
}
}
- stdout, err := NewCommand("ls-tree", "-l", t.ID.String()).RunInDirBytes(t.repo.Path)
+ stdout, err := NewCommandContext(t.repo.Ctx, "ls-tree", "-l", t.ID.String()).RunInDirBytes(t.repo.Path)
if err != nil {
if strings.Contains(err.Error(), "fatal: Not a valid object name") || strings.Contains(err.Error(), "fatal: not a tree object") {
return nil, ErrNotExist{
@@ -104,7 +104,7 @@ func (t *Tree) ListEntriesRecursive() (Entries, error) {
if t.entriesRecursiveParsed {
return t.entriesRecursive, nil
}
- stdout, err := NewCommand("ls-tree", "-t", "-l", "-r", t.ID.String()).RunInDirBytes(t.repo.Path)
+ stdout, err := NewCommandContext(t.repo.Ctx, "ls-tree", "-t", "-l", "-r", t.ID.String()).RunInDirBytes(t.repo.Path)
if err != nil {
return nil, err
}
diff --git a/modules/gitgraph/graph.go b/modules/gitgraph/graph.go
index 8f04941147..9495014f71 100644
--- a/modules/gitgraph/graph.go
+++ b/modules/gitgraph/graph.go
@@ -51,7 +51,7 @@ func GetCommitGraph(r *git.Repository, page, maxAllowedColors int, hidePRRefs bo
args = append(args, files...)
}
- graphCmd := git.NewCommand("log")
+ graphCmd := git.NewCommandContext(r.Ctx, "log")
graphCmd.AddArguments(args...)
graph := NewGraph()
diff --git a/modules/indexer/code/bleve.go b/modules/indexer/code/bleve.go
index 3a44a8dc05..64e974dc18 100644
--- a/modules/indexer/code/bleve.go
+++ b/modules/indexer/code/bleve.go
@@ -6,6 +6,7 @@ package code
import (
"bufio"
+ "context"
"fmt"
"io"
"os"
@@ -180,7 +181,7 @@ func NewBleveIndexer(indexDir string) (*BleveIndexer, bool, error) {
return indexer, created, err
}
-func (b *BleveIndexer) addUpdate(batchWriter git.WriteCloserError, batchReader *bufio.Reader, commitSha string,
+func (b *BleveIndexer) addUpdate(ctx context.Context, batchWriter git.WriteCloserError, batchReader *bufio.Reader, commitSha string,
update fileUpdate, repo *repo_model.Repository, batch *gitea_bleve.FlushingBatch) error {
// Ignore vendored files in code search
if setting.Indexer.ExcludeVendored && analyze.IsVendor(update.Filename) {
@@ -190,7 +191,7 @@ func (b *BleveIndexer) addUpdate(batchWriter git.WriteCloserError, batchReader *
size := update.Size
if !update.Sized {
- stdout, err := git.NewCommand("cat-file", "-s", update.BlobSha).
+ stdout, err := git.NewCommandContext(ctx, "cat-file", "-s", update.BlobSha).
RunInDir(repo.RepoPath())
if err != nil {
return err
@@ -271,21 +272,21 @@ func (b *BleveIndexer) Close() {
}
// Index indexes the data
-func (b *BleveIndexer) Index(repo *repo_model.Repository, sha string, changes *repoChanges) error {
+func (b *BleveIndexer) Index(ctx context.Context, repo *repo_model.Repository, sha string, changes *repoChanges) error {
batch := gitea_bleve.NewFlushingBatch(b.indexer, maxBatchSize)
if len(changes.Updates) > 0 {
// Now because of some insanity with git cat-file not immediately failing if not run in a valid git directory we need to run git rev-parse first!
- if err := git.EnsureValidGitRepository(git.DefaultContext, repo.RepoPath()); err != nil {
+ if err := git.EnsureValidGitRepository(ctx, repo.RepoPath()); err != nil {
log.Error("Unable to open git repo: %s for %-v: %v", repo.RepoPath(), repo, err)
return err
}
- batchWriter, batchReader, cancel := git.CatFileBatch(git.DefaultContext, repo.RepoPath())
+ batchWriter, batchReader, cancel := git.CatFileBatch(ctx, repo.RepoPath())
defer cancel()
for _, update := range changes.Updates {
- if err := b.addUpdate(batchWriter, batchReader, sha, update, repo, batch); err != nil {
+ if err := b.addUpdate(ctx, batchWriter, batchReader, sha, update, repo, batch); err != nil {
return err
}
}
diff --git a/modules/indexer/code/elastic_search.go b/modules/indexer/code/elastic_search.go
index 169dffd78b..eb57d4d68a 100644
--- a/modules/indexer/code/elastic_search.go
+++ b/modules/indexer/code/elastic_search.go
@@ -177,7 +177,7 @@ func (b *ElasticSearchIndexer) init() (bool, error) {
return exists, nil
}
-func (b *ElasticSearchIndexer) addUpdate(batchWriter git.WriteCloserError, batchReader *bufio.Reader, sha string, update fileUpdate, repo *repo_model.Repository) ([]elastic.BulkableRequest, error) {
+func (b *ElasticSearchIndexer) addUpdate(ctx context.Context, batchWriter git.WriteCloserError, batchReader *bufio.Reader, sha string, update fileUpdate, repo *repo_model.Repository) ([]elastic.BulkableRequest, error) {
// Ignore vendored files in code search
if setting.Indexer.ExcludeVendored && analyze.IsVendor(update.Filename) {
return nil, nil
@@ -186,7 +186,7 @@ func (b *ElasticSearchIndexer) addUpdate(batchWriter git.WriteCloserError, batch
size := update.Size
if !update.Sized {
- stdout, err := git.NewCommand("cat-file", "-s", update.BlobSha).
+ stdout, err := git.NewCommandContext(ctx, "cat-file", "-s", update.BlobSha).
RunInDir(repo.RepoPath())
if err != nil {
return nil, err
@@ -244,7 +244,7 @@ func (b *ElasticSearchIndexer) addDelete(filename string, repo *repo_model.Repos
}
// Index will save the index data
-func (b *ElasticSearchIndexer) Index(repo *repo_model.Repository, sha string, changes *repoChanges) error {
+func (b *ElasticSearchIndexer) Index(ctx context.Context, repo *repo_model.Repository, sha string, changes *repoChanges) error {
reqs := make([]elastic.BulkableRequest, 0)
if len(changes.Updates) > 0 {
// Now because of some insanity with git cat-file not immediately failing if not run in a valid git directory we need to run git rev-parse first!
@@ -253,11 +253,11 @@ func (b *ElasticSearchIndexer) Index(repo *repo_model.Repository, sha string, ch
return err
}
- batchWriter, batchReader, cancel := git.CatFileBatch(git.DefaultContext, repo.RepoPath())
+ batchWriter, batchReader, cancel := git.CatFileBatch(ctx, repo.RepoPath())
defer cancel()
for _, update := range changes.Updates {
- updateReqs, err := b.addUpdate(batchWriter, batchReader, sha, update, repo)
+ updateReqs, err := b.addUpdate(ctx, batchWriter, batchReader, sha, update, repo)
if err != nil {
return err
}
diff --git a/modules/indexer/code/git.go b/modules/indexer/code/git.go
index b76f6d6778..247354054c 100644
--- a/modules/indexer/code/git.go
+++ b/modules/indexer/code/git.go
@@ -5,6 +5,7 @@
package code
import (
+ "context"
"strconv"
"strings"
@@ -27,8 +28,8 @@ type repoChanges struct {
RemovedFilenames []string
}
-func getDefaultBranchSha(repo *repo_model.Repository) (string, error) {
- stdout, err := git.NewCommand("show-ref", "-s", git.BranchPrefix+repo.DefaultBranch).RunInDir(repo.RepoPath())
+func getDefaultBranchSha(ctx context.Context, repo *repo_model.Repository) (string, error) {
+ stdout, err := git.NewCommandContext(ctx, "show-ref", "-s", git.BranchPrefix+repo.DefaultBranch).RunInDir(repo.RepoPath())
if err != nil {
return "", err
}
@@ -36,16 +37,16 @@ func getDefaultBranchSha(repo *repo_model.Repository) (string, error) {
}
// getRepoChanges returns changes to repo since last indexer update
-func getRepoChanges(repo *repo_model.Repository, revision string) (*repoChanges, error) {
+func getRepoChanges(ctx context.Context, repo *repo_model.Repository, revision string) (*repoChanges, error) {
status, err := repo_model.GetIndexerStatus(repo, repo_model.RepoIndexerTypeCode)
if err != nil {
return nil, err
}
if len(status.CommitSha) == 0 {
- return genesisChanges(repo, revision)
+ return genesisChanges(ctx, repo, revision)
}
- return nonGenesisChanges(repo, revision)
+ return nonGenesisChanges(ctx, repo, revision)
}
func isIndexable(entry *git.TreeEntry) bool {
@@ -89,9 +90,9 @@ func parseGitLsTreeOutput(stdout []byte) ([]fileUpdate, error) {
}
// genesisChanges get changes to add repo to the indexer for the first time
-func genesisChanges(repo *repo_model.Repository, revision string) (*repoChanges, error) {
+func genesisChanges(ctx context.Context, repo *repo_model.Repository, revision string) (*repoChanges, error) {
var changes repoChanges
- stdout, err := git.NewCommand("ls-tree", "--full-tree", "-l", "-r", revision).
+ stdout, err := git.NewCommandContext(ctx, "ls-tree", "--full-tree", "-l", "-r", revision).
RunInDirBytes(repo.RepoPath())
if err != nil {
return nil, err
@@ -101,8 +102,8 @@ func genesisChanges(repo *repo_model.Repository, revision string) (*repoChanges,
}
// nonGenesisChanges get changes since the previous indexer update
-func nonGenesisChanges(repo *repo_model.Repository, revision string) (*repoChanges, error) {
- diffCmd := git.NewCommand("diff", "--name-status",
+func nonGenesisChanges(ctx context.Context, repo *repo_model.Repository, revision string) (*repoChanges, error) {
+ diffCmd := git.NewCommandContext(ctx, "diff", "--name-status",
repo.CodeIndexerStatus.CommitSha, revision)
stdout, err := diffCmd.RunInDir(repo.RepoPath())
if err != nil {
@@ -112,7 +113,7 @@ func nonGenesisChanges(repo *repo_model.Repository, revision string) (*repoChang
if err = indexer.Delete(repo.ID); err != nil {
return nil, err
}
- return genesisChanges(repo, revision)
+ return genesisChanges(ctx, repo, revision)
}
var changes repoChanges
updatedFilenames := make([]string, 0, 10)
@@ -166,7 +167,7 @@ func nonGenesisChanges(repo *repo_model.Repository, revision string) (*repoChang
}
}
- cmd := git.NewCommand("ls-tree", "--full-tree", "-l", revision, "--")
+ cmd := git.NewCommandContext(ctx, "ls-tree", "--full-tree", "-l", revision, "--")
cmd.AddArguments(updatedFilenames...)
lsTreeStdout, err := cmd.RunInDirBytes(repo.RepoPath())
if err != nil {
diff --git a/modules/indexer/code/indexer.go b/modules/indexer/code/indexer.go
index a616d0e662..a17484cae5 100644
--- a/modules/indexer/code/indexer.go
+++ b/modules/indexer/code/indexer.go
@@ -42,7 +42,7 @@ type SearchResultLanguages struct {
// Indexer defines an interface to index and search code contents
type Indexer interface {
- Index(repo *repo_model.Repository, sha string, changes *repoChanges) error
+ Index(ctx context.Context, repo *repo_model.Repository, sha string, changes *repoChanges) error
Delete(repoID int64) error
Search(repoIDs []int64, language, keyword string, page, pageSize int, isMatch bool) (int64, []*SearchResult, []*SearchResultLanguages, error)
Close()
@@ -82,7 +82,7 @@ var (
indexerQueue queue.UniqueQueue
)
-func index(indexer Indexer, repoID int64) error {
+func index(ctx context.Context, indexer Indexer, repoID int64) error {
repo, err := repo_model.GetRepositoryByID(repoID)
if repo_model.IsErrRepoNotExist(err) {
return indexer.Delete(repoID)
@@ -91,18 +91,18 @@ func index(indexer Indexer, repoID int64) error {
return err
}
- sha, err := getDefaultBranchSha(repo)
+ sha, err := getDefaultBranchSha(ctx, repo)
if err != nil {
return err
}
- changes, err := getRepoChanges(repo, sha)
+ changes, err := getRepoChanges(ctx, repo, sha)
if err != nil {
return err
} else if changes == nil {
return nil
}
- if err := indexer.Index(repo, sha, changes); err != nil {
+ if err := indexer.Index(ctx, repo, sha, changes); err != nil {
return err
}
@@ -150,7 +150,7 @@ func Init() {
}
log.Trace("IndexerData Process Repo: %d", indexerData.RepoID)
- if err := index(indexer, indexerData.RepoID); err != nil {
+ if err := index(ctx, indexer, indexerData.RepoID); err != nil {
log.Error("index: %v", err)
continue
}
diff --git a/modules/indexer/code/indexer_test.go b/modules/indexer/code/indexer_test.go
index 42f21ed0df..71d58c08c5 100644
--- a/modules/indexer/code/indexer_test.go
+++ b/modules/indexer/code/indexer_test.go
@@ -9,6 +9,7 @@ import (
"testing"
"code.gitea.io/gitea/models/unittest"
+ "code.gitea.io/gitea/modules/git"
_ "code.gitea.io/gitea/models"
@@ -22,7 +23,7 @@ func TestMain(m *testing.M) {
func testIndexer(name string, t *testing.T, indexer Indexer) {
t.Run(name, func(t *testing.T) {
var repoID int64 = 1
- err := index(indexer, repoID)
+ err := index(git.DefaultContext, indexer, repoID)
assert.NoError(t, err)
var (
keywords = []struct {
diff --git a/modules/indexer/code/wrapped.go b/modules/indexer/code/wrapped.go
index 7e97a6a324..e7b5b89eef 100644
--- a/modules/indexer/code/wrapped.go
+++ b/modules/indexer/code/wrapped.go
@@ -5,6 +5,7 @@
package code
import (
+ "context"
"fmt"
"sync"
@@ -57,12 +58,12 @@ func (w *wrappedIndexer) get() (Indexer, error) {
return w.internal, nil
}
-func (w *wrappedIndexer) Index(repo *repo_model.Repository, sha string, changes *repoChanges) error {
+func (w *wrappedIndexer) Index(ctx context.Context, repo *repo_model.Repository, sha string, changes *repoChanges) error {
indexer, err := w.get()
if err != nil {
return err
}
- return indexer.Index(repo, sha, changes)
+ return indexer.Index(ctx, repo, sha, changes)
}
func (w *wrappedIndexer) Delete(repoID int64) error {
diff --git a/modules/lfs/pointer_scanner_nogogit.go b/modules/lfs/pointer_scanner_nogogit.go
index d8076b9021..cdf88c51b0 100644
--- a/modules/lfs/pointer_scanner_nogogit.go
+++ b/modules/lfs/pointer_scanner_nogogit.go
@@ -37,7 +37,7 @@ func SearchPointerBlobs(ctx context.Context, repo *git.Repository, pointerChan c
go createPointerResultsFromCatFileBatch(ctx, catFileBatchReader, &wg, pointerChan)
// 3. Take the shas of the blobs and batch read them
- go pipeline.CatFileBatch(shasToBatchReader, catFileBatchWriter, &wg, basePath)
+ go pipeline.CatFileBatch(ctx, shasToBatchReader, catFileBatchWriter, &wg, basePath)
// 2. From the provided objects restrict to blobs <=1k
go pipeline.BlobsLessThan1024FromCatFileBatchCheck(catFileCheckReader, shasToBatchWriter, &wg)
@@ -47,11 +47,11 @@ func SearchPointerBlobs(ctx context.Context, repo *git.Repository, pointerChan c
revListReader, revListWriter := io.Pipe()
shasToCheckReader, shasToCheckWriter := io.Pipe()
wg.Add(2)
- go pipeline.CatFileBatchCheck(shasToCheckReader, catFileCheckWriter, &wg, basePath)
+ go pipeline.CatFileBatchCheck(ctx, shasToCheckReader, catFileCheckWriter, &wg, basePath)
go pipeline.BlobsFromRevListObjects(revListReader, shasToCheckWriter, &wg)
- go pipeline.RevListAllObjects(revListWriter, &wg, basePath, errChan)
+ go pipeline.RevListAllObjects(ctx, revListWriter, &wg, basePath, errChan)
} else {
- go pipeline.CatFileBatchCheckAllObjects(catFileCheckWriter, &wg, basePath, errChan)
+ go pipeline.CatFileBatchCheckAllObjects(ctx, catFileCheckWriter, &wg, basePath, errChan)
}
wg.Wait()
diff --git a/modules/markup/html.go b/modules/markup/html.go
index 827be1a9af..f082b97dbb 100644
--- a/modules/markup/html.go
+++ b/modules/markup/html.go
@@ -1070,7 +1070,7 @@ func sha1CurrentPatternProcessor(ctx *RenderContext, node *html.Node) {
if !inCache {
if ctx.GitRepo == nil {
var err error
- ctx.GitRepo, err = git.OpenRepository(ctx.Metas["repoPath"])
+ ctx.GitRepo, err = git.OpenRepositoryCtx(ctx.Ctx, ctx.Metas["repoPath"])
if err != nil {
log.Error("unable to open repository: %s Error: %v", ctx.Metas["repoPath"], err)
return
diff --git a/modules/markup/html_test.go b/modules/markup/html_test.go
index 3fd9ef9f83..2824dca504 100644
--- a/modules/markup/html_test.go
+++ b/modules/markup/html_test.go
@@ -10,6 +10,7 @@ import (
"testing"
"code.gitea.io/gitea/modules/emoji"
+ "code.gitea.io/gitea/modules/git"
. "code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/setting"
@@ -28,6 +29,7 @@ func TestRender_Commits(t *testing.T) {
setting.AppURL = TestAppURL
test := func(input, expected string) {
buffer, err := RenderString(&RenderContext{
+ Ctx: git.DefaultContext,
Filename: ".md",
URLPrefix: TestRepoURL,
Metas: localMetas,
diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go
index 083484813a..84295b3d4e 100644
--- a/modules/markup/markdown/markdown_test.go
+++ b/modules/markup/markdown/markdown_test.go
@@ -8,6 +8,7 @@ import (
"strings"
"testing"
+ "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup"
. "code.gitea.io/gitea/modules/markup/markdown"
@@ -279,6 +280,7 @@ func TestTotal_RenderWiki(t *testing.T) {
for i := 0; i < len(sameCases); i++ {
line, err := RenderString(&markup.RenderContext{
+ Ctx: git.DefaultContext,
URLPrefix: AppSubURL,
Metas: localMetas,
IsWiki: true,
@@ -318,6 +320,7 @@ func TestTotal_RenderString(t *testing.T) {
for i := 0; i < len(sameCases); i++ {
line, err := RenderString(&markup.RenderContext{
+ Ctx: git.DefaultContext,
URLPrefix: util.URLJoin(AppSubURL, "src", "master/"),
Metas: localMetas,
}, sameCases[i])
diff --git a/modules/notification/action/action.go b/modules/notification/action/action.go
index 95b0d6cbef..bdf4a50995 100644
--- a/modules/notification/action/action.go
+++ b/modules/notification/action/action.go
@@ -12,9 +12,11 @@ import (
"code.gitea.io/gitea/models"
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
+ "code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification/base"
+ "code.gitea.io/gitea/modules/process"
"code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/util"
)
@@ -206,11 +208,14 @@ func (a *actionNotifier) NotifyForkRepository(doer *user_model.User, oldRepo, re
}
func (a *actionNotifier) NotifyPullRequestReview(pr *models.PullRequest, review *models.Review, comment *models.Comment, mentions []*user_model.User) {
+ ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("actionNotifier.NotifyPullRequestReview Pull[%d] #%d in [%d]", pr.ID, pr.Index, pr.BaseRepoID))
+ defer finished()
+
if err := review.LoadReviewer(); err != nil {
log.Error("LoadReviewer '%d/%d': %v", review.ID, review.ReviewerID, err)
return
}
- if err := review.LoadCodeComments(); err != nil {
+ if err := review.LoadCodeComments(ctx); err != nil {
log.Error("LoadCodeComments '%d/%d': %v", review.Reviewer.ID, review.ID, err)
return
}
@@ -330,7 +335,7 @@ func (a *actionNotifier) NotifyPushCommits(pusher *user_model.User, repo *repo_m
}
}
-func (a *actionNotifier) NotifyCreateRef(doer *user_model.User, repo *repo_model.Repository, refType, refFullName string) {
+func (a *actionNotifier) NotifyCreateRef(doer *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string) {
opType := models.ActionCommitRepo
if refType == "tag" {
// has sent same action in `NotifyPushCommits`, so skip it.
@@ -389,7 +394,7 @@ func (a *actionNotifier) NotifySyncPushCommits(pusher *user_model.User, repo *re
}
}
-func (a *actionNotifier) NotifySyncCreateRef(doer *user_model.User, repo *repo_model.Repository, refType, refFullName string) {
+func (a *actionNotifier) NotifySyncCreateRef(doer *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string) {
if err := models.NotifyWatchers(&models.Action{
ActUserID: repo.OwnerID,
ActUser: repo.MustOwner(),
diff --git a/modules/notification/base/notifier.go b/modules/notification/base/notifier.go
index 937476475a..177864994d 100644
--- a/modules/notification/base/notifier.go
+++ b/modules/notification/base/notifier.go
@@ -53,11 +53,11 @@ type Notifier interface {
NotifyDeleteRelease(doer *user_model.User, rel *models.Release)
NotifyPushCommits(pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits)
- NotifyCreateRef(doer *user_model.User, repo *repo_model.Repository, refType, refFullName string)
+ NotifyCreateRef(doer *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string)
NotifyDeleteRef(doer *user_model.User, repo *repo_model.Repository, refType, refFullName string)
NotifySyncPushCommits(pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits)
- NotifySyncCreateRef(doer *user_model.User, repo *repo_model.Repository, refType, refFullName string)
+ NotifySyncCreateRef(doer *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string)
NotifySyncDeleteRef(doer *user_model.User, repo *repo_model.Repository, refType, refFullName string)
NotifyRepoPendingTransfer(doer, newOwner *user_model.User, repo *repo_model.Repository)
diff --git a/modules/notification/base/null.go b/modules/notification/base/null.go
index e284c72b12..9524f02f1b 100644
--- a/modules/notification/base/null.go
+++ b/modules/notification/base/null.go
@@ -142,7 +142,7 @@ func (*NullNotifier) NotifyPushCommits(pusher *user_model.User, repo *repo_model
}
// NotifyCreateRef notifies branch or tag creation to notifiers
-func (*NullNotifier) NotifyCreateRef(doer *user_model.User, repo *repo_model.Repository, refType, refFullName string) {
+func (*NullNotifier) NotifyCreateRef(doer *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string) {
}
// NotifyDeleteRef notifies branch or tag deletion to notifiers
@@ -162,7 +162,7 @@ func (*NullNotifier) NotifySyncPushCommits(pusher *user_model.User, repo *repo_m
}
// NotifySyncCreateRef places a place holder function
-func (*NullNotifier) NotifySyncCreateRef(doer *user_model.User, repo *repo_model.Repository, refType, refFullName string) {
+func (*NullNotifier) NotifySyncCreateRef(doer *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string) {
}
// NotifySyncDeleteRef places a place holder function
diff --git a/modules/notification/mail/mail.go b/modules/notification/mail/mail.go
index 6ad0ca0d85..b96871597f 100644
--- a/modules/notification/mail/mail.go
+++ b/modules/notification/mail/mail.go
@@ -10,8 +10,10 @@ import (
"code.gitea.io/gitea/models"
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
+ "code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification/base"
+ "code.gitea.io/gitea/modules/process"
"code.gitea.io/gitea/services/mailer"
)
@@ -30,6 +32,9 @@ func NewNotifier() base.Notifier {
func (m *mailNotifier) NotifyCreateIssueComment(doer *user_model.User, repo *repo_model.Repository,
issue *models.Issue, comment *models.Comment, mentions []*user_model.User) {
+ ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("mailNotifier.NotifyCreateIssueComment Issue[%d] #%d in [%d]", issue.ID, issue.Index, issue.RepoID))
+ defer finished()
+
var act models.ActionType
if comment.Type == models.CommentTypeClose {
act = models.ActionCloseIssue
@@ -43,7 +48,7 @@ func (m *mailNotifier) NotifyCreateIssueComment(doer *user_model.User, repo *rep
act = 0
}
- if err := mailer.MailParticipantsComment(comment, act, issue, mentions); err != nil {
+ if err := mailer.MailParticipantsComment(ctx, comment, act, issue, mentions); err != nil {
log.Error("MailParticipantsComment: %v", err)
}
}
@@ -94,6 +99,9 @@ func (m *mailNotifier) NotifyNewPullRequest(pr *models.PullRequest, mentions []*
}
func (m *mailNotifier) NotifyPullRequestReview(pr *models.PullRequest, r *models.Review, comment *models.Comment, mentions []*user_model.User) {
+ ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("mailNotifier.NotifyPullRequestReview Pull[%d] #%d in [%d]", pr.ID, pr.Index, pr.BaseRepoID))
+ defer finished()
+
var act models.ActionType
if comment.Type == models.CommentTypeClose {
act = models.ActionCloseIssue
@@ -102,13 +110,16 @@ func (m *mailNotifier) NotifyPullRequestReview(pr *models.PullRequest, r *models
} else if comment.Type == models.CommentTypeComment {
act = models.ActionCommentPull
}
- if err := mailer.MailParticipantsComment(comment, act, pr.Issue, mentions); err != nil {
+ if err := mailer.MailParticipantsComment(ctx, comment, act, pr.Issue, mentions); err != nil {
log.Error("MailParticipantsComment: %v", err)
}
}
func (m *mailNotifier) NotifyPullRequestCodeComment(pr *models.PullRequest, comment *models.Comment, mentions []*user_model.User) {
- if err := mailer.MailMentionsComment(pr, comment, mentions); err != nil {
+ ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("mailNotifier.NotifyPullRequestCodeComment Pull[%d] #%d in [%d]", pr.ID, pr.Index, pr.BaseRepoID))
+ defer finished()
+
+ if err := mailer.MailMentionsComment(ctx, pr, comment, mentions); err != nil {
log.Error("MailMentionsComment: %v", err)
}
}
@@ -143,6 +154,9 @@ func (m *mailNotifier) NotifyMergePullRequest(pr *models.PullRequest, doer *user
}
func (m *mailNotifier) NotifyPullRequestPushCommits(doer *user_model.User, pr *models.PullRequest, comment *models.Comment) {
+ ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("mailNotifier.NotifyPullRequestPushCommits Pull[%d] #%d in [%d]", pr.ID, pr.Index, pr.BaseRepoID))
+ defer finished()
+
var err error
if err = comment.LoadIssue(); err != nil {
log.Error("comment.LoadIssue: %v", err)
@@ -160,19 +174,25 @@ func (m *mailNotifier) NotifyPullRequestPushCommits(doer *user_model.User, pr *m
log.Error("comment.Issue.PullRequest.LoadBaseRepo: %v", err)
return
}
- if err := comment.LoadPushCommits(); err != nil {
+ if err := comment.LoadPushCommits(ctx); err != nil {
log.Error("comment.LoadPushCommits: %v", err)
}
m.NotifyCreateIssueComment(doer, comment.Issue.Repo, comment.Issue, comment, nil)
}
func (m *mailNotifier) NotifyPullRevieweDismiss(doer *user_model.User, review *models.Review, comment *models.Comment) {
- if err := mailer.MailParticipantsComment(comment, models.ActionPullReviewDismissed, review.Issue, nil); err != nil {
+ ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("mailNotifier.NotifyPullRevieweDismiss Review[%d] in Issue[%d]", review.ID, review.IssueID))
+ defer finished()
+
+ if err := mailer.MailParticipantsComment(ctx, comment, models.ActionPullReviewDismissed, review.Issue, nil); err != nil {
log.Error("MailParticipantsComment: %v", err)
}
}
func (m *mailNotifier) NotifyNewRelease(rel *models.Release) {
+ ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("mailNotifier.NotifyNewRelease rel[%d]%s in [%d]", rel.ID, rel.Title, rel.RepoID))
+ defer finished()
+
if err := rel.LoadAttributes(); err != nil {
log.Error("NotifyNewRelease: %v", err)
return
@@ -182,7 +202,7 @@ func (m *mailNotifier) NotifyNewRelease(rel *models.Release) {
return
}
- mailer.MailNewRelease(rel)
+ mailer.MailNewRelease(ctx, rel)
}
func (m *mailNotifier) NotifyRepoPendingTransfer(doer, newOwner *user_model.User, repo *repo_model.Repository) {
diff --git a/modules/notification/notification.go b/modules/notification/notification.go
index be8eb1c252..201f652b3c 100644
--- a/modules/notification/notification.go
+++ b/modules/notification/notification.go
@@ -259,9 +259,9 @@ func NotifyPushCommits(pusher *user_model.User, repo *repo_model.Repository, opt
}
// NotifyCreateRef notifies branch or tag creation to notifiers
-func NotifyCreateRef(pusher *user_model.User, repo *repo_model.Repository, refType, refFullName string) {
+func NotifyCreateRef(pusher *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string) {
for _, notifier := range notifiers {
- notifier.NotifyCreateRef(pusher, repo, refType, refFullName)
+ notifier.NotifyCreateRef(pusher, repo, refType, refFullName, refID)
}
}
@@ -280,9 +280,9 @@ func NotifySyncPushCommits(pusher *user_model.User, repo *repo_model.Repository,
}
// NotifySyncCreateRef notifies branch or tag creation to notifiers
-func NotifySyncCreateRef(pusher *user_model.User, repo *repo_model.Repository, refType, refFullName string) {
+func NotifySyncCreateRef(pusher *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string) {
for _, notifier := range notifiers {
- notifier.NotifySyncCreateRef(pusher, repo, refType, refFullName)
+ notifier.NotifySyncCreateRef(pusher, repo, refType, refFullName, refID)
}
}
diff --git a/modules/notification/webhook/webhook.go b/modules/notification/webhook/webhook.go
index 16ec31318b..39b50b3cd1 100644
--- a/modules/notification/webhook/webhook.go
+++ b/modules/notification/webhook/webhook.go
@@ -5,6 +5,8 @@
package webhook
import (
+ "fmt"
+
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/perm"
repo_model "code.gitea.io/gitea/models/repo"
@@ -13,8 +15,10 @@ import (
"code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/convert"
"code.gitea.io/gitea/modules/git"
+ "code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification/base"
+ "code.gitea.io/gitea/modules/process"
"code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
@@ -35,6 +39,9 @@ func NewNotifier() base.Notifier {
}
func (m *webhookNotifier) NotifyIssueClearLabels(doer *user_model.User, issue *models.Issue) {
+ ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("webhook.NotifyIssueClearLabels User: %s[%d] Issue[%d] #%d in [%d]", doer.Name, doer.ID, issue.ID, issue.Index, issue.RepoID))
+ defer finished()
+
if err := issue.LoadPoster(); err != nil {
log.Error("loadPoster: %v", err)
return
@@ -56,7 +63,7 @@ func (m *webhookNotifier) NotifyIssueClearLabels(doer *user_model.User, issue *m
err = webhook_services.PrepareWebhooks(issue.Repo, webhook.HookEventPullRequestLabel, &api.PullRequestPayload{
Action: api.HookIssueLabelCleared,
Index: issue.Index,
- PullRequest: convert.ToAPIPullRequest(issue.PullRequest, nil),
+ PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
Repository: convert.ToRepo(issue.Repo, mode),
Sender: convert.ToUser(doer, nil),
})
@@ -140,6 +147,9 @@ func (m *webhookNotifier) NotifyMigrateRepository(doer, u *user_model.User, repo
}
func (m *webhookNotifier) NotifyIssueChangeAssignee(doer *user_model.User, issue *models.Issue, assignee *user_model.User, removed bool, comment *models.Comment) {
+ ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("webhook.NotifyIssueChangeAssignee User: %s[%d] Issue[%d] #%d in [%d] Assignee %s[%d] removed: %t", doer.Name, doer.ID, issue.ID, issue.Index, issue.RepoID, assignee.Name, assignee.ID, removed))
+ defer finished()
+
if issue.IsPull {
mode, _ := models.AccessLevelUnit(doer, issue.Repo, unit.TypePullRequests)
@@ -150,7 +160,7 @@ func (m *webhookNotifier) NotifyIssueChangeAssignee(doer *user_model.User, issue
issue.PullRequest.Issue = issue
apiPullRequest := &api.PullRequestPayload{
Index: issue.Index,
- PullRequest: convert.ToAPIPullRequest(issue.PullRequest, nil),
+ PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
Repository: convert.ToRepo(issue.Repo, mode),
Sender: convert.ToUser(doer, nil),
}
@@ -186,6 +196,9 @@ func (m *webhookNotifier) NotifyIssueChangeAssignee(doer *user_model.User, issue
}
func (m *webhookNotifier) NotifyIssueChangeTitle(doer *user_model.User, issue *models.Issue, oldTitle string) {
+ ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("webhook.NotifyIssueChangeTitle User: %s[%d] Issue[%d] #%d in [%d]", doer.Name, doer.ID, issue.ID, issue.Index, issue.RepoID))
+ defer finished()
+
mode, _ := models.AccessLevel(issue.Poster, issue.Repo)
var err error
if issue.IsPull {
@@ -202,7 +215,7 @@ func (m *webhookNotifier) NotifyIssueChangeTitle(doer *user_model.User, issue *m
From: oldTitle,
},
},
- PullRequest: convert.ToAPIPullRequest(issue.PullRequest, nil),
+ PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
Repository: convert.ToRepo(issue.Repo, mode),
Sender: convert.ToUser(doer, nil),
})
@@ -227,6 +240,9 @@ func (m *webhookNotifier) NotifyIssueChangeTitle(doer *user_model.User, issue *m
}
func (m *webhookNotifier) NotifyIssueChangeStatus(doer *user_model.User, issue *models.Issue, actionComment *models.Comment, isClosed bool) {
+ ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("webhook.NotifyIssueChangeStatus User: %s[%d] Issue[%d] #%d in [%d]", doer.Name, doer.ID, issue.ID, issue.Index, issue.RepoID))
+ defer finished()
+
mode, _ := models.AccessLevel(issue.Poster, issue.Repo)
var err error
if issue.IsPull {
@@ -237,7 +253,7 @@ func (m *webhookNotifier) NotifyIssueChangeStatus(doer *user_model.User, issue *
// Merge pull request calls issue.changeStatus so we need to handle separately.
apiPullRequest := &api.PullRequestPayload{
Index: issue.Index,
- PullRequest: convert.ToAPIPullRequest(issue.PullRequest, nil),
+ PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
Repository: convert.ToRepo(issue.Repo, mode),
Sender: convert.ToUser(doer, nil),
}
@@ -289,6 +305,9 @@ func (m *webhookNotifier) NotifyNewIssue(issue *models.Issue, mentions []*user_m
}
func (m *webhookNotifier) NotifyNewPullRequest(pull *models.PullRequest, mentions []*user_model.User) {
+ ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("webhook.NotifyNewPullRequest Pull[%d] #%d in [%d]", pull.ID, pull.Index, pull.BaseRepoID))
+ defer finished()
+
if err := pull.LoadIssue(); err != nil {
log.Error("pull.LoadIssue: %v", err)
return
@@ -306,7 +325,7 @@ func (m *webhookNotifier) NotifyNewPullRequest(pull *models.PullRequest, mention
if err := webhook_services.PrepareWebhooks(pull.Issue.Repo, webhook.HookEventPullRequest, &api.PullRequestPayload{
Action: api.HookIssueOpened,
Index: pull.Issue.Index,
- PullRequest: convert.ToAPIPullRequest(pull, nil),
+ PullRequest: convert.ToAPIPullRequest(ctx, pull, nil),
Repository: convert.ToRepo(pull.Issue.Repo, mode),
Sender: convert.ToUser(pull.Issue.Poster, nil),
}); err != nil {
@@ -315,6 +334,9 @@ func (m *webhookNotifier) NotifyNewPullRequest(pull *models.PullRequest, mention
}
func (m *webhookNotifier) NotifyIssueChangeContent(doer *user_model.User, issue *models.Issue, oldContent string) {
+ ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("webhook.NotifyIssueChangeContent User: %s[%d] Issue[%d] #%d in [%d]", doer.Name, doer.ID, issue.ID, issue.Index, issue.RepoID))
+ defer finished()
+
mode, _ := models.AccessLevel(issue.Poster, issue.Repo)
var err error
if issue.IsPull {
@@ -327,7 +349,7 @@ func (m *webhookNotifier) NotifyIssueChangeContent(doer *user_model.User, issue
From: oldContent,
},
},
- PullRequest: convert.ToAPIPullRequest(issue.PullRequest, nil),
+ PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
Repository: convert.ToRepo(issue.Repo, mode),
Sender: convert.ToUser(doer, nil),
})
@@ -480,6 +502,9 @@ func (m *webhookNotifier) NotifyDeleteComment(doer *user_model.User, comment *mo
func (m *webhookNotifier) NotifyIssueChangeLabels(doer *user_model.User, issue *models.Issue,
addedLabels []*models.Label, removedLabels []*models.Label) {
+ ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("webhook.NotifyIssueChangeLabels User: %s[%d] Issue[%d] #%d in [%d]", doer.Name, doer.ID, issue.ID, issue.Index, issue.RepoID))
+ defer finished()
+
var err error
if err = issue.LoadRepo(); err != nil {
@@ -505,7 +530,7 @@ func (m *webhookNotifier) NotifyIssueChangeLabels(doer *user_model.User, issue *
err = webhook_services.PrepareWebhooks(issue.Repo, webhook.HookEventPullRequestLabel, &api.PullRequestPayload{
Action: api.HookIssueLabelUpdated,
Index: issue.Index,
- PullRequest: convert.ToAPIPullRequest(issue.PullRequest, nil),
+ PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
Repository: convert.ToRepo(issue.Repo, perm.AccessModeNone),
Sender: convert.ToUser(doer, nil),
})
@@ -524,6 +549,9 @@ func (m *webhookNotifier) NotifyIssueChangeLabels(doer *user_model.User, issue *
}
func (m *webhookNotifier) NotifyIssueChangeMilestone(doer *user_model.User, issue *models.Issue, oldMilestoneID int64) {
+ ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("webhook.NotifyIssueChangeMilestone User: %s[%d] Issue[%d] #%d in [%d]", doer.Name, doer.ID, issue.ID, issue.Index, issue.RepoID))
+ defer finished()
+
var hookAction api.HookIssueAction
var err error
if issue.MilestoneID > 0 {
@@ -547,7 +575,7 @@ func (m *webhookNotifier) NotifyIssueChangeMilestone(doer *user_model.User, issu
err = webhook_services.PrepareWebhooks(issue.Repo, webhook.HookEventPullRequestMilestone, &api.PullRequestPayload{
Action: hookAction,
Index: issue.Index,
- PullRequest: convert.ToAPIPullRequest(issue.PullRequest, nil),
+ PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
Repository: convert.ToRepo(issue.Repo, mode),
Sender: convert.ToUser(doer, nil),
})
@@ -566,8 +594,11 @@ func (m *webhookNotifier) NotifyIssueChangeMilestone(doer *user_model.User, issu
}
func (m *webhookNotifier) NotifyPushCommits(pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
+ ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("webhook.NotifyPushCommits User: %s[%d] in %s[%d]", pusher.Name, pusher.ID, repo.FullName(), repo.ID))
+ defer finished()
+
apiPusher := convert.ToUser(pusher, nil)
- apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL())
+ apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(ctx, repo.RepoPath(), repo.HTMLURL())
if err != nil {
log.Error("commits.ToAPIPayloadCommits failed: %v", err)
return
@@ -589,6 +620,9 @@ func (m *webhookNotifier) NotifyPushCommits(pusher *user_model.User, repo *repo_
}
func (*webhookNotifier) NotifyMergePullRequest(pr *models.PullRequest, doer *user_model.User) {
+ ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("webhook.NotifyMergePullRequest Pull[%d] #%d in [%d]", pr.ID, pr.Index, pr.BaseRepoID))
+ defer finished()
+
// Reload pull request information.
if err := pr.LoadAttributes(); err != nil {
log.Error("LoadAttributes: %v", err)
@@ -614,7 +648,7 @@ func (*webhookNotifier) NotifyMergePullRequest(pr *models.PullRequest, doer *use
// Merge pull request calls issue.changeStatus so we need to handle separately.
apiPullRequest := &api.PullRequestPayload{
Index: pr.Issue.Index,
- PullRequest: convert.ToAPIPullRequest(pr, nil),
+ PullRequest: convert.ToAPIPullRequest(ctx, pr, nil),
Repository: convert.ToRepo(pr.Issue.Repo, mode),
Sender: convert.ToUser(doer, nil),
Action: api.HookIssueClosed,
@@ -627,6 +661,9 @@ func (*webhookNotifier) NotifyMergePullRequest(pr *models.PullRequest, doer *use
}
func (m *webhookNotifier) NotifyPullRequestChangeTargetBranch(doer *user_model.User, pr *models.PullRequest, oldBranch string) {
+ ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("webhook.NotifyPullRequestChangeTargetBranch Pull[%d] #%d in [%d]", pr.ID, pr.Index, pr.BaseRepoID))
+ defer finished()
+
issue := pr.Issue
if !issue.IsPull {
return
@@ -647,7 +684,7 @@ func (m *webhookNotifier) NotifyPullRequestChangeTargetBranch(doer *user_model.U
From: oldBranch,
},
},
- PullRequest: convert.ToAPIPullRequest(issue.PullRequest, nil),
+ PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
Repository: convert.ToRepo(issue.Repo, mode),
Sender: convert.ToUser(doer, nil),
})
@@ -658,6 +695,9 @@ func (m *webhookNotifier) NotifyPullRequestChangeTargetBranch(doer *user_model.U
}
func (m *webhookNotifier) NotifyPullRequestReview(pr *models.PullRequest, review *models.Review, comment *models.Comment, mentions []*user_model.User) {
+ ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("webhook.NotifyPullRequestReview Pull[%d] #%d in [%d]", pr.ID, pr.Index, pr.BaseRepoID))
+ defer finished()
+
var reviewHookType webhook.HookEventType
switch review.Type {
@@ -686,7 +726,7 @@ func (m *webhookNotifier) NotifyPullRequestReview(pr *models.PullRequest, review
if err := webhook_services.PrepareWebhooks(review.Issue.Repo, reviewHookType, &api.PullRequestPayload{
Action: api.HookIssueReviewed,
Index: review.Issue.Index,
- PullRequest: convert.ToAPIPullRequest(pr, nil),
+ PullRequest: convert.ToAPIPullRequest(ctx, pr, nil),
Repository: convert.ToRepo(review.Issue.Repo, mode),
Sender: convert.ToUser(review.Reviewer, nil),
Review: &api.ReviewPayload{
@@ -698,28 +738,14 @@ func (m *webhookNotifier) NotifyPullRequestReview(pr *models.PullRequest, review
}
}
-func (m *webhookNotifier) NotifyCreateRef(pusher *user_model.User, repo *repo_model.Repository, refType, refFullName string) {
+func (m *webhookNotifier) NotifyCreateRef(pusher *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string) {
apiPusher := convert.ToUser(pusher, nil)
apiRepo := convert.ToRepo(repo, perm.AccessModeNone)
refName := git.RefEndName(refFullName)
- gitRepo, err := git.OpenRepository(repo.RepoPath())
- if err != nil {
- log.Error("OpenRepository[%s]: %v", repo.RepoPath(), err)
- return
- }
-
- shaSum, err := gitRepo.GetRefCommitID(refFullName)
- if err != nil {
- gitRepo.Close()
- log.Error("GetRefCommitID[%s]: %v", refFullName, err)
- return
- }
- gitRepo.Close()
-
- if err = webhook_services.PrepareWebhooks(repo, webhook.HookEventCreate, &api.CreatePayload{
+ if err := webhook_services.PrepareWebhooks(repo, webhook.HookEventCreate, &api.CreatePayload{
Ref: refName,
- Sha: shaSum,
+ Sha: refID,
RefType: refType,
Repo: apiRepo,
Sender: apiPusher,
@@ -729,6 +755,9 @@ func (m *webhookNotifier) NotifyCreateRef(pusher *user_model.User, repo *repo_mo
}
func (m *webhookNotifier) NotifyPullRequestSynchronized(doer *user_model.User, pr *models.PullRequest) {
+ ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("webhook.NotifyPullRequestSynchronized Pull[%d] #%d in [%d]", pr.ID, pr.Index, pr.BaseRepoID))
+ defer finished()
+
if err := pr.LoadIssue(); err != nil {
log.Error("pr.LoadIssue: %v", err)
return
@@ -741,7 +770,7 @@ func (m *webhookNotifier) NotifyPullRequestSynchronized(doer *user_model.User, p
if err := webhook_services.PrepareWebhooks(pr.Issue.Repo, webhook.HookEventPullRequestSync, &api.PullRequestPayload{
Action: api.HookIssueSynchronized,
Index: pr.Issue.Index,
- PullRequest: convert.ToAPIPullRequest(pr, nil),
+ PullRequest: convert.ToAPIPullRequest(ctx, pr, nil),
Repository: convert.ToRepo(pr.Issue.Repo, perm.AccessModeNone),
Sender: convert.ToUser(doer, nil),
}); err != nil {
@@ -795,8 +824,11 @@ func (m *webhookNotifier) NotifyDeleteRelease(doer *user_model.User, rel *models
}
func (m *webhookNotifier) NotifySyncPushCommits(pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
+ ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("webhook.NotifySyncPushCommits User: %s[%d] in %s[%d]", pusher.Name, pusher.ID, repo.FullName(), repo.ID))
+ defer finished()
+
apiPusher := convert.ToUser(pusher, nil)
- apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL())
+ apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(ctx, repo.RepoPath(), repo.HTMLURL())
if err != nil {
log.Error("commits.ToAPIPayloadCommits failed: %v", err)
return
@@ -817,8 +849,8 @@ func (m *webhookNotifier) NotifySyncPushCommits(pusher *user_model.User, repo *r
}
}
-func (m *webhookNotifier) NotifySyncCreateRef(pusher *user_model.User, repo *repo_model.Repository, refType, refFullName string) {
- m.NotifyCreateRef(pusher, repo, refType, refFullName)
+func (m *webhookNotifier) NotifySyncCreateRef(pusher *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string) {
+ m.NotifyCreateRef(pusher, repo, refType, refFullName, refID)
}
func (m *webhookNotifier) NotifySyncDeleteRef(pusher *user_model.User, repo *repo_model.Repository, refType, refFullName string) {
diff --git a/modules/process/manager.go b/modules/process/manager.go
index 7cde9f9451..a9a2862958 100644
--- a/modules/process/manager.go
+++ b/modules/process/manager.go
@@ -210,32 +210,32 @@ func (pm *Manager) Processes(onlyRoots bool) []*Process {
// Exec a command and use the default timeout.
func (pm *Manager) Exec(desc, cmdName string, args ...string) (string, string, error) {
- return pm.ExecDir(-1, "", desc, cmdName, args...)
+ return pm.ExecDir(DefaultContext, -1, "", desc, cmdName, args...)
}
// ExecTimeout a command and use a specific timeout duration.
func (pm *Manager) ExecTimeout(timeout time.Duration, desc, cmdName string, args ...string) (string, string, error) {
- return pm.ExecDir(timeout, "", desc, cmdName, args...)
+ return pm.ExecDir(DefaultContext, timeout, "", desc, cmdName, args...)
}
// ExecDir a command and use the default timeout.
-func (pm *Manager) ExecDir(timeout time.Duration, dir, desc, cmdName string, args ...string) (string, string, error) {
- return pm.ExecDirEnv(timeout, dir, desc, nil, cmdName, args...)
+func (pm *Manager) ExecDir(ctx context.Context, timeout time.Duration, dir, desc, cmdName string, args ...string) (string, string, error) {
+ return pm.ExecDirEnv(ctx, timeout, dir, desc, nil, cmdName, args...)
}
// ExecDirEnv runs a command in given path and environment variables, and waits for its completion
// up to the given timeout (or DefaultTimeout if -1 is given).
// Returns its complete stdout and stderr
// outputs and an error, if any (including timeout)
-func (pm *Manager) ExecDirEnv(timeout time.Duration, dir, desc string, env []string, cmdName string, args ...string) (string, string, error) {
- return pm.ExecDirEnvStdIn(timeout, dir, desc, env, nil, cmdName, args...)
+func (pm *Manager) ExecDirEnv(ctx context.Context, timeout time.Duration, dir, desc string, env []string, cmdName string, args ...string) (string, string, error) {
+ return pm.ExecDirEnvStdIn(ctx, timeout, dir, desc, env, nil, cmdName, args...)
}
// ExecDirEnvStdIn runs a command in given path and environment variables with provided stdIN, and waits for its completion
// up to the given timeout (or DefaultTimeout if -1 is given).
// Returns its complete stdout and stderr
// outputs and an error, if any (including timeout)
-func (pm *Manager) ExecDirEnvStdIn(timeout time.Duration, dir, desc string, env []string, stdIn io.Reader, cmdName string, args ...string) (string, string, error) {
+func (pm *Manager) ExecDirEnvStdIn(ctx context.Context, timeout time.Duration, dir, desc string, env []string, stdIn io.Reader, cmdName string, args ...string) (string, string, error) {
if timeout == -1 {
timeout = 60 * time.Second
}
@@ -243,7 +243,7 @@ func (pm *Manager) ExecDirEnvStdIn(timeout time.Duration, dir, desc string, env
stdOut := new(bytes.Buffer)
stdErr := new(bytes.Buffer)
- ctx, _, finished := pm.AddContextTimeout(DefaultContext, timeout, desc)
+ ctx, _, finished := pm.AddContextTimeout(ctx, timeout, desc)
defer finished()
cmd := exec.CommandContext(ctx, cmdName, args...)
diff --git a/modules/repository/commits.go b/modules/repository/commits.go
index 9ff9a9531b..c4a69f4a01 100644
--- a/modules/repository/commits.go
+++ b/modules/repository/commits.go
@@ -5,6 +5,7 @@
package repository
import (
+ "context"
"fmt"
"net/url"
"time"
@@ -48,7 +49,7 @@ func NewPushCommits() *PushCommits {
}
// toAPIPayloadCommit converts a single PushCommit to an api.PayloadCommit object.
-func (pc *PushCommits) toAPIPayloadCommit(repoPath, repoLink string, commit *PushCommit) (*api.PayloadCommit, error) {
+func (pc *PushCommits) toAPIPayloadCommit(ctx context.Context, repoPath, repoLink string, commit *PushCommit) (*api.PayloadCommit, error) {
var err error
authorUsername := ""
author, ok := pc.emailUsers[commit.AuthorEmail]
@@ -75,7 +76,7 @@ func (pc *PushCommits) toAPIPayloadCommit(repoPath, repoLink string, commit *Pus
committerUsername = committer.Name
}
- fileStatus, err := git.GetCommitFileStatus(repoPath, commit.Sha1)
+ fileStatus, err := git.GetCommitFileStatus(ctx, repoPath, commit.Sha1)
if err != nil {
return nil, fmt.Errorf("FileStatus [commit_sha1: %s]: %v", commit.Sha1, err)
}
@@ -103,7 +104,7 @@ func (pc *PushCommits) toAPIPayloadCommit(repoPath, repoLink string, commit *Pus
// ToAPIPayloadCommits converts a PushCommits object to api.PayloadCommit format.
// It returns all converted commits and, if provided, the head commit or an error otherwise.
-func (pc *PushCommits) ToAPIPayloadCommits(repoPath, repoLink string) ([]*api.PayloadCommit, *api.PayloadCommit, error) {
+func (pc *PushCommits) ToAPIPayloadCommits(ctx context.Context, repoPath, repoLink string) ([]*api.PayloadCommit, *api.PayloadCommit, error) {
commits := make([]*api.PayloadCommit, len(pc.Commits))
var headCommit *api.PayloadCommit
@@ -111,7 +112,7 @@ func (pc *PushCommits) ToAPIPayloadCommits(repoPath, repoLink string) ([]*api.Pa
pc.emailUsers = make(map[string]*user_model.User)
}
for i, commit := range pc.Commits {
- apiCommit, err := pc.toAPIPayloadCommit(repoPath, repoLink, commit)
+ apiCommit, err := pc.toAPIPayloadCommit(ctx, repoPath, repoLink, commit)
if err != nil {
return nil, nil, err
}
@@ -123,7 +124,7 @@ func (pc *PushCommits) ToAPIPayloadCommits(repoPath, repoLink string) ([]*api.Pa
}
if pc.HeadCommit != nil && headCommit == nil {
var err error
- headCommit, err = pc.toAPIPayloadCommit(repoPath, repoLink, pc.HeadCommit)
+ headCommit, err = pc.toAPIPayloadCommit(ctx, repoPath, repoLink, pc.HeadCommit)
if err != nil {
return nil, nil, err
}
diff --git a/modules/repository/commits_test.go b/modules/repository/commits_test.go
index d01388fe10..37181d2dcd 100644
--- a/modules/repository/commits_test.go
+++ b/modules/repository/commits_test.go
@@ -50,7 +50,7 @@ func TestPushCommits_ToAPIPayloadCommits(t *testing.T) {
pushCommits.HeadCommit = &PushCommit{Sha1: "69554a6"}
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 16}).(*repo_model.Repository)
- payloadCommits, headCommit, err := pushCommits.ToAPIPayloadCommits(repo.RepoPath(), "/user2/repo16")
+ payloadCommits, headCommit, err := pushCommits.ToAPIPayloadCommits(git.DefaultContext, repo.RepoPath(), "/user2/repo16")
assert.NoError(t, err)
assert.Len(t, payloadCommits, 3)
assert.NotNil(t, headCommit)
diff --git a/modules/repository/generate.go b/modules/repository/generate.go
index 3f83f51bb7..c7ba617919 100644
--- a/modules/repository/generate.go
+++ b/modules/repository/generate.go
@@ -99,7 +99,7 @@ func checkGiteaTemplate(tmpDir string) (*models.GiteaTemplate, error) {
return gt, nil
}
-func generateRepoCommit(repo, templateRepo, generateRepo *repo_model.Repository, tmpDir string) error {
+func generateRepoCommit(ctx context.Context, repo, templateRepo, generateRepo *repo_model.Repository, tmpDir string) error {
commitTimeStr := time.Now().Format(time.RFC3339)
authorSig := repo.Owner.NewGitSig()
@@ -115,7 +115,7 @@ func generateRepoCommit(repo, templateRepo, generateRepo *repo_model.Repository,
// Clone to temporary path and do the init commit.
templateRepoPath := templateRepo.RepoPath()
- if err := git.Clone(templateRepoPath, tmpDir, git.CloneRepoOptions{
+ if err := git.Clone(ctx, templateRepoPath, tmpDir, git.CloneRepoOptions{
Depth: 1,
Branch: templateRepo.DefaultBranch,
}); err != nil {
@@ -172,19 +172,19 @@ func generateRepoCommit(repo, templateRepo, generateRepo *repo_model.Repository,
}
}
- if err := git.InitRepository(tmpDir, false); err != nil {
+ if err := git.InitRepository(ctx, tmpDir, false); err != nil {
return err
}
repoPath := repo.RepoPath()
- if stdout, err := git.NewCommand("remote", "add", "origin", repoPath).
+ if stdout, err := git.NewCommandContext(ctx, "remote", "add", "origin", repoPath).
SetDescription(fmt.Sprintf("generateRepoCommit (git remote add): %s to %s", templateRepoPath, tmpDir)).
RunInDirWithEnv(tmpDir, env); err != nil {
log.Error("Unable to add %v as remote origin to temporary repo to %s: stdout %s\nError: %v", repo, tmpDir, stdout, err)
return fmt.Errorf("git remote add: %v", err)
}
- return initRepoCommit(tmpDir, repo, repo.Owner, templateRepo.DefaultBranch)
+ return initRepoCommit(ctx, tmpDir, repo, repo.Owner, templateRepo.DefaultBranch)
}
func generateGitContent(ctx context.Context, repo, templateRepo, generateRepo *repo_model.Repository) (err error) {
@@ -199,7 +199,7 @@ func generateGitContent(ctx context.Context, repo, templateRepo, generateRepo *r
}
}()
- if err = generateRepoCommit(repo, templateRepo, generateRepo, tmpDir); err != nil {
+ if err = generateRepoCommit(ctx, repo, templateRepo, generateRepo, tmpDir); err != nil {
return fmt.Errorf("generateRepoCommit: %v", err)
}
@@ -209,7 +209,7 @@ func generateGitContent(ctx context.Context, repo, templateRepo, generateRepo *r
}
repo.DefaultBranch = templateRepo.DefaultBranch
- gitRepo, err := git.OpenRepository(repo.RepoPath())
+ gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath())
if err != nil {
return fmt.Errorf("openRepository: %v", err)
}
@@ -273,7 +273,7 @@ func GenerateRepository(ctx context.Context, doer, owner *user_model.User, templ
}
}
- if err = checkInitRepository(owner.Name, generateRepo.Name); err != nil {
+ if err = checkInitRepository(ctx, owner.Name, generateRepo.Name); err != nil {
return generateRepo, err
}
diff --git a/modules/repository/init.go b/modules/repository/init.go
index cc59b8f29f..c5dd8cb909 100644
--- a/modules/repository/init.go
+++ b/modules/repository/init.go
@@ -40,7 +40,7 @@ func prepareRepoCommit(ctx context.Context, repo *repo_model.Repository, tmpDir,
)
// Clone to temporary path and do the init commit.
- if stdout, err := git.NewCommand("clone", repoPath, tmpDir).
+ if stdout, err := git.NewCommandContext(ctx, "clone", repoPath, tmpDir).
SetDescription(fmt.Sprintf("prepareRepoCommit (git clone): %s to %s", repoPath, tmpDir)).
RunInDirWithEnv("", env); err != nil {
log.Error("Failed to clone from %v into %s: stdout: %s\nError: %v", repo, tmpDir, stdout, err)
@@ -103,7 +103,7 @@ func prepareRepoCommit(ctx context.Context, repo *repo_model.Repository, tmpDir,
}
// initRepoCommit temporarily changes with work directory.
-func initRepoCommit(tmpPath string, repo *repo_model.Repository, u *user_model.User, defaultBranch string) (err error) {
+func initRepoCommit(ctx context.Context, tmpPath string, repo *repo_model.Repository, u *user_model.User, defaultBranch string) (err error) {
commitTimeStr := time.Now().Format(time.RFC3339)
sig := u.NewGitSig()
@@ -117,7 +117,7 @@ func initRepoCommit(tmpPath string, repo *repo_model.Repository, u *user_model.U
committerName := sig.Name
committerEmail := sig.Email
- if stdout, err := git.NewCommand("add", "--all").
+ if stdout, err := git.NewCommandContext(ctx, "add", "--all").
SetDescription(fmt.Sprintf("initRepoCommit (git add): %s", tmpPath)).
RunInDir(tmpPath); err != nil {
log.Error("git add --all failed: Stdout: %s\nError: %v", stdout, err)
@@ -135,7 +135,7 @@ func initRepoCommit(tmpPath string, repo *repo_model.Repository, u *user_model.U
}
if git.CheckGitVersionAtLeast("1.7.9") == nil {
- sign, keyID, signer, _ := asymkey_service.SignInitialCommit(tmpPath, u)
+ sign, keyID, signer, _ := asymkey_service.SignInitialCommit(ctx, tmpPath, u)
if sign {
args = append(args, "-S"+keyID)
@@ -154,7 +154,7 @@ func initRepoCommit(tmpPath string, repo *repo_model.Repository, u *user_model.U
"GIT_COMMITTER_EMAIL="+committerEmail,
)
- if stdout, err := git.NewCommand(args...).
+ if stdout, err := git.NewCommandContext(ctx, args...).
SetDescription(fmt.Sprintf("initRepoCommit (git commit): %s", tmpPath)).
RunInDirWithEnv(tmpPath, env); err != nil {
log.Error("Failed to commit: %v: Stdout: %s\nError: %v", args, stdout, err)
@@ -165,7 +165,7 @@ func initRepoCommit(tmpPath string, repo *repo_model.Repository, u *user_model.U
defaultBranch = setting.Repository.DefaultBranch
}
- if stdout, err := git.NewCommand("push", "origin", "HEAD:"+defaultBranch).
+ if stdout, err := git.NewCommandContext(ctx, "push", "origin", "HEAD:"+defaultBranch).
SetDescription(fmt.Sprintf("initRepoCommit (git push): %s", tmpPath)).
RunInDirWithEnv(tmpPath, models.InternalPushingEnvironment(u, repo)); err != nil {
log.Error("Failed to push back to HEAD: Stdout: %s\nError: %v", stdout, err)
@@ -175,7 +175,7 @@ func initRepoCommit(tmpPath string, repo *repo_model.Repository, u *user_model.U
return nil
}
-func checkInitRepository(owner, name string) (err error) {
+func checkInitRepository(ctx context.Context, owner, name string) (err error) {
// Somehow the directory could exist.
repoPath := repo_model.RepoPath(owner, name)
isExist, err := util.IsExist(repoPath)
@@ -191,7 +191,7 @@ func checkInitRepository(owner, name string) (err error) {
}
// Init git bare new repository.
- if err = git.InitRepository(repoPath, true); err != nil {
+ if err = git.InitRepository(ctx, repoPath, true); err != nil {
return fmt.Errorf("git.InitRepository: %v", err)
} else if err = createDelegateHooks(repoPath); err != nil {
return fmt.Errorf("createDelegateHooks: %v", err)
@@ -201,7 +201,7 @@ func checkInitRepository(owner, name string) (err error) {
// InitRepository initializes README and .gitignore if needed.
func initRepository(ctx context.Context, repoPath string, u *user_model.User, repo *repo_model.Repository, opts models.CreateRepoOptions) (err error) {
- if err = checkInitRepository(repo.OwnerName, repo.Name); err != nil {
+ if err = checkInitRepository(ctx, repo.OwnerName, repo.Name); err != nil {
return err
}
@@ -222,7 +222,7 @@ func initRepository(ctx context.Context, repoPath string, u *user_model.User, re
}
// Apply changes and commit.
- if err = initRepoCommit(tmpDir, repo, u, opts.DefaultBranch); err != nil {
+ if err = initRepoCommit(ctx, tmpDir, repo, u, opts.DefaultBranch); err != nil {
return fmt.Errorf("initRepoCommit: %v", err)
}
}
@@ -241,7 +241,7 @@ func initRepository(ctx context.Context, repoPath string, u *user_model.User, re
if len(opts.DefaultBranch) > 0 {
repo.DefaultBranch = opts.DefaultBranch
- gitRepo, err := git.OpenRepository(repo.RepoPath())
+ gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath())
if err != nil {
return fmt.Errorf("openRepository: %v", err)
}
diff --git a/modules/repository/push.go b/modules/repository/push.go
index 038735f585..728e61c839 100644
--- a/modules/repository/push.go
+++ b/modules/repository/push.go
@@ -5,6 +5,7 @@
package repository
import (
+ "context"
"strings"
repo_model "code.gitea.io/gitea/models/repo"
@@ -98,12 +99,12 @@ func (opts *PushUpdateOptions) RepoFullName() string {
}
// IsForcePush detect if a push is a force push
-func IsForcePush(opts *PushUpdateOptions) (bool, error) {
+func IsForcePush(ctx context.Context, opts *PushUpdateOptions) (bool, error) {
if !opts.IsUpdateBranch() {
return false, nil
}
- output, err := git.NewCommand("rev-list", "--max-count=1", opts.OldCommitID, "^"+opts.NewCommitID).
+ output, err := git.NewCommandContext(ctx, "rev-list", "--max-count=1", opts.OldCommitID, "^"+opts.NewCommitID).
RunInDir(repo_model.RepoPath(opts.RepoUserName, opts.RepoName))
if err != nil {
return false, err
diff --git a/modules/repository/repo.go b/modules/repository/repo.go
index eb3f58e937..a8d65a4429 100644
--- a/modules/repository/repo.go
+++ b/modules/repository/repo.go
@@ -36,11 +36,11 @@ var commonWikiURLSuffixes = []string{".wiki.git", ".git/wiki"}
// WikiRemoteURL returns accessible repository URL for wiki if exists.
// Otherwise, it returns an empty string.
-func WikiRemoteURL(remote string) string {
+func WikiRemoteURL(ctx context.Context, remote string) string {
remote = strings.TrimSuffix(remote, ".git")
for _, suffix := range commonWikiURLSuffixes {
wikiURL := remote + suffix
- if git.IsRepoURLAccessible(wikiURL) {
+ if git.IsRepoURLAccessible(ctx, wikiURL) {
return wikiURL
}
}
@@ -71,7 +71,7 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User,
return repo, fmt.Errorf("Failed to remove %s: %v", repoPath, err)
}
- if err = git.CloneWithContext(ctx, opts.CloneAddr, repoPath, git.CloneRepoOptions{
+ if err = git.Clone(ctx, opts.CloneAddr, repoPath, git.CloneRepoOptions{
Mirror: true,
Quiet: true,
Timeout: migrateTimeout,
@@ -81,13 +81,13 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User,
if opts.Wiki {
wikiPath := repo_model.WikiPath(u.Name, opts.RepoName)
- wikiRemotePath := WikiRemoteURL(opts.CloneAddr)
+ wikiRemotePath := WikiRemoteURL(ctx, opts.CloneAddr)
if len(wikiRemotePath) > 0 {
if err := util.RemoveAll(wikiPath); err != nil {
return repo, fmt.Errorf("Failed to remove %s: %v", wikiPath, err)
}
- if err = git.CloneWithContext(ctx, wikiRemotePath, wikiPath, git.CloneRepoOptions{
+ if err = git.Clone(ctx, wikiRemotePath, wikiPath, git.CloneRepoOptions{
Mirror: true,
Quiet: true,
Timeout: migrateTimeout,
@@ -116,7 +116,7 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User,
return repo, fmt.Errorf("error in MigrateRepositoryGitData(git update-server-info): %v", err)
}
- gitRepo, err := git.OpenRepository(repoPath)
+ gitRepo, err := git.OpenRepositoryCtx(ctx, repoPath)
if err != nil {
return repo, fmt.Errorf("OpenRepository: %v", err)
}
@@ -196,7 +196,7 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User,
repo.IsMirror = true
err = models.UpdateRepository(repo, false)
} else {
- repo, err = CleanUpMigrateInfo(repo)
+ repo, err = CleanUpMigrateInfo(ctx, repo)
}
return repo, err
@@ -217,7 +217,7 @@ func cleanUpMigrateGitConfig(configPath string) error {
}
// CleanUpMigrateInfo finishes migrating repository and/or wiki with things that don't need to be done for mirrors.
-func CleanUpMigrateInfo(repo *repo_model.Repository) (*repo_model.Repository, error) {
+func CleanUpMigrateInfo(ctx context.Context, repo *repo_model.Repository) (*repo_model.Repository, error) {
repoPath := repo.RepoPath()
if err := createDelegateHooks(repoPath); err != nil {
return repo, fmt.Errorf("createDelegateHooks: %v", err)
@@ -228,7 +228,7 @@ func CleanUpMigrateInfo(repo *repo_model.Repository) (*repo_model.Repository, er
}
}
- _, err := git.NewCommand("remote", "rm", "origin").RunInDir(repoPath)
+ _, err := git.NewCommandContext(ctx, "remote", "rm", "origin").RunInDir(repoPath)
if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") {
return repo, fmt.Errorf("CleanUpMigrateInfo: %v", err)
}
diff --git a/modules/templates/helper.go b/modules/templates/helper.go
index fc07b49c71..833b582073 100644
--- a/modules/templates/helper.go
+++ b/modules/templates/helper.go
@@ -7,6 +7,7 @@ package templates
import (
"bytes"
+ "context"
"errors"
"fmt"
"html"
@@ -635,17 +636,18 @@ func Sha1(str string) string {
}
// RenderCommitMessage renders commit message with XSS-safe and special links.
-func RenderCommitMessage(msg, urlPrefix string, metas map[string]string) template.HTML {
- return RenderCommitMessageLink(msg, urlPrefix, "", metas)
+func RenderCommitMessage(ctx context.Context, msg, urlPrefix string, metas map[string]string) template.HTML {
+ return RenderCommitMessageLink(ctx, msg, urlPrefix, "", metas)
}
// RenderCommitMessageLink renders commit message as a XXS-safe link to the provided
// default url, handling for special links.
-func RenderCommitMessageLink(msg, urlPrefix, urlDefault string, metas map[string]string) template.HTML {
+func RenderCommitMessageLink(ctx context.Context, msg, urlPrefix, urlDefault string, metas map[string]string) template.HTML {
cleanMsg := template.HTMLEscapeString(msg)
// we can safely assume that it will not return any error, since there
// shouldn't be any special HTML.
fullMessage, err := markup.RenderCommitMessage(&markup.RenderContext{
+ Ctx: ctx,
URLPrefix: urlPrefix,
DefaultLink: urlDefault,
Metas: metas,
@@ -663,7 +665,7 @@ func RenderCommitMessageLink(msg, urlPrefix, urlDefault string, metas map[string
// RenderCommitMessageLinkSubject renders commit message as a XXS-safe link to
// the provided default url, handling for special links without email to links.
-func RenderCommitMessageLinkSubject(msg, urlPrefix, urlDefault string, metas map[string]string) template.HTML {
+func RenderCommitMessageLinkSubject(ctx context.Context, msg, urlPrefix, urlDefault string, metas map[string]string) template.HTML {
msgLine := strings.TrimLeftFunc(msg, unicode.IsSpace)
lineEnd := strings.IndexByte(msgLine, '\n')
if lineEnd > 0 {
@@ -677,6 +679,7 @@ func RenderCommitMessageLinkSubject(msg, urlPrefix, urlDefault string, metas map
// we can safely assume that it will not return any error, since there
// shouldn't be any special HTML.
renderedMessage, err := markup.RenderCommitMessageSubject(&markup.RenderContext{
+ Ctx: ctx,
URLPrefix: urlPrefix,
DefaultLink: urlDefault,
Metas: metas,
@@ -689,7 +692,7 @@ func RenderCommitMessageLinkSubject(msg, urlPrefix, urlDefault string, metas map
}
// RenderCommitBody extracts the body of a commit message without its title.
-func RenderCommitBody(msg, urlPrefix string, metas map[string]string) template.HTML {
+func RenderCommitBody(ctx context.Context, msg, urlPrefix string, metas map[string]string) template.HTML {
msgLine := strings.TrimRightFunc(msg, unicode.IsSpace)
lineEnd := strings.IndexByte(msgLine, '\n')
if lineEnd > 0 {
@@ -703,6 +706,7 @@ func RenderCommitBody(msg, urlPrefix string, metas map[string]string) template.H
}
renderedMessage, err := markup.RenderCommitMessage(&markup.RenderContext{
+ Ctx: ctx,
URLPrefix: urlPrefix,
Metas: metas,
}, template.HTMLEscapeString(msgLine))
@@ -714,8 +718,9 @@ func RenderCommitBody(msg, urlPrefix string, metas map[string]string) template.H
}
// RenderIssueTitle renders issue/pull title with defined post processors
-func RenderIssueTitle(text, urlPrefix string, metas map[string]string) template.HTML {
+func RenderIssueTitle(ctx context.Context, text, urlPrefix string, metas map[string]string) template.HTML {
renderedText, err := markup.RenderIssueTitle(&markup.RenderContext{
+ Ctx: ctx,
URLPrefix: urlPrefix,
Metas: metas,
}, template.HTMLEscapeString(text))
@@ -750,9 +755,10 @@ func ReactionToEmoji(reaction string) template.HTML {
}
// RenderNote renders the contents of a git-notes file as a commit message.
-func RenderNote(msg, urlPrefix string, metas map[string]string) template.HTML {
+func RenderNote(ctx context.Context, msg, urlPrefix string, metas map[string]string) template.HTML {
cleanMsg := template.HTMLEscapeString(msg)
fullMessage, err := markup.RenderCommitMessage(&markup.RenderContext{
+ Ctx: ctx,
URLPrefix: urlPrefix,
Metas: metas,
}, cleanMsg)
@@ -891,10 +897,10 @@ type remoteAddress struct {
Password string
}
-func mirrorRemoteAddress(m repo_model.RemoteMirrorer) remoteAddress {
+func mirrorRemoteAddress(ctx context.Context, m repo_model.RemoteMirrorer) remoteAddress {
a := remoteAddress{}
- u, err := git.GetRemoteAddress(git.DefaultContext, m.GetRepository().RepoPath(), m.GetRemoteName())
+ u, err := git.GetRemoteAddress(ctx, m.GetRepository().RepoPath(), m.GetRemoteName())
if err != nil {
log.Error("GetRemoteAddress %v", err)
return a
diff --git a/modules/test/context_tests.go b/modules/test/context_tests.go
index 62ec21f6fe..1bc1d104bc 100644
--- a/modules/test/context_tests.go
+++ b/modules/test/context_tests.go
@@ -67,7 +67,7 @@ func LoadRepo(t *testing.T, ctx *context.Context, repoID int64) {
// LoadRepoCommit loads a repo's commit into a test context.
func LoadRepoCommit(t *testing.T, ctx *context.Context) {
- gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath())
+ gitRepo, err := git.OpenRepositoryCtx(ctx, ctx.Repo.Repository.RepoPath())
assert.NoError(t, err)
defer gitRepo.Close()
branch, err := gitRepo.GetHEADBranch()
@@ -89,7 +89,7 @@ func LoadUser(t *testing.T, ctx *context.Context, userID int64) {
func LoadGitRepo(t *testing.T, ctx *context.Context) {
assert.NoError(t, ctx.Repo.Repository.GetOwner(db.DefaultContext))
var err error
- ctx.Repo.GitRepo, err = git.OpenRepository(ctx.Repo.Repository.RepoPath())
+ ctx.Repo.GitRepo, err = git.OpenRepositoryCtx(ctx, ctx.Repo.Repository.RepoPath())
assert.NoError(t, err)
}
diff --git a/modules/web/route.go b/modules/web/route.go
index 1d9c92bd7a..19193773d5 100644
--- a/modules/web/route.go
+++ b/modules/web/route.go
@@ -30,6 +30,7 @@ func Wrap(handlers ...interface{}) http.HandlerFunc {
func(ctx *context.Context),
func(ctx *context.Context) goctx.CancelFunc,
func(*context.APIContext),
+ func(*context.APIContext) goctx.CancelFunc,
func(*context.PrivateContext),
func(*context.PrivateContext) goctx.CancelFunc,
func(http.Handler) http.Handler:
@@ -60,6 +61,15 @@ func Wrap(handlers ...interface{}) http.HandlerFunc {
if ctx.Written() {
return
}
+ case func(*context.APIContext) goctx.CancelFunc:
+ ctx := context.GetAPIContext(req)
+ cancel := t(ctx)
+ if cancel != nil {
+ defer cancel()
+ }
+ if ctx.Written() {
+ return
+ }
case func(*context.PrivateContext) goctx.CancelFunc:
ctx := context.GetPrivateContext(req)
cancel := t(ctx)