From 5cb0c9aa0d7eed087055b1efca79628957207d36 Mon Sep 17 00:00:00 2001 From: zeripath Date: Wed, 19 Jan 2022 23:26:57 +0000 Subject: [PATCH] Propagate context and ensure git commands run in request context (#17868) This PR continues the work in #17125 by progressively ensuring that git commands run within the request context. This now means that the if there is a git repo already open in the context it will be used instead of reopening it. Signed-off-by: Andrew Thornton --- cmd/admin.go | 2 +- cmd/doctor.go | 3 +- integrations/api_repo_file_helpers.go | 3 +- .../api_repo_get_contents_list_test.go | 2 +- integrations/api_repo_get_contents_test.go | 2 +- .../git_helper_for_declarative_test.go | 4 +- integrations/mirror_pull_test.go | 2 +- integrations/pull_merge_test.go | 6 +- integrations/pull_update_test.go | 15 +- integrations/repo_tag_test.go | 6 +- integrations/repofiles_delete_test.go | 17 +- integrations/repofiles_update_test.go | 20 +-- models/admin/notice.go | 8 +- models/db/context.go | 7 +- models/db/engine.go | 1 + models/issue_comment.go | 25 +-- models/issue_comment_test.go | 5 +- models/issue_xref_test.go | 2 +- models/migrations/v136.go | 3 +- models/migrations/v156.go | 2 +- models/migrations/v82.go | 2 +- models/pull.go | 3 +- models/repo/repo.go | 3 +- models/repo_activity.go | 13 +- models/review.go | 20 +-- models/review_test.go | 11 +- modules/context/api.go | 48 +++-- modules/context/context.go | 6 + modules/context/private.go | 41 +++++ modules/context/repo.go | 10 +- modules/convert/git_commit.go | 5 +- modules/convert/pull.go | 11 +- modules/convert/pull_review.go | 13 +- modules/convert/pull_test.go | 5 +- modules/doctor/authorizedkeys.go | 3 +- modules/doctor/checkOldArchives.go | 3 +- modules/doctor/dbconsistency.go | 8 +- modules/doctor/dbversion.go | 6 +- modules/doctor/doctor.go | 4 +- modules/doctor/fix16961.go | 3 +- modules/doctor/mergebase.go | 11 +- modules/doctor/misc.go | 17 +- modules/doctor/paths.go | 3 +- modules/doctor/storage.go | 4 +- modules/doctor/usertype.go | 4 +- modules/git/blame.go | 6 - modules/git/commit.go | 31 ++-- modules/git/commit_info_test.go | 2 +- modules/git/commit_test.go | 6 +- modules/git/diff.go | 14 +- modules/git/pipeline/catfile.go | 13 +- modules/git/pipeline/lfs.go | 2 +- modules/git/pipeline/lfs_nogogit.go | 4 +- modules/git/pipeline/namerev.go | 5 +- modules/git/pipeline/revlist.go | 9 +- modules/git/repo.go | 169 ++---------------- modules/git/repo_base.go | 49 +++++ modules/git/repo_base_gogit.go | 3 +- modules/git/repo_base_nogogit.go | 3 +- modules/git/repo_branch.go | 4 +- modules/git/repo_branch_gogit.go | 12 +- modules/git/repo_commit.go | 6 +- modules/git/repo_compare.go | 15 +- modules/git/repo_test.go | 2 +- modules/git/tree.go | 2 +- modules/git/tree_entry_nogogit.go | 16 +- modules/git/tree_nogogit.go | 4 +- modules/gitgraph/graph.go | 2 +- modules/indexer/code/bleve.go | 13 +- modules/indexer/code/elastic_search.go | 10 +- modules/indexer/code/git.go | 23 +-- modules/indexer/code/indexer.go | 12 +- modules/indexer/code/indexer_test.go | 3 +- modules/indexer/code/wrapped.go | 5 +- modules/lfs/pointer_scanner_nogogit.go | 8 +- modules/markup/html.go | 2 +- modules/markup/html_test.go | 2 + modules/markup/markdown/markdown_test.go | 3 + modules/notification/action/action.go | 11 +- modules/notification/base/notifier.go | 4 +- modules/notification/base/null.go | 4 +- modules/notification/mail/mail.go | 32 +++- modules/notification/notification.go | 8 +- modules/notification/webhook/webhook.go | 98 ++++++---- modules/process/manager.go | 16 +- modules/repository/commits.go | 11 +- modules/repository/commits_test.go | 2 +- modules/repository/generate.go | 16 +- modules/repository/init.go | 22 +-- modules/repository/push.go | 5 +- modules/repository/repo.go | 18 +- modules/templates/helper.go | 24 ++- modules/test/context_tests.go | 4 +- modules/web/route.go | 10 ++ routers/api/v1/api.go | 12 +- routers/api/v1/misc/markdown.go | 6 +- routers/api/v1/misc/signing.go | 2 +- routers/api/v1/repo/blob.go | 2 +- routers/api/v1/repo/branch.go | 8 +- routers/api/v1/repo/commits.go | 31 ++-- routers/api/v1/repo/file.go | 8 +- routers/api/v1/repo/notes.go | 4 +- routers/api/v1/repo/pull.go | 33 ++-- routers/api/v1/repo/pull_review.go | 29 +-- routers/api/v1/repo/release.go | 2 +- routers/api/v1/repo/release_tags.go | 2 +- routers/api/v1/repo/repo.go | 4 +- routers/api/v1/repo/status.go | 2 +- routers/api/v1/repo/tag.go | 4 +- routers/api/v1/repo/tree.go | 2 +- routers/api/v1/repo/wiki.go | 8 +- routers/api/v1/utils/git.go | 2 +- routers/private/default_branch.go | 27 +-- routers/private/hook_pre_receive.go | 8 +- routers/private/hook_verification.go | 4 +- routers/private/internal.go | 4 +- routers/private/internal_repo.go | 2 +- routers/private/serv.go | 2 +- routers/web/admin/repos.go | 2 +- routers/web/feed/convert.go | 2 +- routers/web/org/home.go | 1 + routers/web/repo/activity.go | 6 +- routers/web/repo/branch.go | 10 +- routers/web/repo/commit.go | 7 +- routers/web/repo/compare.go | 15 +- routers/web/repo/editor.go | 15 +- routers/web/repo/http.go | 36 ++-- routers/web/repo/issue.go | 10 +- routers/web/repo/lfs.go | 4 +- routers/web/repo/pull.go | 40 +++-- routers/web/repo/pull_review.go | 8 +- routers/web/repo/release.go | 4 +- routers/web/repo/setting.go | 14 +- routers/web/repo/wiki.go | 9 +- routers/web/user/home.go | 2 +- services/agit/agit.go | 8 +- services/asymkey/sign.go | 43 ++--- services/gitdiff/gitdiff.go | 10 +- services/gitdiff/gitdiff_test.go | 3 +- services/mailer/mail.go | 1 + services/mailer/mail_comment.go | 8 +- services/mailer/mail_issue.go | 2 + services/mailer/mail_release.go | 8 +- services/migrations/dump.go | 10 +- services/migrations/gitea_uploader.go | 6 +- services/mirror/mirror_pull.go | 24 +-- services/mirror/mirror_push.go | 14 +- services/pull/check.go | 62 ++++--- services/pull/commit_status.go | 12 +- services/pull/lfs.go | 9 +- services/pull/merge.go | 71 ++++---- services/pull/patch.go | 30 ++-- services/pull/pull.go | 111 ++++++------ services/pull/review.go | 25 +-- services/pull/temp_repo.go | 17 +- services/pull/update.go | 13 +- services/release/release.go | 13 +- services/release/release_test.go | 2 +- services/repository/adopt.go | 7 +- services/repository/archiver/archiver.go | 11 +- services/repository/branch.go | 38 ++-- services/repository/check.go | 2 +- services/repository/files/commit.go | 11 +- services/repository/files/content.go | 25 +-- services/repository/files/content_test.go | 36 ++-- services/repository/files/delete.go | 20 ++- services/repository/files/diff.go | 5 +- services/repository/files/diff_test.go | 10 +- services/repository/files/file.go | 7 +- services/repository/files/file_test.go | 4 +- services/repository/files/temp_repo.go | 33 ++-- services/repository/files/tree.go | 8 +- services/repository/files/tree_test.go | 2 +- services/repository/files/update.go | 24 ++- services/repository/files/upload.go | 5 +- services/repository/hooks.go | 4 +- services/repository/push.go | 17 +- services/repository/repository.go | 5 +- services/wiki/wiki.go | 42 ++--- services/wiki/wiki_test.go | 24 +-- templates/explore/repo_list.tmpl | 3 +- templates/repo/branch/list.tmpl | 4 +- templates/repo/commit_page.tmpl | 6 +- templates/repo/commits_list.tmpl | 4 +- templates/repo/commits_list_small.tmpl | 4 +- templates/repo/graph/commits.tmpl | 2 +- templates/repo/header.tmpl | 2 +- templates/repo/home.tmpl | 3 +- templates/repo/issue/view_title.tmpl | 2 +- templates/repo/settings/options.tmpl | 6 +- templates/repo/view_list.tmpl | 6 +- templates/user/dashboard/feeds.tmpl | 2 +- tools/fuzz.go | 2 + 193 files changed, 1264 insertions(+), 1154 deletions(-) create mode 100644 modules/git/repo_base.go diff --git a/cmd/admin.go b/cmd/admin.go index 3c7f7c8a7c..3042df8bb7 100644 --- a/cmd/admin.go +++ b/cmd/admin.go @@ -669,7 +669,7 @@ func runRepoSyncReleases(_ *cli.Context) error { log.Trace("Processing next %d repos of %d", len(repos), count) for _, repo := range repos { log.Trace("Synchronizing repo %s with path %s", repo.FullName(), repo.RepoPath()) - gitRepo, err := git.OpenRepository(repo.RepoPath()) + gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath()) if err != nil { log.Warn("OpenRepository: %v", err) continue diff --git a/cmd/doctor.go b/cmd/doctor.go index 54c18b83dc..35cc305f4c 100644 --- a/cmd/doctor.go +++ b/cmd/doctor.go @@ -5,7 +5,6 @@ package cmd import ( - "context" "fmt" golog "log" "os" @@ -116,7 +115,7 @@ func runRecreateTable(ctx *cli.Context) error { } recreateTables := migrations.RecreateTables(beans...) - return db.InitEngineWithMigration(context.Background(), func(x *xorm.Engine) error { + return db.InitEngineWithMigration(stdCtx, func(x *xorm.Engine) error { if err := migrations.EnsureUpToDate(x); err != nil { return err } diff --git a/integrations/api_repo_file_helpers.go b/integrations/api_repo_file_helpers.go index 53f74df245..465e03d53c 100644 --- a/integrations/api_repo_file_helpers.go +++ b/integrations/api_repo_file_helpers.go @@ -7,6 +7,7 @@ package integrations import ( repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/git" api "code.gitea.io/gitea/modules/structs" files_service "code.gitea.io/gitea/services/repository/files" ) @@ -20,7 +21,7 @@ func createFileInBranch(user *user_model.User, repo *repo_model.Repository, tree Author: nil, Committer: nil, } - return files_service.CreateOrUpdateRepoFile(repo, user, opts) + return files_service.CreateOrUpdateRepoFile(git.DefaultContext, repo, user, opts) } func createFile(user *user_model.User, repo *repo_model.Repository, treePath string) (*api.FileResponse, error) { diff --git a/integrations/api_repo_get_contents_list_test.go b/integrations/api_repo_get_contents_list_test.go index 449798a328..b8822d13ec 100644 --- a/integrations/api_repo_get_contents_list_test.go +++ b/integrations/api_repo_get_contents_list_test.go @@ -72,7 +72,7 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) { // Make a new branch in repo1 newBranch := "test_branch" - err := repo_service.CreateNewBranch(user2, repo1, repo1.DefaultBranch, newBranch) + err := repo_service.CreateNewBranch(git.DefaultContext, user2, repo1, repo1.DefaultBranch, newBranch) assert.NoError(t, err) // Get the commit ID of the default branch gitRepo, err := git.OpenRepository(repo1.RepoPath()) diff --git a/integrations/api_repo_get_contents_test.go b/integrations/api_repo_get_contents_test.go index ddb988422c..bc8471c70d 100644 --- a/integrations/api_repo_get_contents_test.go +++ b/integrations/api_repo_get_contents_test.go @@ -73,7 +73,7 @@ func testAPIGetContents(t *testing.T, u *url.URL) { // Make a new branch in repo1 newBranch := "test_branch" - err := repo_service.CreateNewBranch(user2, repo1, repo1.DefaultBranch, newBranch) + err := repo_service.CreateNewBranch(git.DefaultContext, user2, repo1, repo1.DefaultBranch, newBranch) assert.NoError(t, err) // Get the commit ID of the default branch gitRepo, err := git.OpenRepository(repo1.RepoPath()) diff --git a/integrations/git_helper_for_declarative_test.go b/integrations/git_helper_for_declarative_test.go index 8105bcab8d..282bdd04a9 100644 --- a/integrations/git_helper_for_declarative_test.go +++ b/integrations/git_helper_for_declarative_test.go @@ -128,7 +128,7 @@ func doGitCloneFail(u *url.URL) func(*testing.T) { tmpDir, err := os.MkdirTemp("", "doGitCloneFail") assert.NoError(t, err) defer util.RemoveAll(tmpDir) - assert.Error(t, git.Clone(u.String(), tmpDir, git.CloneRepoOptions{})) + assert.Error(t, git.Clone(git.DefaultContext, u.String(), tmpDir, git.CloneRepoOptions{})) exist, err := util.IsExist(filepath.Join(tmpDir, "README.md")) assert.NoError(t, err) assert.False(t, exist) @@ -138,7 +138,7 @@ func doGitCloneFail(u *url.URL) func(*testing.T) { func doGitInitTestRepository(dstPath string) func(*testing.T) { return func(t *testing.T) { // Init repository in dstPath - assert.NoError(t, git.InitRepository(dstPath, false)) + assert.NoError(t, git.InitRepository(git.DefaultContext, dstPath, false)) // forcibly set default branch to master _, err := git.NewCommand("symbolic-ref", "HEAD", git.BranchPrefix+"master").RunInDir(dstPath) assert.NoError(t, err) diff --git a/integrations/mirror_pull_test.go b/integrations/mirror_pull_test.go index 2bc50b614f..eb798fe2ce 100644 --- a/integrations/mirror_pull_test.go +++ b/integrations/mirror_pull_test.go @@ -86,7 +86,7 @@ func TestMirrorPull(t *testing.T) { release, err := models.GetRelease(repo.ID, "v0.2") assert.NoError(t, err) - assert.NoError(t, release_service.DeleteReleaseByID(release.ID, user, true)) + assert.NoError(t, release_service.DeleteReleaseByID(ctx, release.ID, user, true)) ok = mirror_service.SyncPullMirror(ctx, mirror.ID) assert.True(t, ok) diff --git a/integrations/pull_merge_test.go b/integrations/pull_merge_test.go index a0b87eeee8..ed34a0f6d6 100644 --- a/integrations/pull_merge_test.go +++ b/integrations/pull_merge_test.go @@ -241,11 +241,11 @@ func TestCantMergeConflict(t *testing.T) { gitRepo, err := git.OpenRepository(repo_model.RepoPath(user1.Name, repo1.Name)) assert.NoError(t, err) - err = pull.Merge(pr, user1, gitRepo, repo_model.MergeStyleMerge, "", "CONFLICT") + err = pull.Merge(git.DefaultContext, pr, user1, gitRepo, repo_model.MergeStyleMerge, "", "CONFLICT") assert.Error(t, err, "Merge should return an error due to conflict") assert.True(t, models.IsErrMergeConflicts(err), "Merge error is not a conflict error") - err = pull.Merge(pr, user1, gitRepo, repo_model.MergeStyleRebase, "", "CONFLICT") + err = pull.Merge(git.DefaultContext, pr, user1, gitRepo, repo_model.MergeStyleRebase, "", "CONFLICT") assert.Error(t, err, "Merge should return an error due to conflict") assert.True(t, models.IsErrRebaseConflicts(err), "Merge error is not a conflict error") gitRepo.Close() @@ -329,7 +329,7 @@ func TestCantMergeUnrelated(t *testing.T) { BaseBranch: "base", }).(*models.PullRequest) - err = pull.Merge(pr, user1, gitRepo, repo_model.MergeStyleMerge, "", "UNRELATED") + err = pull.Merge(git.DefaultContext, pr, user1, gitRepo, repo_model.MergeStyleMerge, "", "UNRELATED") assert.Error(t, err, "Merge should return an error due to unrelated") assert.True(t, models.IsErrMergeUnrelatedHistories(err), "Merge error is not a unrelated histories error") gitRepo.Close() diff --git a/integrations/pull_update_test.go b/integrations/pull_update_test.go index ede0cc9896..b80b85fbeb 100644 --- a/integrations/pull_update_test.go +++ b/integrations/pull_update_test.go @@ -13,6 +13,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/git" pull_service "code.gitea.io/gitea/services/pull" repo_service "code.gitea.io/gitea/services/repository" files_service "code.gitea.io/gitea/services/repository/files" @@ -28,7 +29,7 @@ func TestAPIPullUpdate(t *testing.T) { pr := createOutdatedPR(t, user, org26) //Test GetDiverging - diffCount, err := pull_service.GetDiverging(pr) + diffCount, err := pull_service.GetDiverging(git.DefaultContext, pr) assert.NoError(t, err) assert.EqualValues(t, 1, diffCount.Behind) assert.EqualValues(t, 1, diffCount.Ahead) @@ -41,7 +42,7 @@ func TestAPIPullUpdate(t *testing.T) { session.MakeRequest(t, req, http.StatusOK) //Test GetDiverging after update - diffCount, err = pull_service.GetDiverging(pr) + diffCount, err = pull_service.GetDiverging(git.DefaultContext, pr) assert.NoError(t, err) assert.EqualValues(t, 0, diffCount.Behind) assert.EqualValues(t, 2, diffCount.Ahead) @@ -56,7 +57,7 @@ func TestAPIPullUpdateByRebase(t *testing.T) { pr := createOutdatedPR(t, user, org26) //Test GetDiverging - diffCount, err := pull_service.GetDiverging(pr) + diffCount, err := pull_service.GetDiverging(git.DefaultContext, pr) assert.NoError(t, err) assert.EqualValues(t, 1, diffCount.Behind) assert.EqualValues(t, 1, diffCount.Ahead) @@ -69,7 +70,7 @@ func TestAPIPullUpdateByRebase(t *testing.T) { session.MakeRequest(t, req, http.StatusOK) //Test GetDiverging after update - diffCount, err = pull_service.GetDiverging(pr) + diffCount, err = pull_service.GetDiverging(git.DefaultContext, pr) assert.NoError(t, err) assert.EqualValues(t, 0, diffCount.Behind) assert.EqualValues(t, 1, diffCount.Ahead) @@ -98,7 +99,7 @@ func createOutdatedPR(t *testing.T, actor, forkOrg *user_model.User) *models.Pul assert.NotEmpty(t, headRepo) //create a commit on base Repo - _, err = files_service.CreateOrUpdateRepoFile(baseRepo, actor, &files_service.UpdateRepoFileOptions{ + _, err = files_service.CreateOrUpdateRepoFile(git.DefaultContext, baseRepo, actor, &files_service.UpdateRepoFileOptions{ TreePath: "File_A", Message: "Add File A", Content: "File A", @@ -121,7 +122,7 @@ func createOutdatedPR(t *testing.T, actor, forkOrg *user_model.User) *models.Pul assert.NoError(t, err) //create a commit on head Repo - _, err = files_service.CreateOrUpdateRepoFile(headRepo, actor, &files_service.UpdateRepoFileOptions{ + _, err = files_service.CreateOrUpdateRepoFile(git.DefaultContext, headRepo, actor, &files_service.UpdateRepoFileOptions{ TreePath: "File_B", Message: "Add File on PR branch", Content: "File B", @@ -160,7 +161,7 @@ func createOutdatedPR(t *testing.T, actor, forkOrg *user_model.User) *models.Pul BaseRepo: baseRepo, Type: models.PullRequestGitea, } - err = pull_service.NewPullRequest(baseRepo, pullIssue, nil, nil, pullRequest, nil) + err = pull_service.NewPullRequest(git.DefaultContext, baseRepo, pullIssue, nil, nil, pullRequest, nil) assert.NoError(t, err) issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{Title: "Test Pull -to-update-"}).(*models.Issue) diff --git a/integrations/repo_tag_test.go b/integrations/repo_tag_test.go index 4c06044c6e..fbca1ac7db 100644 --- a/integrations/repo_tag_test.go +++ b/integrations/repo_tag_test.go @@ -29,7 +29,7 @@ func TestCreateNewTagProtected(t *testing.T) { t.Run("API", func(t *testing.T) { defer PrintCurrentTest(t)() - err := release.CreateNewTag(owner, repo, "master", "v-1", "first tag") + err := release.CreateNewTag(git.DefaultContext, owner, repo, "master", "v-1", "first tag") assert.NoError(t, err) err = models.InsertProtectedTag(&models.ProtectedTag{ @@ -44,11 +44,11 @@ func TestCreateNewTagProtected(t *testing.T) { }) assert.NoError(t, err) - err = release.CreateNewTag(owner, repo, "master", "v-2", "second tag") + err = release.CreateNewTag(git.DefaultContext, owner, repo, "master", "v-2", "second tag") assert.Error(t, err) assert.True(t, models.IsErrProtectedTagName(err)) - err = release.CreateNewTag(owner, repo, "master", "v-1.1", "third tag") + err = release.CreateNewTag(git.DefaultContext, owner, repo, "master", "v-1.1", "third tag") assert.NoError(t, err) }) diff --git a/integrations/repofiles_delete_test.go b/integrations/repofiles_delete_test.go index d2bfe31d95..09794f8c9f 100644 --- a/integrations/repofiles_delete_test.go +++ b/integrations/repofiles_delete_test.go @@ -10,6 +10,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" + "code.gitea.io/gitea/modules/git" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/test" files_service "code.gitea.io/gitea/services/repository/files" @@ -80,7 +81,7 @@ func testDeleteRepoFile(t *testing.T, u *url.URL) { opts := getDeleteRepoFileOptions(repo) t.Run("Delete README.md file", func(t *testing.T) { - fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts) + fileResponse, err := files_service.DeleteRepoFile(git.DefaultContext, repo, doer, opts) assert.NoError(t, err) expectedFileResponse := getExpectedDeleteFileResponse(u) assert.NotNil(t, fileResponse) @@ -92,7 +93,7 @@ func testDeleteRepoFile(t *testing.T, u *url.URL) { }) t.Run("Verify README.md has been deleted", func(t *testing.T) { - fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts) + fileResponse, err := files_service.DeleteRepoFile(git.DefaultContext, repo, doer, opts) assert.Nil(t, fileResponse) expectedError := "repository file does not exist [path: " + opts.TreePath + "]" assert.EqualError(t, err, expectedError) @@ -122,7 +123,7 @@ func testDeleteRepoFileWithoutBranchNames(t *testing.T, u *url.URL) { opts.NewBranch = "" t.Run("Delete README.md without Branch Name", func(t *testing.T) { - fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts) + fileResponse, err := files_service.DeleteRepoFile(git.DefaultContext, repo, doer, opts) assert.NoError(t, err) expectedFileResponse := getExpectedDeleteFileResponse(u) assert.NotNil(t, fileResponse) @@ -151,7 +152,7 @@ func TestDeleteRepoFileErrors(t *testing.T) { t.Run("Bad branch", func(t *testing.T) { opts := getDeleteRepoFileOptions(repo) opts.OldBranch = "bad_branch" - fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts) + fileResponse, err := files_service.DeleteRepoFile(git.DefaultContext, repo, doer, opts) assert.Error(t, err) assert.Nil(t, fileResponse) expectedError := "branch does not exist [name: " + opts.OldBranch + "]" @@ -162,7 +163,7 @@ func TestDeleteRepoFileErrors(t *testing.T) { opts := getDeleteRepoFileOptions(repo) origSHA := opts.SHA opts.SHA = "bad_sha" - fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts) + fileResponse, err := files_service.DeleteRepoFile(git.DefaultContext, repo, doer, opts) assert.Nil(t, fileResponse) assert.Error(t, err) expectedError := "sha does not match [given: " + opts.SHA + ", expected: " + origSHA + "]" @@ -172,7 +173,7 @@ func TestDeleteRepoFileErrors(t *testing.T) { t.Run("New branch already exists", func(t *testing.T) { opts := getDeleteRepoFileOptions(repo) opts.NewBranch = "develop" - fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts) + fileResponse, err := files_service.DeleteRepoFile(git.DefaultContext, repo, doer, opts) assert.Nil(t, fileResponse) assert.Error(t, err) expectedError := "branch already exists [name: " + opts.NewBranch + "]" @@ -182,7 +183,7 @@ func TestDeleteRepoFileErrors(t *testing.T) { t.Run("TreePath is empty:", func(t *testing.T) { opts := getDeleteRepoFileOptions(repo) opts.TreePath = "" - fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts) + fileResponse, err := files_service.DeleteRepoFile(git.DefaultContext, repo, doer, opts) assert.Nil(t, fileResponse) assert.Error(t, err) expectedError := "path contains a malformed path component [path: ]" @@ -192,7 +193,7 @@ func TestDeleteRepoFileErrors(t *testing.T) { t.Run("TreePath is a git directory:", func(t *testing.T) { opts := getDeleteRepoFileOptions(repo) opts.TreePath = ".git" - fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts) + fileResponse, err := files_service.DeleteRepoFile(git.DefaultContext, repo, doer, opts) assert.Nil(t, fileResponse) assert.Error(t, err) expectedError := "path contains a malformed path component [path: " + opts.TreePath + "]" diff --git a/integrations/repofiles_update_test.go b/integrations/repofiles_update_test.go index 7099cbdde9..97cdd67ba3 100644 --- a/integrations/repofiles_update_test.go +++ b/integrations/repofiles_update_test.go @@ -198,7 +198,7 @@ func TestCreateOrUpdateRepoFileForCreate(t *testing.T) { opts := getCreateRepoFileOptions(repo) // test - fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts) + fileResponse, err := files_service.CreateOrUpdateRepoFile(git.DefaultContext, repo, doer, opts) // asserts assert.NoError(t, err) @@ -234,7 +234,7 @@ func TestCreateOrUpdateRepoFileForUpdate(t *testing.T) { opts := getUpdateRepoFileOptions(repo) // test - fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts) + fileResponse, err := files_service.CreateOrUpdateRepoFile(git.DefaultContext, repo, doer, opts) // asserts assert.NoError(t, err) @@ -269,7 +269,7 @@ func TestCreateOrUpdateRepoFileForUpdateWithFileMove(t *testing.T) { opts.TreePath = "README_new.md" // new file name, README_new.md // test - fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts) + fileResponse, err := files_service.CreateOrUpdateRepoFile(git.DefaultContext, repo, doer, opts) // asserts assert.NoError(t, err) @@ -319,7 +319,7 @@ func TestCreateOrUpdateRepoFileWithoutBranchNames(t *testing.T) { opts.NewBranch = "" // test - fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts) + fileResponse, err := files_service.CreateOrUpdateRepoFile(git.DefaultContext, repo, doer, opts) // asserts assert.NoError(t, err) @@ -349,7 +349,7 @@ func TestCreateOrUpdateRepoFileErrors(t *testing.T) { t.Run("bad branch", func(t *testing.T) { opts := getUpdateRepoFileOptions(repo) opts.OldBranch = "bad_branch" - fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts) + fileResponse, err := files_service.CreateOrUpdateRepoFile(git.DefaultContext, repo, doer, opts) assert.Error(t, err) assert.Nil(t, fileResponse) expectedError := "branch does not exist [name: " + opts.OldBranch + "]" @@ -360,7 +360,7 @@ func TestCreateOrUpdateRepoFileErrors(t *testing.T) { opts := getUpdateRepoFileOptions(repo) origSHA := opts.SHA opts.SHA = "bad_sha" - fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts) + fileResponse, err := files_service.CreateOrUpdateRepoFile(git.DefaultContext, repo, doer, opts) assert.Nil(t, fileResponse) assert.Error(t, err) expectedError := "sha does not match [given: " + opts.SHA + ", expected: " + origSHA + "]" @@ -370,7 +370,7 @@ func TestCreateOrUpdateRepoFileErrors(t *testing.T) { t.Run("new branch already exists", func(t *testing.T) { opts := getUpdateRepoFileOptions(repo) opts.NewBranch = "develop" - fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts) + fileResponse, err := files_service.CreateOrUpdateRepoFile(git.DefaultContext, repo, doer, opts) assert.Nil(t, fileResponse) assert.Error(t, err) expectedError := "branch already exists [name: " + opts.NewBranch + "]" @@ -380,7 +380,7 @@ func TestCreateOrUpdateRepoFileErrors(t *testing.T) { t.Run("treePath is empty:", func(t *testing.T) { opts := getUpdateRepoFileOptions(repo) opts.TreePath = "" - fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts) + fileResponse, err := files_service.CreateOrUpdateRepoFile(git.DefaultContext, repo, doer, opts) assert.Nil(t, fileResponse) assert.Error(t, err) expectedError := "path contains a malformed path component [path: ]" @@ -390,7 +390,7 @@ func TestCreateOrUpdateRepoFileErrors(t *testing.T) { t.Run("treePath is a git directory:", func(t *testing.T) { opts := getUpdateRepoFileOptions(repo) opts.TreePath = ".git" - fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts) + fileResponse, err := files_service.CreateOrUpdateRepoFile(git.DefaultContext, repo, doer, opts) assert.Nil(t, fileResponse) assert.Error(t, err) expectedError := "path contains a malformed path component [path: " + opts.TreePath + "]" @@ -400,7 +400,7 @@ func TestCreateOrUpdateRepoFileErrors(t *testing.T) { t.Run("create file that already exists", func(t *testing.T) { opts := getCreateRepoFileOptions(repo) opts.TreePath = "README.md" //already exists - fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts) + fileResponse, err := files_service.CreateOrUpdateRepoFile(git.DefaultContext, repo, doer, opts) assert.Nil(t, fileResponse) assert.Error(t, err) expectedError := "repository file already exists [path: " + opts.TreePath + "]" diff --git a/models/admin/notice.go b/models/admin/notice.go index 2d71b426dd..daf095f680 100644 --- a/models/admin/notice.go +++ b/models/admin/notice.go @@ -56,6 +56,7 @@ func CreateNotice(ctx context.Context, tp NoticeType, desc string, args ...inter // CreateRepositoryNotice creates new system notice with type NoticeRepository. func CreateRepositoryNotice(desc string, args ...interface{}) error { + // Note we use the db.DefaultContext here rather than passing in a context as the context may be cancelled return CreateNotice(db.DefaultContext, NoticeRepository, desc, args...) } @@ -65,7 +66,8 @@ func RemoveAllWithNotice(ctx context.Context, title, path string) { if err := util.RemoveAll(path); err != nil { desc := fmt.Sprintf("%s [%s]: %v", title, path, err) log.Warn(title+" [%s]: %v", path, err) - if err = CreateNotice(ctx, NoticeRepository, desc); err != nil { + // Note we use the db.DefaultContext here rather than passing in a context as the context may be cancelled + if err = CreateNotice(db.DefaultContext, NoticeRepository, desc); err != nil { log.Error("CreateRepositoryNotice: %v", err) } } @@ -77,7 +79,9 @@ func RemoveStorageWithNotice(ctx context.Context, bucket storage.ObjectStorage, if err := bucket.Delete(path); err != nil { desc := fmt.Sprintf("%s [%s]: %v", title, path, err) log.Warn(title+" [%s]: %v", path, err) - if err = CreateNotice(ctx, NoticeRepository, desc); err != nil { + + // Note we use the db.DefaultContext here rather than passing in a context as the context may be cancelled + if err = CreateNotice(db.DefaultContext, NoticeRepository, desc); err != nil { log.Error("CreateRepositoryNotice: %v", err) } } diff --git a/models/db/context.go b/models/db/context.go index 55e38ba7e5..833c26ff6c 100644 --- a/models/db/context.go +++ b/models/db/context.go @@ -35,7 +35,7 @@ type Context struct { func WithEngine(ctx context.Context, e Engine) *Context { return &Context{ Context: ctx, - e: e, + e: e.Context(ctx), } } @@ -52,6 +52,11 @@ func (ctx *Context) Value(key interface{}) interface{} { return ctx.Context.Value(key) } +// WithContext returns this engine tied to this context +func (ctx *Context) WithContext(other context.Context) *Context { + return WithEngine(other, ctx.e) +} + // Engined structs provide an Engine type Engined interface { Engine() Engine diff --git a/models/db/engine.go b/models/db/engine.go index 63e1d5547a..665808d701 100755 --- a/models/db/engine.go +++ b/models/db/engine.go @@ -64,6 +64,7 @@ type Engine interface { Distinct(...string) *xorm.Session Query(...interface{}) ([]map[string][]byte, error) Cols(...string) *xorm.Session + Context(ctx context.Context) *xorm.Session } // TableInfo returns table's information via an object diff --git a/models/issue_comment.go b/models/issue_comment.go index f4a6b3ce13..bf36881a00 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -724,7 +724,7 @@ func (c *Comment) CodeCommentURL() string { } // LoadPushCommits Load push commits -func (c *Comment) LoadPushCommits() (err error) { +func (c *Comment) LoadPushCommits(ctx context.Context) (err error) { if c.Content == "" || c.Commits != nil || c.Type != CommentTypePullPush { return nil } @@ -746,11 +746,11 @@ func (c *Comment) LoadPushCommits() (err error) { c.NewCommit = data.CommitIDs[1] } else { repoPath := c.Issue.Repo.RepoPath() - gitRepo, err := git.OpenRepository(repoPath) + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repoPath) if err != nil { return err } - defer gitRepo.Close() + defer closer.Close() c.Commits = ConvertFromGitCommit(gitRepo.GetCommitsFromIDs(data.CommitIDs), c.Issue.Repo) c.CommitsNum = int64(len(c.Commits)) @@ -1272,6 +1272,7 @@ func findCodeComments(ctx context.Context, opts FindCommentsOptions, issue *Issu var err error if comment.RenderedContent, err = markdown.RenderString(&markup.RenderContext{ + Ctx: ctx, URLPrefix: issue.Repo.Link(), Metas: issue.Repo.ComposeMetas(), }, comment.Content); err != nil { @@ -1282,19 +1283,19 @@ func findCodeComments(ctx context.Context, opts FindCommentsOptions, issue *Issu } // FetchCodeCommentsByLine fetches the code comments for a given treePath and line number -func FetchCodeCommentsByLine(issue *Issue, currentUser *user_model.User, treePath string, line int64) ([]*Comment, error) { +func FetchCodeCommentsByLine(ctx context.Context, issue *Issue, currentUser *user_model.User, treePath string, line int64) ([]*Comment, error) { opts := FindCommentsOptions{ Type: CommentTypeCode, IssueID: issue.ID, TreePath: treePath, Line: line, } - return findCodeComments(db.DefaultContext, opts, issue, currentUser, nil) + return findCodeComments(ctx, opts, issue, currentUser, nil) } // FetchCodeComments will return a 2d-map: ["Path"]["Line"] = Comments at line -func FetchCodeComments(issue *Issue, currentUser *user_model.User) (CodeComments, error) { - return fetchCodeComments(db.DefaultContext, issue, currentUser) +func FetchCodeComments(ctx context.Context, issue *Issue, currentUser *user_model.User) (CodeComments, error) { + return fetchCodeComments(ctx, issue, currentUser) } // UpdateCommentsMigrationsByType updates comments' migrations information via given git service type and original id and poster id @@ -1318,7 +1319,7 @@ func UpdateCommentsMigrationsByType(tp structs.GitServiceType, originalAuthorID } // CreatePushPullComment create push code to pull base comment -func CreatePushPullComment(pusher *user_model.User, pr *PullRequest, oldCommitID, newCommitID string) (comment *Comment, err error) { +func CreatePushPullComment(ctx context.Context, pusher *user_model.User, pr *PullRequest, oldCommitID, newCommitID string) (comment *Comment, err error) { if pr.HasMerged || oldCommitID == "" || newCommitID == "" { return nil, nil } @@ -1331,7 +1332,7 @@ func CreatePushPullComment(pusher *user_model.User, pr *PullRequest, oldCommitID var data PushActionContent - data.CommitIDs, data.IsForcePush, err = getCommitIDsFromRepo(pr.BaseRepo, oldCommitID, newCommitID, pr.BaseBranch) + data.CommitIDs, data.IsForcePush, err = getCommitIDsFromRepo(ctx, pr.BaseRepo, oldCommitID, newCommitID, pr.BaseBranch) if err != nil { return nil, err } @@ -1353,13 +1354,13 @@ func CreatePushPullComment(pusher *user_model.User, pr *PullRequest, oldCommitID // getCommitsFromRepo get commit IDs from repo in between oldCommitID and newCommitID // isForcePush will be true if oldCommit isn't on the branch // Commit on baseBranch will skip -func getCommitIDsFromRepo(repo *repo_model.Repository, oldCommitID, newCommitID, baseBranch string) (commitIDs []string, isForcePush bool, err error) { +func getCommitIDsFromRepo(ctx context.Context, repo *repo_model.Repository, oldCommitID, newCommitID, baseBranch string) (commitIDs []string, isForcePush bool, err error) { repoPath := repo.RepoPath() - gitRepo, err := git.OpenRepository(repoPath) + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repoPath) if err != nil { return nil, false, err } - defer gitRepo.Close() + defer closer.Close() oldCommit, err := gitRepo.GetCommit(oldCommitID) if err != nil { diff --git a/models/issue_comment_test.go b/models/issue_comment_test.go index ec318688ee..d323a08167 100644 --- a/models/issue_comment_test.go +++ b/models/issue_comment_test.go @@ -8,6 +8,7 @@ import ( "testing" "time" + "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" @@ -49,7 +50,7 @@ func TestFetchCodeComments(t *testing.T) { issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue) user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User) - res, err := FetchCodeComments(issue, user) + res, err := FetchCodeComments(db.DefaultContext, issue, user) assert.NoError(t, err) assert.Contains(t, res, "README.md") assert.Contains(t, res["README.md"], int64(4)) @@ -57,7 +58,7 @@ func TestFetchCodeComments(t *testing.T) { assert.Equal(t, int64(4), res["README.md"][4][0].ID) user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User) - res, err = FetchCodeComments(issue, user2) + res, err = FetchCodeComments(db.DefaultContext, issue, user2) assert.NoError(t, err) assert.Len(t, res, 1) } diff --git a/models/issue_xref_test.go b/models/issue_xref_test.go index 9b1cb5e2d5..1deeb44ad5 100644 --- a/models/issue_xref_test.go +++ b/models/issue_xref_test.go @@ -162,7 +162,7 @@ func testCreatePR(t *testing.T, repo, doer int64, title, content string) *PullRe d := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: doer}).(*user_model.User) i := &Issue{RepoID: r.ID, PosterID: d.ID, Poster: d, Title: title, Content: content, IsPull: true} pr := &PullRequest{HeadRepoID: repo, BaseRepoID: repo, HeadBranch: "head", BaseBranch: "base", Status: PullRequestStatusMergeable} - assert.NoError(t, NewPullRequest(r, i, nil, nil, pr)) + assert.NoError(t, NewPullRequest(db.DefaultContext, r, i, nil, nil, pr)) pr.Issue = i return pr } diff --git a/models/migrations/v136.go b/models/migrations/v136.go index 101cf75955..94372e4142 100644 --- a/models/migrations/v136.go +++ b/models/migrations/v136.go @@ -12,6 +12,7 @@ import ( "time" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" @@ -90,7 +91,7 @@ func addCommitDivergenceToPulls(x *xorm.Engine) error { gitRefName := fmt.Sprintf("refs/pull/%d/head", pr.Index) - divergence, err := git.GetDivergingCommits(repoPath, pr.BaseBranch, gitRefName) + divergence, err := git.GetDivergingCommits(graceful.GetManager().HammerContext(), repoPath, pr.BaseBranch, gitRefName) if err != nil { log.Warn("Could not recalculate Divergence for pull: %d", pr.ID) pr.CommitsAhead = 0 diff --git a/models/migrations/v156.go b/models/migrations/v156.go index 7158d7bb6b..77aed3ff57 100644 --- a/models/migrations/v156.go +++ b/models/migrations/v156.go @@ -109,7 +109,7 @@ func fixPublisherIDforTagReleases(x *xorm.Engine) error { return err } } - gitRepo, err = git.OpenRepository(repoPath(repo.OwnerName, repo.Name)) + gitRepo, err = git.OpenRepositoryCtx(git.DefaultContext, repoPath(repo.OwnerName, repo.Name)) if err != nil { log.Error("Error whilst opening git repo for [%d]%s/%s. Error: %v", repo.ID, repo.OwnerName, repo.Name, err) return err diff --git a/models/migrations/v82.go b/models/migrations/v82.go index 67bb0cb8c9..7f9f979d95 100644 --- a/models/migrations/v82.go +++ b/models/migrations/v82.go @@ -99,7 +99,7 @@ func fixReleaseSha1OnReleaseTable(x *xorm.Engine) error { userCache[repo.OwnerID] = user } - gitRepo, err = git.OpenRepository(RepoPath(user.Name, repo.Name)) + gitRepo, err = git.OpenRepositoryCtx(git.DefaultContext, RepoPath(user.Name, repo.Name)) if err != nil { return err } diff --git a/models/pull.go b/models/pull.go index 243d40b1fa..d6bfbbf09f 100644 --- a/models/pull.go +++ b/models/pull.go @@ -436,7 +436,7 @@ func (pr *PullRequest) SetMerged() (bool, error) { } // NewPullRequest creates new pull request with labels for repository. -func NewPullRequest(repo *repo_model.Repository, issue *Issue, labelIDs []int64, uuids []string, pr *PullRequest) (err error) { +func NewPullRequest(outerCtx context.Context, repo *repo_model.Repository, issue *Issue, labelIDs []int64, uuids []string, pr *PullRequest) (err error) { idx, err := db.GetNextResourceIndex("issue_index", repo.ID) if err != nil { return fmt.Errorf("generate pull request index failed: %v", err) @@ -449,6 +449,7 @@ func NewPullRequest(repo *repo_model.Repository, issue *Issue, labelIDs []int64, return err } defer committer.Close() + ctx.WithContext(outerCtx) if err = newIssue(ctx, issue.Poster, NewIssueOptions{ Repo: repo, diff --git a/models/repo/repo.go b/models/repo/repo.go index 5108231cd8..a78d287315 100644 --- a/models/repo/repo.go +++ b/models/repo/repo.go @@ -485,8 +485,9 @@ func (repo *Repository) CanEnableEditor() bool { } // DescriptionHTML does special handles to description and return HTML string. -func (repo *Repository) DescriptionHTML() template.HTML { +func (repo *Repository) DescriptionHTML(ctx context.Context) template.HTML { desc, err := markup.RenderDescriptionHTML(&markup.RenderContext{ + Ctx: ctx, URLPrefix: repo.HTMLURL(), Metas: repo.ComposeMetas(), }, repo.Description) diff --git a/models/repo_activity.go b/models/repo_activity.go index 5bdea80b97..7475be2b11 100644 --- a/models/repo_activity.go +++ b/models/repo_activity.go @@ -5,6 +5,7 @@ package models import ( + "context" "fmt" "sort" "time" @@ -43,7 +44,7 @@ type ActivityStats struct { } // GetActivityStats return stats for repository at given time range -func GetActivityStats(repo *repo_model.Repository, timeFrom time.Time, releases, issues, prs, code bool) (*ActivityStats, error) { +func GetActivityStats(ctx context.Context, repo *repo_model.Repository, timeFrom time.Time, releases, issues, prs, code bool) (*ActivityStats, error) { stats := &ActivityStats{Code: &git.CodeActivityStats{}} if releases { if err := stats.FillReleases(repo.ID, timeFrom); err != nil { @@ -64,11 +65,11 @@ func GetActivityStats(repo *repo_model.Repository, timeFrom time.Time, releases, return nil, fmt.Errorf("FillUnresolvedIssues: %v", err) } if code { - gitRepo, err := git.OpenRepository(repo.RepoPath()) + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) if err != nil { return nil, fmt.Errorf("OpenRepository: %v", err) } - defer gitRepo.Close() + defer closer.Close() code, err := gitRepo.GetCodeActivityStats(timeFrom, repo.DefaultBranch) if err != nil { @@ -80,12 +81,12 @@ func GetActivityStats(repo *repo_model.Repository, timeFrom time.Time, releases, } // GetActivityStatsTopAuthors returns top author stats for git commits for all branches -func GetActivityStatsTopAuthors(repo *repo_model.Repository, timeFrom time.Time, count int) ([]*ActivityAuthorData, error) { - gitRepo, err := git.OpenRepository(repo.RepoPath()) +func GetActivityStatsTopAuthors(ctx context.Context, repo *repo_model.Repository, timeFrom time.Time, count int) ([]*ActivityAuthorData, error) { + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) if err != nil { return nil, fmt.Errorf("OpenRepository: %v", err) } - defer gitRepo.Close() + defer closer.Close() code, err := gitRepo.GetCodeActivityStats(timeFrom, "") if err != nil { diff --git a/models/review.go b/models/review.go index 88d0819089..8b0092b1ed 100644 --- a/models/review.go +++ b/models/review.go @@ -86,7 +86,8 @@ func init() { db.RegisterModel(new(Review)) } -func (r *Review) loadCodeComments(ctx context.Context) (err error) { +// LoadCodeComments loads CodeComments +func (r *Review) LoadCodeComments(ctx context.Context) (err error) { if r.CodeComments != nil { return } @@ -97,11 +98,6 @@ func (r *Review) loadCodeComments(ctx context.Context) (err error) { return } -// LoadCodeComments loads CodeComments -func (r *Review) LoadCodeComments() error { - return r.loadCodeComments(db.DefaultContext) -} - func (r *Review) loadIssue(e db.Engine) (err error) { if r.Issue != nil { return @@ -137,12 +133,13 @@ func (r *Review) LoadReviewerTeam() error { return r.loadReviewerTeam(db.GetEngine(db.DefaultContext)) } -func (r *Review) loadAttributes(ctx context.Context) (err error) { +// LoadAttributes loads all attributes except CodeComments +func (r *Review) LoadAttributes(ctx context.Context) (err error) { e := db.GetEngine(ctx) if err = r.loadIssue(e); err != nil { return } - if err = r.loadCodeComments(ctx); err != nil { + if err = r.LoadCodeComments(ctx); err != nil { return } if err = r.loadReviewer(e); err != nil { @@ -154,11 +151,6 @@ func (r *Review) loadAttributes(ctx context.Context) (err error) { return } -// LoadAttributes loads all attributes except CodeComments -func (r *Review) LoadAttributes() error { - return r.loadAttributes(db.DefaultContext) -} - func getReviewByID(e db.Engine, id int64) (*Review, error) { review := new(Review) if has, err := e.ID(id).Get(review); err != nil { @@ -405,7 +397,7 @@ func SubmitReview(doer *user_model.User, issue *Issue, reviewType ReviewType, co return nil, nil, err } } else { - if err := review.loadCodeComments(ctx); err != nil { + if err := review.LoadCodeComments(ctx); err != nil { return nil, nil, err } if reviewType != ReviewTypeApprove && len(review.CodeComments) == 0 && len(strings.TrimSpace(content)) == 0 { diff --git a/models/review_test.go b/models/review_test.go index b9d156ceb1..20810dac4a 100644 --- a/models/review_test.go +++ b/models/review_test.go @@ -7,6 +7,7 @@ package models import ( "testing" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" @@ -28,23 +29,23 @@ func TestGetReviewByID(t *testing.T) { func TestReview_LoadAttributes(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) review := unittest.AssertExistsAndLoadBean(t, &Review{ID: 1}).(*Review) - assert.NoError(t, review.LoadAttributes()) + assert.NoError(t, review.LoadAttributes(db.DefaultContext)) assert.NotNil(t, review.Issue) assert.NotNil(t, review.Reviewer) invalidReview1 := unittest.AssertExistsAndLoadBean(t, &Review{ID: 2}).(*Review) - assert.Error(t, invalidReview1.LoadAttributes()) + assert.Error(t, invalidReview1.LoadAttributes(db.DefaultContext)) invalidReview2 := unittest.AssertExistsAndLoadBean(t, &Review{ID: 3}).(*Review) - assert.Error(t, invalidReview2.LoadAttributes()) + assert.Error(t, invalidReview2.LoadAttributes(db.DefaultContext)) } func TestReview_LoadCodeComments(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) review := unittest.AssertExistsAndLoadBean(t, &Review{ID: 4}).(*Review) - assert.NoError(t, review.LoadAttributes()) - assert.NoError(t, review.LoadCodeComments()) + assert.NoError(t, review.LoadAttributes(db.DefaultContext)) + assert.NoError(t, review.LoadCodeComments(db.DefaultContext)) assert.Len(t, review.CodeComments, 1) assert.Equal(t, int64(4), review.CodeComments["README.md"][int64(4)][0].Line) } 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) diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 7a2347650a..7a723a138a 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -778,10 +778,10 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route { m.Combo("/forks").Get(repo.ListForks). Post(reqToken(), reqRepoReader(unit.TypeCode), bind(api.CreateForkOption{}), repo.CreateFork) m.Group("/branches", func() { - m.Get("", repo.ListBranches) - m.Get("/*", repo.GetBranch) - m.Delete("/*", context.ReferencesGitRepo(false), reqRepoWriter(unit.TypeCode), repo.DeleteBranch) - m.Post("", reqRepoWriter(unit.TypeCode), bind(api.CreateBranchRepoOption{}), repo.CreateBranch) + m.Get("", context.ReferencesGitRepo(false), repo.ListBranches) + m.Get("/*", context.ReferencesGitRepo(false), repo.GetBranch) + m.Delete("/*", reqRepoWriter(unit.TypeCode), context.ReferencesGitRepo(false), repo.DeleteBranch) + m.Post("", reqRepoWriter(unit.TypeCode), context.ReferencesGitRepo(false), bind(api.CreateBranchRepoOption{}), repo.CreateBranch) }, reqRepoReader(unit.TypeCode)) m.Group("/branch_protections", func() { m.Get("", repo.ListBranchProtections) @@ -957,7 +957,7 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route { Post(reqToken(), bind(api.CreateStatusOption{}), repo.NewCommitStatus) }, reqRepoReader(unit.TypeCode)) m.Group("/commits", func() { - m.Get("", repo.GetAllCommits) + m.Get("", context.ReferencesGitRepo(false), repo.GetAllCommits) m.Group("/{ref}", func() { m.Get("/status", repo.GetCombinedCommitStatusByRef) m.Get("/statuses", repo.GetCommitStatusesByRef) @@ -965,7 +965,7 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route { }, reqRepoReader(unit.TypeCode)) m.Group("/git", func() { m.Group("/commits", func() { - m.Get("/{sha}", repo.GetSingleCommit) + m.Get("/{sha}", context.ReferencesGitRepo(false), repo.GetSingleCommit) m.Get("/{sha}.{diffType:diff|patch}", repo.DownloadCommitDiffOrPatch) }) m.Get("/refs", repo.GetGitAllRefs) diff --git a/routers/api/v1/misc/markdown.go b/routers/api/v1/misc/markdown.go index f1007b7ee2..eb0b78a5ba 100644 --- a/routers/api/v1/misc/markdown.go +++ b/routers/api/v1/misc/markdown.go @@ -78,6 +78,7 @@ func Markdown(ctx *context.APIContext) { } if err := markdown.Render(&markup.RenderContext{ + Ctx: ctx, URLPrefix: urlPrefix, Metas: meta, IsWiki: form.Wiki, @@ -87,6 +88,7 @@ func Markdown(ctx *context.APIContext) { } default: if err := markdown.RenderRaw(&markup.RenderContext{ + Ctx: ctx, URLPrefix: form.Context, }, strings.NewReader(form.Text), ctx.Resp); err != nil { ctx.InternalServerError(err) @@ -117,7 +119,9 @@ func MarkdownRaw(ctx *context.APIContext) { // "422": // "$ref": "#/responses/validationError" defer ctx.Req.Body.Close() - if err := markdown.RenderRaw(&markup.RenderContext{}, ctx.Req.Body, ctx.Resp); err != nil { + if err := markdown.RenderRaw(&markup.RenderContext{ + Ctx: ctx, + }, ctx.Req.Body, ctx.Resp); err != nil { ctx.InternalServerError(err) return } diff --git a/routers/api/v1/misc/signing.go b/routers/api/v1/misc/signing.go index e2833173a3..b99e560ccf 100644 --- a/routers/api/v1/misc/signing.go +++ b/routers/api/v1/misc/signing.go @@ -52,7 +52,7 @@ func SigningKey(ctx *context.APIContext) { path = ctx.Repo.Repository.RepoPath() } - content, err := asymkey_service.PublicSigningKey(path) + content, err := asymkey_service.PublicSigningKey(ctx, path) if err != nil { ctx.Error(http.StatusInternalServerError, "gpg export", err) return diff --git a/routers/api/v1/repo/blob.go b/routers/api/v1/repo/blob.go index e3ce0e1916..19d893a68b 100644 --- a/routers/api/v1/repo/blob.go +++ b/routers/api/v1/repo/blob.go @@ -45,7 +45,7 @@ func GetBlob(ctx *context.APIContext) { ctx.Error(http.StatusBadRequest, "", "sha not provided") return } - if blob, err := files_service.GetBlobBySHA(ctx.Repo.Repository, sha); err != nil { + if blob, err := files_service.GetBlobBySHA(ctx, ctx.Repo.Repository, sha); err != nil { ctx.Error(http.StatusBadRequest, "", err) } else { ctx.JSON(http.StatusOK, blob) diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index 56266b406a..c5480bf2c5 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -53,7 +53,7 @@ func GetBranch(ctx *context.APIContext) { branchName := ctx.Params("*") - branch, err := repo_service.GetBranch(ctx.Repo.Repository, branchName) + branch, err := ctx.Repo.GitRepo.GetBranch(branchName) if err != nil { if git.IsErrBranchNotExist(err) { ctx.NotFound(err) @@ -176,7 +176,7 @@ func CreateBranch(ctx *context.APIContext) { opt.OldBranchName = ctx.Repo.Repository.DefaultBranch } - err := repo_service.CreateNewBranch(ctx.User, ctx.Repo.Repository, opt.OldBranchName, opt.BranchName) + err := repo_service.CreateNewBranch(ctx, ctx.User, ctx.Repo.Repository, opt.OldBranchName, opt.BranchName) if err != nil { if models.IsErrBranchDoesNotExist(err) { @@ -198,7 +198,7 @@ func CreateBranch(ctx *context.APIContext) { return } - branch, err := repo_service.GetBranch(ctx.Repo.Repository, opt.BranchName) + branch, err := ctx.Repo.GitRepo.GetBranch(opt.BranchName) if err != nil { ctx.Error(http.StatusInternalServerError, "GetBranch", err) return @@ -257,7 +257,7 @@ func ListBranches(ctx *context.APIContext) { listOptions := utils.GetListOptions(ctx) skip, _ := listOptions.GetStartEnd() - branches, totalNumOfBranches, err := repo_service.GetBranches(ctx.Repo.Repository, skip, listOptions.PageSize) + branches, totalNumOfBranches, err := ctx.Repo.GitRepo.GetBranches(skip, listOptions.PageSize) if err != nil { ctx.Error(http.StatusInternalServerError, "GetBranches", err) return diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index a2b4298602..b6c47e0685 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -62,13 +62,7 @@ func GetSingleCommit(ctx *context.APIContext) { } func getCommit(ctx *context.APIContext, identifier string) { - gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath()) - if err != nil { - ctx.Error(http.StatusInternalServerError, "OpenRepository", err) - return - } - defer gitRepo.Close() - commit, err := gitRepo.GetCommit(identifier) + commit, err := ctx.Repo.GitRepo.GetCommit(identifier) if err != nil { if git.IsErrNotExist(err) { ctx.NotFound(identifier) @@ -78,7 +72,7 @@ func getCommit(ctx *context.APIContext, identifier string) { return } - json, err := convert.ToCommit(ctx.Repo.Repository, commit, nil) + json, err := convert.ToCommit(ctx.Repo.Repository, ctx.Repo.GitRepo, commit, nil) if err != nil { ctx.Error(http.StatusInternalServerError, "toCommit", err) return @@ -136,13 +130,6 @@ func GetAllCommits(ctx *context.APIContext) { return } - gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath()) - if err != nil { - ctx.Error(http.StatusInternalServerError, "OpenRepository", err) - return - } - defer gitRepo.Close() - listOptions := utils.GetListOptions(ctx) if listOptions.Page <= 0 { listOptions.Page = 1 @@ -158,26 +145,27 @@ func GetAllCommits(ctx *context.APIContext) { var ( commitsCountTotal int64 commits []*git.Commit + err error ) if len(path) == 0 { var baseCommit *git.Commit if len(sha) == 0 { // no sha supplied - use default branch - head, err := gitRepo.GetHEADBranch() + head, err := ctx.Repo.GitRepo.GetHEADBranch() if err != nil { ctx.Error(http.StatusInternalServerError, "GetHEADBranch", err) return } - baseCommit, err = gitRepo.GetBranchCommit(head.Name) + baseCommit, err = ctx.Repo.GitRepo.GetBranchCommit(head.Name) if err != nil { ctx.Error(http.StatusInternalServerError, "GetCommit", err) return } } else { // get commit specified by sha - baseCommit, err = gitRepo.GetCommit(sha) + baseCommit, err = ctx.Repo.GitRepo.GetCommit(sha) if err != nil { ctx.Error(http.StatusInternalServerError, "GetCommit", err) return @@ -202,7 +190,7 @@ func GetAllCommits(ctx *context.APIContext) { sha = ctx.Repo.Repository.DefaultBranch } - commitsCountTotal, err = gitRepo.FileCommitsCount(sha, path) + commitsCountTotal, err = ctx.Repo.GitRepo.FileCommitsCount(sha, path) if err != nil { ctx.Error(http.StatusInternalServerError, "FileCommitsCount", err) return @@ -211,7 +199,7 @@ func GetAllCommits(ctx *context.APIContext) { return } - commits, err = gitRepo.CommitsByFileAndRange(sha, path, listOptions.Page) + commits, err = ctx.Repo.GitRepo.CommitsByFileAndRange(sha, path, listOptions.Page) if err != nil { ctx.Error(http.StatusInternalServerError, "CommitsByFileAndRange", err) return @@ -225,7 +213,7 @@ func GetAllCommits(ctx *context.APIContext) { apiCommits := make([]*api.Commit, len(commits)) for i, commit := range commits { // Create json struct - apiCommits[i], err = convert.ToCommit(ctx.Repo.Repository, commit, userCache) + apiCommits[i], err = convert.ToCommit(ctx.Repo.Repository, ctx.Repo.GitRepo, commit, userCache) if err != nil { ctx.Error(http.StatusInternalServerError, "toCommit", err) return @@ -282,6 +270,7 @@ func DownloadCommitDiffOrPatch(ctx *context.APIContext) { // "$ref": "#/responses/notFound" repoPath := repo_model.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name) if err := git.GetRawDiff( + ctx, repoPath, ctx.Params(":sha"), git.RawDiffType(ctx.Params(":diffType")), diff --git a/routers/api/v1/repo/file.go b/routers/api/v1/repo/file.go index f02c7ea345..a27e383bc3 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -122,7 +122,7 @@ func GetArchive(ctx *context.APIContext) { repoPath := repo_model.RepoPath(ctx.Params(":username"), ctx.Params(":reponame")) if ctx.Repo.GitRepo == nil { - gitRepo, err := git.OpenRepository(repoPath) + gitRepo, err := git.OpenRepositoryCtx(ctx, repoPath) if err != nil { ctx.Error(http.StatusInternalServerError, "OpenRepository", err) return @@ -402,7 +402,7 @@ func createOrUpdateFile(ctx *context.APIContext, opts *files_service.UpdateRepoF } opts.Content = string(content) - return files_service.CreateOrUpdateRepoFile(ctx.Repo.Repository, ctx.User, opts) + return files_service.CreateOrUpdateRepoFile(ctx, ctx.Repo.Repository, ctx.User, opts) } // DeleteFile Delete a file in a repository @@ -489,7 +489,7 @@ func DeleteFile(ctx *context.APIContext) { opts.Message = ctx.Tr("repo.editor.delete", opts.TreePath) } - if fileResponse, err := files_service.DeleteRepoFile(ctx.Repo.Repository, ctx.User, opts); err != nil { + if fileResponse, err := files_service.DeleteRepoFile(ctx, ctx.Repo.Repository, ctx.User, opts); err != nil { if git.IsErrBranchNotExist(err) || models.IsErrRepoFileDoesNotExist(err) || git.IsErrNotExist(err) { ctx.Error(http.StatusNotFound, "DeleteFile", err) return @@ -555,7 +555,7 @@ func GetContents(ctx *context.APIContext) { treePath := ctx.Params("*") ref := ctx.FormTrim("ref") - if fileList, err := files_service.GetContentsOrList(ctx.Repo.Repository, treePath, ref); err != nil { + if fileList, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref); err != nil { if git.IsErrNotExist(err) { ctx.NotFound("GetContentsOrList", err) return diff --git a/routers/api/v1/repo/notes.go b/routers/api/v1/repo/notes.go index a5f9512983..edcb27b1ec 100644 --- a/routers/api/v1/repo/notes.go +++ b/routers/api/v1/repo/notes.go @@ -55,7 +55,7 @@ func GetNote(ctx *context.APIContext) { } func getNote(ctx *context.APIContext, identifier string) { - gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath()) + gitRepo, err := git.OpenRepositoryCtx(ctx, ctx.Repo.Repository.RepoPath()) if err != nil { ctx.Error(http.StatusInternalServerError, "OpenRepository", err) return @@ -72,7 +72,7 @@ func getNote(ctx *context.APIContext, identifier string) { return } - cmt, err := convert.ToCommit(ctx.Repo.Repository, note.Commit, nil) + cmt, err := convert.ToCommit(ctx.Repo.Repository, gitRepo, note.Commit, nil) if err != nil { ctx.Error(http.StatusInternalServerError, "ToCommit", err) return diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 158abd4651..52ff8425de 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -119,7 +119,7 @@ func ListPullRequests(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "LoadHeadRepo", err) return } - apiPrs[i] = convert.ToAPIPullRequest(prs[i], ctx.User) + apiPrs[i] = convert.ToAPIPullRequest(ctx, prs[i], ctx.User) } ctx.SetLinkHeader(int(maxResults), listOptions.PageSize) @@ -175,7 +175,7 @@ func GetPullRequest(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "LoadHeadRepo", err) return } - ctx.JSON(http.StatusOK, convert.ToAPIPullRequest(pr, ctx.User)) + ctx.JSON(http.StatusOK, convert.ToAPIPullRequest(ctx, pr, ctx.User)) } // DownloadPullDiffOrPatch render a pull's raw diff or patch @@ -235,7 +235,7 @@ func DownloadPullDiffOrPatch(ctx *context.APIContext) { binary := ctx.FormBool("binary") - if err := pull_service.DownloadDiffOrPatch(pr, ctx, patch, binary); err != nil { + if err := pull_service.DownloadDiffOrPatch(ctx, pr, ctx, patch, binary); err != nil { ctx.InternalServerError(err) return } @@ -411,7 +411,7 @@ func CreatePullRequest(ctx *context.APIContext) { } } - if err := pull_service.NewPullRequest(repo, prIssue, labelIDs, []string{}, pr, assigneeIDs); err != nil { + if err := pull_service.NewPullRequest(ctx, repo, prIssue, labelIDs, []string{}, pr, assigneeIDs); err != nil { if models.IsErrUserDoesNotHaveAccessToRepo(err) { ctx.Error(http.StatusBadRequest, "UserDoesNotHaveAccessToRepo", err) return @@ -421,7 +421,7 @@ func CreatePullRequest(ctx *context.APIContext) { } log.Trace("Pull request created: %d/%d", repo.ID, prIssue.ID) - ctx.JSON(http.StatusCreated, convert.ToAPIPullRequest(pr, ctx.User)) + ctx.JSON(http.StatusCreated, convert.ToAPIPullRequest(ctx, pr, ctx.User)) } // EditPullRequest does what it says @@ -598,7 +598,7 @@ func EditPullRequest(ctx *context.APIContext) { ctx.Error(http.StatusNotFound, "NewBaseBranchNotExist", fmt.Errorf("new base '%s' not exist", form.Base)) return } - if err := pull_service.ChangeTargetBranch(pr, ctx.User, form.Base); err != nil { + if err := pull_service.ChangeTargetBranch(ctx, pr, ctx.User, form.Base); err != nil { if models.IsErrPullRequestAlreadyExists(err) { ctx.Error(http.StatusConflict, "IsErrPullRequestAlreadyExists", err) return @@ -628,7 +628,7 @@ func EditPullRequest(ctx *context.APIContext) { } // TODO this should be 200, not 201 - ctx.JSON(http.StatusCreated, convert.ToAPIPullRequest(pr, ctx.User)) + ctx.JSON(http.StatusCreated, convert.ToAPIPullRequest(ctx, pr, ctx.User)) } // IsPullRequestMerged checks if a PR exists given an index @@ -792,7 +792,7 @@ func MergePullRequest(ctx *context.APIContext) { return } - if err := pull_service.CheckPRReadyToMerge(pr, false); err != nil { + if err := pull_service.CheckPRReadyToMerge(ctx, pr, false); err != nil { if !models.IsErrNotAllowedToMerge(err) { ctx.Error(http.StatusInternalServerError, "CheckPRReadyToMerge", err) return @@ -810,7 +810,7 @@ func MergePullRequest(ctx *context.APIContext) { } } - if _, err := pull_service.IsSignedIfRequired(pr, ctx.User); err != nil { + if _, err := pull_service.IsSignedIfRequired(ctx, pr, ctx.User); err != nil { if !asymkey_service.IsErrWontSign(err) { ctx.Error(http.StatusInternalServerError, "IsSignedIfRequired", err) return @@ -838,7 +838,7 @@ func MergePullRequest(ctx *context.APIContext) { message += "\n\n" + form.MergeMessageField } - if err := pull_service.Merge(pr, ctx.User, ctx.Repo.GitRepo, repo_model.MergeStyle(form.Do), form.HeadCommitID, message); err != nil { + if err := pull_service.Merge(ctx, pr, ctx.User, ctx.Repo.GitRepo, repo_model.MergeStyle(form.Do), form.HeadCommitID, message); err != nil { if models.IsErrInvalidMergeStyle(err) { ctx.Error(http.StatusMethodNotAllowed, "Invalid merge style", fmt.Errorf("%s is not allowed an allowed merge style for this repository", repo_model.MergeStyle(form.Do))) return @@ -888,7 +888,7 @@ func MergePullRequest(ctx *context.APIContext) { if ctx.Repo != nil && ctx.Repo.Repository != nil && ctx.Repo.Repository.ID == pr.HeadRepoID && ctx.Repo.GitRepo != nil { headRepo = ctx.Repo.GitRepo } else { - headRepo, err = git.OpenRepository(pr.HeadRepo.RepoPath()) + headRepo, err = git.OpenRepositoryCtx(ctx, pr.HeadRepo.RepoPath()) if err != nil { ctx.ServerError(fmt.Sprintf("OpenRepository[%s]", pr.HeadRepo.RepoPath()), err) return @@ -982,7 +982,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) headRepo = ctx.Repo.Repository headGitRepo = ctx.Repo.GitRepo } else { - headGitRepo, err = git.OpenRepository(repo_model.RepoPath(headUser.Name, headRepo.Name)) + headGitRepo, err = git.OpenRepositoryCtx(ctx, repo_model.RepoPath(headUser.Name, headRepo.Name)) if err != nil { ctx.Error(http.StatusInternalServerError, "OpenRepository", err) return nil, nil, nil, nil, "", "" @@ -1135,7 +1135,7 @@ func UpdatePullRequest(ctx *context.APIContext) { // default merge commit message message := fmt.Sprintf("Merge branch '%s' into %s", pr.BaseBranch, pr.HeadBranch) - if err = pull_service.Update(pr, ctx.User, message, rebase); err != nil { + if err = pull_service.Update(ctx, pr, ctx.User, message, rebase); err != nil { if models.IsErrMergeConflicts(err) { ctx.Error(http.StatusConflict, "Update", "merge failed because of conflict") return @@ -1204,12 +1204,13 @@ func GetPullRequestCommits(ctx *context.APIContext) { } var prInfo *git.CompareInfo - baseGitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath()) + baseGitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, pr.BaseRepo.RepoPath()) if err != nil { ctx.ServerError("OpenRepository", err) return } - defer baseGitRepo.Close() + defer closer.Close() + if pr.HasMerged { prInfo, err = baseGitRepo.GetCompareInfo(pr.BaseRepo.RepoPath(), pr.MergeBase, pr.GetGitRefName(), false, false) } else { @@ -1236,7 +1237,7 @@ func GetPullRequestCommits(ctx *context.APIContext) { apiCommits := make([]*api.Commit, 0, end-start) for i := start; i < end; i++ { - apiCommit, err := convert.ToCommit(ctx.Repo.Repository, commits[i], userCache) + apiCommit, err := convert.ToCommit(ctx.Repo.Repository, baseGitRepo, commits[i], userCache) if err != nil { ctx.ServerError("toCommit", err) return diff --git a/routers/api/v1/repo/pull_review.go b/routers/api/v1/repo/pull_review.go index ec5f045647..9eb63bafad 100644 --- a/routers/api/v1/repo/pull_review.go +++ b/routers/api/v1/repo/pull_review.go @@ -97,7 +97,7 @@ func ListPullReviews(ctx *context.APIContext) { return } - apiReviews, err := convert.ToPullReviewList(allReviews, ctx.User) + apiReviews, err := convert.ToPullReviewList(ctx, allReviews, ctx.User) if err != nil { ctx.Error(http.StatusInternalServerError, "convertToPullReviewList", err) return @@ -148,7 +148,7 @@ func GetPullReview(ctx *context.APIContext) { return } - apiReview, err := convert.ToPullReview(review, ctx.User) + apiReview, err := convert.ToPullReview(ctx, review, ctx.User) if err != nil { ctx.Error(http.StatusInternalServerError, "convertToPullReview", err) return @@ -198,7 +198,7 @@ func GetPullReviewComments(ctx *context.APIContext) { return } - apiComments, err := convert.ToPullReviewCommentList(review, ctx.User) + apiComments, err := convert.ToPullReviewCommentList(ctx, review, ctx.User) if err != nil { ctx.Error(http.StatusInternalServerError, "convertToPullReviewCommentList", err) return @@ -328,12 +328,13 @@ func CreatePullReview(ctx *context.APIContext) { // if CommitID is empty, set it as lastCommitID if opts.CommitID == "" { - gitRepo, err := git.OpenRepository(pr.Issue.Repo.RepoPath()) + + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, pr.Issue.Repo.RepoPath()) if err != nil { ctx.Error(http.StatusInternalServerError, "git.OpenRepository", err) return } - defer gitRepo.Close() + defer closer.Close() headCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName()) if err != nil { @@ -351,7 +352,7 @@ func CreatePullReview(ctx *context.APIContext) { line = c.OldLineNum * -1 } - if _, err := pull_service.CreateCodeComment( + if _, err := pull_service.CreateCodeComment(ctx, ctx.User, ctx.Repo.GitRepo, pr.Issue, @@ -368,14 +369,14 @@ func CreatePullReview(ctx *context.APIContext) { } // create review and associate all pending review comments - review, _, err := pull_service.SubmitReview(ctx.User, ctx.Repo.GitRepo, pr.Issue, reviewType, opts.Body, opts.CommitID, nil) + review, _, err := pull_service.SubmitReview(ctx, ctx.User, ctx.Repo.GitRepo, pr.Issue, reviewType, opts.Body, opts.CommitID, nil) if err != nil { ctx.Error(http.StatusInternalServerError, "SubmitReview", err) return } // convert response - apiReview, err := convert.ToPullReview(review, ctx.User) + apiReview, err := convert.ToPullReview(ctx, review, ctx.User) if err != nil { ctx.Error(http.StatusInternalServerError, "convertToPullReview", err) return @@ -456,14 +457,14 @@ func SubmitPullReview(ctx *context.APIContext) { } // create review and associate all pending review comments - review, _, err = pull_service.SubmitReview(ctx.User, ctx.Repo.GitRepo, pr.Issue, reviewType, opts.Body, headCommitID, nil) + review, _, err = pull_service.SubmitReview(ctx, ctx.User, ctx.Repo.GitRepo, pr.Issue, reviewType, opts.Body, headCommitID, nil) if err != nil { ctx.Error(http.StatusInternalServerError, "SubmitReview", err) return } // convert response - apiReview, err := convert.ToPullReview(review, ctx.User) + apiReview, err := convert.ToPullReview(ctx, review, ctx.User) if err != nil { ctx.Error(http.StatusInternalServerError, "convertToPullReview", err) return @@ -555,7 +556,7 @@ func prepareSingleReview(ctx *context.APIContext) (*models.Review, *models.PullR return nil, nil, true } - if err := review.LoadAttributes(); err != nil && !user_model.IsErrUserNotExist(err) { + if err := review.LoadAttributes(ctx); err != nil && !user_model.IsErrUserNotExist(err) { ctx.Error(http.StatusInternalServerError, "ReviewLoadAttributes", err) return nil, nil, true } @@ -765,7 +766,7 @@ func apiReviewRequest(ctx *context.APIContext, opts api.PullReviewRequestOptions } if isAdd { - apiReviews, err := convert.ToPullReviewList(reviews, ctx.User) + apiReviews, err := convert.ToPullReviewList(ctx, reviews, ctx.User) if err != nil { ctx.Error(http.StatusInternalServerError, "convertToPullReviewList", err) return @@ -883,7 +884,7 @@ func dismissReview(ctx *context.APIContext, msg string, isDismiss bool) { return } - _, err := pull_service.DismissReview(review.ID, msg, ctx.User, isDismiss) + _, err := pull_service.DismissReview(ctx, review.ID, msg, ctx.User, isDismiss) if err != nil { ctx.Error(http.StatusInternalServerError, "pull_service.DismissReview", err) return @@ -895,7 +896,7 @@ func dismissReview(ctx *context.APIContext, msg string, isDismiss bool) { } // convert response - apiReview, err := convert.ToPullReview(review, ctx.User) + apiReview, err := convert.ToPullReview(ctx, review, ctx.User) if err != nil { ctx.Error(http.StatusInternalServerError, "convertToPullReview", err) return diff --git a/routers/api/v1/repo/release.go b/routers/api/v1/repo/release.go index 299eaddbc8..3cdd798151 100644 --- a/routers/api/v1/repo/release.go +++ b/routers/api/v1/repo/release.go @@ -356,7 +356,7 @@ func DeleteRelease(ctx *context.APIContext) { ctx.NotFound() return } - if err := releaseservice.DeleteReleaseByID(id, ctx.User, false); err != nil { + if err := releaseservice.DeleteReleaseByID(ctx, id, ctx.User, false); err != nil { ctx.Error(http.StatusInternalServerError, "DeleteReleaseByID", err) return } diff --git a/routers/api/v1/repo/release_tags.go b/routers/api/v1/repo/release_tags.go index 4b853d44bb..d77bdf0331 100644 --- a/routers/api/v1/repo/release_tags.go +++ b/routers/api/v1/repo/release_tags.go @@ -110,7 +110,7 @@ func DeleteReleaseByTag(ctx *context.APIContext) { return } - if err = releaseservice.DeleteReleaseByID(release.ID, ctx.User, false); err != nil { + if err = releaseservice.DeleteReleaseByID(ctx, release.ID, ctx.User, false); err != nil { ctx.Error(http.StatusInternalServerError, "DeleteReleaseByID", err) } diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index 340ba0f6d5..5f17dc9268 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -704,7 +704,7 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err if ctx.Repo.GitRepo == nil && !repo.IsEmpty { var err error - ctx.Repo.GitRepo, err = git.OpenRepository(ctx.Repo.Repository.RepoPath()) + ctx.Repo.GitRepo, err = git.OpenRepositoryCtx(ctx, ctx.Repo.Repository.RepoPath()) if err != nil { ctx.Error(http.StatusInternalServerError, "Unable to OpenRepository", err) return err @@ -1023,7 +1023,7 @@ func Delete(ctx *context.APIContext) { ctx.Repo.GitRepo.Close() } - if err := repo_service.DeleteRepository(ctx.User, repo, true); err != nil { + if err := repo_service.DeleteRepository(ctx, ctx.User, repo, true); err != nil { ctx.Error(http.StatusInternalServerError, "DeleteRepository", err) return } diff --git a/routers/api/v1/repo/status.go b/routers/api/v1/repo/status.go index 9d0fbbddad..72d790794e 100644 --- a/routers/api/v1/repo/status.go +++ b/routers/api/v1/repo/status.go @@ -62,7 +62,7 @@ func NewCommitStatus(ctx *context.APIContext) { Description: form.Description, Context: form.Context, } - if err := files_service.CreateCommitStatus(ctx.Repo.Repository, ctx.User, sha, status); err != nil { + if err := files_service.CreateCommitStatus(ctx, ctx.Repo.Repository, ctx.User, sha, status); err != nil { ctx.Error(http.StatusInternalServerError, "CreateCommitStatus", err) return } diff --git a/routers/api/v1/repo/tag.go b/routers/api/v1/repo/tag.go index 13a625bafb..a60f4f320f 100644 --- a/routers/api/v1/repo/tag.go +++ b/routers/api/v1/repo/tag.go @@ -191,7 +191,7 @@ func CreateTag(ctx *context.APIContext) { return } - if err := releaseservice.CreateNewTag(ctx.User, ctx.Repo.Repository, commit.ID.String(), form.TagName, form.Message); err != nil { + if err := releaseservice.CreateNewTag(ctx, ctx.User, ctx.Repo.Repository, commit.ID.String(), form.TagName, form.Message); err != nil { if models.IsErrTagAlreadyExists(err) { ctx.Error(http.StatusConflict, "tag exist", err) return @@ -255,7 +255,7 @@ func DeleteTag(ctx *context.APIContext) { return } - if err = releaseservice.DeleteReleaseByID(tag.ID, ctx.User, true); err != nil { + if err = releaseservice.DeleteReleaseByID(ctx, tag.ID, ctx.User, true); err != nil { ctx.Error(http.StatusInternalServerError, "DeleteReleaseByID", err) } diff --git a/routers/api/v1/repo/tree.go b/routers/api/v1/repo/tree.go index 411d87d6c5..b8294ebe31 100644 --- a/routers/api/v1/repo/tree.go +++ b/routers/api/v1/repo/tree.go @@ -60,7 +60,7 @@ func GetTree(ctx *context.APIContext) { ctx.Error(http.StatusBadRequest, "", "sha not provided") return } - if tree, err := files_service.GetTreeBySHA(ctx.Repo.Repository, sha, ctx.FormInt("page"), ctx.FormInt("per_page"), ctx.FormBool("recursive")); err != nil { + if tree, err := files_service.GetTreeBySHA(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, sha, ctx.FormInt("page"), ctx.FormInt("per_page"), ctx.FormBool("recursive")); err != nil { ctx.Error(http.StatusBadRequest, "", err.Error()) } else { ctx.SetTotalCountHeader(int64(tree.TotalCount)) diff --git a/routers/api/v1/repo/wiki.go b/routers/api/v1/repo/wiki.go index bef32f367b..94f1dbe0ea 100644 --- a/routers/api/v1/repo/wiki.go +++ b/routers/api/v1/repo/wiki.go @@ -71,7 +71,7 @@ func NewWikiPage(ctx *context.APIContext) { } form.ContentBase64 = string(content) - if err := wiki_service.AddWikiPage(ctx.User, ctx.Repo.Repository, wikiName, form.ContentBase64, form.Message); err != nil { + if err := wiki_service.AddWikiPage(ctx, ctx.User, ctx.Repo.Repository, wikiName, form.ContentBase64, form.Message); err != nil { if models.IsErrWikiReservedName(err) { ctx.Error(http.StatusBadRequest, "IsErrWikiReservedName", err) } else if models.IsErrWikiAlreadyExist(err) { @@ -144,7 +144,7 @@ func EditWikiPage(ctx *context.APIContext) { } form.ContentBase64 = string(content) - if err := wiki_service.EditWikiPage(ctx.User, ctx.Repo.Repository, oldWikiName, newWikiName, form.ContentBase64, form.Message); err != nil { + if err := wiki_service.EditWikiPage(ctx, ctx.User, ctx.Repo.Repository, oldWikiName, newWikiName, form.ContentBase64, form.Message); err != nil { ctx.Error(http.StatusInternalServerError, "EditWikiPage", err) return } @@ -233,7 +233,7 @@ func DeleteWikiPage(ctx *context.APIContext) { wikiName := wiki_service.NormalizeWikiName(ctx.Params(":pageName")) - if err := wiki_service.DeleteWikiPage(ctx.User, ctx.Repo.Repository, wikiName); err != nil { + if err := wiki_service.DeleteWikiPage(ctx, ctx.User, ctx.Repo.Repository, wikiName); err != nil { if err.Error() == "file does not exist" { ctx.NotFound(err) return @@ -458,7 +458,7 @@ func findEntryForFile(commit *git.Commit, target string) (*git.TreeEntry, error) // findWikiRepoCommit opens the wiki repo and returns the latest commit, writing to context on error. // The caller is responsible for closing the returned repo again func findWikiRepoCommit(ctx *context.APIContext) (*git.Repository, *git.Commit) { - wikiRepo, err := git.OpenRepository(ctx.Repo.Repository.WikiPath()) + wikiRepo, err := git.OpenRepositoryCtx(ctx, ctx.Repo.Repository.WikiPath()) if err != nil { if git.IsErrNotExist(err) || err.Error() == "no such file or directory" { diff --git a/routers/api/v1/utils/git.go b/routers/api/v1/utils/git.go index ac68c3625d..a941c1fc2c 100644 --- a/routers/api/v1/utils/git.go +++ b/routers/api/v1/utils/git.go @@ -36,7 +36,7 @@ func ResolveRefOrSha(ctx *context.APIContext, ref string) string { func GetGitRefs(ctx *context.APIContext, filter string) ([]*git.Reference, string, error) { if ctx.Repo.GitRepo == nil { var err error - ctx.Repo.GitRepo, err = git.OpenRepository(ctx.Repo.Repository.RepoPath()) + ctx.Repo.GitRepo, err = git.OpenRepositoryCtx(ctx, ctx.Repo.Repository.RepoPath()) if err != nil { return nil, "OpenRepository", err } diff --git a/routers/private/default_branch.go b/routers/private/default_branch.go index 5fdd0df735..55ffef43f3 100644 --- a/routers/private/default_branch.go +++ b/routers/private/default_branch.go @@ -12,7 +12,6 @@ import ( repo_model "code.gitea.io/gitea/models/repo" gitea_context "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" - "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/private" ) @@ -34,38 +33,18 @@ func SetDefaultBranch(ctx *gitea_context.PrivateContext) { ownerName := ctx.Params(":owner") repoName := ctx.Params(":repo") branch := ctx.Params(":branch") - repo, err := repo_model.GetRepositoryByOwnerAndName(ownerName, repoName) - if err != nil { - log.Error("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err) - ctx.JSON(http.StatusInternalServerError, private.Response{ - Err: fmt.Sprintf("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err), - }) - return - } - if repo.OwnerName == "" { - repo.OwnerName = ownerName - } - repo.DefaultBranch = branch - gitRepo, err := git.OpenRepository(repo.RepoPath()) - if err != nil { - ctx.JSON(http.StatusInternalServerError, private.Response{ - Err: fmt.Sprintf("Failed to get git repository: %s/%s Error: %v", ownerName, repoName, err), - }) - return - } - if err := gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil { + ctx.Repo.Repository.DefaultBranch = branch + if err := ctx.Repo.GitRepo.SetDefaultBranch(ctx.Repo.Repository.DefaultBranch); err != nil { if !git.IsErrUnsupportedVersion(err) { - gitRepo.Close() ctx.JSON(http.StatusInternalServerError, private.Response{ Err: fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err), }) return } } - gitRepo.Close() - if err := repo_model.UpdateDefaultBranch(repo); err != nil { + if err := repo_model.UpdateDefaultBranch(ctx.Repo.Repository); err != nil { ctx.JSON(http.StatusInternalServerError, private.Response{ Err: fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err), }) diff --git a/routers/private/hook_pre_receive.go b/routers/private/hook_pre_receive.go index b8f61cbb11..649bfa5cf3 100644 --- a/routers/private/hook_pre_receive.go +++ b/routers/private/hook_pre_receive.go @@ -183,7 +183,7 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID, refFullN // 2. Disallow force pushes to protected branches if git.EmptySHA != oldCommitID { - output, err := git.NewCommand("rev-list", "--max-count=1", oldCommitID, "^"+newCommitID).RunInDirWithEnv(repo.RepoPath(), ctx.env) + output, err := git.NewCommandContext(ctx, "rev-list", "--max-count=1", oldCommitID, "^"+newCommitID).RunInDirWithEnv(repo.RepoPath(), ctx.env) if err != nil { log.Error("Unable to detect force push between: %s and %s in %-v Error: %v", oldCommitID, newCommitID, repo, err) ctx.JSON(http.StatusInternalServerError, private.Response{ @@ -228,7 +228,7 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID, refFullN globs := protectBranch.GetProtectedFilePatterns() if len(globs) > 0 { - _, err := pull_service.CheckFileProtection(oldCommitID, newCommitID, globs, 1, ctx.env, gitRepo) + _, err := pull_service.CheckFileProtection(gitRepo, oldCommitID, newCommitID, globs, 1, ctx.env) if err != nil { if !models.IsErrFilePathProtected(err) { log.Error("Unable to check file protection for commits from %s to %s in %-v: %v", oldCommitID, newCommitID, repo, err) @@ -270,7 +270,7 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID, refFullN // Allow commits that only touch unprotected files globs := protectBranch.GetUnprotectedFilePatterns() if len(globs) > 0 { - unprotectedFilesOnly, err := pull_service.CheckUnprotectedFiles(oldCommitID, newCommitID, globs, ctx.env, gitRepo) + unprotectedFilesOnly, err := pull_service.CheckUnprotectedFiles(gitRepo, oldCommitID, newCommitID, globs, ctx.env) if err != nil { log.Error("Unable to check file protection for commits from %s to %s in %-v: %v", oldCommitID, newCommitID, repo, err) ctx.JSON(http.StatusInternalServerError, private.Response{ @@ -337,7 +337,7 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID, refFullN } // Check all status checks and reviews are ok - if err := pull_service.CheckPRReadyToMerge(pr, true); err != nil { + if err := pull_service.CheckPRReadyToMerge(ctx, pr, true); err != nil { if models.IsErrNotAllowedToMerge(err) { log.Warn("Forbidden: User %d is not allowed push to protected branch %s in %-v and pr #%d is not ready to be merged: %s", ctx.opts.UserID, branchName, repo, pr.Index, err.Error()) ctx.JSON(http.StatusForbidden, private.Response{ diff --git a/routers/private/hook_verification.go b/routers/private/hook_verification.go index 1b44550cff..5940045dc6 100644 --- a/routers/private/hook_verification.go +++ b/routers/private/hook_verification.go @@ -44,7 +44,7 @@ func verifyCommits(oldCommitID, newCommitID string, repo *git.Repository, env [] }() // This is safe as force pushes are already forbidden - err = git.NewCommand("rev-list", oldCommitID+"..."+newCommitID). + err = git.NewCommandContext(repo.Ctx, "rev-list", oldCommitID+"..."+newCommitID). RunInDirTimeoutEnvFullPipelineFunc(env, -1, repo.Path, stdoutWriter, nil, nil, func(ctx context.Context, cancel context.CancelFunc) error { @@ -88,7 +88,7 @@ func readAndVerifyCommit(sha string, repo *git.Repository, env []string) error { }() hash := git.MustIDFromString(sha) - return git.NewCommand("cat-file", "commit", sha). + return git.NewCommandContext(repo.Ctx, "cat-file", "commit", sha). RunInDirTimeoutEnvFullPipelineFunc(env, -1, repo.Path, stdoutWriter, nil, nil, func(ctx context.Context, cancel context.CancelFunc) error { diff --git a/routers/private/internal.go b/routers/private/internal.go index 183ab5e98a..4960f561c1 100644 --- a/routers/private/internal.go +++ b/routers/private/internal.go @@ -57,8 +57,8 @@ func Routes() *web.Route { r.Post("/ssh/{id}/update/{repoid}", UpdatePublicKeyInRepo) r.Post("/ssh/log", bind(private.SSHLogOption{}), SSHLog) r.Post("/hook/pre-receive/{owner}/{repo}", RepoAssignment, bind(private.HookOptions{}), HookPreReceive) - r.Post("/hook/post-receive/{owner}/{repo}", bind(private.HookOptions{}), HookPostReceive) - r.Post("/hook/proc-receive/{owner}/{repo}", RepoAssignment, bind(private.HookOptions{}), HookProcReceive) + r.Post("/hook/post-receive/{owner}/{repo}", context.OverrideContext, bind(private.HookOptions{}), HookPostReceive) + r.Post("/hook/proc-receive/{owner}/{repo}", context.OverrideContext, RepoAssignment, bind(private.HookOptions{}), HookProcReceive) r.Post("/hook/set-default-branch/{owner}/{repo}/{branch}", RepoAssignment, SetDefaultBranch) r.Get("/serv/none/{keyid}", ServNoCommand) r.Get("/serv/command/{keyid}/{owner}/{repo}", ServCommand) diff --git a/routers/private/internal_repo.go b/routers/private/internal_repo.go index 8d0fd72235..ade862c613 100644 --- a/routers/private/internal_repo.go +++ b/routers/private/internal_repo.go @@ -43,7 +43,7 @@ func RepoAssignment(ctx *gitea_context.PrivateContext) context.CancelFunc { return nil } - gitRepo, err := git.OpenRepository(repo.RepoPath()) + gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath()) if err != nil { log.Error("Failed to open repository: %s/%s Error: %v", ownerName, repoName, err) ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ diff --git a/routers/private/serv.go b/routers/private/serv.go index 6bf0ceeca2..65989d868b 100644 --- a/routers/private/serv.go +++ b/routers/private/serv.go @@ -401,7 +401,7 @@ func ServCommand(ctx *context.PrivateContext) { } // Finally if we're trying to touch the wiki we should init it - if err = wiki_service.InitWiki(repo); err != nil { + if err = wiki_service.InitWiki(ctx, repo); err != nil { log.Error("Failed to initialize the wiki in %-v Error: %v", repo, err) ctx.JSON(http.StatusInternalServerError, private.ErrServCommand{ Results: results, diff --git a/routers/web/admin/repos.go b/routers/web/admin/repos.go index 7e3ec96fce..c4290fc22b 100644 --- a/routers/web/admin/repos.go +++ b/routers/web/admin/repos.go @@ -52,7 +52,7 @@ func DeleteRepo(ctx *context.Context) { ctx.Repo.GitRepo.Close() } - if err := repo_service.DeleteRepository(ctx.User, repo, true); err != nil { + if err := repo_service.DeleteRepository(ctx, ctx.User, repo, true); err != nil { ctx.ServerError("DeleteRepository", err) return } diff --git a/routers/web/feed/convert.go b/routers/web/feed/convert.go index 4743668621..4dbd9c9d0e 100644 --- a/routers/web/feed/convert.go +++ b/routers/web/feed/convert.go @@ -180,7 +180,7 @@ func feedActionsToFeedItems(ctx *context.Context, actions []*models.Action) (ite desc += fmt.Sprintf("%s\n%s", html.EscapeString(fmt.Sprintf("%s/commit/%s", act.GetRepoLink(), commit.Sha1)), commit.Sha1, - templates.RenderCommitMessage(commit.Message, repoLink, nil), + templates.RenderCommitMessage(ctx, commit.Message, repoLink, nil), ) } diff --git a/routers/web/org/home.go b/routers/web/org/home.go index 3a42044281..43f6211af3 100644 --- a/routers/web/org/home.go +++ b/routers/web/org/home.go @@ -40,6 +40,7 @@ func Home(ctx *context.Context) { ctx.Data["Title"] = org.DisplayName() if len(org.Description) != 0 { desc, err := markdown.RenderString(&markup.RenderContext{ + Ctx: ctx, URLPrefix: ctx.Repo.RepoLink, Metas: map[string]string{"mode": "document"}, GitRepo: ctx.Repo.GitRepo, diff --git a/routers/web/repo/activity.go b/routers/web/repo/activity.go index ae37b51403..b2f25ebe72 100644 --- a/routers/web/repo/activity.go +++ b/routers/web/repo/activity.go @@ -52,7 +52,7 @@ func Activity(ctx *context.Context) { ctx.Data["PeriodText"] = ctx.Tr("repo.activity.period." + ctx.Data["Period"].(string)) var err error - if ctx.Data["Activity"], err = models.GetActivityStats(ctx.Repo.Repository, timeFrom, + if ctx.Data["Activity"], err = models.GetActivityStats(ctx, ctx.Repo.Repository, timeFrom, ctx.Repo.CanRead(unit.TypeReleases), ctx.Repo.CanRead(unit.TypeIssues), ctx.Repo.CanRead(unit.TypePullRequests), @@ -61,7 +61,7 @@ func Activity(ctx *context.Context) { return } - if ctx.PageData["repoActivityTopAuthors"], err = models.GetActivityStatsTopAuthors(ctx.Repo.Repository, timeFrom, 10); err != nil { + if ctx.PageData["repoActivityTopAuthors"], err = models.GetActivityStatsTopAuthors(ctx, ctx.Repo.Repository, timeFrom, 10); err != nil { ctx.ServerError("GetActivityStatsTopAuthors", err) return } @@ -94,7 +94,7 @@ func ActivityAuthors(ctx *context.Context) { } var err error - authors, err := models.GetActivityStatsTopAuthors(ctx.Repo.Repository, timeFrom, 10) + authors, err := models.GetActivityStatsTopAuthors(ctx, ctx.Repo.Repository, timeFrom, 10) if err != nil { ctx.ServerError("GetActivityStatsTopAuthors", err) return diff --git a/routers/web/repo/branch.go b/routers/web/repo/branch.go index 29b8acd61c..ed1b2a9b1a 100644 --- a/routers/web/repo/branch.go +++ b/routers/web/repo/branch.go @@ -256,7 +256,7 @@ func loadOneBranch(ctx *context.Context, rawBranch, defaultBranch *git.Branch, p Behind: -1, } if defaultBranch != nil { - divergence, err = files_service.CountDivergingCommits(ctx.Repo.Repository, git.BranchPrefix+branchName) + divergence, err = files_service.CountDivergingCommits(ctx, ctx.Repo.Repository, git.BranchPrefix+branchName) if err != nil { log.Error("CountDivergingCommits", err) } @@ -289,7 +289,7 @@ func loadOneBranch(ctx *context.Context, rawBranch, defaultBranch *git.Branch, p if pr.HasMerged { baseGitRepo, ok := repoIDToGitRepo[pr.BaseRepoID] if !ok { - baseGitRepo, err = git.OpenRepository(pr.BaseRepo.RepoPath()) + baseGitRepo, err = git.OpenRepositoryCtx(ctx, pr.BaseRepo.RepoPath()) if err != nil { ctx.ServerError("OpenRepository", err) return nil @@ -363,11 +363,11 @@ func CreateBranch(ctx *context.Context) { if ctx.Repo.IsViewBranch { target = ctx.Repo.BranchName } - err = release_service.CreateNewTag(ctx.User, ctx.Repo.Repository, target, form.NewBranchName, "") + err = release_service.CreateNewTag(ctx, ctx.User, ctx.Repo.Repository, target, form.NewBranchName, "") } else if ctx.Repo.IsViewBranch { - err = repo_service.CreateNewBranch(ctx.User, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName) + err = repo_service.CreateNewBranch(ctx, ctx.User, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName) } else { - err = repo_service.CreateNewBranchFromCommit(ctx.User, ctx.Repo.Repository, ctx.Repo.CommitID, form.NewBranchName) + err = repo_service.CreateNewBranchFromCommit(ctx, ctx.User, ctx.Repo.Repository, ctx.Repo.CommitID, form.NewBranchName) } if err != nil { if models.IsErrTagAlreadyExists(err) { diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index 304f76158c..12457e45ee 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -118,12 +118,12 @@ func Graph(ctx *context.Context) { return } - graphCommitsCount, err := ctx.Repo.GetCommitGraphsCount(hidePRRefs, realBranches, files) + graphCommitsCount, err := ctx.Repo.GetCommitGraphsCount(ctx, hidePRRefs, realBranches, files) if err != nil { log.Warn("GetCommitGraphsCount error for generate graph exclude prs: %t branches: %s in %-v, Will Ignore branches and try again. Underlying Error: %v", hidePRRefs, branches, ctx.Repo.Repository, err) realBranches = []string{} branches = []string{} - graphCommitsCount, err = ctx.Repo.GetCommitGraphsCount(hidePRRefs, realBranches, files) + graphCommitsCount, err = ctx.Repo.GetCommitGraphsCount(ctx, hidePRRefs, realBranches, files) if err != nil { ctx.ServerError("GetCommitGraphsCount", err) return @@ -265,7 +265,7 @@ func Diff(ctx *context.Context) { ) if ctx.Data["PageIsWiki"] != nil { - gitRepo, err = git.OpenRepository(ctx.Repo.Repository.WikiPath()) + gitRepo, err = git.OpenRepositoryCtx(ctx, ctx.Repo.Repository.WikiPath()) if err != nil { ctx.ServerError("Repo.GitRepo.GetCommit", err) return @@ -388,6 +388,7 @@ func RawDiff(ctx *context.Context) { repoPath = repo_model.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name) } if err := git.GetRawDiff( + ctx, repoPath, ctx.Params(":sha"), git.RawDiffType(ctx.Params(":ext")), diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 3b07c35cb0..22e6be2021 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -6,6 +6,7 @@ package repo import ( "bufio" + gocontext "context" "encoding/csv" "errors" "fmt" @@ -136,14 +137,14 @@ func setCsvCompareContext(ctx *context.Context) { return csvReader, reader, err } - baseReader, baseBlobCloser, err := csvReaderFromCommit(&markup.RenderContext{Filename: diffFile.OldName}, baseCommit) + baseReader, baseBlobCloser, err := csvReaderFromCommit(&markup.RenderContext{Ctx: ctx, Filename: diffFile.OldName}, baseCommit) if baseBlobCloser != nil { defer baseBlobCloser.Close() } if err == errTooLarge { return CsvDiffResult{nil, err.Error()} } - headReader, headBlobCloser, err := csvReaderFromCommit(&markup.RenderContext{Filename: diffFile.Name}, headCommit) + headReader, headBlobCloser, err := csvReaderFromCommit(&markup.RenderContext{Ctx: ctx, Filename: diffFile.Name}, headCommit) if headBlobCloser != nil { defer headBlobCloser.Close() } @@ -382,7 +383,7 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo { ci.HeadRepo = ctx.Repo.Repository ci.HeadGitRepo = ctx.Repo.GitRepo } else if has { - ci.HeadGitRepo, err = git.OpenRepository(ci.HeadRepo.RepoPath()) + ci.HeadGitRepo, err = git.OpenRepositoryCtx(ctx, ci.HeadRepo.RepoPath()) if err != nil { ctx.ServerError("OpenRepository", err) return nil @@ -442,7 +443,7 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo { if canRead { ctx.Data["RootRepo"] = rootRepo if !fileOnly { - branches, tags, err := getBranchesAndTagsForRepo(rootRepo) + branches, tags, err := getBranchesAndTagsForRepo(ctx, rootRepo) if err != nil { ctx.ServerError("GetBranchesForRepo", err) return nil @@ -467,7 +468,7 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo { if canRead { ctx.Data["OwnForkRepo"] = ownForkRepo if !fileOnly { - branches, tags, err := getBranchesAndTagsForRepo(ownForkRepo) + branches, tags, err := getBranchesAndTagsForRepo(ctx, ownForkRepo) if err != nil { ctx.ServerError("GetBranchesForRepo", err) return nil @@ -653,8 +654,8 @@ func PrepareCompareDiff( return false } -func getBranchesAndTagsForRepo(repo *repo_model.Repository) (branches, tags []string, err error) { - gitRepo, err := git.OpenRepository(repo.RepoPath()) +func getBranchesAndTagsForRepo(ctx gocontext.Context, repo *repo_model.Repository) (branches, tags []string, err error) { + gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath()) if err != nil { return nil, nil, err } diff --git a/routers/web/repo/editor.go b/routers/web/repo/editor.go index 5904ba2f0b..c4fff1e421 100644 --- a/routers/web/repo/editor.go +++ b/routers/web/repo/editor.go @@ -26,7 +26,6 @@ import ( "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/routers/utils" "code.gitea.io/gitea/services/forms" - repo_service "code.gitea.io/gitea/services/repository" files_service "code.gitea.io/gitea/services/repository/files" ) @@ -41,7 +40,7 @@ const ( ) func renderCommitRights(ctx *context.Context) bool { - canCommitToBranch, err := ctx.Repo.CanCommitToBranch(ctx.User) + canCommitToBranch, err := ctx.Repo.CanCommitToBranch(ctx, ctx.User) if err != nil { log.Error("CanCommitToBranch: %v", err) } @@ -242,7 +241,7 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b message += "\n\n" + form.CommitMessage } - if _, err := files_service.CreateOrUpdateRepoFile(ctx.Repo.Repository, ctx.User, &files_service.UpdateRepoFileOptions{ + if _, err := files_service.CreateOrUpdateRepoFile(ctx, ctx.Repo.Repository, ctx.User, &files_service.UpdateRepoFileOptions{ LastCommitID: form.LastCommit, OldBranch: ctx.Repo.BranchName, NewBranch: branchName, @@ -367,7 +366,7 @@ func DiffPreviewPost(ctx *context.Context) { return } - diff, err := files_service.GetDiffPreview(ctx.Repo.Repository, ctx.Repo.BranchName, treePath, form.Content) + diff, err := files_service.GetDiffPreview(ctx, ctx.Repo.Repository, ctx.Repo.BranchName, treePath, form.Content) if err != nil { ctx.Error(http.StatusInternalServerError, "GetDiffPreview: "+err.Error()) return @@ -448,7 +447,7 @@ func DeleteFilePost(ctx *context.Context) { message += "\n\n" + form.CommitMessage } - if _, err := files_service.DeleteRepoFile(ctx.Repo.Repository, ctx.User, &files_service.DeleteRepoFileOptions{ + if _, err := files_service.DeleteRepoFile(ctx, ctx.Repo.Repository, ctx.User, &files_service.DeleteRepoFileOptions{ LastCommitID: form.LastCommit, OldBranch: ctx.Repo.BranchName, NewBranch: branchName, @@ -610,7 +609,7 @@ func UploadFilePost(ctx *context.Context) { } if oldBranchName != branchName { - if _, err := repo_service.GetBranch(ctx.Repo.Repository, branchName); err == nil { + if _, err := ctx.Repo.GitRepo.GetBranch(branchName); err == nil { ctx.Data["Err_NewBranchName"] = true ctx.RenderWithErr(ctx.Tr("repo.editor.branch_already_exists", branchName), tplUploadFile, &form) return @@ -654,7 +653,7 @@ func UploadFilePost(ctx *context.Context) { message += "\n\n" + form.CommitMessage } - if err := files_service.UploadRepoFiles(ctx.Repo.Repository, ctx.User, &files_service.UploadRepoFileOptions{ + if err := files_service.UploadRepoFiles(ctx, ctx.Repo.Repository, ctx.User, &files_service.UploadRepoFileOptions{ LastCommitID: ctx.Repo.CommitID, OldBranch: oldBranchName, NewBranch: branchName, @@ -802,7 +801,7 @@ func GetUniquePatchBranchName(ctx *context.Context) string { prefix := ctx.User.LowerName + "-patch-" for i := 1; i <= 1000; i++ { branchName := fmt.Sprintf("%s%d", prefix, i) - if _, err := repo_service.GetBranch(ctx.Repo.Repository, branchName); err != nil { + if _, err := ctx.Repo.GitRepo.GetBranch(branchName); err != nil { if git.IsErrBranchNotExist(err) { return branchName } diff --git a/routers/web/repo/http.go b/routers/web/repo/http.go index 6ebf6a789a..5e73843920 100644 --- a/routers/web/repo/http.go +++ b/routers/web/repo/http.go @@ -8,6 +8,7 @@ package repo import ( "bytes" "compress/gzip" + gocontext "context" "fmt" "net/http" "os" @@ -324,12 +325,12 @@ func dummyInfoRefs(ctx *context.Context) { } }() - if err := git.InitRepository(tmpDir, true); err != nil { + if err := git.InitRepository(ctx, tmpDir, true); err != nil { log.Error("Failed to init bare repo for git-receive-pack cache: %v", err) return } - refs, err := git.NewCommand("receive-pack", "--stateless-rpc", "--advertise-refs", ".").RunInDirBytes(tmpDir) + refs, err := git.NewCommandContext(ctx, "receive-pack", "--stateless-rpc", "--advertise-refs", ".").RunInDirBytes(tmpDir) if err != nil { log.Error(fmt.Sprintf("%v - %s", err, string(refs))) } @@ -412,17 +413,17 @@ func (h *serviceHandler) sendFile(contentType, file string) { // one or more key=value pairs separated by colons var safeGitProtocolHeader = regexp.MustCompile(`^[0-9a-zA-Z]+=[0-9a-zA-Z]+(:[0-9a-zA-Z]+=[0-9a-zA-Z]+)*$`) -func getGitConfig(option, dir string) string { - out, err := git.NewCommand("config", option).RunInDir(dir) +func getGitConfig(ctx gocontext.Context, option, dir string) string { + out, err := git.NewCommandContext(ctx, "config", option).RunInDir(dir) if err != nil { log.Error("%v - %s", err, out) } return out[0 : len(out)-1] } -func getConfigSetting(service, dir string) bool { +func getConfigSetting(ctx gocontext.Context, service, dir string) bool { service = strings.ReplaceAll(service, "-", "") - setting := getGitConfig("http."+service, dir) + setting := getGitConfig(ctx, "http."+service, dir) if service == "uploadpack" { return setting != "false" @@ -431,7 +432,7 @@ func getConfigSetting(service, dir string) bool { return setting == "true" } -func hasAccess(service string, h serviceHandler, checkContentType bool) bool { +func hasAccess(ctx gocontext.Context, service string, h serviceHandler, checkContentType bool) bool { if checkContentType { if h.r.Header.Get("Content-Type") != fmt.Sprintf("application/x-git-%s-request", service) { return false @@ -448,10 +449,10 @@ func hasAccess(service string, h serviceHandler, checkContentType bool) bool { return h.cfg.UploadPack } - return getConfigSetting(service, h.dir) + return getConfigSetting(ctx, service, h.dir) } -func serviceRPC(h serviceHandler, service string) { +func serviceRPC(ctx gocontext.Context, h serviceHandler, service string) { defer func() { if err := h.r.Body.Close(); err != nil { log.Error("serviceRPC: Close: %v", err) @@ -459,7 +460,7 @@ func serviceRPC(h serviceHandler, service string) { }() - if !hasAccess(service, h, true) { + if !hasAccess(ctx, service, h, true) { h.w.WriteHeader(http.StatusUnauthorized) return } @@ -486,7 +487,6 @@ func serviceRPC(h serviceHandler, service string) { h.environ = append(h.environ, "GIT_PROTOCOL="+protocol) } - // ctx, cancel := gocontext.WithCancel(git.DefaultContext) ctx, _, finished := process.GetManager().AddContext(h.r.Context(), fmt.Sprintf("%s %s %s [repo_path: %s]", git.GitExecutable, service, "--stateless-rpc", h.dir)) defer finished() @@ -508,7 +508,7 @@ func serviceRPC(h serviceHandler, service string) { func ServiceUploadPack(ctx *context.Context) { h := httpBase(ctx) if h != nil { - serviceRPC(*h, "upload-pack") + serviceRPC(ctx, *h, "upload-pack") } } @@ -516,7 +516,7 @@ func ServiceUploadPack(ctx *context.Context) { func ServiceReceivePack(ctx *context.Context) { h := httpBase(ctx) if h != nil { - serviceRPC(*h, "receive-pack") + serviceRPC(ctx, *h, "receive-pack") } } @@ -528,8 +528,8 @@ func getServiceType(r *http.Request) string { return strings.Replace(serviceType, "git-", "", 1) } -func updateServerInfo(dir string) []byte { - out, err := git.NewCommand("update-server-info").RunInDirBytes(dir) +func updateServerInfo(ctx gocontext.Context, dir string) []byte { + out, err := git.NewCommandContext(ctx, "update-server-info").RunInDirBytes(dir) if err != nil { log.Error(fmt.Sprintf("%v - %s", err, string(out))) } @@ -551,7 +551,7 @@ func GetInfoRefs(ctx *context.Context) { return } h.setHeaderNoCache() - if hasAccess(getServiceType(h.r), *h, false) { + if hasAccess(ctx, getServiceType(h.r), *h, false) { service := getServiceType(h.r) if protocol := h.r.Header.Get("Git-Protocol"); protocol != "" && safeGitProtocolHeader.MatchString(protocol) { @@ -559,7 +559,7 @@ func GetInfoRefs(ctx *context.Context) { } h.environ = append(os.Environ(), h.environ...) - refs, err := git.NewCommand(service, "--stateless-rpc", "--advertise-refs", ".").RunInDirTimeoutEnv(h.environ, -1, h.dir) + refs, err := git.NewCommandContext(ctx, service, "--stateless-rpc", "--advertise-refs", ".").RunInDirTimeoutEnv(h.environ, -1, h.dir) if err != nil { log.Error(fmt.Sprintf("%v - %s", err, string(refs))) } @@ -570,7 +570,7 @@ func GetInfoRefs(ctx *context.Context) { _, _ = h.w.Write([]byte("0000")) _, _ = h.w.Write(refs) } else { - updateServerInfo(h.dir) + updateServerInfo(ctx, h.dir) h.sendFile("text/plain; charset=utf-8", "info/refs") } } diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index 9dee477537..ba871127f7 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -263,7 +263,7 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption uti } } - commitStatus, err := pull_service.GetIssuesLastCommitStatus(issues) + commitStatus, err := pull_service.GetIssuesLastCommitStatus(ctx, issues) if err != nil { ctx.ServerError("GetIssuesLastCommitStatus", err) return @@ -1434,14 +1434,14 @@ func ViewIssue(ctx *context.Context) { if comment.Review == nil { continue } - if err = comment.Review.LoadAttributes(); err != nil { + if err = comment.Review.LoadAttributes(ctx); err != nil { if !user_model.IsErrUserNotExist(err) { ctx.ServerError("Review.LoadAttributes", err) return } comment.Review.Reviewer = user_model.NewGhostUser() } - if err = comment.Review.LoadCodeComments(); err != nil { + if err = comment.Review.LoadCodeComments(ctx); err != nil { ctx.ServerError("Review.LoadCodeComments", err) return } @@ -1471,7 +1471,7 @@ func ViewIssue(ctx *context.Context) { } } else if comment.Type == models.CommentTypePullPush { participants = addParticipant(comment.Poster, participants) - if err = comment.LoadPushCommits(); err != nil { + if err = comment.LoadPushCommits(ctx); err != nil { ctx.ServerError("LoadPushCommits", err) return } @@ -1583,7 +1583,7 @@ func ViewIssue(ctx *context.Context) { } ctx.Data["WillSign"] = false if ctx.User != nil { - sign, key, _, err := asymkey_service.SignMerge(pull, ctx.User, pull.BaseRepo.RepoPath(), pull.BaseBranch, pull.GetGitRefName()) + sign, key, _, err := asymkey_service.SignMerge(ctx, pull, ctx.User, pull.BaseRepo.RepoPath(), pull.BaseBranch, pull.GetGitRefName()) ctx.Data["WillSign"] = sign ctx.Data["SigningKey"] = key if err != nil { diff --git a/routers/web/repo/lfs.go b/routers/web/repo/lfs.go index 8943641381..6b5e1dd624 100644 --- a/routers/web/repo/lfs.go +++ b/routers/web/repo/lfs.go @@ -115,7 +115,7 @@ func LFSLocks(ctx *context.Context) { } }() - if err := git.Clone(ctx.Repo.Repository.RepoPath(), tmpBasePath, git.CloneRepoOptions{ + if err := git.Clone(ctx, ctx.Repo.Repository.RepoPath(), tmpBasePath, git.CloneRepoOptions{ Bare: true, Shared: true, }); err != nil { @@ -124,7 +124,7 @@ func LFSLocks(ctx *context.Context) { return } - gitRepo, err := git.OpenRepository(tmpBasePath) + gitRepo, err := git.OpenRepositoryCtx(ctx, tmpBasePath) if err != nil { log.Error("Unable to open temporary repository: %s (%v)", tmpBasePath, err) ctx.ServerError("LFSLocks", fmt.Errorf("Failed to open new temporary repository in: %s %v", tmpBasePath, err)) diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index 6e68fd0c69..517156863e 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -413,12 +413,17 @@ func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.Compare } ctx.Data["EnableStatusCheck"] = pull.ProtectedBranch != nil && pull.ProtectedBranch.EnableStatusCheck - baseGitRepo, err := git.OpenRepository(pull.BaseRepo.RepoPath()) - if err != nil { - ctx.ServerError("OpenRepository", err) - return nil + var baseGitRepo *git.Repository + if pull.BaseRepoID == ctx.Repo.Repository.ID && ctx.Repo.GitRepo != nil { + baseGitRepo = ctx.Repo.GitRepo + } else { + baseGitRepo, err := git.OpenRepositoryCtx(ctx, pull.BaseRepo.RepoPath()) + if err != nil { + ctx.ServerError("OpenRepository", err) + return nil + } + defer baseGitRepo.Close() } - defer baseGitRepo.Close() if !baseGitRepo.IsBranchExist(pull.BaseBranch) { ctx.Data["IsPullRequestBroken"] = true @@ -464,7 +469,7 @@ func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.Compare var headBranchSha string // HeadRepo may be missing if pull.HeadRepo != nil { - headGitRepo, err := git.OpenRepository(pull.HeadRepo.RepoPath()) + headGitRepo, err := git.OpenRepositoryCtx(ctx, pull.HeadRepo.RepoPath()) if err != nil { ctx.ServerError("OpenRepository", err) return nil @@ -491,12 +496,13 @@ func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.Compare } if headBranchExist { + var err error ctx.Data["UpdateAllowed"], ctx.Data["UpdateByRebaseAllowed"], err = pull_service.IsUserAllowedToUpdate(pull, ctx.User) if err != nil { ctx.ServerError("IsUserAllowedToUpdate", err) return nil } - ctx.Data["GetCommitMessages"] = pull_service.GetSquashMergeCommitMessages(pull) + ctx.Data["GetCommitMessages"] = pull_service.GetSquashMergeCommitMessages(ctx, pull) } sha, err := baseGitRepo.GetRefCommitID(pull.GetGitRefName()) @@ -695,7 +701,7 @@ func ViewPullFiles(ctx *context.Context) { return } - if err = diff.LoadComments(issue, ctx.User); err != nil { + if err = diff.LoadComments(ctx, issue, ctx.User); err != nil { ctx.ServerError("LoadComments", err) return } @@ -804,7 +810,7 @@ func UpdatePullRequest(ctx *context.Context) { // default merge commit message message := fmt.Sprintf("Merge branch '%s' into %s", issue.PullRequest.BaseBranch, issue.PullRequest.HeadBranch) - if err = pull_service.Update(issue.PullRequest, ctx.User, message, rebase); err != nil { + if err = pull_service.Update(ctx, issue.PullRequest, ctx.User, message, rebase); err != nil { if models.IsErrMergeConflicts(err) { conflictError := err.(models.ErrMergeConflicts) flashError, err := ctx.RenderToString(tplAlertDetails, map[string]interface{}{ @@ -916,7 +922,7 @@ func MergePullRequest(ctx *context.Context) { return } - if err := pull_service.CheckPRReadyToMerge(pr, false); err != nil { + if err := pull_service.CheckPRReadyToMerge(ctx, pr, false); err != nil { if !models.IsErrNotAllowedToMerge(err) { ctx.ServerError("Merge PR status", err) return @@ -969,7 +975,7 @@ func MergePullRequest(ctx *context.Context) { return } - if err = pull_service.Merge(pr, ctx.User, ctx.Repo.GitRepo, repo_model.MergeStyle(form.Do), form.HeadCommitID, message); err != nil { + if err = pull_service.Merge(ctx, pr, ctx.User, ctx.Repo.GitRepo, repo_model.MergeStyle(form.Do), form.HeadCommitID, message); err != nil { if models.IsErrInvalidMergeStyle(err) { ctx.Flash.Error(ctx.Tr("repo.pulls.invalid_merge_option")) ctx.Redirect(issue.Link()) @@ -1065,7 +1071,7 @@ func MergePullRequest(ctx *context.Context) { if ctx.Repo != nil && ctx.Repo.Repository != nil && pr.HeadRepoID == ctx.Repo.Repository.ID && ctx.Repo.GitRepo != nil { headRepo = ctx.Repo.GitRepo } else { - headRepo, err = git.OpenRepository(pr.HeadRepo.RepoPath()) + headRepo, err = git.OpenRepositoryCtx(ctx, pr.HeadRepo.RepoPath()) if err != nil { ctx.ServerError(fmt.Sprintf("OpenRepository[%s]", pr.HeadRepo.RepoPath()), err) return @@ -1184,7 +1190,7 @@ func CompareAndPullRequestPost(ctx *context.Context) { // FIXME: check error in the case two people send pull request at almost same time, give nice error prompt // instead of 500. - if err := pull_service.NewPullRequest(repo, pullIssue, labelIDs, attachments, pullRequest, assigneeIDs); err != nil { + if err := pull_service.NewPullRequest(ctx, repo, pullIssue, labelIDs, attachments, pullRequest, assigneeIDs); err != nil { if models.IsErrUserDoesNotHaveAccessToRepo(err) { ctx.Error(http.StatusBadRequest, "UserDoesNotHaveAccessToRepo", err.Error()) return @@ -1276,7 +1282,7 @@ func CleanUpPullRequest(ctx *context.Context) { gitBaseRepo = ctx.Repo.GitRepo } else { // If not just open it - gitBaseRepo, err = git.OpenRepository(pr.BaseRepo.RepoPath()) + gitBaseRepo, err = git.OpenRepositoryCtx(ctx, pr.BaseRepo.RepoPath()) if err != nil { ctx.ServerError(fmt.Sprintf("OpenRepository[%s]", pr.BaseRepo.RepoPath()), err) return @@ -1291,7 +1297,7 @@ func CleanUpPullRequest(ctx *context.Context) { gitRepo = ctx.Repo.GitRepo } else if pr.BaseRepoID != pr.HeadRepoID { // Otherwise just load it up - gitRepo, err = git.OpenRepository(pr.HeadRepo.RepoPath()) + gitRepo, err = git.OpenRepositoryCtx(ctx, pr.HeadRepo.RepoPath()) if err != nil { ctx.ServerError(fmt.Sprintf("OpenRepository[%s]", pr.HeadRepo.RepoPath()), err) return @@ -1375,7 +1381,7 @@ func DownloadPullDiffOrPatch(ctx *context.Context, patch bool) { binary := ctx.FormBool("binary") - if err := pull_service.DownloadDiffOrPatch(pr, ctx, patch, binary); err != nil { + if err := pull_service.DownloadDiffOrPatch(ctx, pr, ctx, patch, binary); err != nil { ctx.ServerError("DownloadDiffOrPatch", err) return } @@ -1404,7 +1410,7 @@ func UpdatePullRequestTarget(ctx *context.Context) { return } - if err := pull_service.ChangeTargetBranch(pr, ctx.User, targetBranch); err != nil { + if err := pull_service.ChangeTargetBranch(ctx, pr, ctx.User, targetBranch); err != nil { if models.IsErrPullRequestAlreadyExists(err) { err := err.(models.ErrPullRequestAlreadyExists) diff --git a/routers/web/repo/pull_review.go b/routers/web/repo/pull_review.go index 257aa737f6..c92f08a88a 100644 --- a/routers/web/repo/pull_review.go +++ b/routers/web/repo/pull_review.go @@ -68,7 +68,7 @@ func CreateCodeComment(ctx *context.Context) { signedLine *= -1 } - comment, err := pull_service.CreateCodeComment( + comment, err := pull_service.CreateCodeComment(ctx, ctx.User, ctx.Repo.GitRepo, issue, @@ -152,7 +152,7 @@ func UpdateResolveConversation(ctx *context.Context) { } func renderConversation(ctx *context.Context, comment *models.Comment) { - comments, err := models.FetchCodeCommentsByLine(comment.Issue, ctx.User, comment.TreePath, comment.Line) + comments, err := models.FetchCodeCommentsByLine(ctx, comment.Issue, ctx.User, comment.TreePath, comment.Line) if err != nil { ctx.ServerError("FetchCodeCommentsByLine", err) return @@ -217,7 +217,7 @@ func SubmitReview(ctx *context.Context) { attachments = form.Files } - _, comm, err := pull_service.SubmitReview(ctx.User, ctx.Repo.GitRepo, issue, reviewType, form.Content, form.CommitID, attachments) + _, comm, err := pull_service.SubmitReview(ctx, ctx.User, ctx.Repo.GitRepo, issue, reviewType, form.Content, form.CommitID, attachments) if err != nil { if models.IsContentEmptyErr(err) { ctx.Flash.Error(ctx.Tr("repo.issues.review.content.empty")) @@ -234,7 +234,7 @@ func SubmitReview(ctx *context.Context) { // DismissReview dismissing stale review by repo admin func DismissReview(ctx *context.Context) { form := web.GetForm(ctx).(*forms.DismissReviewForm) - comm, err := pull_service.DismissReview(form.ReviewID, form.Message, ctx.User, true) + comm, err := pull_service.DismissReview(ctx, form.ReviewID, form.Message, ctx.User, true) if err != nil { ctx.ServerError("pull_service.DismissReview", err) return diff --git a/routers/web/repo/release.go b/routers/web/repo/release.go index f2ab8b85f2..13623e15ef 100644 --- a/routers/web/repo/release.go +++ b/routers/web/repo/release.go @@ -325,7 +325,7 @@ func NewReleasePost(ctx *context.Context) { } if len(form.TagOnly) > 0 { - if err = releaseservice.CreateNewTag(ctx.User, ctx.Repo.Repository, form.Target, form.TagName, msg); err != nil { + if err = releaseservice.CreateNewTag(ctx, ctx.User, ctx.Repo.Repository, form.Target, form.TagName, msg); err != nil { if models.IsErrTagAlreadyExists(err) { e := err.(models.ErrTagAlreadyExists) ctx.Flash.Error(ctx.Tr("repo.branch.tag_collision", e.TagName)) @@ -516,7 +516,7 @@ func DeleteTag(ctx *context.Context) { } func deleteReleaseOrTag(ctx *context.Context, isDelTag bool) { - if err := releaseservice.DeleteReleaseByID(ctx.FormInt64("id"), ctx.User, isDelTag); err != nil { + if err := releaseservice.DeleteReleaseByID(ctx, ctx.FormInt64("id"), ctx.User, isDelTag); err != nil { ctx.Flash.Error("DeleteReleaseByID: " + err.Error()) } else { if isDelTag { diff --git a/routers/web/repo/setting.go b/routers/web/repo/setting.go index b7269b6718..d1c03b59a6 100644 --- a/routers/web/repo/setting.go +++ b/routers/web/repo/setting.go @@ -66,7 +66,7 @@ func Settings(ctx *context.Context) { ctx.Data["DisableNewPushMirrors"] = setting.Mirror.DisableNewPush ctx.Data["DefaultMirrorInterval"] = setting.Mirror.DefaultInterval - signing, _ := asymkey_service.SigningKey(ctx.Repo.Repository.RepoPath()) + signing, _ := asymkey_service.SigningKey(ctx, ctx.Repo.Repository.RepoPath()) ctx.Data["SigningKeyAvailable"] = len(signing) > 0 ctx.Data["SigningSettings"] = setting.Repository.Signing ctx.Data["CodeIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled @@ -221,7 +221,7 @@ func SettingsPost(ctx *context.Context) { return } - if err := mirror_service.UpdateAddress(ctx.Repo.Mirror, address); err != nil { + if err := mirror_service.UpdateAddress(ctx, ctx.Repo.Mirror, address); err != nil { ctx.ServerError("UpdateAddress", err) return } @@ -297,7 +297,7 @@ func SettingsPost(ctx *context.Context) { return } - if err = mirror_service.RemovePushMirrorRemote(m); err != nil { + if err = mirror_service.RemovePushMirrorRemote(ctx, m); err != nil { ctx.ServerError("RemovePushMirrorRemote", err) return } @@ -354,7 +354,7 @@ func SettingsPost(ctx *context.Context) { return } - if err := mirror_service.AddPushMirrorRemote(m, address); err != nil { + if err := mirror_service.AddPushMirrorRemote(ctx, m, address); err != nil { if err := repo_model.DeletePushMirrorByID(m.ID); err != nil { log.Error("DeletePushMirrorByID %v", err) } @@ -578,7 +578,7 @@ func SettingsPost(ctx *context.Context) { } repo.IsMirror = false - if _, err := repository.CleanUpMigrateInfo(repo); err != nil { + if _, err := repository.CleanUpMigrateInfo(ctx, repo); err != nil { ctx.ServerError("CleanUpMigrateInfo", err) return } else if err = repo_model.DeleteMirrorByRepoID(ctx.Repo.Repository.ID); err != nil { @@ -723,7 +723,7 @@ func SettingsPost(ctx *context.Context) { ctx.Repo.GitRepo.Close() } - if err := repo_service.DeleteRepository(ctx.User, ctx.Repo.Repository, true); err != nil { + if err := repo_service.DeleteRepository(ctx, ctx.User, ctx.Repo.Repository, true); err != nil { ctx.ServerError("DeleteRepository", err) return } @@ -742,7 +742,7 @@ func SettingsPost(ctx *context.Context) { return } - err := wiki_service.DeleteWiki(repo) + err := wiki_service.DeleteWiki(ctx, repo) if err != nil { log.Error("Delete Wiki: %v", err.Error()) } diff --git a/routers/web/repo/wiki.go b/routers/web/repo/wiki.go index d8666c7a29..e57aa5ec29 100644 --- a/routers/web/repo/wiki.go +++ b/routers/web/repo/wiki.go @@ -90,7 +90,7 @@ func findEntryForFile(commit *git.Commit, target string) (*git.TreeEntry, error) } func findWikiRepoCommit(ctx *context.Context) (*git.Repository, *git.Commit, error) { - wikiRepo, err := git.OpenRepository(ctx.Repo.Repository.WikiPath()) + wikiRepo, err := git.OpenRepositoryCtx(ctx, ctx.Repo.Repository.WikiPath()) if err != nil { ctx.ServerError("OpenRepository", err) return nil, nil, err @@ -220,6 +220,7 @@ func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) { } var rctx = &markup.RenderContext{ + Ctx: ctx, URLPrefix: ctx.Repo.RepoLink, Metas: ctx.Repo.Repository.ComposeDocumentMetas(), IsWiki: true, @@ -657,7 +658,7 @@ func NewWikiPost(ctx *context.Context) { form.Message = ctx.Tr("repo.editor.add", form.Title) } - if err := wiki_service.AddWikiPage(ctx.User, ctx.Repo.Repository, wikiName, form.Content, form.Message); err != nil { + if err := wiki_service.AddWikiPage(ctx, ctx.User, ctx.Repo.Repository, wikiName, form.Content, form.Message); err != nil { if models.IsErrWikiReservedName(err) { ctx.Data["Err_Title"] = true ctx.RenderWithErr(ctx.Tr("repo.wiki.reserved_page", wikiName), tplWikiNew, &form) @@ -709,7 +710,7 @@ func EditWikiPost(ctx *context.Context) { form.Message = ctx.Tr("repo.editor.update", form.Title) } - if err := wiki_service.EditWikiPage(ctx.User, ctx.Repo.Repository, oldWikiName, newWikiName, form.Content, form.Message); err != nil { + if err := wiki_service.EditWikiPage(ctx, ctx.User, ctx.Repo.Repository, oldWikiName, newWikiName, form.Content, form.Message); err != nil { ctx.ServerError("EditWikiPage", err) return } @@ -724,7 +725,7 @@ func DeleteWikiPagePost(ctx *context.Context) { wikiName = "Home" } - if err := wiki_service.DeleteWikiPage(ctx.User, ctx.Repo.Repository, wikiName); err != nil { + if err := wiki_service.DeleteWikiPage(ctx, ctx.User, ctx.Repo.Repository, wikiName); err != nil { ctx.ServerError("DeleteWikiPage", err) return } diff --git a/routers/web/user/home.go b/routers/web/user/home.go index 367cf5ca08..04459573af 100644 --- a/routers/web/user/home.go +++ b/routers/web/user/home.go @@ -540,7 +540,7 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) { } } - commitStatus, err := pull_service.GetIssuesLastCommitStatus(issues) + commitStatus, err := pull_service.GetIssuesLastCommitStatus(ctx, issues) if err != nil { ctx.ServerError("GetIssuesLastCommitStatus", err) return diff --git a/services/agit/agit.go b/services/agit/agit.go index 34f097b42d..eae20a8187 100644 --- a/services/agit/agit.go +++ b/services/agit/agit.go @@ -156,7 +156,7 @@ func ProcRecive(ctx *context.PrivateContext, opts *private.HookOptions) []privat Flow: models.PullRequestFlowAGit, } - if err := pull_service.NewPullRequest(repo, prIssue, []int64{}, []string{}, pr, []int64{}); err != nil { + if err := pull_service.NewPullRequest(ctx, repo, prIssue, []int64{}, []string{}, pr, []int64{}); err != nil { if models.IsErrUserDoesNotHaveAccessToRepo(err) { ctx.Error(http.StatusBadRequest, "UserDoesNotHaveAccessToRepo", err.Error()) return nil @@ -205,7 +205,7 @@ func ProcRecive(ctx *context.PrivateContext, opts *private.HookOptions) []privat } if !forcePush { - output, err := git.NewCommand("rev-list", "--max-count=1", oldCommitID, "^"+opts.NewCommitIDs[i]).RunInDirWithEnv(repo.RepoPath(), os.Environ()) + output, err := git.NewCommandContext(ctx, "rev-list", "--max-count=1", oldCommitID, "^"+opts.NewCommitIDs[i]).RunInDirWithEnv(repo.RepoPath(), os.Environ()) if err != nil { log.Error("Unable to detect force push between: %s and %s in %-v Error: %v", oldCommitID, opts.NewCommitIDs[i], repo, err) ctx.JSON(http.StatusInternalServerError, private.Response{ @@ -224,7 +224,7 @@ func ProcRecive(ctx *context.PrivateContext, opts *private.HookOptions) []privat } pr.HeadCommitID = opts.NewCommitIDs[i] - if err = pull_service.UpdateRef(pr); err != nil { + if err = pull_service.UpdateRef(ctx, pr); err != nil { log.Error("Failed to update pull ref. Error: %v", err) ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ "Err": fmt.Sprintf("Failed to update pull ref. Error: %v", err), @@ -249,7 +249,7 @@ func ProcRecive(ctx *context.PrivateContext, opts *private.HookOptions) []privat }) return nil } - comment, err := models.CreatePushPullComment(pusher, pr, oldCommitID, opts.NewCommitIDs[i]) + comment, err := models.CreatePushPullComment(ctx, pusher, pr, oldCommitID, opts.NewCommitIDs[i]) if err == nil && comment != nil { notification.NotifyPullRequestPushCommits(pusher, pr, comment) } diff --git a/services/asymkey/sign.go b/services/asymkey/sign.go index 79c5db18a6..c93863a8fd 100644 --- a/services/asymkey/sign.go +++ b/services/asymkey/sign.go @@ -5,6 +5,7 @@ package asymkey import ( + "context" "fmt" "strings" @@ -82,22 +83,22 @@ func IsErrWontSign(err error) bool { } // SigningKey returns the KeyID and git Signature for the repo -func SigningKey(repoPath string) (string, *git.Signature) { +func SigningKey(ctx context.Context, repoPath string) (string, *git.Signature) { if setting.Repository.Signing.SigningKey == "none" { return "", nil } if setting.Repository.Signing.SigningKey == "default" || setting.Repository.Signing.SigningKey == "" { // Can ignore the error here as it means that commit.gpgsign is not set - value, _ := git.NewCommand("config", "--get", "commit.gpgsign").RunInDir(repoPath) + value, _ := git.NewCommandContext(ctx, "config", "--get", "commit.gpgsign").RunInDir(repoPath) sign, valid := git.ParseBool(strings.TrimSpace(value)) if !sign || !valid { return "", nil } - signingKey, _ := git.NewCommand("config", "--get", "user.signingkey").RunInDir(repoPath) - signingName, _ := git.NewCommand("config", "--get", "user.name").RunInDir(repoPath) - signingEmail, _ := git.NewCommand("config", "--get", "user.email").RunInDir(repoPath) + signingKey, _ := git.NewCommandContext(ctx, "config", "--get", "user.signingkey").RunInDir(repoPath) + signingName, _ := git.NewCommandContext(ctx, "config", "--get", "user.name").RunInDir(repoPath) + signingEmail, _ := git.NewCommandContext(ctx, "config", "--get", "user.email").RunInDir(repoPath) return strings.TrimSpace(signingKey), &git.Signature{ Name: strings.TrimSpace(signingName), Email: strings.TrimSpace(signingEmail), @@ -111,13 +112,13 @@ func SigningKey(repoPath string) (string, *git.Signature) { } // PublicSigningKey gets the public signing key within a provided repository directory -func PublicSigningKey(repoPath string) (string, error) { - signingKey, _ := SigningKey(repoPath) +func PublicSigningKey(ctx context.Context, repoPath string) (string, error) { + signingKey, _ := SigningKey(ctx, repoPath) if signingKey == "" { return "", nil } - content, stderr, err := process.GetManager().ExecDir(-1, repoPath, + content, stderr, err := process.GetManager().ExecDir(ctx, -1, repoPath, "gpg --export -a", "gpg", "--export", "-a", signingKey) if err != nil { log.Error("Unable to get default signing key in %s: %s, %s, %v", repoPath, signingKey, stderr, err) @@ -127,9 +128,9 @@ func PublicSigningKey(repoPath string) (string, error) { } // SignInitialCommit determines if we should sign the initial commit to this repository -func SignInitialCommit(repoPath string, u *user_model.User) (bool, string, *git.Signature, error) { +func SignInitialCommit(ctx context.Context, repoPath string, u *user_model.User) (bool, string, *git.Signature, error) { rules := signingModeFromStrings(setting.Repository.Signing.InitialCommit) - signingKey, sig := SigningKey(repoPath) + signingKey, sig := SigningKey(ctx, repoPath) if signingKey == "" { return false, "", nil, &ErrWontSign{noKey} } @@ -163,9 +164,9 @@ Loop: } // SignWikiCommit determines if we should sign the commits to this repository wiki -func SignWikiCommit(repoWikiPath string, u *user_model.User) (bool, string, *git.Signature, error) { +func SignWikiCommit(ctx context.Context, repoWikiPath string, u *user_model.User) (bool, string, *git.Signature, error) { rules := signingModeFromStrings(setting.Repository.Signing.Wiki) - signingKey, sig := SigningKey(repoWikiPath) + signingKey, sig := SigningKey(ctx, repoWikiPath) if signingKey == "" { return false, "", nil, &ErrWontSign{noKey} } @@ -194,7 +195,7 @@ Loop: return false, "", nil, &ErrWontSign{twofa} } case parentSigned: - gitRepo, err := git.OpenRepository(repoWikiPath) + gitRepo, err := git.OpenRepositoryCtx(ctx, repoWikiPath) if err != nil { return false, "", nil, err } @@ -216,9 +217,9 @@ Loop: } // SignCRUDAction determines if we should sign a CRUD commit to this repository -func SignCRUDAction(repoPath string, u *user_model.User, tmpBasePath, parentCommit string) (bool, string, *git.Signature, error) { +func SignCRUDAction(ctx context.Context, repoPath string, u *user_model.User, tmpBasePath, parentCommit string) (bool, string, *git.Signature, error) { rules := signingModeFromStrings(setting.Repository.Signing.CRUDActions) - signingKey, sig := SigningKey(repoPath) + signingKey, sig := SigningKey(ctx, repoPath) if signingKey == "" { return false, "", nil, &ErrWontSign{noKey} } @@ -247,7 +248,7 @@ Loop: return false, "", nil, &ErrWontSign{twofa} } case parentSigned: - gitRepo, err := git.OpenRepository(tmpBasePath) + gitRepo, err := git.OpenRepositoryCtx(ctx, tmpBasePath) if err != nil { return false, "", nil, err } @@ -269,14 +270,14 @@ Loop: } // SignMerge determines if we should sign a PR merge commit to the base repository -func SignMerge(pr *models.PullRequest, u *user_model.User, tmpBasePath, baseCommit, headCommit string) (bool, string, *git.Signature, error) { +func SignMerge(ctx context.Context, pr *models.PullRequest, u *user_model.User, tmpBasePath, baseCommit, headCommit string) (bool, string, *git.Signature, error) { if err := pr.LoadBaseRepo(); err != nil { log.Error("Unable to get Base Repo for pull request") return false, "", nil, err } repo := pr.BaseRepo - signingKey, signer := SigningKey(repo.RepoPath()) + signingKey, signer := SigningKey(ctx, repo.RepoPath()) if signingKey == "" { return false, "", nil, &ErrWontSign{noKey} } @@ -321,7 +322,7 @@ Loop: } case baseSigned: if gitRepo == nil { - gitRepo, err = git.OpenRepository(tmpBasePath) + gitRepo, err = git.OpenRepositoryCtx(ctx, tmpBasePath) if err != nil { return false, "", nil, err } @@ -337,7 +338,7 @@ Loop: } case headSigned: if gitRepo == nil { - gitRepo, err = git.OpenRepository(tmpBasePath) + gitRepo, err = git.OpenRepositoryCtx(ctx, tmpBasePath) if err != nil { return false, "", nil, err } @@ -353,7 +354,7 @@ Loop: } case commitsSigned: if gitRepo == nil { - gitRepo, err = git.OpenRepository(tmpBasePath) + gitRepo, err = git.OpenRepositoryCtx(ctx, tmpBasePath) if err != nil { return false, "", nil, err } diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index fb3e32935d..43af2deb4b 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -685,8 +685,8 @@ type Diff struct { } // LoadComments loads comments into each line -func (diff *Diff) LoadComments(issue *models.Issue, currentUser *user_model.User) error { - allComments, err := models.FetchCodeComments(issue, currentUser) +func (diff *Diff) LoadComments(ctx context.Context, issue *models.Issue, currentUser *user_model.User) error { + allComments, err := models.FetchCodeComments(ctx, issue, currentUser) if err != nil { return err } @@ -1407,7 +1407,7 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff IndexFile: indexFilename, WorkTree: worktree, } - ctx, cancel := context.WithCancel(git.DefaultContext) + ctx, cancel := context.WithCancel(ctx) if err := checker.Init(ctx); err != nil { log.Error("Unable to open checker for %s. Error: %v", opts.AfterCommitID, err) } else { @@ -1484,12 +1484,12 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff if len(opts.BeforeCommitID) == 0 || opts.BeforeCommitID == git.EmptySHA { shortstatArgs = []string{git.EmptyTreeSHA, opts.AfterCommitID} } - diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(repoPath, shortstatArgs...) + diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(ctx, repoPath, shortstatArgs...) if err != nil && strings.Contains(err.Error(), "no merge base") { // git >= 2.28 now returns an error if base and head have become unrelated. // previously it would return the results of git diff --shortstat base head so let's try that... shortstatArgs = []string{opts.BeforeCommitID, opts.AfterCommitID} - diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(repoPath, shortstatArgs...) + diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(ctx, repoPath, shortstatArgs...) } if err != nil { return nil, err diff --git a/services/gitdiff/gitdiff_test.go b/services/gitdiff/gitdiff_test.go index b64ac092aa..2f8b8491cd 100644 --- a/services/gitdiff/gitdiff_test.go +++ b/services/gitdiff/gitdiff_test.go @@ -13,6 +13,7 @@ import ( "testing" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" @@ -670,7 +671,7 @@ func TestDiff_LoadComments(t *testing.T) { issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue) user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User) diff := setupDefaultDiff() - assert.NoError(t, diff.LoadComments(issue, user)) + assert.NoError(t, diff.LoadComments(db.DefaultContext, issue, user)) assert.Len(t, diff.Files[0].Sections[0].Lines[0].Comments, 2) } diff --git a/services/mailer/mail.go b/services/mailer/mail.go index 20552be584..5244cd0433 100644 --- a/services/mailer/mail.go +++ b/services/mailer/mail.go @@ -231,6 +231,7 @@ func composeIssueCommentMessages(ctx *mailCommentContext, lang string, recipient // This is the body of the new issue or comment, not the mail body body, err := markdown.RenderString(&markup.RenderContext{ + Ctx: ctx, URLPrefix: ctx.Issue.Repo.HTMLURL(), Metas: ctx.Issue.Repo.ComposeMetas(), }, ctx.Content) diff --git a/services/mailer/mail_comment.go b/services/mailer/mail_comment.go index 3662164092..a42458b2c2 100644 --- a/services/mailer/mail_comment.go +++ b/services/mailer/mail_comment.go @@ -5,6 +5,8 @@ package mailer import ( + "context" + "code.gitea.io/gitea/models" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/log" @@ -12,7 +14,7 @@ import ( ) // MailParticipantsComment sends new comment emails to repository watchers and mentioned people. -func MailParticipantsComment(c *models.Comment, opType models.ActionType, issue *models.Issue, mentions []*user_model.User) error { +func MailParticipantsComment(ctx context.Context, c *models.Comment, opType models.ActionType, issue *models.Issue, mentions []*user_model.User) error { if setting.MailService == nil { // No mail service configured return nil @@ -24,6 +26,7 @@ func MailParticipantsComment(c *models.Comment, opType models.ActionType, issue } if err := mailIssueCommentToParticipants( &mailCommentContext{ + Context: ctx, Issue: issue, Doer: c.Poster, ActionType: opType, @@ -36,7 +39,7 @@ func MailParticipantsComment(c *models.Comment, opType models.ActionType, issue } // MailMentionsComment sends email to users mentioned in a code comment -func MailMentionsComment(pr *models.PullRequest, c *models.Comment, mentions []*user_model.User) (err error) { +func MailMentionsComment(ctx context.Context, pr *models.PullRequest, c *models.Comment, mentions []*user_model.User) (err error) { if setting.MailService == nil { // No mail service configured return nil @@ -46,6 +49,7 @@ func MailMentionsComment(pr *models.PullRequest, c *models.Comment, mentions []* visited[c.Poster.ID] = true if err = mailIssueCommentBatch( &mailCommentContext{ + Context: ctx, Issue: pr.Issue, Doer: c.Poster, ActionType: models.ActionCommentPull, diff --git a/services/mailer/mail_issue.go b/services/mailer/mail_issue.go index c9cc2e015a..1df8332116 100644 --- a/services/mailer/mail_issue.go +++ b/services/mailer/mail_issue.go @@ -5,6 +5,7 @@ package mailer import ( + "context" "fmt" "code.gitea.io/gitea/models" @@ -21,6 +22,7 @@ func fallbackMailSubject(issue *models.Issue) string { } type mailCommentContext struct { + context.Context Issue *models.Issue Doer *user_model.User ActionType models.ActionType diff --git a/services/mailer/mail_release.go b/services/mailer/mail_release.go index ee4c6f3a59..1ca9ad02d7 100644 --- a/services/mailer/mail_release.go +++ b/services/mailer/mail_release.go @@ -6,6 +6,7 @@ package mailer import ( "bytes" + "context" "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" @@ -25,7 +26,7 @@ const ( ) // MailNewRelease send new release notify to all all repo watchers. -func MailNewRelease(rel *models.Release) { +func MailNewRelease(ctx context.Context, rel *models.Release) { if setting.MailService == nil { // No mail service configured return @@ -51,15 +52,16 @@ func MailNewRelease(rel *models.Release) { } for lang, tos := range langMap { - mailNewRelease(lang, tos, rel) + mailNewRelease(ctx, lang, tos, rel) } } -func mailNewRelease(lang string, tos []string, rel *models.Release) { +func mailNewRelease(ctx context.Context, lang string, tos []string, rel *models.Release) { locale := translation.NewLocale(lang) var err error rel.RenderedNote, err = markdown.RenderString(&markup.RenderContext{ + Ctx: ctx, URLPrefix: rel.Repo.Link(), Metas: rel.Repo.ComposeMetas(), }, rel.Note) diff --git a/services/migrations/dump.go b/services/migrations/dump.go index 1a8a3d296c..f4e797b357 100644 --- a/services/migrations/dump.go +++ b/services/migrations/dump.go @@ -150,7 +150,7 @@ func (g *RepositoryDumper) CreateRepo(repo *base.Repository, opts base.MigrateOp return err } - err = git.Clone(remoteAddr, repoPath, git.CloneRepoOptions{ + err = git.Clone(g.ctx, remoteAddr, repoPath, git.CloneRepoOptions{ Mirror: true, Quiet: true, Timeout: migrateTimeout, @@ -161,13 +161,13 @@ func (g *RepositoryDumper) CreateRepo(repo *base.Repository, opts base.MigrateOp if opts.Wiki { wikiPath := g.wikiPath() - wikiRemotePath := repository.WikiRemoteURL(remoteAddr) + wikiRemotePath := repository.WikiRemoteURL(g.ctx, remoteAddr) if len(wikiRemotePath) > 0 { if err := os.MkdirAll(wikiPath, os.ModePerm); err != nil { return fmt.Errorf("Failed to remove %s: %v", wikiPath, err) } - if err := git.Clone(wikiRemotePath, wikiPath, git.CloneRepoOptions{ + if err := git.Clone(g.ctx, wikiRemotePath, wikiPath, git.CloneRepoOptions{ Mirror: true, Quiet: true, Timeout: migrateTimeout, @@ -181,7 +181,7 @@ func (g *RepositoryDumper) CreateRepo(repo *base.Repository, opts base.MigrateOp } } - g.gitRepo, err = git.OpenRepository(g.gitPath()) + g.gitRepo, err = git.OpenRepositoryCtx(g.ctx, g.gitPath()) return err } @@ -478,7 +478,7 @@ func (g *RepositoryDumper) CreatePullRequests(prs ...*base.PullRequest) error { } if ok { - _, err = git.NewCommand("fetch", remote, pr.Head.Ref).RunInDir(g.gitPath()) + _, err = git.NewCommandContext(g.ctx, "fetch", remote, pr.Head.Ref).RunInDir(g.gitPath()) if err != nil { log.Error("Fetch branch from %s failed: %v", pr.Head.CloneURL, err) } else { diff --git a/services/migrations/gitea_uploader.go b/services/migrations/gitea_uploader.go index 035ca2e5a2..e939046e90 100644 --- a/services/migrations/gitea_uploader.go +++ b/services/migrations/gitea_uploader.go @@ -132,7 +132,7 @@ func (g *GiteaLocalUploader) CreateRepo(repo *base.Repository, opts base.Migrate if err != nil { return err } - g.gitRepo, err = git.OpenRepository(r.RepoPath()) + g.gitRepo, err = git.OpenRepositoryCtx(g.ctx, r.RepoPath()) return err } @@ -669,7 +669,7 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR } if ok { - _, err = git.NewCommand("fetch", remote, pr.Head.Ref).RunInDir(g.repo.RepoPath()) + _, err = git.NewCommandContext(g.ctx, "fetch", remote, pr.Head.Ref).RunInDir(g.repo.RepoPath()) if err != nil { log.Error("Fetch branch from %s failed: %v", pr.Head.CloneURL, err) } else { @@ -693,7 +693,7 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR } else { head = pr.Head.Ref // Ensure the closed PR SHA still points to an existing ref - _, err = git.NewCommand("rev-list", "--quiet", "-1", pr.Head.SHA).RunInDir(g.repo.RepoPath()) + _, err = git.NewCommandContext(g.ctx, "rev-list", "--quiet", "-1", pr.Head.SHA).RunInDir(g.repo.RepoPath()) if err != nil { if pr.Head.SHA != "" { // Git update-ref remove bad references with a relative path diff --git a/services/mirror/mirror_pull.go b/services/mirror/mirror_pull.go index da2221d915..b9333c77f3 100644 --- a/services/mirror/mirror_pull.go +++ b/services/mirror/mirror_pull.go @@ -30,30 +30,30 @@ import ( const gitShortEmptySha = "0000000" // UpdateAddress writes new address to Git repository and database -func UpdateAddress(m *repo_model.Mirror, addr string) error { +func UpdateAddress(ctx context.Context, m *repo_model.Mirror, addr string) error { remoteName := m.GetRemoteName() repoPath := m.Repo.RepoPath() // Remove old remote - _, err := git.NewCommand("remote", "rm", remoteName).RunInDir(repoPath) + _, err := git.NewCommandContext(ctx, "remote", "rm", remoteName).RunInDir(repoPath) if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") { return err } - _, err = git.NewCommand("remote", "add", remoteName, "--mirror=fetch", addr).RunInDir(repoPath) + _, err = git.NewCommandContext(ctx, "remote", "add", remoteName, "--mirror=fetch", addr).RunInDir(repoPath) if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") { return err } if m.Repo.HasWiki() { wikiPath := m.Repo.WikiPath() - wikiRemotePath := repo_module.WikiRemoteURL(addr) + wikiRemotePath := repo_module.WikiRemoteURL(ctx, addr) // Remove old remote of wiki - _, err := git.NewCommand("remote", "rm", remoteName).RunInDir(wikiPath) + _, err := git.NewCommandContext(ctx, "remote", "rm", remoteName).RunInDir(wikiPath) if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") { return err } - _, err = git.NewCommand("remote", "add", remoteName, "--mirror=fetch", wikiRemotePath).RunInDir(wikiPath) + _, err = git.NewCommandContext(ctx, "remote", "add", remoteName, "--mirror=fetch", wikiRemotePath).RunInDir(wikiPath) if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") { return err } @@ -250,7 +250,7 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo } output := stderrBuilder.String() - gitRepo, err := git.OpenRepository(repoPath) + gitRepo, err := git.OpenRepositoryCtx(ctx, repoPath) if err != nil { log.Error("OpenRepository: %v", err) return nil, false @@ -337,7 +337,7 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo } log.Trace("SyncMirrors [repo: %-v]: invalidating mirror branch caches...", m.Repo) - branches, _, err := git.GetBranchesByPath(m.Repo.RepoPath(), 0, 0) + branches, _, err := git.GetBranchesByPath(ctx, m.Repo.RepoPath(), 0, 0) if err != nil { log.Error("GetBranches: %v", err) return nil, false @@ -427,7 +427,7 @@ func SyncPullMirror(ctx context.Context, repoID int64) bool { OldCommitID: git.EmptySHA, NewCommitID: commitID, }, repo_module.NewPushCommits()) - notification.NotifySyncCreateRef(m.Repo.MustOwner(), m.Repo, tp, result.refName) + notification.NotifySyncCreateRef(m.Repo.MustOwner(), m.Repo, tp, result.refName, commitID) continue } @@ -438,12 +438,12 @@ func SyncPullMirror(ctx context.Context, repoID int64) bool { } // Push commits - oldCommitID, err := git.GetFullCommitID(gitRepo.Path, result.oldCommitID) + oldCommitID, err := git.GetFullCommitID(gitRepo.Ctx, gitRepo.Path, result.oldCommitID) if err != nil { log.Error("GetFullCommitID [%d]: %v", m.RepoID, err) continue } - newCommitID, err := git.GetFullCommitID(gitRepo.Path, result.newCommitID) + newCommitID, err := git.GetFullCommitID(gitRepo.Ctx, gitRepo.Path, result.newCommitID) if err != nil { log.Error("GetFullCommitID [%d]: %v", m.RepoID, err) continue @@ -470,7 +470,7 @@ func SyncPullMirror(ctx context.Context, repoID int64) bool { log.Trace("SyncMirrors [repo: %-v]: done notifying updated branches/tags - now updating last commit time", m.Repo) // Get latest commit date and update to current repository updated time - commitDate, err := git.GetLatestCommitTime(m.Repo.RepoPath()) + commitDate, err := git.GetLatestCommitTime(ctx, m.Repo.RepoPath()) if err != nil { log.Error("GetLatestCommitDate [%d]: %v", m.RepoID, err) return false diff --git a/services/mirror/mirror_push.go b/services/mirror/mirror_push.go index e1c395ea74..9895f0c995 100644 --- a/services/mirror/mirror_push.go +++ b/services/mirror/mirror_push.go @@ -26,15 +26,15 @@ import ( var stripExitStatus = regexp.MustCompile(`exit status \d+ - `) // AddPushMirrorRemote registers the push mirror remote. -func AddPushMirrorRemote(m *repo_model.PushMirror, addr string) error { +func AddPushMirrorRemote(ctx context.Context, m *repo_model.PushMirror, addr string) error { addRemoteAndConfig := func(addr, path string) error { - if _, err := git.NewCommand("remote", "add", "--mirror=push", m.RemoteName, addr).RunInDir(path); err != nil { + if _, err := git.NewCommandContext(ctx, "remote", "add", "--mirror=push", m.RemoteName, addr).RunInDir(path); err != nil { return err } - if _, err := git.NewCommand("config", "--add", "remote."+m.RemoteName+".push", "+refs/heads/*:refs/heads/*").RunInDir(path); err != nil { + if _, err := git.NewCommandContext(ctx, "config", "--add", "remote."+m.RemoteName+".push", "+refs/heads/*:refs/heads/*").RunInDir(path); err != nil { return err } - if _, err := git.NewCommand("config", "--add", "remote."+m.RemoteName+".push", "+refs/tags/*:refs/tags/*").RunInDir(path); err != nil { + if _, err := git.NewCommandContext(ctx, "config", "--add", "remote."+m.RemoteName+".push", "+refs/tags/*:refs/tags/*").RunInDir(path); err != nil { return err } return nil @@ -45,7 +45,7 @@ func AddPushMirrorRemote(m *repo_model.PushMirror, addr string) error { } if m.Repo.HasWiki() { - wikiRemoteURL := repository.WikiRemoteURL(addr) + wikiRemoteURL := repository.WikiRemoteURL(ctx, addr) if len(wikiRemoteURL) > 0 { if err := addRemoteAndConfig(wikiRemoteURL, m.Repo.WikiPath()); err != nil { return err @@ -57,8 +57,8 @@ func AddPushMirrorRemote(m *repo_model.PushMirror, addr string) error { } // RemovePushMirrorRemote removes the push mirror remote. -func RemovePushMirrorRemote(m *repo_model.PushMirror) error { - cmd := git.NewCommand("remote", "rm", m.RemoteName) +func RemovePushMirrorRemote(ctx context.Context, m *repo_model.PushMirror) error { + cmd := git.NewCommandContext(ctx, "remote", "rm", m.RemoteName) if _, err := cmd.RunInDir(m.Repo.RepoPath()); err != nil { return err diff --git a/services/pull/check.go b/services/pull/check.go index 363a716b28..3615c6c654 100644 --- a/services/pull/check.go +++ b/services/pull/check.go @@ -21,6 +21,7 @@ import ( "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification" + "code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/queue" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" @@ -69,7 +70,7 @@ func checkAndUpdateStatus(pr *models.PullRequest) { // getMergeCommit checks if a pull request got merged // Returns the git.Commit of the pull request if merged -func getMergeCommit(pr *models.PullRequest) (*git.Commit, error) { +func getMergeCommit(ctx context.Context, pr *models.PullRequest) (*git.Commit, error) { if pr.BaseRepo == nil { var err error pr.BaseRepo, err = repo_model.GetRepositoryByID(pr.BaseRepoID) @@ -91,7 +92,7 @@ func getMergeCommit(pr *models.PullRequest) (*git.Commit, error) { headFile := pr.GetGitRefName() // Check if a pull request is merged into BaseBranch - _, err = git.NewCommand("merge-base", "--is-ancestor", headFile, pr.BaseBranch). + _, err = git.NewCommandContext(ctx, "merge-base", "--is-ancestor", headFile, pr.BaseBranch). RunInDirWithEnv(pr.BaseRepo.RepoPath(), []string{"GIT_INDEX_FILE=" + indexTmpPath, "GIT_DIR=" + pr.BaseRepo.RepoPath()}) if err != nil { // Errors are signaled by a non-zero status that is not 1 @@ -112,7 +113,7 @@ func getMergeCommit(pr *models.PullRequest) (*git.Commit, error) { cmd := commitID[:40] + ".." + pr.BaseBranch // Get the commit from BaseBranch where the pull request got merged - mergeCommit, err := git.NewCommand("rev-list", "--ancestry-path", "--merges", "--reverse", cmd). + mergeCommit, err := git.NewCommandContext(ctx, "rev-list", "--ancestry-path", "--merges", "--reverse", cmd). RunInDirWithEnv("", []string{"GIT_INDEX_FILE=" + indexTmpPath, "GIT_DIR=" + pr.BaseRepo.RepoPath()}) if err != nil { return nil, fmt.Errorf("git rev-list --ancestry-path --merges --reverse: %v", err) @@ -121,7 +122,7 @@ func getMergeCommit(pr *models.PullRequest) (*git.Commit, error) { mergeCommit = commitID[:40] } - gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath()) + gitRepo, err := git.OpenRepositoryCtx(ctx, pr.BaseRepo.RepoPath()) if err != nil { return nil, fmt.Errorf("OpenRepository: %v", err) } @@ -137,7 +138,7 @@ func getMergeCommit(pr *models.PullRequest) (*git.Commit, error) { // manuallyMerged checks if a pull request got manually merged // When a pull request got manually merged mark the pull request as merged -func manuallyMerged(pr *models.PullRequest) bool { +func manuallyMerged(ctx context.Context, pr *models.PullRequest) bool { if err := pr.LoadBaseRepo(); err != nil { log.Error("PullRequest[%d].LoadBaseRepo: %v", pr.ID, err) return false @@ -153,7 +154,7 @@ func manuallyMerged(pr *models.PullRequest) bool { return false } - commit, err := getMergeCommit(pr) + commit, err := getMergeCommit(ctx, pr) if err != nil { log.Error("PullRequest[%d].getMergeCommit: %v", pr.ID, err) return false @@ -219,28 +220,39 @@ func handle(data ...queue.Data) { for _, datum := range data { id, _ := strconv.ParseInt(datum.(string), 10, 64) - log.Trace("Testing PR ID %d from the pull requests patch checking queue", id) - - pr, err := models.GetPullRequestByID(id) - if err != nil { - log.Error("GetPullRequestByID[%s]: %v", datum, err) - continue - } else if pr.HasMerged { - continue - } else if manuallyMerged(pr) { - continue - } else if err = TestPatch(pr); err != nil { - log.Error("testPatch[%d]: %v", pr.ID, err) - pr.Status = models.PullRequestStatusError - if err := pr.UpdateCols("status"); err != nil { - log.Error("update pr [%d] status to PullRequestStatusError failed: %v", pr.ID, err) - } - continue - } - checkAndUpdateStatus(pr) + testPR(id) } } +func testPR(id int64) { + ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("Test PR[%d] from patch checking queue", id)) + defer finished() + + pr, err := models.GetPullRequestByID(id) + if err != nil { + log.Error("GetPullRequestByID[%d]: %v", id, err) + return + } + + if pr.HasMerged { + return + } + + if manuallyMerged(ctx, pr) { + return + } + + if err := TestPatch(pr); err != nil { + log.Error("testPatch[%d]: %v", pr.ID, err) + pr.Status = models.PullRequestStatusError + if err := pr.UpdateCols("status"); err != nil { + log.Error("update pr [%d] status to PullRequestStatusError failed: %v", pr.ID, err) + } + return + } + checkAndUpdateStatus(pr) +} + // CheckPrsForBaseBranch check all pulls with bseBrannch func CheckPrsForBaseBranch(baseRepo *repo_model.Repository, baseBranchName string) error { prs, err := models.GetUnmergedPullRequestsByBaseInfo(baseRepo.ID, baseBranchName) diff --git a/services/pull/commit_status.go b/services/pull/commit_status.go index 5324b49fa9..d605f8e301 100644 --- a/services/pull/commit_status.go +++ b/services/pull/commit_status.go @@ -6,6 +6,8 @@ package pull import ( + "context" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/git" @@ -80,7 +82,7 @@ func IsCommitStatusContextSuccess(commitStatuses []*models.CommitStatus, require } // IsPullCommitStatusPass returns if all required status checks PASS -func IsPullCommitStatusPass(pr *models.PullRequest) (bool, error) { +func IsPullCommitStatusPass(ctx context.Context, pr *models.PullRequest) (bool, error) { if err := pr.LoadProtectedBranch(); err != nil { return false, errors.Wrap(err, "GetLatestCommitStatus") } @@ -88,7 +90,7 @@ func IsPullCommitStatusPass(pr *models.PullRequest) (bool, error) { return true, nil } - state, err := GetPullRequestCommitStatusState(pr) + state, err := GetPullRequestCommitStatusState(ctx, pr) if err != nil { return false, err } @@ -96,18 +98,18 @@ func IsPullCommitStatusPass(pr *models.PullRequest) (bool, error) { } // GetPullRequestCommitStatusState returns pull request merged commit status state -func GetPullRequestCommitStatusState(pr *models.PullRequest) (structs.CommitStatusState, error) { +func GetPullRequestCommitStatusState(ctx context.Context, pr *models.PullRequest) (structs.CommitStatusState, error) { // Ensure HeadRepo is loaded if err := pr.LoadHeadRepo(); err != nil { return "", errors.Wrap(err, "LoadHeadRepo") } // check if all required status checks are successful - headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath()) + headGitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, pr.HeadRepo.RepoPath()) if err != nil { return "", errors.Wrap(err, "OpenRepository") } - defer headGitRepo.Close() + defer closer.Close() if pr.Flow == models.PullRequestFlowGithub && !headGitRepo.IsBranchExist(pr.HeadBranch) { return "", errors.New("Head branch does not exist, can not merge") diff --git a/services/pull/lfs.go b/services/pull/lfs.go index c9ec415cb1..fada9b6121 100644 --- a/services/pull/lfs.go +++ b/services/pull/lfs.go @@ -7,6 +7,7 @@ package pull import ( "bufio" + "context" "io" "strconv" "sync" @@ -18,7 +19,7 @@ import ( ) // LFSPush pushes lfs objects referred to in new commits in the head repository from the base repository -func LFSPush(tmpBasePath, mergeHeadSHA, mergeBaseSHA string, pr *models.PullRequest) error { +func LFSPush(ctx context.Context, tmpBasePath, mergeHeadSHA, mergeBaseSHA string, pr *models.PullRequest) error { // Now we have to implement git lfs push // git rev-list --objects --filter=blob:limit=1k HEAD --not base // pass blob shas in to git cat-file --batch-check (possibly unnecessary) @@ -41,19 +42,19 @@ func LFSPush(tmpBasePath, mergeHeadSHA, mergeBaseSHA string, pr *models.PullRequ go createLFSMetaObjectsFromCatFileBatch(catFileBatchReader, &wg, pr) // 5. Take the shas of the blobs and batch read them - go pipeline.CatFileBatch(shasToBatchReader, catFileBatchWriter, &wg, tmpBasePath) + go pipeline.CatFileBatch(ctx, shasToBatchReader, catFileBatchWriter, &wg, tmpBasePath) // 4. From the provided objects restrict to blobs <=1k go pipeline.BlobsLessThan1024FromCatFileBatchCheck(catFileCheckReader, shasToBatchWriter, &wg) // 3. Run batch-check on the objects retrieved from rev-list - go pipeline.CatFileBatchCheck(shasToCheckReader, catFileCheckWriter, &wg, tmpBasePath) + go pipeline.CatFileBatchCheck(ctx, shasToCheckReader, catFileCheckWriter, &wg, tmpBasePath) // 2. Check each object retrieved rejecting those without names as they will be commits or trees go pipeline.BlobsFromRevListObjects(revListReader, shasToCheckWriter, &wg) // 1. Run rev-list objects from mergeHead to mergeBase - go pipeline.RevListObjects(revListWriter, &wg, tmpBasePath, mergeHeadSHA, mergeBaseSHA, errChan) + go pipeline.RevListObjects(ctx, revListWriter, &wg, tmpBasePath, mergeHeadSHA, mergeBaseSHA, errChan) wg.Wait() select { diff --git a/services/pull/merge.go b/services/pull/merge.go index f6a6415bff..2a67507a87 100644 --- a/services/pull/merge.go +++ b/services/pull/merge.go @@ -8,6 +8,7 @@ package pull import ( "bufio" "bytes" + "context" "fmt" "os" "path/filepath" @@ -34,7 +35,7 @@ import ( // Merge merges pull request to base repository. // Caller should check PR is ready to be merged (review and status checks) // FIXME: add repoWorkingPull make sure two merges does not happen at same time. -func Merge(pr *models.PullRequest, doer *user_model.User, baseGitRepo *git.Repository, mergeStyle repo_model.MergeStyle, expectedHeadCommitID, message string) (err error) { +func Merge(ctx context.Context, pr *models.PullRequest, doer *user_model.User, baseGitRepo *git.Repository, mergeStyle repo_model.MergeStyle, expectedHeadCommitID, message string) (err error) { if err = pr.LoadHeadRepo(); err != nil { log.Error("LoadHeadRepo: %v", err) return fmt.Errorf("LoadHeadRepo: %v", err) @@ -59,7 +60,7 @@ func Merge(pr *models.PullRequest, doer *user_model.User, baseGitRepo *git.Repos go AddTestPullRequestTask(doer, pr.BaseRepo.ID, pr.BaseBranch, false, "", "") }() - pr.MergedCommitID, err = rawMerge(pr, doer, mergeStyle, expectedHeadCommitID, message) + pr.MergedCommitID, err = rawMerge(ctx, pr, doer, mergeStyle, expectedHeadCommitID, message) if err != nil { return err } @@ -117,7 +118,7 @@ func Merge(pr *models.PullRequest, doer *user_model.User, baseGitRepo *git.Repos } // rawMerge perform the merge operation without changing any pull information in database -func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle repo_model.MergeStyle, expectedHeadCommitID, message string) (string, error) { +func rawMerge(ctx context.Context, pr *models.PullRequest, doer *user_model.User, mergeStyle repo_model.MergeStyle, expectedHeadCommitID, message string) (string, error) { err := git.LoadGitVersion() if err != nil { log.Error("git.LoadGitVersion: %v", err) @@ -125,7 +126,7 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle repo_mod } // Clone base repo. - tmpBasePath, err := createTemporaryRepo(pr) + tmpBasePath, err := createTemporaryRepo(ctx, pr) if err != nil { log.Error("CreateTemporaryPath: %v", err) return "", err @@ -141,7 +142,7 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle repo_mod stagingBranch := "staging" if expectedHeadCommitID != "" { - trackingCommitID, err := git.NewCommand("show-ref", "--hash", git.BranchPrefix+trackingBranch).RunInDir(tmpBasePath) + trackingCommitID, err := git.NewCommandContext(ctx, "show-ref", "--hash", git.BranchPrefix+trackingBranch).RunInDir(tmpBasePath) if err != nil { log.Error("show-ref[%s] --hash refs/heads/trackingn: %v", tmpBasePath, git.BranchPrefix+trackingBranch, err) return "", fmt.Errorf("getDiffTree: %v", err) @@ -157,7 +158,7 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle repo_mod var outbuf, errbuf strings.Builder // Enable sparse-checkout - sparseCheckoutList, err := getDiffTree(tmpBasePath, baseBranch, trackingBranch) + sparseCheckoutList, err := getDiffTree(ctx, tmpBasePath, baseBranch, trackingBranch) if err != nil { log.Error("getDiffTree(%s, %s, %s): %v", tmpBasePath, baseBranch, trackingBranch, err) return "", fmt.Errorf("getDiffTree: %v", err) @@ -178,11 +179,11 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle repo_mod var gitConfigCommand func() *git.Command if git.CheckGitVersionAtLeast("1.8.0") == nil { gitConfigCommand = func() *git.Command { - return git.NewCommand("config", "--local") + return git.NewCommandContext(ctx, "config", "--local") } } else { gitConfigCommand = func() *git.Command { - return git.NewCommand("config") + return git.NewCommandContext(ctx, "config") } } @@ -223,7 +224,7 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle repo_mod errbuf.Reset() // Read base branch index - if err := git.NewCommand("read-tree", "HEAD").RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil { + if err := git.NewCommandContext(ctx, "read-tree", "HEAD").RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil { log.Error("git read-tree HEAD: %v\n%s\n%s", err, outbuf.String(), errbuf.String()) return "", fmt.Errorf("Unable to read base branch in to the index: %v\n%s\n%s", err, outbuf.String(), errbuf.String()) } @@ -236,7 +237,7 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle repo_mod // Determine if we should sign signArg := "" if git.CheckGitVersionAtLeast("1.7.9") == nil { - sign, keyID, signer, _ := asymkey_service.SignMerge(pr, doer, tmpBasePath, "HEAD", trackingBranch) + sign, keyID, signer, _ := asymkey_service.SignMerge(ctx, pr, doer, tmpBasePath, "HEAD", trackingBranch) if sign { signArg = "-S" + keyID if pr.BaseRepo.GetTrustModel() == repo_model.CommitterTrustModel || pr.BaseRepo.GetTrustModel() == repo_model.CollaboratorCommitterTrustModel { @@ -262,13 +263,13 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle repo_mod // Merge commits. switch mergeStyle { case repo_model.MergeStyleMerge: - cmd := git.NewCommand("merge", "--no-ff", "--no-commit", trackingBranch) + cmd := git.NewCommandContext(ctx, "merge", "--no-ff", "--no-commit", trackingBranch) if err := runMergeCommand(pr, mergeStyle, cmd, tmpBasePath); err != nil { log.Error("Unable to merge tracking into base: %v", err) return "", err } - if err := commitAndSignNoAuthor(pr, message, signArg, tmpBasePath, env); err != nil { + if err := commitAndSignNoAuthor(ctx, pr, message, signArg, tmpBasePath, env); err != nil { log.Error("Unable to make final commit: %v", err) return "", err } @@ -278,7 +279,7 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle repo_mod fallthrough case repo_model.MergeStyleRebaseMerge: // Checkout head branch - if err := git.NewCommand("checkout", "-b", stagingBranch, trackingBranch).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil { + if err := git.NewCommandContext(ctx, "checkout", "-b", stagingBranch, trackingBranch).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil { log.Error("git checkout base prior to merge post staging rebase [%s:%s -> %s:%s]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String()) return "", fmt.Errorf("git checkout base prior to merge post staging rebase [%s:%s -> %s:%s]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String()) } @@ -286,7 +287,7 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle repo_mod errbuf.Reset() // Rebase before merging - if err := git.NewCommand("rebase", baseBranch).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil { + if err := git.NewCommandContext(ctx, "rebase", baseBranch).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil { // Rebase will leave a REBASE_HEAD file in .git if there is a conflict if _, statErr := os.Stat(filepath.Join(tmpBasePath, ".git", "REBASE_HEAD")); statErr == nil { var commitSha string @@ -334,14 +335,14 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle repo_mod } // Checkout base branch again - if err := git.NewCommand("checkout", baseBranch).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil { + if err := git.NewCommandContext(ctx, "checkout", baseBranch).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil { log.Error("git checkout base prior to merge post staging rebase [%s:%s -> %s:%s]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String()) return "", fmt.Errorf("git checkout base prior to merge post staging rebase [%s:%s -> %s:%s]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String()) } outbuf.Reset() errbuf.Reset() - cmd := git.NewCommand("merge") + cmd := git.NewCommandContext(ctx, "merge") if mergeStyle == repo_model.MergeStyleRebase { cmd.AddArguments("--ff-only") } else { @@ -355,14 +356,14 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle repo_mod return "", err } if mergeStyle == repo_model.MergeStyleRebaseMerge { - if err := commitAndSignNoAuthor(pr, message, signArg, tmpBasePath, env); err != nil { + if err := commitAndSignNoAuthor(ctx, pr, message, signArg, tmpBasePath, env); err != nil { log.Error("Unable to make final commit: %v", err) return "", err } } case repo_model.MergeStyleSquash: // Merge with squash - cmd := git.NewCommand("merge", "--squash", trackingBranch) + cmd := git.NewCommandContext(ctx, "merge", "--squash", trackingBranch) if err := runMergeCommand(pr, mergeStyle, cmd, tmpBasePath); err != nil { log.Error("Unable to merge --squash tracking into base: %v", err) return "", err @@ -374,7 +375,7 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle repo_mod } sig := pr.Issue.Poster.NewGitSig() if signArg == "" { - if err := git.NewCommand("commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", message).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, &outbuf, &errbuf); err != nil { + if err := git.NewCommandContext(ctx, "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", message).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, &outbuf, &errbuf); err != nil { log.Error("git commit [%s:%s -> %s:%s]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String()) return "", fmt.Errorf("git commit [%s:%s -> %s:%s]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String()) } @@ -383,7 +384,7 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle repo_mod // add trailer message += fmt.Sprintf("\nCo-authored-by: %s\nCo-committed-by: %s\n", sig.String(), sig.String()) } - if err := git.NewCommand("commit", signArg, fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", message).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, &outbuf, &errbuf); err != nil { + if err := git.NewCommandContext(ctx, "commit", signArg, fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", message).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, &outbuf, &errbuf); err != nil { log.Error("git commit [%s:%s -> %s:%s]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String()) return "", fmt.Errorf("git commit [%s:%s -> %s:%s]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String()) } @@ -395,15 +396,15 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle repo_mod } // OK we should cache our current head and origin/headbranch - mergeHeadSHA, err := git.GetFullCommitID(tmpBasePath, "HEAD") + mergeHeadSHA, err := git.GetFullCommitID(ctx, tmpBasePath, "HEAD") if err != nil { return "", fmt.Errorf("Failed to get full commit id for HEAD: %v", err) } - mergeBaseSHA, err := git.GetFullCommitID(tmpBasePath, "original_"+baseBranch) + mergeBaseSHA, err := git.GetFullCommitID(ctx, tmpBasePath, "original_"+baseBranch) if err != nil { return "", fmt.Errorf("Failed to get full commit id for origin/%s: %v", pr.BaseBranch, err) } - mergeCommitID, err := git.GetFullCommitID(tmpBasePath, baseBranch) + mergeCommitID, err := git.GetFullCommitID(ctx, tmpBasePath, baseBranch) if err != nil { return "", fmt.Errorf("Failed to get full commit id for the new merge: %v", err) } @@ -412,7 +413,7 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle repo_mod // I think in the interests of data safety - failures to push to the lfs should prevent // the merge as you can always remerge. if setting.LFS.StartServer { - if err := LFSPush(tmpBasePath, mergeHeadSHA, mergeBaseSHA, pr); err != nil { + if err := LFSPush(ctx, tmpBasePath, mergeHeadSHA, mergeBaseSHA, pr); err != nil { return "", err } } @@ -441,9 +442,9 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle repo_mod var pushCmd *git.Command if mergeStyle == repo_model.MergeStyleRebaseUpdate { // force push the rebase result to head branch - pushCmd = git.NewCommand("push", "-f", "head_repo", stagingBranch+":"+git.BranchPrefix+pr.HeadBranch) + pushCmd = git.NewCommandContext(ctx, "push", "-f", "head_repo", stagingBranch+":"+git.BranchPrefix+pr.HeadBranch) } else { - pushCmd = git.NewCommand("push", "origin", baseBranch+":"+git.BranchPrefix+pr.BaseBranch) + pushCmd = git.NewCommandContext(ctx, "push", "origin", baseBranch+":"+git.BranchPrefix+pr.BaseBranch) } // Push back to upstream. @@ -471,15 +472,15 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle repo_mod return mergeCommitID, nil } -func commitAndSignNoAuthor(pr *models.PullRequest, message, signArg, tmpBasePath string, env []string) error { +func commitAndSignNoAuthor(ctx context.Context, pr *models.PullRequest, message, signArg, tmpBasePath string, env []string) error { var outbuf, errbuf strings.Builder if signArg == "" { - if err := git.NewCommand("commit", "-m", message).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, &outbuf, &errbuf); err != nil { + if err := git.NewCommandContext(ctx, "commit", "-m", message).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, &outbuf, &errbuf); err != nil { log.Error("git commit [%s:%s -> %s:%s]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String()) return fmt.Errorf("git commit [%s:%s -> %s:%s]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String()) } } else { - if err := git.NewCommand("commit", signArg, "-m", message).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, &outbuf, &errbuf); err != nil { + if err := git.NewCommandContext(ctx, "commit", signArg, "-m", message).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, &outbuf, &errbuf); err != nil { log.Error("git commit [%s:%s -> %s:%s]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String()) return fmt.Errorf("git commit [%s:%s -> %s:%s]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String()) } @@ -518,11 +519,11 @@ func runMergeCommand(pr *models.PullRequest, mergeStyle repo_model.MergeStyle, c var escapedSymbols = regexp.MustCompile(`([*[?! \\])`) -func getDiffTree(repoPath, baseBranch, headBranch string) (string, error) { +func getDiffTree(ctx context.Context, repoPath, baseBranch, headBranch string) (string, error) { getDiffTreeFromBranch := func(repoPath, baseBranch, headBranch string) (string, error) { var outbuf, errbuf strings.Builder // Compute the diff-tree for sparse-checkout - if err := git.NewCommand("diff-tree", "--no-commit-id", "--name-only", "-r", "-z", "--root", baseBranch, headBranch, "--").RunInDirPipeline(repoPath, &outbuf, &errbuf); err != nil { + if err := git.NewCommandContext(ctx, "diff-tree", "--no-commit-id", "--name-only", "-r", "-z", "--root", baseBranch, headBranch, "--").RunInDirPipeline(repoPath, &outbuf, &errbuf); err != nil { return "", fmt.Errorf("git diff-tree [%s base:%s head:%s]: %s", repoPath, baseBranch, headBranch, errbuf.String()) } return outbuf.String(), nil @@ -562,7 +563,7 @@ func getDiffTree(repoPath, baseBranch, headBranch string) (string, error) { } // IsSignedIfRequired check if merge will be signed if required -func IsSignedIfRequired(pr *models.PullRequest, doer *user_model.User) (bool, error) { +func IsSignedIfRequired(ctx context.Context, pr *models.PullRequest, doer *user_model.User) (bool, error) { if err := pr.LoadProtectedBranch(); err != nil { return false, err } @@ -571,7 +572,7 @@ func IsSignedIfRequired(pr *models.PullRequest, doer *user_model.User) (bool, er return true, nil } - sign, _, _, err := asymkey_service.SignMerge(pr, doer, pr.BaseRepo.RepoPath(), pr.BaseBranch, pr.GetGitRefName()) + sign, _, _, err := asymkey_service.SignMerge(ctx, pr, doer, pr.BaseRepo.RepoPath(), pr.BaseBranch, pr.GetGitRefName()) return sign, err } @@ -595,7 +596,7 @@ func IsUserAllowedToMerge(pr *models.PullRequest, p models.Permission, user *use } // CheckPRReadyToMerge checks whether the PR is ready to be merged (reviews and status checks) -func CheckPRReadyToMerge(pr *models.PullRequest, skipProtectedFilesCheck bool) (err error) { +func CheckPRReadyToMerge(ctx context.Context, pr *models.PullRequest, skipProtectedFilesCheck bool) (err error) { if err = pr.LoadBaseRepo(); err != nil { return fmt.Errorf("LoadBaseRepo: %v", err) } @@ -607,7 +608,7 @@ func CheckPRReadyToMerge(pr *models.PullRequest, skipProtectedFilesCheck bool) ( return nil } - isPass, err := IsPullCommitStatusPass(pr) + isPass, err := IsPullCommitStatusPass(ctx, pr) if err != nil { return err } diff --git a/services/pull/patch.go b/services/pull/patch.go index 0eba3f86ed..a632167916 100644 --- a/services/pull/patch.go +++ b/services/pull/patch.go @@ -26,17 +26,18 @@ import ( ) // DownloadDiffOrPatch will write the patch for the pr to the writer -func DownloadDiffOrPatch(pr *models.PullRequest, w io.Writer, patch, binary bool) error { +func DownloadDiffOrPatch(ctx context.Context, pr *models.PullRequest, w io.Writer, patch, binary bool) error { if err := pr.LoadBaseRepo(); err != nil { log.Error("Unable to load base repository ID %d for pr #%d [%d]", pr.BaseRepoID, pr.Index, pr.ID) return err } - gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath()) + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, pr.BaseRepo.RepoPath()) if err != nil { return fmt.Errorf("OpenRepository: %v", err) } - defer gitRepo.Close() + defer closer.Close() + if err := gitRepo.GetDiffOrPatch(pr.MergeBase, pr.GetGitRefName(), w, patch, binary); err != nil { log.Error("Unable to get patch file from %s to %s in %s Error: %v", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err) return fmt.Errorf("Unable to get patch file from %s to %s in %s Error: %v", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err) @@ -53,8 +54,11 @@ var patchErrorSuffices = []string{ // TestPatch will test whether a simple patch will apply func TestPatch(pr *models.PullRequest) error { + ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("TestPatch: Repo[%d]#%d", pr.BaseRepoID, pr.Index)) + defer finished() + // Clone base repo. - tmpBasePath, err := createTemporaryRepo(pr) + tmpBasePath, err := createTemporaryRepo(ctx, pr) if err != nil { log.Error("CreateTemporaryPath: %v", err) return err @@ -65,14 +69,14 @@ func TestPatch(pr *models.PullRequest) error { } }() - gitRepo, err := git.OpenRepository(tmpBasePath) + gitRepo, err := git.OpenRepositoryCtx(ctx, tmpBasePath) if err != nil { return fmt.Errorf("OpenRepository: %v", err) } defer gitRepo.Close() // 1. update merge base - pr.MergeBase, err = git.NewCommand("merge-base", "--", "base", "tracking").RunInDir(tmpBasePath) + pr.MergeBase, err = git.NewCommandContext(ctx, "merge-base", "--", "base", "tracking").RunInDir(tmpBasePath) if err != nil { var err2 error pr.MergeBase, err2 = gitRepo.GetRefCommitID(git.BranchPrefix + "base") @@ -322,7 +326,7 @@ func checkConflicts(pr *models.PullRequest, gitRepo *git.Repository, tmpBasePath pr.Status = models.PullRequestStatusChecking // 3. Read the base branch in to the index of the temporary repository - _, err = git.NewCommand("read-tree", "base").RunInDir(tmpBasePath) + _, err = git.NewCommandContext(gitRepo.Ctx, "read-tree", "base").RunInDir(tmpBasePath) if err != nil { return false, fmt.Errorf("git read-tree %s: %v", pr.BaseBranch, err) } @@ -365,7 +369,7 @@ func checkConflicts(pr *models.PullRequest, gitRepo *git.Repository, tmpBasePath // 7. Run the check command conflict = false - err = git.NewCommand(args...). + err = git.NewCommandContext(gitRepo.Ctx, args...). RunInDirTimeoutEnvFullPipelineFunc( nil, -1, tmpBasePath, nil, stderrWriter, nil, @@ -432,11 +436,11 @@ func checkConflicts(pr *models.PullRequest, gitRepo *git.Repository, tmpBasePath } // CheckFileProtection check file Protection -func CheckFileProtection(oldCommitID, newCommitID string, patterns []glob.Glob, limit int, env []string, repo *git.Repository) ([]string, error) { +func CheckFileProtection(repo *git.Repository, oldCommitID, newCommitID string, patterns []glob.Glob, limit int, env []string) ([]string, error) { if len(patterns) == 0 { return nil, nil } - affectedFiles, err := git.GetAffectedFiles(oldCommitID, newCommitID, env, repo) + affectedFiles, err := git.GetAffectedFiles(repo, oldCommitID, newCommitID, env) if err != nil { return nil, err } @@ -462,11 +466,11 @@ func CheckFileProtection(oldCommitID, newCommitID string, patterns []glob.Glob, } // CheckUnprotectedFiles check if the commit only touches unprotected files -func CheckUnprotectedFiles(oldCommitID, newCommitID string, patterns []glob.Glob, env []string, repo *git.Repository) (bool, error) { +func CheckUnprotectedFiles(repo *git.Repository, oldCommitID, newCommitID string, patterns []glob.Glob, env []string) (bool, error) { if len(patterns) == 0 { return false, nil } - affectedFiles, err := git.GetAffectedFiles(oldCommitID, newCommitID, env, repo) + affectedFiles, err := git.GetAffectedFiles(repo, oldCommitID, newCommitID, env) if err != nil { return false, err } @@ -498,7 +502,7 @@ func checkPullFilesProtection(pr *models.PullRequest, gitRepo *git.Repository) e } var err error - pr.ChangedProtectedFiles, err = CheckFileProtection(pr.MergeBase, "tracking", pr.ProtectedBranch.GetProtectedFilePatterns(), 10, os.Environ(), gitRepo) + pr.ChangedProtectedFiles, err = CheckFileProtection(gitRepo, pr.MergeBase, "tracking", pr.ProtectedBranch.GetProtectedFilePatterns(), 10, os.Environ()) if err != nil && !models.IsErrFilePathProtected(err) { return err } diff --git a/services/pull/pull.go b/services/pull/pull.go index db3533ebe4..4f691c0eba 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -9,6 +9,7 @@ import ( "bytes" "context" "fmt" + "io" "regexp" "strings" "time" @@ -22,24 +23,25 @@ import ( "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification" + "code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/setting" issue_service "code.gitea.io/gitea/services/issue" ) // NewPullRequest creates new pull request with labels for repository. -func NewPullRequest(repo *repo_model.Repository, pull *models.Issue, labelIDs []int64, uuids []string, pr *models.PullRequest, assigneeIDs []int64) error { +func NewPullRequest(ctx context.Context, repo *repo_model.Repository, pull *models.Issue, labelIDs []int64, uuids []string, pr *models.PullRequest, assigneeIDs []int64) error { if err := TestPatch(pr); err != nil { return err } - divergence, err := GetDiverging(pr) + divergence, err := GetDiverging(ctx, pr) if err != nil { return err } pr.CommitsAhead = divergence.Ahead pr.CommitsBehind = divergence.Behind - if err := models.NewPullRequest(repo, pull, labelIDs, uuids, pr); err != nil { + if err := models.NewPullRequest(ctx, repo, pull, labelIDs, uuids, pr); err != nil { return err } @@ -52,10 +54,16 @@ func NewPullRequest(repo *repo_model.Repository, pull *models.Issue, labelIDs [] pr.Issue = pull pull.PullRequest = pr + // Now - even if the request context has been cancelled as the PR has been created + // in the db and there is no way to cancel that transaction we have to proceed - therefore + // create new context and work from there + prCtx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("NewPullRequest: %s:%d", repo.FullName(), pr.Index)) + defer finished() + if pr.Flow == models.PullRequestFlowGithub { - err = PushToBaseRepo(pr) + err = PushToBaseRepo(prCtx, pr) } else { - err = UpdateRef(pr) + err = UpdateRef(prCtx, pr) } if err != nil { return err @@ -75,7 +83,7 @@ func NewPullRequest(repo *repo_model.Repository, pull *models.Issue, labelIDs [] } // add first push codes comment - baseGitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath()) + baseGitRepo, err := git.OpenRepositoryCtx(prCtx, pr.BaseRepo.RepoPath()) if err != nil { return err } @@ -115,7 +123,7 @@ func NewPullRequest(repo *repo_model.Repository, pull *models.Issue, labelIDs [] } // ChangeTargetBranch changes the target branch of this pull request, as the given user. -func ChangeTargetBranch(pr *models.PullRequest, doer *user_model.User, targetBranch string) (err error) { +func ChangeTargetBranch(ctx context.Context, pr *models.PullRequest, doer *user_model.User, targetBranch string) (err error) { // Current target branch is already the same if pr.BaseBranch == targetBranch { return nil @@ -141,7 +149,7 @@ func ChangeTargetBranch(pr *models.PullRequest, doer *user_model.User, targetBra } // Check if branches are equal - branchesEqual, err := IsHeadEqualWithBranch(pr, targetBranch) + branchesEqual, err := IsHeadEqualWithBranch(ctx, pr, targetBranch) if err != nil { return err } @@ -184,7 +192,7 @@ func ChangeTargetBranch(pr *models.PullRequest, doer *user_model.User, targetBra } // Update Commit Divergence - divergence, err := GetDiverging(pr) + divergence, err := GetDiverging(ctx, pr) if err != nil { return err } @@ -211,12 +219,12 @@ func ChangeTargetBranch(pr *models.PullRequest, doer *user_model.User, targetBra return nil } -func checkForInvalidation(requests models.PullRequestList, repoID int64, doer *user_model.User, branch string) error { +func checkForInvalidation(ctx context.Context, requests models.PullRequestList, repoID int64, doer *user_model.User, branch string) error { repo, err := repo_model.GetRepositoryByID(repoID) if err != nil { return fmt.Errorf("GetRepositoryByID: %v", err) } - gitRepo, err := git.OpenRepository(repo.RepoPath()) + gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath()) if err != nil { return fmt.Errorf("git.OpenRepository: %v", err) } @@ -251,13 +259,13 @@ func AddTestPullRequestTask(doer *user_model.User, repoID int64, branch string, if err = requests.LoadAttributes(); err != nil { log.Error("PullRequestList.LoadAttributes: %v", err) } - if invalidationErr := checkForInvalidation(requests, repoID, doer, branch); invalidationErr != nil { + if invalidationErr := checkForInvalidation(ctx, requests, repoID, doer, branch); invalidationErr != nil { log.Error("checkForInvalidation: %v", invalidationErr) } if err == nil { for _, pr := range prs { if newCommitID != "" && newCommitID != git.EmptySHA { - changed, err := checkIfPRContentChanged(pr, oldCommitID, newCommitID) + changed, err := checkIfPRContentChanged(ctx, pr, oldCommitID, newCommitID) if err != nil { log.Error("checkIfPRContentChanged: %v", err) } @@ -270,7 +278,7 @@ func AddTestPullRequestTask(doer *user_model.User, repoID int64, branch string, if err := models.MarkReviewsAsNotStale(pr.IssueID, newCommitID); err != nil { log.Error("MarkReviewsAsNotStale: %v", err) } - divergence, err := GetDiverging(pr) + divergence, err := GetDiverging(ctx, pr) if err != nil { log.Error("GetDiverging: %v", err) } else { @@ -290,7 +298,7 @@ func AddTestPullRequestTask(doer *user_model.User, repoID int64, branch string, for _, pr := range prs { log.Trace("Updating PR[%d]: composing new test task", pr.ID) if pr.Flow == models.PullRequestFlowGithub { - if err := PushToBaseRepo(pr); err != nil { + if err := PushToBaseRepo(ctx, pr); err != nil { log.Error("PushToBaseRepo: %v", err) continue } @@ -299,7 +307,7 @@ func AddTestPullRequestTask(doer *user_model.User, repoID int64, branch string, } AddToTaskQueue(pr) - comment, err := models.CreatePushPullComment(doer, pr, oldCommitID, newCommitID) + comment, err := models.CreatePushPullComment(ctx, doer, pr, oldCommitID, newCommitID) if err == nil && comment != nil { notification.NotifyPullRequestPushCommits(doer, pr, comment) } @@ -312,7 +320,7 @@ func AddTestPullRequestTask(doer *user_model.User, repoID int64, branch string, return } for _, pr := range prs { - divergence, err := GetDiverging(pr) + divergence, err := GetDiverging(ctx, pr) if err != nil { if models.IsErrBranchDoesNotExist(err) && !git.IsBranchExist(ctx, pr.HeadRepo.RepoPath(), pr.HeadBranch) { log.Warn("Cannot test PR %s/%d: head_branch %s no longer exists", pr.BaseRepo.Name, pr.IssueID, pr.HeadBranch) @@ -332,7 +340,7 @@ func AddTestPullRequestTask(doer *user_model.User, repoID int64, branch string, // checkIfPRContentChanged checks if diff to target branch has changed by push // A commit can be considered to leave the PR untouched if the patch/diff with its merge base is unchanged -func checkIfPRContentChanged(pr *models.PullRequest, oldCommitID, newCommitID string) (hasChanged bool, err error) { +func checkIfPRContentChanged(ctx context.Context, pr *models.PullRequest, oldCommitID, newCommitID string) (hasChanged bool, err error) { if err = pr.LoadHeadRepo(); err != nil { return false, fmt.Errorf("LoadHeadRepo: %v", err) } else if pr.HeadRepo == nil { @@ -344,7 +352,7 @@ func checkIfPRContentChanged(pr *models.PullRequest, oldCommitID, newCommitID st return false, fmt.Errorf("LoadBaseRepo: %v", err) } - headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath()) + headGitRepo, err := git.OpenRepositoryCtx(ctx, pr.HeadRepo.RepoPath()) if err != nil { return false, fmt.Errorf("OpenRepository: %v", err) } @@ -404,11 +412,11 @@ func checkIfPRContentChanged(pr *models.PullRequest, oldCommitID, newCommitID st // PushToBaseRepo pushes commits from branches of head repository to // corresponding branches of base repository. // FIXME: Only push branches that are actually updates? -func PushToBaseRepo(pr *models.PullRequest) (err error) { - return pushToBaseRepoHelper(pr, "") +func PushToBaseRepo(ctx context.Context, pr *models.PullRequest) (err error) { + return pushToBaseRepoHelper(ctx, pr, "") } -func pushToBaseRepoHelper(pr *models.PullRequest, prefixHeadBranch string) (err error) { +func pushToBaseRepoHelper(ctx context.Context, pr *models.PullRequest, prefixHeadBranch string) (err error) { log.Trace("PushToBaseRepo[%d]: pushing commits to base repo '%s'", pr.BaseRepoID, pr.GetGitRefName()) if err := pr.LoadHeadRepo(); err != nil { @@ -432,7 +440,7 @@ func pushToBaseRepoHelper(pr *models.PullRequest, prefixHeadBranch string) (err gitRefName := pr.GetGitRefName() - if err := git.Push(git.DefaultContext, headRepoPath, git.PushOptions{ + if err := git.Push(ctx, headRepoPath, git.PushOptions{ Remote: baseRepoPath, Branch: prefixHeadBranch + pr.HeadBranch + ":" + gitRefName, Force: true, @@ -452,8 +460,8 @@ func pushToBaseRepoHelper(pr *models.PullRequest, prefixHeadBranch string) (err log.Info("Can't push with %s%s", prefixHeadBranch, pr.HeadBranch) return err } - log.Info("Retrying to push with "+git.BranchPrefix+"%s", pr.HeadBranch) - err = pushToBaseRepoHelper(pr, git.BranchPrefix) + log.Info("Retrying to push with %s%s", git.BranchPrefix, pr.HeadBranch) + err = pushToBaseRepoHelper(ctx, pr, git.BranchPrefix) return err } log.Error("Unable to push PR head for %s#%d (%-v:%s) due to Error: %v", pr.BaseRepo.FullName(), pr.Index, pr.BaseRepo, gitRefName, err) @@ -464,14 +472,14 @@ func pushToBaseRepoHelper(pr *models.PullRequest, prefixHeadBranch string) (err } // UpdateRef update refs/pull/id/head directly for agit flow pull request -func UpdateRef(pr *models.PullRequest) (err error) { +func UpdateRef(ctx context.Context, pr *models.PullRequest) (err error) { log.Trace("UpdateRef[%d]: upgate pull request ref in base repo '%s'", pr.ID, pr.GetGitRefName()) if err := pr.LoadBaseRepo(); err != nil { log.Error("Unable to load base repository for PR[%d] Error: %v", pr.ID, err) return err } - _, err = git.NewCommand("update-ref", pr.GetGitRefName(), pr.HeadCommitID).RunInDir(pr.BaseRepo.RepoPath()) + _, err = git.NewCommandContext(ctx, "update-ref", pr.GetGitRefName(), pr.HeadCommitID).RunInDir(pr.BaseRepo.RepoPath()) if err != nil { log.Error("Unable to update ref in base repository for PR[%d] Error: %v", pr.ID, err) } @@ -525,8 +533,8 @@ func CloseBranchPulls(doer *user_model.User, repoID int64, branch string) error } // CloseRepoBranchesPulls close all pull requests which head branches are in the given repository, but only whose base repo is not in the given repository -func CloseRepoBranchesPulls(doer *user_model.User, repo *repo_model.Repository) error { - branches, _, err := git.GetBranchesByPath(repo.RepoPath(), 0, 0) +func CloseRepoBranchesPulls(ctx context.Context, doer *user_model.User, repo *repo_model.Repository) error { + branches, _, err := git.GetBranchesByPath(ctx, repo.RepoPath(), 0, 0) if err != nil { return err } @@ -563,7 +571,7 @@ func CloseRepoBranchesPulls(doer *user_model.User, repo *repo_model.Repository) var commitMessageTrailersPattern = regexp.MustCompile(`(?:^|\n\n)(?:[\w-]+[ \t]*:[^\n]+\n*(?:[ \t]+[^\n]+\n*)*)+$`) // GetSquashMergeCommitMessages returns the commit messages between head and merge base (if there is one) -func GetSquashMergeCommitMessages(pr *models.PullRequest) string { +func GetSquashMergeCommitMessages(ctx context.Context, pr *models.PullRequest) string { if err := pr.LoadIssue(); err != nil { log.Error("Cannot load issue %d for PR id %d: Error: %v", pr.IssueID, pr.ID, err) return "" @@ -583,12 +591,12 @@ func GetSquashMergeCommitMessages(pr *models.PullRequest) string { } } - gitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath()) + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, pr.HeadRepo.RepoPath()) if err != nil { log.Error("Unable to open head repository: Error: %v", err) return "" } - defer gitRepo.Close() + defer closer.Close() var headCommit *git.Commit if pr.Flow == models.PullRequestFlowGithub { @@ -719,7 +727,7 @@ func GetSquashMergeCommitMessages(pr *models.PullRequest) string { } // GetIssuesLastCommitStatus returns a map -func GetIssuesLastCommitStatus(issues models.IssueList) (map[int64]*models.CommitStatus, error) { +func GetIssuesLastCommitStatus(ctx context.Context, issues models.IssueList) (map[int64]*models.CommitStatus, error) { if err := issues.LoadPullRequests(); err != nil { return nil, err } @@ -744,7 +752,7 @@ func GetIssuesLastCommitStatus(issues models.IssueList) (map[int64]*models.Commi } gitRepo, ok := gitRepos[issue.RepoID] if !ok { - gitRepo, err = git.OpenRepository(issue.Repo.RepoPath()) + gitRepo, err = git.OpenRepositoryCtx(ctx, issue.Repo.RepoPath()) if err != nil { log.Error("Cannot open git repository %-v for issue #%d[%d]. Error: %v", issue.Repo, issue.Index, issue.ID, err) continue @@ -762,20 +770,6 @@ func GetIssuesLastCommitStatus(issues models.IssueList) (map[int64]*models.Commi return res, nil } -// GetLastCommitStatus returns list of commit statuses for latest commit on this pull request. -func GetLastCommitStatus(pr *models.PullRequest) (status *models.CommitStatus, err error) { - if err = pr.LoadBaseRepo(); err != nil { - return nil, err - } - gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath()) - if err != nil { - return nil, err - } - defer gitRepo.Close() - - return getLastCommitStatus(gitRepo, pr) -} - // getLastCommitStatus get pr's last commit status. PR's last commit status is the head commit id's last commit status func getLastCommitStatus(gitRepo *git.Repository, pr *models.PullRequest) (status *models.CommitStatus, err error) { sha, err := gitRepo.GetRefCommitID(pr.GetGitRefName()) @@ -791,16 +785,16 @@ func getLastCommitStatus(gitRepo *git.Repository, pr *models.PullRequest) (statu } // IsHeadEqualWithBranch returns if the commits of branchName are available in pull request head -func IsHeadEqualWithBranch(pr *models.PullRequest, branchName string) (bool, error) { +func IsHeadEqualWithBranch(ctx context.Context, pr *models.PullRequest, branchName string) (bool, error) { var err error if err = pr.LoadBaseRepo(); err != nil { return false, err } - baseGitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath()) + baseGitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, pr.BaseRepo.RepoPath()) if err != nil { return false, err } - defer baseGitRepo.Close() + defer closer.Close() baseCommit, err := baseGitRepo.GetBranchCommit(branchName) if err != nil { @@ -810,11 +804,18 @@ func IsHeadEqualWithBranch(pr *models.PullRequest, branchName string) (bool, err if err = pr.LoadHeadRepo(); err != nil { return false, err } - headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath()) - if err != nil { - return false, err + var headGitRepo *git.Repository + if pr.HeadRepoID == pr.BaseRepoID { + headGitRepo = baseGitRepo + } else { + var closer io.Closer + + headGitRepo, closer, err = git.RepositoryFromContextOrOpen(ctx, pr.HeadRepo.RepoPath()) + if err != nil { + return false, err + } + defer closer.Close() } - defer headGitRepo.Close() var headCommit *git.Commit if pr.Flow == models.PullRequestFlowGithub { diff --git a/services/pull/review.go b/services/pull/review.go index 42292ac209..0db3168895 100644 --- a/services/pull/review.go +++ b/services/pull/review.go @@ -6,6 +6,7 @@ package pull import ( + "context" "fmt" "io" "regexp" @@ -22,7 +23,8 @@ import ( ) // CreateCodeComment creates a comment on the code line -func CreateCodeComment(doer *user_model.User, gitRepo *git.Repository, issue *models.Issue, line int64, content, treePath string, isReview bool, replyReviewID int64, latestCommitID string) (*models.Comment, error) { +func CreateCodeComment(ctx context.Context, doer *user_model.User, gitRepo *git.Repository, issue *models.Issue, line int64, content, treePath string, isReview bool, replyReviewID int64, latestCommitID string) (*models.Comment, error) { + var ( existsReview bool err error @@ -47,7 +49,7 @@ func CreateCodeComment(doer *user_model.User, gitRepo *git.Repository, issue *mo return nil, err } - comment, err := createCodeComment( + comment, err := createCodeComment(ctx, doer, issue.Repo, issue, @@ -87,7 +89,7 @@ func CreateCodeComment(doer *user_model.User, gitRepo *git.Repository, issue *mo } } - comment, err := createCodeComment( + comment, err := createCodeComment(ctx, doer, issue.Repo, issue, @@ -102,7 +104,7 @@ func CreateCodeComment(doer *user_model.User, gitRepo *git.Repository, issue *mo if !isReview && !existsReview { // Submit the review we've just created so the comment shows up in the issue view - if _, _, err = SubmitReview(doer, gitRepo, issue, models.ReviewTypeComment, "", latestCommitID, nil); err != nil { + if _, _, err = SubmitReview(ctx, doer, gitRepo, issue, models.ReviewTypeComment, "", latestCommitID, nil); err != nil { return nil, err } } @@ -115,7 +117,7 @@ func CreateCodeComment(doer *user_model.User, gitRepo *git.Repository, issue *mo var notEnoughLines = regexp.MustCompile(`exit status 128 - fatal: file .* has only \d+ lines?`) // createCodeComment creates a plain code comment at the specified line / path -func createCodeComment(doer *user_model.User, repo *repo_model.Repository, issue *models.Issue, content, treePath string, line, reviewID int64) (*models.Comment, error) { +func createCodeComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, issue *models.Issue, content, treePath string, line, reviewID int64) (*models.Comment, error) { var commitID, patch string if err := issue.LoadPullRequest(); err != nil { return nil, fmt.Errorf("GetPullRequestByIssueID: %v", err) @@ -124,11 +126,11 @@ func createCodeComment(doer *user_model.User, repo *repo_model.Repository, issue if err := pr.LoadBaseRepo(); err != nil { return nil, fmt.Errorf("LoadHeadRepo: %v", err) } - gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath()) + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, pr.BaseRepo.RepoPath()) if err != nil { return nil, fmt.Errorf("OpenRepository: %v", err) } - defer gitRepo.Close() + defer closer.Close() invalidated := false head := pr.GetGitRefName() @@ -217,7 +219,7 @@ func createCodeComment(doer *user_model.User, repo *repo_model.Repository, issue } // SubmitReview creates a review out of the existing pending review or creates a new one if no pending review exist -func SubmitReview(doer *user_model.User, gitRepo *git.Repository, issue *models.Issue, reviewType models.ReviewType, content, commitID string, attachmentUUIDs []string) (*models.Review, *models.Comment, error) { +func SubmitReview(ctx context.Context, doer *user_model.User, gitRepo *git.Repository, issue *models.Issue, reviewType models.ReviewType, content, commitID string, attachmentUUIDs []string) (*models.Review, *models.Comment, error) { pr, err := issue.GetPullRequest() if err != nil { return nil, nil, err @@ -235,7 +237,7 @@ func SubmitReview(doer *user_model.User, gitRepo *git.Repository, issue *models. if headCommitID == commitID { stale = false } else { - stale, err = checkIfPRContentChanged(pr, commitID, headCommitID) + stale, err = checkIfPRContentChanged(ctx, pr, commitID, headCommitID) if err != nil { return nil, nil, err } @@ -247,7 +249,6 @@ func SubmitReview(doer *user_model.User, gitRepo *git.Repository, issue *models. return nil, nil, err } - ctx := db.DefaultContext mentions, err := issue.FindAndUpdateIssueMentions(ctx, doer, comm.Content) if err != nil { return nil, nil, err @@ -271,7 +272,7 @@ func SubmitReview(doer *user_model.User, gitRepo *git.Repository, issue *models. } // DismissReview dismissing stale review by repo admin -func DismissReview(reviewID int64, message string, doer *user_model.User, isDismiss bool) (comment *models.Comment, err error) { +func DismissReview(ctx context.Context, reviewID int64, message string, doer *user_model.User, isDismiss bool) (comment *models.Comment, err error) { review, err := models.GetReviewByID(reviewID) if err != nil { return @@ -290,7 +291,7 @@ func DismissReview(reviewID int64, message string, doer *user_model.User, isDism } // load data for notify - if err = review.LoadAttributes(); err != nil { + if err = review.LoadAttributes(ctx); err != nil { return } if err = review.Issue.LoadPullRequest(); err != nil { diff --git a/services/pull/temp_repo.go b/services/pull/temp_repo.go index d5dc8a5c4b..140403172c 100644 --- a/services/pull/temp_repo.go +++ b/services/pull/temp_repo.go @@ -6,6 +6,7 @@ package pull import ( + "context" "fmt" "os" "path/filepath" @@ -20,7 +21,7 @@ import ( // createTemporaryRepo creates a temporary repo with "base" for pr.BaseBranch and "tracking" for pr.HeadBranch // it also create a second base branch called "original_base" -func createTemporaryRepo(pr *models.PullRequest) (string, error) { +func createTemporaryRepo(ctx context.Context, pr *models.PullRequest) (string, error) { if err := pr.LoadHeadRepo(); err != nil { log.Error("LoadHeadRepo: %v", err) return "", fmt.Errorf("LoadHeadRepo: %v", err) @@ -55,7 +56,7 @@ func createTemporaryRepo(pr *models.PullRequest) (string, error) { baseRepoPath := pr.BaseRepo.RepoPath() headRepoPath := pr.HeadRepo.RepoPath() - if err := git.InitRepository(tmpBasePath, false); err != nil { + if err := git.InitRepository(ctx, tmpBasePath, false); err != nil { log.Error("git init tmpBasePath: %v", err) if err := models.RemoveTemporaryPath(tmpBasePath); err != nil { log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err) @@ -92,7 +93,7 @@ func createTemporaryRepo(pr *models.PullRequest) (string, error) { } var outbuf, errbuf strings.Builder - if err := git.NewCommand("remote", "add", "-t", pr.BaseBranch, "-m", pr.BaseBranch, "origin", baseRepoPath).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil { + if err := git.NewCommandContext(ctx, "remote", "add", "-t", pr.BaseBranch, "-m", pr.BaseBranch, "origin", baseRepoPath).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil { log.Error("Unable to add base repository as origin [%s -> %s]: %v\n%s\n%s", pr.BaseRepo.FullName(), tmpBasePath, err, outbuf.String(), errbuf.String()) if err := models.RemoveTemporaryPath(tmpBasePath); err != nil { log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err) @@ -102,7 +103,7 @@ func createTemporaryRepo(pr *models.PullRequest) (string, error) { outbuf.Reset() errbuf.Reset() - if err := git.NewCommand("fetch", "origin", "--no-tags", "--", pr.BaseBranch+":"+baseBranch, pr.BaseBranch+":original_"+baseBranch).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil { + if err := git.NewCommandContext(ctx, "fetch", "origin", "--no-tags", "--", pr.BaseBranch+":"+baseBranch, pr.BaseBranch+":original_"+baseBranch).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil { log.Error("Unable to fetch origin base branch [%s:%s -> base, original_base in %s]: %v:\n%s\n%s", pr.BaseRepo.FullName(), pr.BaseBranch, tmpBasePath, err, outbuf.String(), errbuf.String()) if err := models.RemoveTemporaryPath(tmpBasePath); err != nil { log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err) @@ -112,7 +113,7 @@ func createTemporaryRepo(pr *models.PullRequest) (string, error) { outbuf.Reset() errbuf.Reset() - if err := git.NewCommand("symbolic-ref", "HEAD", git.BranchPrefix+baseBranch).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil { + if err := git.NewCommandContext(ctx, "symbolic-ref", "HEAD", git.BranchPrefix+baseBranch).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil { log.Error("Unable to set HEAD as base branch [%s]: %v\n%s\n%s", tmpBasePath, err, outbuf.String(), errbuf.String()) if err := models.RemoveTemporaryPath(tmpBasePath); err != nil { log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err) @@ -130,7 +131,7 @@ func createTemporaryRepo(pr *models.PullRequest) (string, error) { return "", fmt.Errorf("Unable to head base repository to temporary repo [%s -> tmpBasePath]: %v", pr.HeadRepo.FullName(), err) } - if err := git.NewCommand("remote", "add", remoteRepoName, headRepoPath).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil { + if err := git.NewCommandContext(ctx, "remote", "add", remoteRepoName, headRepoPath).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil { log.Error("Unable to add head repository as head_repo [%s -> %s]: %v\n%s\n%s", pr.HeadRepo.FullName(), tmpBasePath, err, outbuf.String(), errbuf.String()) if err := models.RemoveTemporaryPath(tmpBasePath); err != nil { log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err) @@ -150,11 +151,11 @@ func createTemporaryRepo(pr *models.PullRequest) (string, error) { } else { headBranch = pr.GetGitRefName() } - if err := git.NewCommand("fetch", "--no-tags", remoteRepoName, headBranch+":"+trackingBranch).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil { + if err := git.NewCommandContext(ctx, "fetch", "--no-tags", remoteRepoName, headBranch+":"+trackingBranch).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil { if err := models.RemoveTemporaryPath(tmpBasePath); err != nil { log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err) } - if !git.IsBranchExist(git.DefaultContext, pr.HeadRepo.RepoPath(), pr.HeadBranch) { + if !git.IsBranchExist(ctx, pr.HeadRepo.RepoPath(), pr.HeadBranch) { return "", models.ErrBranchDoesNotExist{ BranchName: pr.HeadBranch, } diff --git a/services/pull/update.go b/services/pull/update.go index 8ca7e4cee7..09cf1ce52e 100644 --- a/services/pull/update.go +++ b/services/pull/update.go @@ -5,6 +5,7 @@ package pull import ( + "context" "fmt" "code.gitea.io/gitea/models" @@ -15,7 +16,7 @@ import ( ) // Update updates pull request with base branch. -func Update(pull *models.PullRequest, doer *user_model.User, message string, rebase bool) error { +func Update(ctx context.Context, pull *models.PullRequest, doer *user_model.User, message string, rebase bool) error { var ( pr *models.PullRequest style repo_model.MergeStyle @@ -48,14 +49,14 @@ func Update(pull *models.PullRequest, doer *user_model.User, message string, reb return fmt.Errorf("LoadBaseRepo: %v", err) } - diffCount, err := GetDiverging(pull) + diffCount, err := GetDiverging(ctx, pull) if err != nil { return err } else if diffCount.Behind == 0 { return fmt.Errorf("HeadBranch of PR %d is up to date", pull.Index) } - _, err = rawMerge(pr, doer, style, "", message) + _, err = rawMerge(ctx, pr, doer, style, "", message) defer func() { if rebase { @@ -113,7 +114,7 @@ func IsUserAllowedToUpdate(pull *models.PullRequest, user *user_model.User) (mer } // GetDiverging determines how many commits a PR is ahead or behind the PR base branch -func GetDiverging(pr *models.PullRequest) (*git.DivergeObject, error) { +func GetDiverging(ctx context.Context, pr *models.PullRequest) (*git.DivergeObject, error) { log.Trace("GetDiverging[%d]: compare commits", pr.ID) if err := pr.LoadBaseRepo(); err != nil { return nil, err @@ -122,7 +123,7 @@ func GetDiverging(pr *models.PullRequest) (*git.DivergeObject, error) { return nil, err } - tmpRepo, err := createTemporaryRepo(pr) + tmpRepo, err := createTemporaryRepo(ctx, pr) if err != nil { if !models.IsErrBranchDoesNotExist(err) { log.Error("CreateTemporaryRepo: %v", err) @@ -135,6 +136,6 @@ func GetDiverging(pr *models.PullRequest) (*git.DivergeObject, error) { } }() - diff, err := git.GetDivergingCommits(tmpRepo, "base", "tracking") + diff, err := git.GetDivergingCommits(ctx, tmpRepo, "base", "tracking") return &diff, err } diff --git a/services/release/release.go b/services/release/release.go index 5fa506bc61..aebf79edc3 100644 --- a/services/release/release.go +++ b/services/release/release.go @@ -5,6 +5,7 @@ package release import ( + "context" "errors" "fmt" "strings" @@ -83,7 +84,7 @@ func createTag(gitRepo *git.Repository, rel *models.Release, msg string) (bool, OldCommitID: git.EmptySHA, NewCommitID: commit.ID.String(), }, commits) - notification.NotifyCreateRef(rel.Publisher, rel.Repo, "tag", git.TagPrefix+rel.TagName) + notification.NotifyCreateRef(rel.Publisher, rel.Repo, "tag", git.TagPrefix+rel.TagName, commit.ID.String()) rel.CreatedUnix = timeutil.TimeStampNow() } commit, err := gitRepo.GetTagCommit(rel.TagName) @@ -141,7 +142,7 @@ func CreateRelease(gitRepo *git.Repository, rel *models.Release, attachmentUUIDs } // CreateNewTag creates a new repository tag -func CreateNewTag(doer *user_model.User, repo *repo_model.Repository, commit, tagName, msg string) error { +func CreateNewTag(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, commit, tagName, msg string) error { isExist, err := models.IsReleaseExist(repo.ID, tagName) if err != nil { return err @@ -151,11 +152,11 @@ func CreateNewTag(doer *user_model.User, repo *repo_model.Repository, commit, ta } } - gitRepo, err := git.OpenRepository(repo.RepoPath()) + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) if err != nil { return err } - defer gitRepo.Close() + defer closer.Close() rel := &models.Release{ RepoID: repo.ID, @@ -283,7 +284,7 @@ func UpdateRelease(doer *user_model.User, gitRepo *git.Repository, rel *models.R } // DeleteReleaseByID deletes a release and corresponding Git tag by given ID. -func DeleteReleaseByID(id int64, doer *user_model.User, delTag bool) error { +func DeleteReleaseByID(ctx context.Context, id int64, doer *user_model.User, delTag bool) error { rel, err := models.GetReleaseByID(id) if err != nil { return fmt.Errorf("GetReleaseByID: %v", err) @@ -295,7 +296,7 @@ func DeleteReleaseByID(id int64, doer *user_model.User, delTag bool) error { } if delTag { - if stdout, err := git.NewCommand("tag", "-d", rel.TagName). + if stdout, err := git.NewCommandContext(ctx, "tag", "-d", rel.TagName). SetDescription(fmt.Sprintf("DeleteReleaseByID (git tag -d): %d", rel.ID)). RunInDir(repo.RepoPath()); err != nil && !strings.Contains(err.Error(), "not found") { log.Error("DeleteReleaseByID (git tag -d): %d in %v Failed:\nStdout: %s\nError: %v", rel.ID, repo, stdout, err) diff --git a/services/release/release_test.go b/services/release/release_test.go index b41eea7e9e..4dc45c06e9 100644 --- a/services/release/release_test.go +++ b/services/release/release_test.go @@ -358,6 +358,6 @@ func TestCreateNewTag(t *testing.T) { user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User) repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository) - assert.NoError(t, CreateNewTag(user, repo, "master", "v2.0", + assert.NoError(t, CreateNewTag(git.DefaultContext, user, repo, "master", "v2.0", "v2.0 is released \n\n BUGFIX: .... \n\n 123")) } diff --git a/services/repository/adopt.go b/services/repository/adopt.go index fc3fdc608f..adc5942a3c 100644 --- a/services/repository/adopt.go +++ b/services/repository/adopt.go @@ -84,7 +84,7 @@ func AdoptRepository(doer, u *user_model.User, opts models.CreateRepoOptions) (* } } - if stdout, err := git.NewCommand("update-server-info"). + if stdout, err := git.NewCommandContext(ctx, "update-server-info"). SetDescription(fmt.Sprintf("CreateRepository(git update-server-info): %s", repoPath)). RunInDir(repoPath); err != nil { log.Error("CreateRepository(git update-server-info) in %v: Stdout: %s\nError: %v", repo, stdout, err) @@ -121,11 +121,14 @@ func adoptRepository(ctx context.Context, repoPath string, u *user_model.User, r } repo.IsEmpty = false - gitRepo, err := git.OpenRepository(repo.RepoPath()) + + // Don't bother looking this repo in the context it won't be there + gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath()) if err != nil { return fmt.Errorf("openRepository: %v", err) } defer gitRepo.Close() + if len(opts.DefaultBranch) > 0 { repo.DefaultBranch = opts.DefaultBranch diff --git a/services/repository/archiver/archiver.go b/services/repository/archiver/archiver.go index 16ee532f78..93ec8bcca2 100644 --- a/services/repository/archiver/archiver.go +++ b/services/repository/archiver/archiver.go @@ -19,6 +19,7 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/queue" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/storage" @@ -115,11 +116,13 @@ func (aReq *ArchiveRequest) GetArchiveName() string { } func doArchive(r *ArchiveRequest) (*repo_model.RepoArchiver, error) { - ctx, committer, err := db.TxContext() + txCtx, committer, err := db.TxContext() if err != nil { return nil, err } defer committer.Close() + ctx, _, finished := process.GetManager().AddContext(txCtx, fmt.Sprintf("ArchiveRequest[%d]: %s", r.RepoID, r.GetArchiveName())) + defer finished() archiver, err := repo_model.GetRepoArchiver(ctx, r.RepoID, r.Type, r.CommitID) if err != nil { @@ -175,7 +178,7 @@ func doArchive(r *ArchiveRequest) (*repo_model.RepoArchiver, error) { return nil, fmt.Errorf("archiver.LoadRepo failed: %v", err) } - gitRepo, err := git.OpenRepository(repo.RepoPath()) + gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath()) if err != nil { return nil, err } @@ -190,13 +193,13 @@ func doArchive(r *ArchiveRequest) (*repo_model.RepoArchiver, error) { if archiver.Type == git.BUNDLE { err = gitRepo.CreateBundle( - graceful.GetManager().ShutdownContext(), + ctx, archiver.CommitID, w, ) } else { err = gitRepo.CreateArchive( - graceful.GetManager().ShutdownContext(), + ctx, archiver.Type, w, setting.Repository.PrefixArchiveFiles, diff --git a/services/repository/branch.go b/services/repository/branch.go index e1775fc12b..b1a6dafb58 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -21,19 +21,19 @@ import ( ) // CreateNewBranch creates a new repository branch -func CreateNewBranch(doer *user_model.User, repo *repo_model.Repository, oldBranchName, branchName string) (err error) { +func CreateNewBranch(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, oldBranchName, branchName string) (err error) { // Check if branch name can be used - if err := checkBranchName(git.DefaultContext, repo, branchName); err != nil { + if err := checkBranchName(ctx, repo, branchName); err != nil { return err } - if !git.IsBranchExist(git.DefaultContext, repo.RepoPath(), oldBranchName) { + if !git.IsBranchExist(ctx, repo.RepoPath(), oldBranchName) { return models.ErrBranchDoesNotExist{ BranchName: oldBranchName, } } - if err := git.Push(git.DefaultContext, repo.RepoPath(), git.PushOptions{ + if err := git.Push(ctx, repo.RepoPath(), git.PushOptions{ Remote: repo.RepoPath(), Branch: fmt.Sprintf("%s:%s%s", oldBranchName, git.BranchPrefix, branchName), Env: models.PushingEnvironment(doer, repo), @@ -47,24 +47,10 @@ func CreateNewBranch(doer *user_model.User, repo *repo_model.Repository, oldBran return nil } -// GetBranch returns a branch by its name -func GetBranch(repo *repo_model.Repository, branch string) (*git.Branch, error) { - if len(branch) == 0 { - return nil, fmt.Errorf("GetBranch: empty string for branch") - } - gitRepo, err := git.OpenRepository(repo.RepoPath()) - if err != nil { - return nil, err - } - defer gitRepo.Close() - - return gitRepo.GetBranch(branch) -} - // GetBranches returns branches from the repository, skipping skip initial branches and // returning at most limit branches, or all branches if limit is 0. -func GetBranches(repo *repo_model.Repository, skip, limit int) ([]*git.Branch, int, error) { - return git.GetBranchesByPath(repo.RepoPath(), skip, limit) +func GetBranches(ctx context.Context, repo *repo_model.Repository, skip, limit int) ([]*git.Branch, int, error) { + return git.GetBranchesByPath(ctx, repo.RepoPath(), skip, limit) } // checkBranchName validates branch name with existing repository branches @@ -98,13 +84,13 @@ func checkBranchName(ctx context.Context, repo *repo_model.Repository, name stri } // CreateNewBranchFromCommit creates a new repository branch -func CreateNewBranchFromCommit(doer *user_model.User, repo *repo_model.Repository, commit, branchName string) (err error) { +func CreateNewBranchFromCommit(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, commit, branchName string) (err error) { // Check if branch name can be used - if err := checkBranchName(git.DefaultContext, repo, branchName); err != nil { + if err := checkBranchName(ctx, repo, branchName); err != nil { return err } - if err := git.Push(git.DefaultContext, repo.RepoPath(), git.PushOptions{ + if err := git.Push(ctx, repo.RepoPath(), git.PushOptions{ Remote: repo.RepoPath(), Branch: fmt.Sprintf("%s:%s%s", commit, git.BranchPrefix, branchName), Env: models.PushingEnvironment(doer, repo), @@ -149,9 +135,13 @@ func RenameBranch(repo *repo_model.Repository, doer *user_model.User, gitRepo *g }); err != nil { return "", err } + refID, err := gitRepo.GetRefCommitID(git.BranchPrefix + to) + if err != nil { + return "", err + } notification.NotifyDeleteRef(doer, repo, "branch", git.BranchPrefix+from) - notification.NotifyCreateRef(doer, repo, "branch", git.BranchPrefix+to) + notification.NotifyCreateRef(doer, repo, "branch", git.BranchPrefix+to, refID) return "", nil } diff --git a/services/repository/check.go b/services/repository/check.go index 7118b006bd..59b8626b46 100644 --- a/services/repository/check.go +++ b/services/repository/check.go @@ -196,7 +196,7 @@ func ReinitMissingRepositories(ctx context.Context) error { default: } log.Trace("Initializing %d/%d...", repo.OwnerID, repo.ID) - if err := git.InitRepository(repo.RepoPath(), true); err != nil { + if err := git.InitRepository(ctx, repo.RepoPath(), true); err != nil { log.Error("Unable (re)initialize repository %d at %s. Error: %v", repo.ID, repo.RepoPath(), err) if err2 := admin_model.CreateRepositoryNotice("InitRepository [%d]: %v", repo.ID, err); err2 != nil { log.Error("CreateRepositoryNotice: %v", err2) diff --git a/services/repository/files/commit.go b/services/repository/files/commit.go index 4e391ff343..e7604e3f92 100644 --- a/services/repository/files/commit.go +++ b/services/repository/files/commit.go @@ -5,6 +5,7 @@ package files import ( + "context" "fmt" "code.gitea.io/gitea/models" @@ -18,14 +19,16 @@ import ( // CreateCommitStatus creates a new CommitStatus given a bunch of parameters // NOTE: All text-values will be trimmed from whitespaces. // Requires: Repo, Creator, SHA -func CreateCommitStatus(repo *repo_model.Repository, creator *user_model.User, sha string, status *models.CommitStatus) error { +func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creator *user_model.User, sha string, status *models.CommitStatus) error { repoPath := repo.RepoPath() // confirm that commit is exist - gitRepo, err := git.OpenRepository(repoPath) + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) if err != nil { return fmt.Errorf("OpenRepository[%s]: %v", repoPath, err) } + defer closer.Close() + if _, err := gitRepo.GetCommit(sha); err != nil { gitRepo.Close() return fmt.Errorf("GetCommit[%s]: %v", sha, err) @@ -45,8 +48,8 @@ func CreateCommitStatus(repo *repo_model.Repository, creator *user_model.User, s } // CountDivergingCommits determines how many commits a branch is ahead or behind the repository's base branch -func CountDivergingCommits(repo *repo_model.Repository, branch string) (*git.DivergeObject, error) { - divergence, err := git.GetDivergingCommits(repo.RepoPath(), repo.DefaultBranch, branch) +func CountDivergingCommits(ctx context.Context, repo *repo_model.Repository, branch string) (*git.DivergeObject, error) { + divergence, err := git.GetDivergingCommits(ctx, repo.RepoPath(), repo.DefaultBranch, branch) if err != nil { return nil, err } diff --git a/services/repository/files/content.go b/services/repository/files/content.go index 10461c82a7..9037a84349 100644 --- a/services/repository/files/content.go +++ b/services/repository/files/content.go @@ -5,6 +5,7 @@ package files import ( + "context" "fmt" "net/url" "path" @@ -39,7 +40,7 @@ func (ct *ContentType) String() string { // GetContentsOrList gets the meta data of a file's contents (*ContentsResponse) if treePath not a tree // directory, otherwise a listing of file contents ([]*ContentsResponse). Ref can be a branch, commit or tag -func GetContentsOrList(repo *repo_model.Repository, treePath, ref string) (interface{}, error) { +func GetContentsOrList(ctx context.Context, repo *repo_model.Repository, treePath, ref string) (interface{}, error) { if repo.IsEmpty { return make([]interface{}, 0), nil } @@ -57,11 +58,11 @@ func GetContentsOrList(repo *repo_model.Repository, treePath, ref string) (inter } treePath = cleanTreePath - gitRepo, err := git.OpenRepository(repo.RepoPath()) + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) if err != nil { return nil, err } - defer gitRepo.Close() + defer closer.Close() // Get the commit object for the ref commit, err := gitRepo.GetCommit(ref) @@ -75,7 +76,7 @@ func GetContentsOrList(repo *repo_model.Repository, treePath, ref string) (inter } if entry.Type() != "tree" { - return GetContents(repo, treePath, origRef, false) + return GetContents(ctx, repo, treePath, origRef, false) } // We are in a directory, so we return a list of FileContentResponse objects @@ -91,7 +92,7 @@ func GetContentsOrList(repo *repo_model.Repository, treePath, ref string) (inter } for _, e := range entries { subTreePath := path.Join(treePath, e.Name()) - fileContentResponse, err := GetContents(repo, subTreePath, origRef, true) + fileContentResponse, err := GetContents(ctx, repo, subTreePath, origRef, true) if err != nil { return nil, err } @@ -101,7 +102,7 @@ func GetContentsOrList(repo *repo_model.Repository, treePath, ref string) (inter } // GetContents gets the meta data on a file's contents. Ref can be a branch, commit or tag -func GetContents(repo *repo_model.Repository, treePath, ref string, forList bool) (*api.ContentsResponse, error) { +func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref string, forList bool) (*api.ContentsResponse, error) { if ref == "" { ref = repo.DefaultBranch } @@ -116,11 +117,11 @@ func GetContents(repo *repo_model.Repository, treePath, ref string, forList bool } treePath = cleanTreePath - gitRepo, err := git.OpenRepository(repo.RepoPath()) + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) if err != nil { return nil, err } - defer gitRepo.Close() + defer closer.Close() // Get the commit object for the ref commit, err := gitRepo.GetCommit(ref) @@ -163,7 +164,7 @@ func GetContents(repo *repo_model.Repository, treePath, ref string, forList bool // Now populate the rest of the ContentsResponse based on entry type if entry.IsRegular() || entry.IsExecutable() { contentsResponse.Type = string(ContentTypeRegular) - if blobResponse, err := GetBlobBySHA(repo, entry.ID.String()); err != nil { + if blobResponse, err := GetBlobBySHA(ctx, repo, entry.ID.String()); err != nil { return nil, err } else if !forList { // We don't show the content if we are getting a list of FileContentResponses @@ -219,12 +220,12 @@ func GetContents(repo *repo_model.Repository, treePath, ref string, forList bool } // GetBlobBySHA get the GitBlobResponse of a repository using a sha hash. -func GetBlobBySHA(repo *repo_model.Repository, sha string) (*api.GitBlobResponse, error) { - gitRepo, err := git.OpenRepository(repo.RepoPath()) +func GetBlobBySHA(ctx context.Context, repo *repo_model.Repository, sha string) (*api.GitBlobResponse, error) { + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) if err != nil { return nil, err } - defer gitRepo.Close() + defer closer.Close() gitBlob, err := gitRepo.GetBlob(sha) if err != nil { return nil, err diff --git a/services/repository/files/content_test.go b/services/repository/files/content_test.go index 68b29b1daa..c21aa31d32 100644 --- a/services/repository/files/content_test.go +++ b/services/repository/files/content_test.go @@ -63,14 +63,14 @@ func TestGetContents(t *testing.T) { expectedContentsResponse := getExpectedReadmeContentsResponse() - t.Run("Get README.md contents with GetContents()", func(t *testing.T) { - fileContentResponse, err := GetContents(ctx.Repo.Repository, treePath, ref, false) + t.Run("Get README.md contents with GetContents(ctx, )", func(t *testing.T) { + fileContentResponse, err := GetContents(ctx, ctx.Repo.Repository, treePath, ref, false) assert.EqualValues(t, expectedContentsResponse, fileContentResponse) assert.NoError(t, err) }) - t.Run("Get README.md contents with ref as empty string (should then use the repo's default branch) with GetContents()", func(t *testing.T) { - fileContentResponse, err := GetContents(ctx.Repo.Repository, treePath, "", false) + t.Run("Get README.md contents with ref as empty string (should then use the repo's default branch) with GetContents(ctx, )", func(t *testing.T) { + fileContentResponse, err := GetContents(ctx, ctx.Repo.Repository, treePath, "", false) assert.EqualValues(t, expectedContentsResponse, fileContentResponse) assert.NoError(t, err) }) @@ -98,14 +98,14 @@ func TestGetContentsOrListForDir(t *testing.T) { readmeContentsResponse, } - t.Run("Get root dir contents with GetContentsOrList()", func(t *testing.T) { - fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, ref) + t.Run("Get root dir contents with GetContentsOrList(ctx, )", func(t *testing.T) { + fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref) assert.EqualValues(t, expectedContentsListResponse, fileContentResponse) assert.NoError(t, err) }) - t.Run("Get root dir contents with ref as empty string (should then use the repo's default branch) with GetContentsOrList()", func(t *testing.T) { - fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, "") + t.Run("Get root dir contents with ref as empty string (should then use the repo's default branch) with GetContentsOrList(ctx, )", func(t *testing.T) { + fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, "") assert.EqualValues(t, expectedContentsListResponse, fileContentResponse) assert.NoError(t, err) }) @@ -126,14 +126,14 @@ func TestGetContentsOrListForFile(t *testing.T) { expectedContentsResponse := getExpectedReadmeContentsResponse() - t.Run("Get README.md contents with GetContentsOrList()", func(t *testing.T) { - fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, ref) + t.Run("Get README.md contents with GetContentsOrList(ctx, )", func(t *testing.T) { + fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref) assert.EqualValues(t, expectedContentsResponse, fileContentResponse) assert.NoError(t, err) }) - t.Run("Get README.md contents with ref as empty string (should then use the repo's default branch) with GetContentsOrList()", func(t *testing.T) { - fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, "") + t.Run("Get README.md contents with ref as empty string (should then use the repo's default branch) with GetContentsOrList(ctx, )", func(t *testing.T) { + fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, "") assert.EqualValues(t, expectedContentsResponse, fileContentResponse) assert.NoError(t, err) }) @@ -155,7 +155,7 @@ func TestGetContentsErrors(t *testing.T) { t.Run("bad treePath", func(t *testing.T) { badTreePath := "bad/tree.md" - fileContentResponse, err := GetContents(repo, badTreePath, ref, false) + fileContentResponse, err := GetContents(ctx, repo, badTreePath, ref, false) assert.Error(t, err) assert.EqualError(t, err, "object does not exist [id: , rel_path: bad]") assert.Nil(t, fileContentResponse) @@ -163,7 +163,7 @@ func TestGetContentsErrors(t *testing.T) { t.Run("bad ref", func(t *testing.T) { badRef := "bad_ref" - fileContentResponse, err := GetContents(repo, treePath, badRef, false) + fileContentResponse, err := GetContents(ctx, repo, treePath, badRef, false) assert.Error(t, err) assert.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]") assert.Nil(t, fileContentResponse) @@ -186,7 +186,7 @@ func TestGetContentsOrListErrors(t *testing.T) { t.Run("bad treePath", func(t *testing.T) { badTreePath := "bad/tree.md" - fileContentResponse, err := GetContentsOrList(repo, badTreePath, ref) + fileContentResponse, err := GetContentsOrList(ctx, repo, badTreePath, ref) assert.Error(t, err) assert.EqualError(t, err, "object does not exist [id: , rel_path: bad]") assert.Nil(t, fileContentResponse) @@ -194,7 +194,7 @@ func TestGetContentsOrListErrors(t *testing.T) { t.Run("bad ref", func(t *testing.T) { badRef := "bad_ref" - fileContentResponse, err := GetContentsOrList(repo, treePath, badRef) + fileContentResponse, err := GetContentsOrList(ctx, repo, treePath, badRef) assert.Error(t, err) assert.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]") assert.Nil(t, fileContentResponse) @@ -213,7 +213,7 @@ func TestGetContentsOrListOfEmptyRepos(t *testing.T) { repo := ctx.Repo.Repository t.Run("empty repo", func(t *testing.T) { - contents, err := GetContentsOrList(repo, "", "") + contents, err := GetContentsOrList(ctx, repo, "", "") assert.NoError(t, err) assert.Empty(t, contents) }) @@ -232,7 +232,7 @@ func TestGetBlobBySHA(t *testing.T) { ctx.SetParams(":id", "1") ctx.SetParams(":sha", sha) - gbr, err := GetBlobBySHA(ctx.Repo.Repository, ctx.Params(":sha")) + gbr, err := GetBlobBySHA(ctx, ctx.Repo.Repository, ctx.Params(":sha")) expectedGBR := &api.GitBlobResponse{ Content: "dHJlZSAyYTJmMWQ0NjcwNzI4YTJlMTAwNDllMzQ1YmQ3YTI3NjQ2OGJlYWI2CmF1dGhvciB1c2VyMSA8YWRkcmVzczFAZXhhbXBsZS5jb20+IDE0ODk5NTY0NzkgLTA0MDAKY29tbWl0dGVyIEV0aGFuIEtvZW5pZyA8ZXRoYW50a29lbmlnQGdtYWlsLmNvbT4gMTQ4OTk1NjQ3OSAtMDQwMAoKSW5pdGlhbCBjb21taXQK", Encoding: "base64", diff --git a/services/repository/files/delete.go b/services/repository/files/delete.go index 15208addda..95d05b5202 100644 --- a/services/repository/files/delete.go +++ b/services/repository/files/delete.go @@ -5,6 +5,7 @@ package files import ( + "context" "fmt" "strings" @@ -13,7 +14,6 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" api "code.gitea.io/gitea/modules/structs" - repo_service "code.gitea.io/gitea/services/repository" ) // DeleteRepoFileOptions holds the repository delete file options @@ -31,7 +31,7 @@ type DeleteRepoFileOptions struct { } // DeleteRepoFile deletes a file in the given repository -func DeleteRepoFile(repo *repo_model.Repository, doer *user_model.User, opts *DeleteRepoFileOptions) (*api.FileResponse, error) { +func DeleteRepoFile(ctx context.Context, repo *repo_model.Repository, doer *user_model.User, opts *DeleteRepoFileOptions) (*api.FileResponse, error) { // If no branch name is set, assume the repo's default branch if opts.OldBranch == "" { opts.OldBranch = repo.DefaultBranch @@ -40,8 +40,14 @@ func DeleteRepoFile(repo *repo_model.Repository, doer *user_model.User, opts *De opts.NewBranch = opts.OldBranch } + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) + if err != nil { + return nil, err + } + defer closer.Close() + // oldBranch must exist for this operation - if _, err := repo_service.GetBranch(repo, opts.OldBranch); err != nil { + if _, err := gitRepo.GetBranch(opts.OldBranch); err != nil { return nil, err } @@ -49,7 +55,7 @@ func DeleteRepoFile(repo *repo_model.Repository, doer *user_model.User, opts *De // Check to make sure the branch does not already exist, otherwise we can't proceed. // If we aren't branching to a new branch, make sure user can commit to the given branch if opts.NewBranch != opts.OldBranch { - newBranch, err := repo_service.GetBranch(repo, opts.NewBranch) + newBranch, err := gitRepo.GetBranch(opts.NewBranch) if err != nil && !git.IsErrBranchNotExist(err) { return nil, err } @@ -58,7 +64,7 @@ func DeleteRepoFile(repo *repo_model.Repository, doer *user_model.User, opts *De BranchName: opts.NewBranch, } } - } else if err := VerifyBranchProtection(repo, doer, opts.OldBranch, opts.TreePath); err != nil { + } else if err := VerifyBranchProtection(ctx, repo, doer, opts.OldBranch, opts.TreePath); err != nil { return nil, err } @@ -74,7 +80,7 @@ func DeleteRepoFile(repo *repo_model.Repository, doer *user_model.User, opts *De author, committer := GetAuthorAndCommitterUsers(opts.Author, opts.Committer, doer) - t, err := NewTemporaryUploadRepository(repo) + t, err := NewTemporaryUploadRepository(ctx, repo) if err != nil { return nil, err } @@ -191,7 +197,7 @@ func DeleteRepoFile(repo *repo_model.Repository, doer *user_model.User, opts *De return nil, err } - file, err := GetFileResponseFromCommit(repo, commit, opts.NewBranch, treePath) + file, err := GetFileResponseFromCommit(ctx, repo, commit, opts.NewBranch, treePath) if err != nil { return nil, err } diff --git a/services/repository/files/diff.go b/services/repository/files/diff.go index fb41237e13..dbe1bef52b 100644 --- a/services/repository/files/diff.go +++ b/services/repository/files/diff.go @@ -5,6 +5,7 @@ package files import ( + "context" "strings" repo_model "code.gitea.io/gitea/models/repo" @@ -12,11 +13,11 @@ import ( ) // GetDiffPreview produces and returns diff result of a file which is not yet committed. -func GetDiffPreview(repo *repo_model.Repository, branch, treePath, content string) (*gitdiff.Diff, error) { +func GetDiffPreview(ctx context.Context, repo *repo_model.Repository, branch, treePath, content string) (*gitdiff.Diff, error) { if branch == "" { branch = repo.DefaultBranch } - t, err := NewTemporaryUploadRepository(repo) + t, err := NewTemporaryUploadRepository(ctx, repo) if err != nil { return nil, err } diff --git a/services/repository/files/diff_test.go b/services/repository/files/diff_test.go index 2f256e888c..c0a378dc4b 100644 --- a/services/repository/files/diff_test.go +++ b/services/repository/files/diff_test.go @@ -117,7 +117,7 @@ func TestGetDiffPreview(t *testing.T) { expectedDiff.NumFiles = len(expectedDiff.Files) t.Run("with given branch", func(t *testing.T) { - diff, err := GetDiffPreview(ctx.Repo.Repository, branch, treePath, content) + diff, err := GetDiffPreview(ctx, ctx.Repo.Repository, branch, treePath, content) assert.NoError(t, err) expectedBs, err := json.Marshal(expectedDiff) assert.NoError(t, err) @@ -127,7 +127,7 @@ func TestGetDiffPreview(t *testing.T) { }) t.Run("empty branch, same results", func(t *testing.T) { - diff, err := GetDiffPreview(ctx.Repo.Repository, "", treePath, content) + diff, err := GetDiffPreview(ctx, ctx.Repo.Repository, "", treePath, content) assert.NoError(t, err) expectedBs, err := json.Marshal(expectedDiff) assert.NoError(t, err) @@ -152,20 +152,20 @@ func TestGetDiffPreviewErrors(t *testing.T) { content := "# repo1\n\nDescription for repo1\nthis is a new line" t.Run("empty repo", func(t *testing.T) { - diff, err := GetDiffPreview(&repo_model.Repository{}, branch, treePath, content) + diff, err := GetDiffPreview(ctx, &repo_model.Repository{}, branch, treePath, content) assert.Nil(t, diff) assert.EqualError(t, err, "repository does not exist [id: 0, uid: 0, owner_name: , name: ]") }) t.Run("bad branch", func(t *testing.T) { badBranch := "bad_branch" - diff, err := GetDiffPreview(ctx.Repo.Repository, badBranch, treePath, content) + diff, err := GetDiffPreview(ctx, ctx.Repo.Repository, badBranch, treePath, content) assert.Nil(t, diff) assert.EqualError(t, err, "branch does not exist [name: "+badBranch+"]") }) t.Run("empty treePath", func(t *testing.T) { - diff, err := GetDiffPreview(ctx.Repo.Repository, branch, "", content) + diff, err := GetDiffPreview(ctx, ctx.Repo.Repository, branch, "", content) assert.Nil(t, diff) assert.EqualError(t, err, "path is invalid [path: ]") }) diff --git a/services/repository/files/file.go b/services/repository/files/file.go index ddbced809e..c6c626c9ce 100644 --- a/services/repository/files/file.go +++ b/services/repository/files/file.go @@ -5,6 +5,7 @@ package files import ( + "context" "fmt" "net/url" "path" @@ -18,9 +19,9 @@ import ( ) // GetFileResponseFromCommit Constructs a FileResponse from a Commit object -func GetFileResponseFromCommit(repo *repo_model.Repository, commit *git.Commit, branch, treeName string) (*api.FileResponse, error) { - fileContents, _ := GetContents(repo, treeName, branch, false) // ok if fails, then will be nil - fileCommitResponse, _ := GetFileCommitResponse(repo, commit) // ok if fails, then will be nil +func GetFileResponseFromCommit(ctx context.Context, repo *repo_model.Repository, commit *git.Commit, branch, treeName string) (*api.FileResponse, error) { + fileContents, _ := GetContents(ctx, repo, treeName, branch, false) // ok if fails, then will be nil + fileCommitResponse, _ := GetFileCommitResponse(repo, commit) // ok if fails, then will be nil verification := GetPayloadCommitVerification(commit) fileResponse := &api.FileResponse{ Content: fileContents, diff --git a/services/repository/files/file_test.go b/services/repository/files/file_test.go index 25303a6d00..24e4949832 100644 --- a/services/repository/files/file_test.go +++ b/services/repository/files/file_test.go @@ -109,12 +109,12 @@ func TestGetFileResponseFromCommit(t *testing.T) { repo := ctx.Repo.Repository branch := repo.DefaultBranch treePath := "README.md" - gitRepo, _ := git.OpenRepository(repo.RepoPath()) + gitRepo, _ := git.OpenRepositoryCtx(ctx, repo.RepoPath()) defer gitRepo.Close() commit, _ := gitRepo.GetBranchCommit(branch) expectedFileResponse := getExpectedFileResponse() - fileResponse, err := GetFileResponseFromCommit(repo, commit, branch, treePath) + fileResponse, err := GetFileResponseFromCommit(ctx, repo, commit, branch, treePath) assert.NoError(t, err) assert.EqualValues(t, expectedFileResponse, fileResponse) } diff --git a/services/repository/files/temp_repo.go b/services/repository/files/temp_repo.go index bc761e3101..229b9ffe80 100644 --- a/services/repository/files/temp_repo.go +++ b/services/repository/files/temp_repo.go @@ -26,18 +26,19 @@ import ( // TemporaryUploadRepository is a type to wrap our upload repositories as a shallow clone type TemporaryUploadRepository struct { + ctx context.Context repo *repo_model.Repository gitRepo *git.Repository basePath string } // NewTemporaryUploadRepository creates a new temporary upload repository -func NewTemporaryUploadRepository(repo *repo_model.Repository) (*TemporaryUploadRepository, error) { +func NewTemporaryUploadRepository(ctx context.Context, repo *repo_model.Repository) (*TemporaryUploadRepository, error) { basePath, err := models.CreateTemporaryPath("upload") if err != nil { return nil, err } - t := &TemporaryUploadRepository{repo: repo, basePath: basePath} + t := &TemporaryUploadRepository{ctx: ctx, repo: repo, basePath: basePath} return t, nil } @@ -51,7 +52,7 @@ func (t *TemporaryUploadRepository) Close() { // Clone the base repository to our path and set branch as the HEAD func (t *TemporaryUploadRepository) Clone(branch string) error { - if _, err := git.NewCommand("clone", "-s", "--bare", "-b", branch, t.repo.RepoPath(), t.basePath).Run(); err != nil { + if _, err := git.NewCommandContext(t.ctx, "clone", "-s", "--bare", "-b", branch, t.repo.RepoPath(), t.basePath).Run(); err != nil { stderr := err.Error() if matched, _ := regexp.MatchString(".*Remote branch .* not found in upstream origin.*", stderr); matched { return git.ErrBranchNotExist{ @@ -68,7 +69,7 @@ func (t *TemporaryUploadRepository) Clone(branch string) error { return fmt.Errorf("Clone: %v %s", err, stderr) } } - gitRepo, err := git.OpenRepository(t.basePath) + gitRepo, err := git.OpenRepositoryCtx(t.ctx, t.basePath) if err != nil { return err } @@ -78,7 +79,7 @@ func (t *TemporaryUploadRepository) Clone(branch string) error { // SetDefaultIndex sets the git index to our HEAD func (t *TemporaryUploadRepository) SetDefaultIndex() error { - if _, err := git.NewCommand("read-tree", "HEAD").RunInDir(t.basePath); err != nil { + if _, err := git.NewCommandContext(t.ctx, "read-tree", "HEAD").RunInDir(t.basePath); err != nil { return fmt.Errorf("SetDefaultIndex: %v", err) } return nil @@ -96,7 +97,7 @@ func (t *TemporaryUploadRepository) LsFiles(filenames ...string) ([]string, erro } } - if err := git.NewCommand(cmdArgs...).RunInDirPipeline(t.basePath, stdOut, stdErr); err != nil { + if err := git.NewCommandContext(t.ctx, cmdArgs...).RunInDirPipeline(t.basePath, stdOut, stdErr); err != nil { log.Error("Unable to run git ls-files for temporary repo: %s (%s) Error: %v\nstdout: %s\nstderr: %s", t.repo.FullName(), t.basePath, err, stdOut.String(), stdErr.String()) err = fmt.Errorf("Unable to run git ls-files for temporary repo of: %s Error: %v\nstdout: %s\nstderr: %s", t.repo.FullName(), err, stdOut.String(), stdErr.String()) return nil, err @@ -123,7 +124,7 @@ func (t *TemporaryUploadRepository) RemoveFilesFromIndex(filenames ...string) er } } - if err := git.NewCommand("update-index", "--remove", "-z", "--index-info").RunInDirFullPipeline(t.basePath, stdOut, stdErr, stdIn); err != nil { + if err := git.NewCommandContext(t.ctx, "update-index", "--remove", "-z", "--index-info").RunInDirFullPipeline(t.basePath, stdOut, stdErr, stdIn); err != nil { log.Error("Unable to update-index for temporary repo: %s (%s) Error: %v\nstdout: %s\nstderr: %s", t.repo.FullName(), t.basePath, err, stdOut.String(), stdErr.String()) return fmt.Errorf("Unable to update-index for temporary repo: %s Error: %v\nstdout: %s\nstderr: %s", t.repo.FullName(), err, stdOut.String(), stdErr.String()) } @@ -135,7 +136,7 @@ func (t *TemporaryUploadRepository) HashObject(content io.Reader) (string, error stdOut := new(bytes.Buffer) stdErr := new(bytes.Buffer) - if err := git.NewCommand("hash-object", "-w", "--stdin").RunInDirFullPipeline(t.basePath, stdOut, stdErr, content); err != nil { + if err := git.NewCommandContext(t.ctx, "hash-object", "-w", "--stdin").RunInDirFullPipeline(t.basePath, stdOut, stdErr, content); err != nil { log.Error("Unable to hash-object to temporary repo: %s (%s) Error: %v\nstdout: %s\nstderr: %s", t.repo.FullName(), t.basePath, err, stdOut.String(), stdErr.String()) return "", fmt.Errorf("Unable to hash-object to temporary repo: %s Error: %v\nstdout: %s\nstderr: %s", t.repo.FullName(), err, stdOut.String(), stdErr.String()) } @@ -145,7 +146,7 @@ func (t *TemporaryUploadRepository) HashObject(content io.Reader) (string, error // AddObjectToIndex adds the provided object hash to the index with the provided mode and path func (t *TemporaryUploadRepository) AddObjectToIndex(mode, objectHash, objectPath string) error { - if _, err := git.NewCommand("update-index", "--add", "--replace", "--cacheinfo", mode, objectHash, objectPath).RunInDir(t.basePath); err != nil { + if _, err := git.NewCommandContext(t.ctx, "update-index", "--add", "--replace", "--cacheinfo", mode, objectHash, objectPath).RunInDir(t.basePath); err != nil { stderr := err.Error() if matched, _ := regexp.MatchString(".*Invalid path '.*", stderr); matched { return models.ErrFilePathInvalid{ @@ -161,7 +162,7 @@ func (t *TemporaryUploadRepository) AddObjectToIndex(mode, objectHash, objectPat // WriteTree writes the current index as a tree to the object db and returns its hash func (t *TemporaryUploadRepository) WriteTree() (string, error) { - stdout, err := git.NewCommand("write-tree").RunInDir(t.basePath) + stdout, err := git.NewCommandContext(t.ctx, "write-tree").RunInDir(t.basePath) if err != nil { log.Error("Unable to write tree in temporary repo: %s(%s): Error: %v", t.repo.FullName(), t.basePath, err) return "", fmt.Errorf("Unable to write-tree in temporary repo for: %s Error: %v", t.repo.FullName(), err) @@ -179,7 +180,7 @@ func (t *TemporaryUploadRepository) GetLastCommitByRef(ref string) (string, erro if ref == "" { ref = "HEAD" } - stdout, err := git.NewCommand("rev-parse", ref).RunInDir(t.basePath) + stdout, err := git.NewCommandContext(t.ctx, "rev-parse", ref).RunInDir(t.basePath) if err != nil { log.Error("Unable to get last ref for %s in temporary repo: %s(%s): Error: %v", ref, t.repo.FullName(), t.basePath, err) return "", fmt.Errorf("Unable to rev-parse %s in temporary repo for: %s Error: %v", ref, t.repo.FullName(), err) @@ -218,7 +219,7 @@ func (t *TemporaryUploadRepository) CommitTreeWithDate(author, committer *user_m // Determine if we should sign if git.CheckGitVersionAtLeast("1.7.9") == nil { - sign, keyID, signer, _ := asymkey_service.SignCRUDAction(t.repo.RepoPath(), author, t.basePath, "HEAD") + sign, keyID, signer, _ := asymkey_service.SignCRUDAction(t.ctx, t.repo.RepoPath(), author, t.basePath, "HEAD") if sign { args = append(args, "-S"+keyID) if t.repo.GetTrustModel() == repo_model.CommitterTrustModel || t.repo.GetTrustModel() == repo_model.CollaboratorCommitterTrustModel { @@ -253,7 +254,7 @@ func (t *TemporaryUploadRepository) CommitTreeWithDate(author, committer *user_m stdout := new(bytes.Buffer) stderr := new(bytes.Buffer) - if err := git.NewCommand(args...).RunInDirTimeoutEnvFullPipeline(env, -1, t.basePath, stdout, stderr, messageBytes); err != nil { + if err := git.NewCommandContext(t.ctx, args...).RunInDirTimeoutEnvFullPipeline(env, -1, t.basePath, stdout, stderr, messageBytes); err != nil { log.Error("Unable to commit-tree in temporary repo: %s (%s) Error: %v\nStdout: %s\nStderr: %s", t.repo.FullName(), t.basePath, err, stdout, stderr) return "", fmt.Errorf("Unable to commit-tree in temporary repo: %s Error: %v\nStdout: %s\nStderr: %s", @@ -266,7 +267,7 @@ func (t *TemporaryUploadRepository) CommitTreeWithDate(author, committer *user_m func (t *TemporaryUploadRepository) Push(doer *user_model.User, commitHash, branch string) error { // Because calls hooks we need to pass in the environment env := models.PushingEnvironment(doer, t.repo) - if err := git.Push(t.gitRepo.Ctx, t.basePath, git.PushOptions{ + if err := git.Push(t.ctx, t.basePath, git.PushOptions{ Remote: t.repo.RepoPath(), Branch: strings.TrimSpace(commitHash) + ":" + git.BranchPrefix + strings.TrimSpace(branch), Env: env, @@ -302,7 +303,7 @@ func (t *TemporaryUploadRepository) DiffIndex() (*gitdiff.Diff, error) { var diff *gitdiff.Diff var finalErr error - if err := git.NewCommand("diff-index", "--src-prefix=\\a/", "--dst-prefix=\\b/", "--cached", "-p", "HEAD"). + if err := git.NewCommandContext(t.ctx, "diff-index", "--src-prefix=\\a/", "--dst-prefix=\\b/", "--cached", "-p", "HEAD"). RunInDirTimeoutEnvFullPipelineFunc(nil, 30*time.Second, t.basePath, stdoutWriter, stderr, nil, func(ctx context.Context, cancel context.CancelFunc) error { _ = stdoutWriter.Close() diff, finalErr = gitdiff.ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, stdoutReader, "") @@ -323,7 +324,7 @@ func (t *TemporaryUploadRepository) DiffIndex() (*gitdiff.Diff, error) { t.repo.FullName(), err, stderr) } - diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(t.basePath, "--cached", "HEAD") + diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(t.ctx, t.basePath, "--cached", "HEAD") if err != nil { return nil, err } diff --git a/services/repository/files/tree.go b/services/repository/files/tree.go index c6b77355ed..caad732887 100644 --- a/services/repository/files/tree.go +++ b/services/repository/files/tree.go @@ -5,6 +5,7 @@ package files import ( + "context" "fmt" "net/url" @@ -16,12 +17,7 @@ import ( ) // GetTreeBySHA get the GitTreeResponse of a repository using a sha hash. -func GetTreeBySHA(repo *repo_model.Repository, sha string, page, perPage int, recursive bool) (*api.GitTreeResponse, error) { - gitRepo, err := git.OpenRepository(repo.RepoPath()) - if err != nil { - return nil, err - } - defer gitRepo.Close() +func GetTreeBySHA(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, sha string, page, perPage int, recursive bool) (*api.GitTreeResponse, error) { gitTree, err := gitRepo.GetTree(sha) if err != nil || gitTree == nil { return nil, models.ErrSHANotFound{ diff --git a/services/repository/files/tree_test.go b/services/repository/files/tree_test.go index 37a6025d6c..e900480d35 100644 --- a/services/repository/files/tree_test.go +++ b/services/repository/files/tree_test.go @@ -29,7 +29,7 @@ func TestGetTreeBySHA(t *testing.T) { ctx.SetParams(":id", "1") ctx.SetParams(":sha", sha) - tree, err := GetTreeBySHA(ctx.Repo.Repository, ctx.Params(":sha"), page, perPage, true) + tree, err := GetTreeBySHA(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, ctx.Params(":sha"), page, perPage, true) assert.NoError(t, err) expectedTree := &api.GitTreeResponse{ SHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d", diff --git a/services/repository/files/update.go b/services/repository/files/update.go index 9fb074ed39..4b8653a7f7 100644 --- a/services/repository/files/update.go +++ b/services/repository/files/update.go @@ -6,6 +6,7 @@ package files import ( "bytes" + "context" "fmt" "path" "strings" @@ -22,7 +23,6 @@ import ( "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" asymkey_service "code.gitea.io/gitea/services/asymkey" - repo_service "code.gitea.io/gitea/services/repository" stdcharset "golang.org/x/net/html/charset" "golang.org/x/text/transform" @@ -125,7 +125,7 @@ func detectEncodingAndBOM(entry *git.TreeEntry, repo *repo_model.Repository) (st } // CreateOrUpdateRepoFile adds or updates a file in the given repository -func CreateOrUpdateRepoFile(repo *repo_model.Repository, doer *user_model.User, opts *UpdateRepoFileOptions) (*structs.FileResponse, error) { +func CreateOrUpdateRepoFile(ctx context.Context, repo *repo_model.Repository, doer *user_model.User, opts *UpdateRepoFileOptions) (*structs.FileResponse, error) { // If no branch name is set, assume default branch if opts.OldBranch == "" { opts.OldBranch = repo.DefaultBranch @@ -134,8 +134,14 @@ func CreateOrUpdateRepoFile(repo *repo_model.Repository, doer *user_model.User, opts.NewBranch = opts.OldBranch } + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) + if err != nil { + return nil, err + } + defer closer.Close() + // oldBranch must exist for this operation - if _, err := repo_service.GetBranch(repo, opts.OldBranch); err != nil { + if _, err := gitRepo.GetBranch(opts.OldBranch); err != nil { return nil, err } @@ -143,7 +149,7 @@ func CreateOrUpdateRepoFile(repo *repo_model.Repository, doer *user_model.User, // Check to make sure the branch does not already exist, otherwise we can't proceed. // If we aren't branching to a new branch, make sure user can commit to the given branch if opts.NewBranch != opts.OldBranch { - existingBranch, err := repo_service.GetBranch(repo, opts.NewBranch) + existingBranch, err := gitRepo.GetBranch(opts.NewBranch) if existingBranch != nil { return nil, models.ErrBranchAlreadyExists{ BranchName: opts.NewBranch, @@ -152,7 +158,7 @@ func CreateOrUpdateRepoFile(repo *repo_model.Repository, doer *user_model.User, if err != nil && !git.IsErrBranchNotExist(err) { return nil, err } - } else if err := VerifyBranchProtection(repo, doer, opts.OldBranch, opts.TreePath); err != nil { + } else if err := VerifyBranchProtection(ctx, repo, doer, opts.OldBranch, opts.TreePath); err != nil { return nil, err } @@ -180,7 +186,7 @@ func CreateOrUpdateRepoFile(repo *repo_model.Repository, doer *user_model.User, author, committer := GetAuthorAndCommitterUsers(opts.Author, opts.Committer, doer) - t, err := NewTemporaryUploadRepository(repo) + t, err := NewTemporaryUploadRepository(ctx, repo) if err != nil { log.Error("%v", err) } @@ -435,7 +441,7 @@ func CreateOrUpdateRepoFile(repo *repo_model.Repository, doer *user_model.User, return nil, err } - file, err := GetFileResponseFromCommit(repo, commit, opts.NewBranch, treePath) + file, err := GetFileResponseFromCommit(ctx, repo, commit, opts.NewBranch, treePath) if err != nil { return nil, err } @@ -443,7 +449,7 @@ func CreateOrUpdateRepoFile(repo *repo_model.Repository, doer *user_model.User, } // VerifyBranchProtection verify the branch protection for modifying the given treePath on the given branch -func VerifyBranchProtection(repo *repo_model.Repository, doer *user_model.User, branchName, treePath string) error { +func VerifyBranchProtection(ctx context.Context, repo *repo_model.Repository, doer *user_model.User, branchName, treePath string) error { protectedBranch, err := models.GetProtectedBranchBy(repo.ID, branchName) if err != nil { return err @@ -460,7 +466,7 @@ func VerifyBranchProtection(repo *repo_model.Repository, doer *user_model.User, } } if protectedBranch.RequireSignedCommits { - _, _, _, err := asymkey_service.SignCRUDAction(repo.RepoPath(), doer, repo.RepoPath(), branchName) + _, _, _, err := asymkey_service.SignCRUDAction(ctx, repo.RepoPath(), doer, repo.RepoPath(), branchName) if err != nil { if !asymkey_service.IsErrWontSign(err) { return err diff --git a/services/repository/files/upload.go b/services/repository/files/upload.go index 28ed461fdc..79fca3ead7 100644 --- a/services/repository/files/upload.go +++ b/services/repository/files/upload.go @@ -5,6 +5,7 @@ package files import ( + "context" "fmt" "os" "path" @@ -49,7 +50,7 @@ func cleanUpAfterFailure(infos *[]uploadInfo, t *TemporaryUploadRepository, orig } // UploadRepoFiles uploads files to the given repository -func UploadRepoFiles(repo *repo_model.Repository, doer *user_model.User, opts *UploadRepoFileOptions) error { +func UploadRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *user_model.User, opts *UploadRepoFileOptions) error { if len(opts.Files) == 0 { return nil } @@ -80,7 +81,7 @@ func UploadRepoFiles(repo *repo_model.Repository, doer *user_model.User, opts *U infos[i] = uploadInfo{upload: upload} } - t, err := NewTemporaryUploadRepository(repo) + t, err := NewTemporaryUploadRepository(ctx, repo) if err != nil { return err } diff --git a/services/repository/hooks.go b/services/repository/hooks.go index f567702e9d..3905249499 100644 --- a/services/repository/hooks.go +++ b/services/repository/hooks.go @@ -54,13 +54,13 @@ func SyncRepositoryHooks(ctx context.Context) error { // GenerateGitHooks generates git hooks from a template repository func GenerateGitHooks(ctx context.Context, templateRepo, generateRepo *repo_model.Repository) error { - generateGitRepo, err := git.OpenRepository(generateRepo.RepoPath()) + generateGitRepo, err := git.OpenRepositoryCtx(ctx, generateRepo.RepoPath()) if err != nil { return err } defer generateGitRepo.Close() - templateGitRepo, err := git.OpenRepository(templateRepo.RepoPath()) + templateGitRepo, err := git.OpenRepositoryCtx(ctx, templateRepo.RepoPath()) if err != nil { return err } diff --git a/services/repository/push.go b/services/repository/push.go index 11854ccb39..62e2104432 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -20,6 +20,7 @@ import ( "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification" + "code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/queue" repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" @@ -77,15 +78,19 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { return nil } + ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("PushUpdates: %s/%s", optsList[0].RepoUserName, optsList[0].RepoName)) + defer finished() + repo, err := repo_model.GetRepositoryByOwnerAndName(optsList[0].RepoUserName, optsList[0].RepoName) if err != nil { return fmt.Errorf("GetRepositoryByOwnerAndName failed: %v", err) } repoPath := repo.RepoPath() - gitRepo, err := git.OpenRepository(repoPath) + + gitRepo, err := git.OpenRepositoryCtx(ctx, repoPath) if err != nil { - return fmt.Errorf("OpenRepository: %v", err) + return fmt.Errorf("OpenRepository[%s]: %v", repoPath, err) } defer gitRepo.Close() @@ -99,7 +104,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { for _, opts := range optsList { if opts.IsNewRef() && opts.IsDelRef() { - return fmt.Errorf("Old and new revisions are both %s", git.EmptySHA) + return fmt.Errorf("old and new revisions are both %s", git.EmptySHA) } if opts.IsTag() { // If is tag reference if pusher == nil || pusher.ID != opts.PusherID { @@ -139,7 +144,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { }, commits) addTags = append(addTags, tagName) - notification.NotifyCreateRef(pusher, repo, "tag", opts.RefFullName) + notification.NotifyCreateRef(pusher, repo, "tag", opts.RefFullName, opts.NewCommitID) } } else if opts.IsBranch() { // If is branch reference if pusher == nil || pusher.ID != opts.PusherID { @@ -184,14 +189,14 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { if err != nil { return fmt.Errorf("newCommit.CommitsBeforeLimit: %v", err) } - notification.NotifyCreateRef(pusher, repo, "branch", opts.RefFullName) + notification.NotifyCreateRef(pusher, repo, "branch", opts.RefFullName, opts.NewCommitID) } else { l, err = newCommit.CommitsBeforeUntil(opts.OldCommitID) if err != nil { return fmt.Errorf("newCommit.CommitsBeforeUntil: %v", err) } - isForce, err := repo_module.IsForcePush(opts) + isForce, err := repo_module.IsForcePush(ctx, opts) if err != nil { log.Error("isForcePush %s:%s failed: %v", repo.FullName(), branch, err) } diff --git a/services/repository/repository.go b/services/repository/repository.go index 17fab57e19..c3ca867187 100644 --- a/services/repository/repository.go +++ b/services/repository/repository.go @@ -5,6 +5,7 @@ package repository import ( + "context" "fmt" "code.gitea.io/gitea/models" @@ -31,8 +32,8 @@ func CreateRepository(doer, owner *user_model.User, opts models.CreateRepoOption } // DeleteRepository deletes a repository for a user or organization. -func DeleteRepository(doer *user_model.User, repo *repo_model.Repository, notify bool) error { - if err := pull_service.CloseRepoBranchesPulls(doer, repo); err != nil { +func DeleteRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, notify bool) error { + if err := pull_service.CloseRepoBranchesPulls(ctx, doer, repo); err != nil { log.Error("CloseRepoBranchesPulls failed: %v", err) } diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go index 4b679a4ad2..938aea96ca 100644 --- a/services/wiki/wiki.go +++ b/services/wiki/wiki.go @@ -6,6 +6,7 @@ package wiki import ( + "context" "fmt" "net/url" "os" @@ -13,7 +14,6 @@ import ( "code.gitea.io/gitea/models" admin_model "code.gitea.io/gitea/models/admin" - "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" @@ -72,16 +72,16 @@ func FilenameToName(filename string) (string, error) { // InitWiki initializes a wiki for repository, // it does nothing when repository already has wiki. -func InitWiki(repo *repo_model.Repository) error { +func InitWiki(ctx context.Context, repo *repo_model.Repository) error { if repo.HasWiki() { return nil } - if err := git.InitRepository(repo.WikiPath(), true); err != nil { + if err := git.InitRepository(ctx, repo.WikiPath(), true); err != nil { return fmt.Errorf("InitRepository: %v", err) } else if err = repo_module.CreateDelegateHooks(repo.WikiPath()); err != nil { return fmt.Errorf("createDelegateHooks: %v", err) - } else if _, err = git.NewCommand("symbolic-ref", "HEAD", git.BranchPrefix+"master").RunInDir(repo.WikiPath()); err != nil { + } else if _, err = git.NewCommandContext(ctx, "symbolic-ref", "HEAD", git.BranchPrefix+"master").RunInDir(repo.WikiPath()); err != nil { return fmt.Errorf("unable to set default wiki branch to master: %v", err) } return nil @@ -119,18 +119,18 @@ func prepareWikiFileName(gitRepo *git.Repository, wikiName string) (bool, string } // updateWikiPage adds a new page to the repository wiki. -func updateWikiPage(doer *user_model.User, repo *repo_model.Repository, oldWikiName, newWikiName, content, message string, isNew bool) (err error) { +func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, oldWikiName, newWikiName, content, message string, isNew bool) (err error) { if err = nameAllowed(newWikiName); err != nil { return err } wikiWorkingPool.CheckIn(fmt.Sprint(repo.ID)) defer wikiWorkingPool.CheckOut(fmt.Sprint(repo.ID)) - if err = InitWiki(repo); err != nil { + if err = InitWiki(ctx, repo); err != nil { return fmt.Errorf("InitWiki: %v", err) } - hasMasterBranch := git.IsBranchExist(git.DefaultContext, repo.WikiPath(), "master") + hasMasterBranch := git.IsBranchExist(ctx, repo.WikiPath(), "master") basePath, err := models.CreateTemporaryPath("update-wiki") if err != nil { @@ -151,12 +151,12 @@ func updateWikiPage(doer *user_model.User, repo *repo_model.Repository, oldWikiN cloneOpts.Branch = "master" } - if err := git.Clone(repo.WikiPath(), basePath, cloneOpts); err != nil { + if err := git.Clone(ctx, repo.WikiPath(), basePath, cloneOpts); err != nil { log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err) return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err) } - gitRepo, err := git.OpenRepository(basePath) + gitRepo, err := git.OpenRepositoryCtx(ctx, basePath) if err != nil { log.Error("Unable to open temporary repository: %s (%v)", basePath, err) return fmt.Errorf("Failed to open new temporary repository in: %s %v", basePath, err) @@ -226,7 +226,7 @@ func updateWikiPage(doer *user_model.User, repo *repo_model.Repository, oldWikiN committer := doer.NewGitSig() - sign, signingKey, signer, _ := asymkey_service.SignWikiCommit(repo.WikiPath(), doer) + sign, signingKey, signer, _ := asymkey_service.SignWikiCommit(ctx, repo.WikiPath(), doer) if sign { commitTreeOpts.KeyID = signingKey if repo.GetTrustModel() == repo_model.CommitterTrustModel || repo.GetTrustModel() == repo_model.CollaboratorCommitterTrustModel { @@ -267,22 +267,22 @@ func updateWikiPage(doer *user_model.User, repo *repo_model.Repository, oldWikiN } // AddWikiPage adds a new wiki page with a given wikiPath. -func AddWikiPage(doer *user_model.User, repo *repo_model.Repository, wikiName, content, message string) error { - return updateWikiPage(doer, repo, "", wikiName, content, message, true) +func AddWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, wikiName, content, message string) error { + return updateWikiPage(ctx, doer, repo, "", wikiName, content, message, true) } // EditWikiPage updates a wiki page identified by its wikiPath, // optionally also changing wikiPath. -func EditWikiPage(doer *user_model.User, repo *repo_model.Repository, oldWikiName, newWikiName, content, message string) error { - return updateWikiPage(doer, repo, oldWikiName, newWikiName, content, message, false) +func EditWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, oldWikiName, newWikiName, content, message string) error { + return updateWikiPage(ctx, doer, repo, oldWikiName, newWikiName, content, message, false) } // DeleteWikiPage deletes a wiki page identified by its path. -func DeleteWikiPage(doer *user_model.User, repo *repo_model.Repository, wikiName string) (err error) { +func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, wikiName string) (err error) { wikiWorkingPool.CheckIn(fmt.Sprint(repo.ID)) defer wikiWorkingPool.CheckOut(fmt.Sprint(repo.ID)) - if err = InitWiki(repo); err != nil { + if err = InitWiki(ctx, repo); err != nil { return fmt.Errorf("InitWiki: %v", err) } @@ -296,7 +296,7 @@ func DeleteWikiPage(doer *user_model.User, repo *repo_model.Repository, wikiName } }() - if err := git.Clone(repo.WikiPath(), basePath, git.CloneRepoOptions{ + if err := git.Clone(ctx, repo.WikiPath(), basePath, git.CloneRepoOptions{ Bare: true, Shared: true, Branch: "master", @@ -305,7 +305,7 @@ func DeleteWikiPage(doer *user_model.User, repo *repo_model.Repository, wikiName return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err) } - gitRepo, err := git.OpenRepository(basePath) + gitRepo, err := git.OpenRepositoryCtx(ctx, basePath) if err != nil { log.Error("Unable to open temporary repository: %s (%v)", basePath, err) return fmt.Errorf("Failed to open new temporary repository in: %s %v", basePath, err) @@ -344,7 +344,7 @@ func DeleteWikiPage(doer *user_model.User, repo *repo_model.Repository, wikiName committer := doer.NewGitSig() - sign, signingKey, signer, _ := asymkey_service.SignWikiCommit(repo.WikiPath(), doer) + sign, signingKey, signer, _ := asymkey_service.SignWikiCommit(ctx, repo.WikiPath(), doer) if sign { commitTreeOpts.KeyID = signingKey if repo.GetTrustModel() == repo_model.CommitterTrustModel || repo.GetTrustModel() == repo_model.CollaboratorCommitterTrustModel { @@ -374,11 +374,11 @@ func DeleteWikiPage(doer *user_model.User, repo *repo_model.Repository, wikiName } // DeleteWiki removes the actual and local copy of repository wiki. -func DeleteWiki(repo *repo_model.Repository) error { +func DeleteWiki(ctx context.Context, repo *repo_model.Repository) error { if err := repo_model.UpdateRepositoryUnits(repo, nil, []unit.Type{unit.TypeWiki}); err != nil { return err } - admin_model.RemoveAllWithNotice(db.DefaultContext, "Delete repository wiki", repo.WikiPath()) + admin_model.RemoveAllWithNotice(ctx, "Delete repository wiki", repo.WikiPath()) return nil } diff --git a/services/wiki/wiki_test.go b/services/wiki/wiki_test.go index f5ee76109c..928d08c828 100644 --- a/services/wiki/wiki_test.go +++ b/services/wiki/wiki_test.go @@ -115,11 +115,11 @@ func TestRepository_InitWiki(t *testing.T) { unittest.PrepareTestEnv(t) // repo1 already has a wiki repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository) - assert.NoError(t, InitWiki(repo1)) + assert.NoError(t, InitWiki(git.DefaultContext, repo1)) // repo2 does not already have a wiki repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}).(*repo_model.Repository) - assert.NoError(t, InitWiki(repo2)) + assert.NoError(t, InitWiki(git.DefaultContext, repo2)) assert.True(t, repo2.HasWiki()) } @@ -136,9 +136,9 @@ func TestRepository_AddWikiPage(t *testing.T) { wikiName := wikiName t.Run("test wiki exist: "+wikiName, func(t *testing.T) { t.Parallel() - assert.NoError(t, AddWikiPage(doer, repo, wikiName, wikiContent, commitMsg)) + assert.NoError(t, AddWikiPage(git.DefaultContext, doer, repo, wikiName, wikiContent, commitMsg)) // Now need to show that the page has been added: - gitRepo, err := git.OpenRepository(repo.WikiPath()) + gitRepo, err := git.OpenRepositoryCtx(git.DefaultContext, repo.WikiPath()) assert.NoError(t, err) defer gitRepo.Close() masterTree, err := gitRepo.GetTree("master") @@ -153,7 +153,7 @@ func TestRepository_AddWikiPage(t *testing.T) { t.Run("check wiki already exist", func(t *testing.T) { t.Parallel() // test for already-existing wiki name - err := AddWikiPage(doer, repo, "Home", wikiContent, commitMsg) + err := AddWikiPage(git.DefaultContext, doer, repo, "Home", wikiContent, commitMsg) assert.Error(t, err) assert.True(t, models.IsErrWikiAlreadyExist(err)) }) @@ -161,7 +161,7 @@ func TestRepository_AddWikiPage(t *testing.T) { t.Run("check wiki reserved name", func(t *testing.T) { t.Parallel() // test for reserved wiki name - err := AddWikiPage(doer, repo, "_edit", wikiContent, commitMsg) + err := AddWikiPage(git.DefaultContext, doer, repo, "_edit", wikiContent, commitMsg) assert.Error(t, err) assert.True(t, models.IsErrWikiReservedName(err)) }) @@ -180,10 +180,10 @@ func TestRepository_EditWikiPage(t *testing.T) { "New/name/with/slashes", } { unittest.PrepareTestEnv(t) - assert.NoError(t, EditWikiPage(doer, repo, "Home", newWikiName, newWikiContent, commitMsg)) + assert.NoError(t, EditWikiPage(git.DefaultContext, doer, repo, "Home", newWikiName, newWikiContent, commitMsg)) // Now need to show that the page has been added: - gitRepo, err := git.OpenRepository(repo.WikiPath()) + gitRepo, err := git.OpenRepositoryCtx(git.DefaultContext, repo.WikiPath()) assert.NoError(t, err) masterTree, err := gitRepo.GetTree("master") assert.NoError(t, err) @@ -204,10 +204,10 @@ func TestRepository_DeleteWikiPage(t *testing.T) { unittest.PrepareTestEnv(t) repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository) doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User) - assert.NoError(t, DeleteWikiPage(doer, repo, "Home")) + assert.NoError(t, DeleteWikiPage(git.DefaultContext, doer, repo, "Home")) // Now need to show that the page has been added: - gitRepo, err := git.OpenRepository(repo.WikiPath()) + gitRepo, err := git.OpenRepositoryCtx(git.DefaultContext, repo.WikiPath()) assert.NoError(t, err) defer gitRepo.Close() masterTree, err := gitRepo.GetTree("master") @@ -220,7 +220,7 @@ func TestRepository_DeleteWikiPage(t *testing.T) { func TestPrepareWikiFileName(t *testing.T) { unittest.PrepareTestEnv(t) repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository) - gitRepo, err := git.OpenRepository(repo.WikiPath()) + gitRepo, err := git.OpenRepositoryCtx(git.DefaultContext, repo.WikiPath()) defer gitRepo.Close() assert.NoError(t, err) @@ -280,7 +280,7 @@ func TestPrepareWikiFileName_FirstPage(t *testing.T) { } }() - err = git.InitRepository(tmpDir, true) + err = git.InitRepository(git.DefaultContext, tmpDir, true) assert.NoError(t, err) gitRepo, err := git.OpenRepository(tmpDir) diff --git a/templates/explore/repo_list.tmpl b/templates/explore/repo_list.tmpl index 4ff48b6076..196c947213 100644 --- a/templates/explore/repo_list.tmpl +++ b/templates/explore/repo_list.tmpl @@ -49,7 +49,8 @@
- {{if .DescriptionHTML}}

{{.DescriptionHTML}}

{{end}} + {{ $description := .DescriptionHTML $.Context}} + {{if $description}}

{{$description}}

{{end}} {{if .Topics }}
{{range .Topics}} diff --git a/templates/repo/branch/list.tmpl b/templates/repo/branch/list.tmpl index fc81901069..ea259949b4 100644 --- a/templates/repo/branch/list.tmpl +++ b/templates/repo/branch/list.tmpl @@ -18,7 +18,7 @@ {{svg "octicon-shield-lock"}} {{end}} {{.DefaultBranch}} -

{{svg "octicon-git-commit" 16 "mr-2"}}{{ShortSha .DefaultBranchBranch.Commit.ID.String}} · {{RenderCommitMessage .DefaultBranchBranch.Commit.CommitMessage .RepoLink .Repository.ComposeMetas}} · {{.i18n.Tr "org.repo_updated"}} {{TimeSince .DefaultBranchBranch.Commit.Committer.When .i18n.Lang}}

+

{{svg "octicon-git-commit" 16 "mr-2"}}{{ShortSha .DefaultBranchBranch.Commit.ID.String}} · {{RenderCommitMessage $.Context .DefaultBranchBranch.Commit.CommitMessage .RepoLink .Repository.ComposeMetas}} · {{.i18n.Tr "org.repo_updated"}} {{TimeSince .DefaultBranchBranch.Commit.Committer.When .i18n.Lang}}

{{if and $.IsWriter (not $.Repository.IsArchived) (not .IsDeleted)}} @@ -59,7 +59,7 @@ {{svg "octicon-shield-lock"}} {{end}} {{.Name}} -

{{svg "octicon-git-commit" 16 "mr-2"}}{{ShortSha .Commit.ID.String}} · {{RenderCommitMessage .Commit.CommitMessage $.RepoLink $.Repository.ComposeMetas}} · {{$.i18n.Tr "org.repo_updated"}} {{TimeSince .Commit.Committer.When $.i18n.Lang}}

+

{{svg "octicon-git-commit" 16 "mr-2"}}{{ShortSha .Commit.ID.String}} · {{RenderCommitMessage $.Context .Commit.CommitMessage $.RepoLink $.Repository.ComposeMetas}} · {{$.i18n.Tr "org.repo_updated"}} {{TimeSince .Commit.Committer.When $.i18n.Lang}}

{{end}} diff --git a/templates/repo/commit_page.tmpl b/templates/repo/commit_page.tmpl index c995273643..931201fed6 100644 --- a/templates/repo/commit_page.tmpl +++ b/templates/repo/commit_page.tmpl @@ -23,9 +23,9 @@ {{.i18n.Tr "repo.diff.browse_source"}} {{end}} -

{{RenderCommitMessage .Commit.Message $.RepoLink $.Repository.ComposeMetas}}{{template "repo/commit_statuses" dict "Status" .CommitStatus "Statuses" .CommitStatuses "root" $}}

+

{{RenderCommitMessage $.Context .Commit.Message $.RepoLink $.Repository.ComposeMetas}}{{template "repo/commit_statuses" dict "Status" .CommitStatus "Statuses" .CommitStatuses "root" $}}

{{if IsMultilineCommitMessage .Commit.Message}} -
{{RenderCommitBody .Commit.Message $.RepoLink $.Repository.ComposeMetas}}
+
{{RenderCommitBody $.Context .Commit.Message $.RepoLink $.Repository.ComposeMetas}}
{{end}} {{if .BranchName}} {{svg "octicon-git-branch" 16 "mr-2"}}{{.BranchName}} @@ -172,7 +172,7 @@ {{TimeSince .NoteCommit.Author.When $.Lang}}
-
{{RenderNote .Note $.RepoLink $.Repository.ComposeMetas}}
+
{{RenderNote $.Context .Note $.RepoLink $.Repository.ComposeMetas}}
{{end}} {{template "repo/diff/box" .}} diff --git a/templates/repo/commits_list.tmpl b/templates/repo/commits_list.tmpl index d43170e525..7bc37175c2 100644 --- a/templates/repo/commits_list.tmpl +++ b/templates/repo/commits_list.tmpl @@ -64,7 +64,7 @@ {{.Summary | RenderEmoji}} {{else }} {{ $commitLink:= printf "%s/commit/%s" $commitRepoLink (PathEscape .ID.String) }} - {{RenderCommitMessageLinkSubject .Message $commitRepoLink $commitLink $.Repository.ComposeMetas}} + {{RenderCommitMessageLinkSubject $.Context .Message $commitRepoLink $commitLink $.Repository.ComposeMetas}} {{end}} {{if IsMultilineCommitMessage .Message}} @@ -72,7 +72,7 @@ {{end}} {{template "repo/commit_statuses" dict "Status" .Status "Statuses" .Statuses "root" $}} {{if IsMultilineCommitMessage .Message}} - + {{end}} {{TimeSince .Author.When $.Lang}} diff --git a/templates/repo/commits_list_small.tmpl b/templates/repo/commits_list_small.tmpl index 1593ac8129..ddcccf0db5 100644 --- a/templates/repo/commits_list_small.tmpl +++ b/templates/repo/commits_list_small.tmpl @@ -47,12 +47,12 @@ {{ $commitLink:= printf "%s/commit/%s" $.comment.Issue.PullRequest.BaseRepo.Link (PathEscape .ID.String) }} - {{RenderCommitMessageLinkSubject .Message ($.comment.Issue.PullRequest.BaseRepo.Link|Escape) $commitLink $.comment.Issue.PullRequest.BaseRepo.ComposeMetas}} + {{RenderCommitMessageLinkSubject $.Context .Message ($.comment.Issue.PullRequest.BaseRepo.Link|Escape) $commitLink $.comment.Issue.PullRequest.BaseRepo.ComposeMetas}} {{if IsMultilineCommitMessage .Message}} {{end}} {{if IsMultilineCommitMessage .Message}} - + {{end}}
{{end}} diff --git a/templates/repo/graph/commits.tmpl b/templates/repo/graph/commits.tmpl index d213c53e96..478121f27c 100644 --- a/templates/repo/graph/commits.tmpl +++ b/templates/repo/graph/commits.tmpl @@ -29,7 +29,7 @@ - {{RenderCommitMessage $commit.Subject $.RepoLink $.Repository.ComposeMetas}} + {{RenderCommitMessage $.Context $commit.Subject $.RepoLink $.Repository.ComposeMetas}} {{range $commit.Refs}} diff --git a/templates/repo/header.tmpl b/templates/repo/header.tmpl index 014623dc9a..01ec0f0c83 100644 --- a/templates/repo/header.tmpl +++ b/templates/repo/header.tmpl @@ -36,7 +36,7 @@ {{end}} - {{if .IsMirror}}{{end}} + {{if .IsMirror}}{{end}} {{if .IsFork}}
{{$.i18n.Tr "repo.forked_from"}} {{.BaseRepo.FullName}}
{{end}} {{if .IsGenerated}}
{{$.i18n.Tr "repo.generated_from"}} {{.TemplateRepo.FullName}}
{{end}} diff --git a/templates/repo/home.tmpl b/templates/repo/home.tmpl index 5ee6cc2df7..9924ae547d 100644 --- a/templates/repo/home.tmpl +++ b/templates/repo/home.tmpl @@ -5,7 +5,8 @@ {{template "base/alert" .}}
- {{if .Repository.DescriptionHTML}}{{.Repository.DescriptionHTML}}{{else if .IsRepositoryAdmin}}{{.i18n.Tr "repo.no_desc"}}{{end}} + {{ $description := .Repository.DescriptionHTML $.Context}} + {{if $description}}{{$description}}{{else if .IsRepositoryAdmin}}{{.i18n.Tr "repo.no_desc"}}{{end}} {{.Repository.Website}}
{{if .RepoSearchEnabled}} diff --git a/templates/repo/issue/view_title.tmpl b/templates/repo/issue/view_title.tmpl index b43015f1d2..07c700afea 100644 --- a/templates/repo/issue/view_title.tmpl +++ b/templates/repo/issue/view_title.tmpl @@ -6,7 +6,7 @@
{{end}}

- {{RenderIssueTitle .Issue.Title $.RepoLink $.Repository.ComposeMetas}} + {{RenderIssueTitle $.Context .Issue.Title $.RepoLink $.Repository.ComposeMetas}} #{{.Issue.Index}} - {{$address := MirrorRemoteAddress .Mirror}} + {{$address := MirrorRemoteAddress $.Context .Mirror}}
@@ -168,7 +168,7 @@ {{range .PushMirrors}} - {{$address := MirrorRemoteAddress .}} + {{$address := MirrorRemoteAddress $.Context .}} {{$address.Address}} {{$.i18n.Tr "repo.settings.mirror_settings.direction.push"}} {{if .LastUpdateUnix}}{{.LastUpdateUnix.AsTime}}{{else}}{{$.i18n.Tr "never"}}{{end}} {{if .LastError}}
{{$.i18n.Tr "error"}}
{{end}} diff --git a/templates/repo/view_list.tmpl b/templates/repo/view_list.tmpl index d490220694..870bbb92e8 100644 --- a/templates/repo/view_list.tmpl +++ b/templates/repo/view_list.tmpl @@ -26,10 +26,10 @@ {{template "repo/commit_statuses" dict "Status" .LatestCommitStatus "Statuses" .LatestCommitStatuses "root" $}} {{ $commitLink:= printf "%s/commit/%s" .RepoLink (PathEscape .LatestCommit.ID.String) }} - {{RenderCommitMessageLinkSubject .LatestCommit.Message $.RepoLink $commitLink $.Repository.ComposeMetas}} + {{RenderCommitMessageLinkSubject $.Context .LatestCommit.Message $.RepoLink $commitLink $.Repository.ComposeMetas}} {{if IsMultilineCommitMessage .LatestCommit.Message}} - + {{end}} {{end}} @@ -81,7 +81,7 @@ {{if $commit}} {{$commitLink := printf "%s/commit/%s" $.RepoLink (PathEscape $commit.ID.String)}} - {{RenderCommitMessageLinkSubject $commit.Message $.RepoLink $commitLink $.Repository.ComposeMetas}} + {{RenderCommitMessageLinkSubject $.Context $commit.Message $.RepoLink $commitLink $.Repository.ComposeMetas}} {{else}}
{{end}} diff --git a/templates/user/dashboard/feeds.tmpl b/templates/user/dashboard/feeds.tmpl index c21ac337ab..16f2edd4f6 100644 --- a/templates/user/dashboard/feeds.tmpl +++ b/templates/user/dashboard/feeds.tmpl @@ -91,7 +91,7 @@ {{avatarHTML ($push.AvatarLink .AuthorEmail) 16 "mr-2" .AuthorName}} {{ShortSha .Sha1}} - {{RenderCommitMessage .Message $repoLink $.ComposeMetas}} + {{RenderCommitMessage $.Context .Message $repoLink $.ComposeMetas}} {{end}} diff --git a/tools/fuzz.go b/tools/fuzz.go index 2f980b3b89..d629096ed6 100644 --- a/tools/fuzz.go +++ b/tools/fuzz.go @@ -9,6 +9,7 @@ package fuzz import ( "bytes" + "context" "io" "code.gitea.io/gitea/modules/markup" @@ -25,6 +26,7 @@ import ( var ( renderContext = markup.RenderContext{ + Ctx: context.Background(), URLPrefix: "https://example.com/go-gitea/gitea", Metas: map[string]string{ "user": "go-gitea",