From 6c9c38a840377685111c0bd1f7ee964fe2cbf075 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Tue, 30 Nov 2021 21:57:59 +0000 Subject: [PATCH 01/48] Propagate context and ensure git commands run in request context This PR continues the work in #17125 by progressively ensuring that git commands run within the request context. Signed-off-by: Andrew Thornton --- cmd/admin.go | 2 +- .../api_repo_get_contents_list_test.go | 2 +- integrations/api_repo_get_contents_test.go | 2 +- .../git_helper_for_declarative_test.go | 4 +- integrations/pull_merge_test.go | 6 +-- integrations/pull_update_test.go | 11 ++-- models/admin/notice.go | 8 ++- modules/git/commit_info_test.go | 2 +- modules/git/repo.go | 11 ++-- modules/git/repo_branch.go | 4 +- modules/indexer/code/bleve.go | 6 +-- modules/indexer/code/elastic_search.go | 4 +- modules/indexer/code/indexer.go | 8 +-- modules/indexer/code/indexer_test.go | 3 +- modules/indexer/code/wrapped.go | 5 +- modules/repository/generate.go | 16 +++--- modules/repository/init.go | 16 +++--- modules/repository/repo.go | 4 +- routers/api/v1/repo/branch.go | 4 +- routers/api/v1/repo/pull.go | 8 +-- routers/api/v1/repo/repo.go | 2 +- routers/api/v1/repo/wiki.go | 6 +-- routers/private/serv.go | 2 +- routers/web/admin/repos.go | 2 +- routers/web/repo/branch.go | 6 +-- routers/web/repo/http.go | 3 +- routers/web/repo/lfs.go | 2 +- routers/web/repo/pull.go | 8 +-- routers/web/repo/setting.go | 4 +- routers/web/repo/wiki.go | 6 +-- services/agit/agit.go | 2 +- services/gitdiff/gitdiff.go | 2 +- services/migrations/dump.go | 6 +-- services/mirror/mirror_pull.go | 2 +- services/pull/merge.go | 51 ++++++++++--------- services/pull/patch.go | 11 ++-- services/pull/pull.go | 32 ++++++------ services/pull/temp_repo.go | 17 ++++--- services/pull/update.go | 11 ++-- services/repository/branch.go | 35 +++++++------ services/repository/check.go | 2 +- services/repository/repository.go | 5 +- services/wiki/wiki.go | 38 +++++++------- services/wiki/wiki_test.go | 24 ++++----- 44 files changed, 209 insertions(+), 196 deletions(-) diff --git a/cmd/admin.go b/cmd/admin.go index d11b824fa12ea..f5b6412751781 100644 --- a/cmd/admin.go +++ b/cmd/admin.go @@ -565,7 +565,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/integrations/api_repo_get_contents_list_test.go b/integrations/api_repo_get_contents_list_test.go index 63ac9cfb5306b..2741e6605665f 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 9843239ec4a70..33253452e0cff 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 71473ce464bc3..c8200fe999947 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)) assert.NoError(t, os.WriteFile(filepath.Join(dstPath, "README.md"), []byte(fmt.Sprintf("# Testing Repository\n\nOriginally created in: %s", dstPath)), 0644)) assert.NoError(t, git.AddChanges(dstPath, true)) signature := git.Signature{ diff --git a/integrations/pull_merge_test.go b/integrations/pull_merge_test.go index 2e219dcc91820..8bd308a57e44b 100644 --- a/integrations/pull_merge_test.go +++ b/integrations/pull_merge_test.go @@ -240,11 +240,11 @@ func TestCantMergeConflict(t *testing.T) { gitRepo, err := git.OpenRepository(models.RepoPath(user1.Name, repo1.Name)) assert.NoError(t, err) - err = pull.Merge(pr, user1, gitRepo, models.MergeStyleMerge, "CONFLICT") + err = pull.Merge(git.DefaultContext, pr, user1, gitRepo, models.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, models.MergeStyleRebase, "CONFLICT") + err = pull.Merge(git.DefaultContext, pr, user1, gitRepo, models.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() @@ -328,7 +328,7 @@ func TestCantMergeUnrelated(t *testing.T) { BaseBranch: "base", }).(*models.PullRequest) - err = pull.Merge(pr, user1, gitRepo, models.MergeStyleMerge, "UNRELATED") + err = pull.Merge(git.DefaultContext, pr, user1, gitRepo, models.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 e5e19475caa43..9e129e7270074 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) @@ -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/models/admin/notice.go b/models/admin/notice.go index 2d71b426dde7e..daf095f680f43 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/modules/git/commit_info_test.go b/modules/git/commit_info_test.go index 8467bdaa9034d..a4c0483004df6 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/repo.go b/modules/git/repo.go index 3950bb4a92aa5..3a005866c1e42 100644 --- a/modules/git/repo.go +++ b/modules/git/repo.go @@ -63,13 +63,13 @@ func IsRepoURLAccessible(url string) bool { } // 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") } @@ -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) diff --git a/modules/git/repo_branch.go b/modules/git/repo_branch.go index 98b1bc8ae7c78..fd02018b84f3e 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/indexer/code/bleve.go b/modules/indexer/code/bleve.go index 97d5fb082cdcd..d739618d278c4 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" @@ -271,11 +272,10 @@ func (b *BleveIndexer) Close() { } // Index indexes the data -func (b *BleveIndexer) Index(repo *models.Repository, sha string, changes *repoChanges) error { +func (b *BleveIndexer) Index(ctx context.Context, repo *models.Repository, sha string, changes *repoChanges) error { batch := gitea_bleve.NewFlushingBatch(b.indexer, maxBatchSize) if len(changes.Updates) > 0 { - - batchWriter, batchReader, cancel := git.CatFileBatch(git.DefaultContext, repo.RepoPath()) + batchWriter, batchReader, cancel := git.CatFileBatch(ctx, repo.RepoPath()) defer cancel() for _, update := range changes.Updates { diff --git a/modules/indexer/code/elastic_search.go b/modules/indexer/code/elastic_search.go index 6e0813dc15803..509098c2e5a66 100644 --- a/modules/indexer/code/elastic_search.go +++ b/modules/indexer/code/elastic_search.go @@ -244,11 +244,11 @@ func (b *ElasticSearchIndexer) addDelete(filename string, repo *models.Repositor } // Index will save the index data -func (b *ElasticSearchIndexer) Index(repo *models.Repository, sha string, changes *repoChanges) error { +func (b *ElasticSearchIndexer) Index(ctx context.Context, repo *models.Repository, sha string, changes *repoChanges) error { reqs := make([]elastic.BulkableRequest, 0) if len(changes.Updates) > 0 { - batchWriter, batchReader, cancel := git.CatFileBatch(git.DefaultContext, repo.RepoPath()) + batchWriter, batchReader, cancel := git.CatFileBatch(ctx, repo.RepoPath()) defer cancel() for _, update := range changes.Updates { diff --git a/modules/indexer/code/indexer.go b/modules/indexer/code/indexer.go index c56b1b2bb0c5d..99019b5efa443 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 *models.Repository, sha string, changes *repoChanges) error + Index(ctx context.Context, repo *models.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 := models.GetRepositoryByID(repoID) if models.IsErrRepoNotExist(err) { return indexer.Delete(repoID) @@ -102,7 +102,7 @@ func index(indexer Indexer, repoID int64) error { 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 b8fedcb3239e6..d1a3300d98de3 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" "github.com/stretchr/testify/assert" ) @@ -20,7 +21,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 5b19f9c625737..948cb9f9e97b0 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 *models.Repository, sha string, changes *repoChanges) error { +func (w *wrappedIndexer) Index(ctx context.Context, repo *models.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/repository/generate.go b/modules/repository/generate.go index 61a8b0d111590..a792d10d47a13 100644 --- a/modules/repository/generate.go +++ b/modules/repository/generate.go @@ -98,7 +98,7 @@ func checkGiteaTemplate(tmpDir string) (*models.GiteaTemplate, error) { return gt, nil } -func generateRepoCommit(repo, templateRepo, generateRepo *models.Repository, tmpDir string) error { +func generateRepoCommit(ctx context.Context, repo, templateRepo, generateRepo *models.Repository, tmpDir string) error { commitTimeStr := time.Now().Format(time.RFC3339) authorSig := repo.Owner.NewGitSig() @@ -114,7 +114,7 @@ func generateRepoCommit(repo, templateRepo, generateRepo *models.Repository, tmp // 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 { @@ -171,19 +171,19 @@ func generateRepoCommit(repo, templateRepo, generateRepo *models.Repository, tmp } } - 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 *models.Repository) (err error) { @@ -198,7 +198,7 @@ func generateGitContent(ctx context.Context, repo, templateRepo, generateRepo *m } }() - 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) } @@ -208,7 +208,7 @@ func generateGitContent(ctx context.Context, repo, templateRepo, generateRepo *m } 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) } @@ -272,7 +272,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 d7f31fabde387..b6a66cb3bedf5 100644 --- a/modules/repository/init.go +++ b/modules/repository/init.go @@ -101,7 +101,7 @@ func prepareRepoCommit(ctx context.Context, repo *models.Repository, tmpDir, rep } // initRepoCommit temporarily changes with work directory. -func initRepoCommit(tmpPath string, repo *models.Repository, u *user_model.User, defaultBranch string) (err error) { +func initRepoCommit(ctx context.Context, tmpPath string, repo *models.Repository, u *user_model.User, defaultBranch string) (err error) { commitTimeStr := time.Now().Format(time.RFC3339) sig := u.NewGitSig() @@ -115,7 +115,7 @@ func initRepoCommit(tmpPath string, repo *models.Repository, u *user_model.User, 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) @@ -152,7 +152,7 @@ func initRepoCommit(tmpPath string, repo *models.Repository, u *user_model.User, "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) @@ -163,7 +163,7 @@ func initRepoCommit(tmpPath string, repo *models.Repository, u *user_model.User, 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) @@ -173,7 +173,7 @@ func initRepoCommit(tmpPath string, repo *models.Repository, u *user_model.User, 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 := models.RepoPath(owner, name) isExist, err := util.IsExist(repoPath) @@ -189,7 +189,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) @@ -199,7 +199,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 *models.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 } @@ -220,7 +220,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) } } diff --git a/modules/repository/repo.go b/modules/repository/repo.go index c7145658f80ff..349635d81275f 100644 --- a/modules/repository/repo.go +++ b/modules/repository/repo.go @@ -70,7 +70,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, @@ -86,7 +86,7 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User, 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, diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index e511152e57454..14384ca7e6964 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -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) { @@ -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 := repo_service.GetBranches(ctx, ctx.Repo.Repository, skip, listOptions.PageSize) if err != nil { ctx.Error(http.StatusInternalServerError, "GetBranches", err) return diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index dd415a34af3f9..d4c9640251e03 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -409,7 +409,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 @@ -596,7 +596,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 @@ -836,7 +836,7 @@ func MergePullRequest(ctx *context.APIContext) { message += "\n\n" + form.MergeMessageField } - if err := pull_service.Merge(pr, ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil { + if err := pull_service.Merge(ctx, pr, ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), 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", models.MergeStyle(form.Do))) return @@ -1119,7 +1119,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 diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index 2f37e10984a0b..80a2d3d0b37d7 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -1020,7 +1020,7 @@ func Delete(ctx *context.APIContext) { ctx.Repo.GitRepo.Close() } - if err := repo_service.DeleteRepository(ctx.User, repo); err != nil { + if err := repo_service.DeleteRepository(ctx, ctx.User, repo); err != nil { ctx.Error(http.StatusInternalServerError, "DeleteRepository", err) return } diff --git a/routers/api/v1/repo/wiki.go b/routers/api/v1/repo/wiki.go index f8969067be273..97fc3286a6d09 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 diff --git a/routers/private/serv.go b/routers/private/serv.go index 9cbd0c2c82e67..6363c3fe91fd3 100644 --- a/routers/private/serv.go +++ b/routers/private/serv.go @@ -390,7 +390,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 69e522ef7e9b2..c82ee2f2cea01 100644 --- a/routers/web/admin/repos.go +++ b/routers/web/admin/repos.go @@ -51,7 +51,7 @@ func DeleteRepo(ctx *context.Context) { ctx.Repo.GitRepo.Close() } - if err := repo_service.DeleteRepository(ctx.User, repo); err != nil { + if err := repo_service.DeleteRepository(ctx, ctx.User, repo); err != nil { ctx.ServerError("DeleteRepository", err) return } diff --git a/routers/web/repo/branch.go b/routers/web/repo/branch.go index 05b45eba4b204..fa9991ce64e75 100644 --- a/routers/web/repo/branch.go +++ b/routers/web/repo/branch.go @@ -172,7 +172,7 @@ func loadBranches(ctx *context.Context, skip, limit int) ([]*Branch, int) { return nil, 0 } - rawBranches, totalNumOfBranches, err := repo_service.GetBranches(ctx.Repo.Repository, skip, limit) + rawBranches, totalNumOfBranches, err := repo_service.GetBranches(ctx, ctx.Repo.Repository, skip, limit) if err != nil { log.Error("GetBranches: %v", err) ctx.ServerError("GetBranches", err) @@ -351,9 +351,9 @@ func CreateBranch(ctx *context.Context) { } err = release_service.CreateNewTag(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/http.go b/routers/web/repo/http.go index 3aa8e84f57fa1..0ff643d4762b5 100644 --- a/routers/web/repo/http.go +++ b/routers/web/repo/http.go @@ -322,7 +322,7 @@ 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 } @@ -484,7 +484,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() diff --git a/routers/web/repo/lfs.go b/routers/web/repo/lfs.go index b1bc06f08b467..7717499b79129 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 { diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index 7593e7fbc8854..795548378acf4 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -767,7 +767,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.HTMLString(string(tplAlertDetails), map[string]interface{}{ @@ -932,7 +932,7 @@ func MergePullRequest(ctx *context.Context) { return } - if err = pull_service.Merge(pr, ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil { + if err = pull_service.Merge(ctx, pr, ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil { if models.IsErrInvalidMergeStyle(err) { ctx.Flash.Error(ctx.Tr("repo.pulls.invalid_merge_option")) ctx.Redirect(issue.Link()) @@ -1132,7 +1132,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 @@ -1379,7 +1379,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/setting.go b/routers/web/repo/setting.go index 4fc1e91c25292..70bb845d868cc 100644 --- a/routers/web/repo/setting.go +++ b/routers/web/repo/setting.go @@ -666,7 +666,7 @@ func SettingsPost(ctx *context.Context) { ctx.Repo.GitRepo.Close() } - if err := repo_service.DeleteRepository(ctx.User, ctx.Repo.Repository); err != nil { + if err := repo_service.DeleteRepository(ctx, ctx.User, ctx.Repo.Repository); err != nil { ctx.ServerError("DeleteRepository", err) return } @@ -685,7 +685,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 44914a3e3d304..b506566b38912 100644 --- a/routers/web/repo/wiki.go +++ b/routers/web/repo/wiki.go @@ -657,7 +657,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) @@ -711,7 +711,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 } @@ -726,7 +726,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/services/agit/agit.go b/services/agit/agit.go index beb21940b0cd1..11b92e20b0954 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 diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index 166660b87e183..f0bb75643b4f2 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -1389,7 +1389,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 { diff --git a/services/migrations/dump.go b/services/migrations/dump.go index afff49a05973a..e1181c85ebbf8 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, @@ -167,7 +167,7 @@ func (g *RepositoryDumper) CreateRepo(repo *base.Repository, opts base.MigrateOp 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 } diff --git a/services/mirror/mirror_pull.go b/services/mirror/mirror_pull.go index 7a2bc125c5883..b443cdec1414b 100644 --- a/services/mirror/mirror_pull.go +++ b/services/mirror/mirror_pull.go @@ -336,7 +336,7 @@ func runSync(ctx context.Context, m *models.Mirror) ([]*mirrorSyncResult, bool) } 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 diff --git a/services/pull/merge.go b/services/pull/merge.go index 75c089eee879e..ecc224330ab46 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" @@ -31,7 +32,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 models.MergeStyle, message string) (err error) { +func Merge(ctx context.Context, pr *models.PullRequest, doer *user_model.User, baseGitRepo *git.Repository, mergeStyle models.MergeStyle, message string) (err error) { if err = pr.LoadHeadRepo(); err != nil { log.Error("LoadHeadRepo: %v", err) return fmt.Errorf("LoadHeadRepo: %v", err) @@ -56,7 +57,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, message) + pr.MergedCommitID, err = rawMerge(ctx, pr, doer, mergeStyle, message) if err != nil { return err } @@ -111,7 +112,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 models.MergeStyle, message string) (string, error) { +func rawMerge(ctx context.Context, pr *models.PullRequest, doer *user_model.User, mergeStyle models.MergeStyle, message string) (string, error) { err := git.LoadGitVersion() if err != nil { log.Error("git.LoadGitVersion: %v", err) @@ -119,7 +120,7 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle models.M } // Clone base repo. - tmpBasePath, err := createTemporaryRepo(pr) + tmpBasePath, err := createTemporaryRepo(ctx, pr) if err != nil { log.Error("CreateTemporaryPath: %v", err) return "", err @@ -137,7 +138,7 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle models.M 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) @@ -158,11 +159,11 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle models.M 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") } } @@ -203,7 +204,7 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle models.M 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()) } @@ -242,13 +243,13 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle models.M // Merge commits. switch mergeStyle { case models.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 } @@ -258,7 +259,7 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle models.M fallthrough case models.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()) } @@ -266,7 +267,7 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle models.M 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 @@ -314,14 +315,14 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle models.M } // 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 == models.MergeStyleRebase { cmd.AddArguments("--ff-only") } else { @@ -335,14 +336,14 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle models.M return "", err } if mergeStyle == models.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 models.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 @@ -354,7 +355,7 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle models.M } 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()) } @@ -363,7 +364,7 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle models.M // 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()) } @@ -421,9 +422,9 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle models.M var pushCmd *git.Command if mergeStyle == models.MergeStyleRebaseUpdate { // force push the rebase result to head brach - 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. @@ -451,15 +452,15 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle models.M 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()) } @@ -498,11 +499,11 @@ func runMergeCommand(pr *models.PullRequest, mergeStyle models.MergeStyle, cmd * 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 diff --git a/services/pull/patch.go b/services/pull/patch.go index b971eb4bf30d1..a1e33b72f1e6a 100644 --- a/services/pull/patch.go +++ b/services/pull/patch.go @@ -16,7 +16,9 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/unit" "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/util" "github.com/gobwas/glob" @@ -50,8 +52,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: %s#%d", pr.BaseRepo.FullName(), 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 @@ -62,14 +67,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") diff --git a/services/pull/pull.go b/services/pull/pull.go index 8bfe20c80e69f..b3020dba2e9f1 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -26,12 +26,12 @@ import ( ) // NewPullRequest creates new pull request with labels for repository. -func NewPullRequest(repo *models.Repository, pull *models.Issue, labelIDs []int64, uuids []string, pr *models.PullRequest, assigneeIDs []int64) error { +func NewPullRequest(ctx context.Context, repo *models.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 } @@ -52,7 +52,7 @@ func NewPullRequest(repo *models.Repository, pull *models.Issue, labelIDs []int6 pull.PullRequest = pr if pr.Flow == models.PullRequestFlowGithub { - err = PushToBaseRepo(pr) + err = PushToBaseRepo(ctx, pr) } else { err = UpdateRef(pr) } @@ -114,7 +114,7 @@ func NewPullRequest(repo *models.Repository, pull *models.Issue, labelIDs []int6 } // 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 @@ -183,7 +183,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 } @@ -269,7 +269,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 { @@ -289,7 +289,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 } @@ -311,7 +311,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) @@ -403,11 +403,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 { @@ -431,7 +431,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, @@ -451,8 +451,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) @@ -524,8 +524,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 *models.Repository) error { - branches, _, err := git.GetBranchesByPath(repo.RepoPath(), 0, 0) +func CloseRepoBranchesPulls(ctx context.Context, doer *user_model.User, repo *models.Repository) error { + branches, _, err := git.GetBranchesByPath(ctx, repo.RepoPath(), 0, 0) if err != nil { return err } diff --git a/services/pull/temp_repo.go b/services/pull/temp_repo.go index e30dba7add60f..873944803ceea 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" @@ -18,7 +19,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) @@ -53,7 +54,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) @@ -90,7 +91,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) @@ -100,7 +101,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) @@ -110,7 +111,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) @@ -128,7 +129,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) @@ -148,11 +149,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 ef6e84598e5dc..0d047fff70e43 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" @@ -14,7 +15,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 models.MergeStyle @@ -47,14 +48,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 { @@ -112,7 +113,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 @@ -121,7 +122,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) diff --git a/services/repository/branch.go b/services/repository/branch.go index f33bac762181c..6fc5260f5f6db 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -5,6 +5,7 @@ package repository import ( + "context" "errors" "fmt" @@ -18,19 +19,19 @@ import ( ) // CreateNewBranch creates a new repository branch -func CreateNewBranch(doer *user_model.User, repo *models.Repository, oldBranchName, branchName string) (err error) { +func CreateNewBranch(ctx context.Context, doer *user_model.User, repo *models.Repository, oldBranchName, branchName string) (err error) { // Check if branch name can be used - if err := checkBranchName(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), @@ -60,32 +61,34 @@ func GetBranch(repo *models.Repository, branch string) (*git.Branch, error) { // 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 *models.Repository, skip, limit int) ([]*git.Branch, int, error) { - return git.GetBranchesByPath(repo.RepoPath(), skip, limit) +func GetBranches(ctx context.Context, repo *models.Repository, skip, limit int) ([]*git.Branch, int, error) { + return git.GetBranchesByPath(ctx, repo.RepoPath(), skip, limit) } // checkBranchName validates branch name with existing repository branches -func checkBranchName(repo *models.Repository, name string) error { - gitRepo, err := git.OpenRepository(repo.RepoPath()) +// FIXME: There really has to be faster mechanism than this. git cat-file --batch-check will check a branchname +// and or tag name... (git.DefaultContext - as a marker to come back to this) +func checkBranchName(ctx context.Context, repo *models.Repository, name string) error { + gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath()) if err != nil { return err } defer gitRepo.Close() - branches, _, err := GetBranches(repo, 0, 0) + branches, _, err := gitRepo.GetBranches(0, 0) if err != nil { return err } for _, branch := range branches { - if branch.Name == name { + if branch == name { return models.ErrBranchAlreadyExists{ - BranchName: branch.Name, + BranchName: branch, } - } else if (len(branch.Name) < len(name) && branch.Name+"/" == name[0:len(branch.Name)+1]) || - (len(branch.Name) > len(name) && name+"/" == branch.Name[0:len(name)+1]) { + } else if (len(branch) < len(name) && branch+"/" == name[0:len(branch)+1]) || + (len(branch) > len(name) && name+"/" == branch[0:len(name)+1]) { return models.ErrBranchNameConflict{ - BranchName: branch.Name, + BranchName: branch, } } } @@ -100,9 +103,9 @@ func checkBranchName(repo *models.Repository, name string) error { } // CreateNewBranchFromCommit creates a new repository branch -func CreateNewBranchFromCommit(doer *user_model.User, repo *models.Repository, commit, branchName string) (err error) { +func CreateNewBranchFromCommit(ctx context.Context, doer *user_model.User, repo *models.Repository, commit, branchName string) (err error) { // Check if branch name can be used - if err := checkBranchName(repo, branchName); err != nil { + if err := checkBranchName(ctx, repo, branchName); err != nil { return err } diff --git a/services/repository/check.go b/services/repository/check.go index ab7539e555549..a2b4022fa0517 100644 --- a/services/repository/check.go +++ b/services/repository/check.go @@ -195,7 +195,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/repository.go b/services/repository/repository.go index 4929f9f446f88..e65ab980b71fb 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" @@ -30,8 +31,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 *models.Repository) error { - if err := pull_service.CloseRepoBranchesPulls(doer, repo); err != nil { +func DeleteRepository(ctx context.Context, doer *user_model.User, repo *models.Repository) 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 cf25c33274b29..82d19725bf665 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" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" @@ -70,16 +70,16 @@ func FilenameToName(filename string) (string, error) { // InitWiki initializes a wiki for repository, // it does nothing when repository already has wiki. -func InitWiki(repo *models.Repository) error { +func InitWiki(ctx context.Context, repo *models.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 @@ -117,18 +117,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 *models.Repository, oldWikiName, newWikiName, content, message string, isNew bool) (err error) { +func updateWikiPage(ctx context.Context, doer *user_model.User, repo *models.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 { @@ -149,12 +149,12 @@ func updateWikiPage(doer *user_model.User, repo *models.Repository, oldWikiName, 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) @@ -265,22 +265,22 @@ func updateWikiPage(doer *user_model.User, repo *models.Repository, oldWikiName, } // AddWikiPage adds a new wiki page with a given wikiPath. -func AddWikiPage(doer *user_model.User, repo *models.Repository, wikiName, content, message string) error { - return updateWikiPage(doer, repo, "", wikiName, content, message, true) +func AddWikiPage(ctx context.Context, doer *user_model.User, repo *models.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 *models.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 *models.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 *models.Repository, wikiName string) (err error) { +func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *models.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) } @@ -294,7 +294,7 @@ func DeleteWikiPage(doer *user_model.User, repo *models.Repository, wikiName str } }() - 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", @@ -303,7 +303,7 @@ func DeleteWikiPage(doer *user_model.User, repo *models.Repository, wikiName str 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) @@ -372,11 +372,11 @@ func DeleteWikiPage(doer *user_model.User, repo *models.Repository, wikiName str } // DeleteWiki removes the actual and local copy of repository wiki. -func DeleteWiki(repo *models.Repository) error { +func DeleteWiki(ctx context.Context, repo *models.Repository) error { if err := models.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 5b42dd590f76b..8b5735cef5c42 100644 --- a/services/wiki/wiki_test.go +++ b/services/wiki/wiki_test.go @@ -114,11 +114,11 @@ func TestRepository_InitWiki(t *testing.T) { unittest.PrepareTestEnv(t) // repo1 already has a wiki repo1 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - assert.NoError(t, InitWiki(repo1)) + assert.NoError(t, InitWiki(git.DefaultContext, repo1)) // repo2 does not already have a wiki repo2 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) - assert.NoError(t, InitWiki(repo2)) + assert.NoError(t, InitWiki(git.DefaultContext, repo2)) assert.True(t, repo2.HasWiki()) } @@ -135,9 +135,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") @@ -152,7 +152,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)) }) @@ -160,7 +160,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)) }) @@ -179,10 +179,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) @@ -203,10 +203,10 @@ func TestRepository_DeleteWikiPage(t *testing.T) { unittest.PrepareTestEnv(t) repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.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") @@ -219,7 +219,7 @@ func TestRepository_DeleteWikiPage(t *testing.T) { func TestPrepareWikiFileName(t *testing.T) { unittest.PrepareTestEnv(t) repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - gitRepo, err := git.OpenRepository(repo.WikiPath()) + gitRepo, err := git.OpenRepositoryCtx(git.DefaultContext, repo.WikiPath()) defer gitRepo.Close() assert.NoError(t, err) @@ -279,7 +279,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) From f57eba053c2460a9ee7ead560b5f767efe3ad414 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Wed, 1 Dec 2021 20:10:43 +0000 Subject: [PATCH 02/48] Improve checkBranchName --- modules/git/repo_branch_gogit.go | 24 ++++++++++++++ modules/git/repo_branch_nogogit.go | 50 +++++++++++++++++++++--------- services/repository/branch.go | 43 +++++++++---------------- 3 files changed, 75 insertions(+), 42 deletions(-) diff --git a/modules/git/repo_branch_gogit.go b/modules/git/repo_branch_gogit.go index 6bf14b3999865..4b7d8421bb125 100644 --- a/modules/git/repo_branch_gogit.go +++ b/modules/git/repo_branch_gogit.go @@ -9,6 +9,7 @@ package git import ( + "context" "strings" "github.com/go-git/go-git/v5/plumbing" @@ -79,3 +80,26 @@ func (repo *Repository) GetBranches(skip, limit int) ([]string, int, error) { return branchNames, count, nil } + +// 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 + } + defer repo.Close() + + i := 0 + iter, err := repo.gogitRepo.References() + if err != nil { + return i, err + } + defer iter.Close() + + err = iter.ForEach(func(ref *plumbing.Reference) error { + err := walkfn(string(ref.Name())) + i++ + return err + }) + return i, err +} diff --git a/modules/git/repo_branch_nogogit.go b/modules/git/repo_branch_nogogit.go index 1928c7515bcab..1584a7b49d88f 100644 --- a/modules/git/repo_branch_nogogit.go +++ b/modules/git/repo_branch_nogogit.go @@ -67,8 +67,26 @@ func (repo *Repository) GetBranches(skip, limit int) ([]string, int, error) { return callShowRef(repo.Ctx, repo.Path, BranchPrefix, "--heads", skip, limit) } +// WalkReferences walks all the references from the repository +func WalkReferences(ctx context.Context, repoPath string, walkfn func(string) error) (int, error) { + return walkShowRef(ctx, repoPath, "", 0, 0, walkfn) +} + // callShowRef return refs, if limit = 0 it will not limit func callShowRef(ctx context.Context, repoPath, prefix, arg string, skip, limit int) (branchNames []string, countAll int, err error) { + countAll, err = walkShowRef(ctx, repoPath, arg, limit, skip, func(branchName string) error { + branchName = strings.TrimPrefix(branchName, prefix) + if len(branchName) > 0 { + branchName = branchName[:len(branchName)-1] + } + branchNames = append(branchNames, branchName) + + return nil + }) + return +} + +func walkShowRef(ctx context.Context, repoPath, arg string, skip, limit int, walkfn func(string) error) (countAll int, err error) { stdoutReader, stdoutWriter := io.Pipe() defer func() { _ = stdoutReader.Close() @@ -77,7 +95,11 @@ func callShowRef(ctx context.Context, repoPath, prefix, arg string, skip, limit go func() { stderrBuilder := &strings.Builder{} - err := NewCommandContext(ctx, "show-ref", arg).RunInDirPipeline(repoPath, stdoutWriter, stderrBuilder) + args := []string{"show-ref"} + if arg != "" { + args = append(args, arg) + } + err := NewCommandContext(ctx, args...).RunInDirPipeline(repoPath, stdoutWriter, stderrBuilder) if err != nil { if stderrBuilder.Len() == 0 { _ = stdoutWriter.Close() @@ -94,10 +116,10 @@ func callShowRef(ctx context.Context, repoPath, prefix, arg string, skip, limit for i < skip { _, isPrefix, err := bufReader.ReadLine() if err == io.EOF { - return branchNames, i, nil + return i, nil } if err != nil { - return nil, 0, err + return 0, err } if !isPrefix { i++ @@ -112,39 +134,39 @@ func callShowRef(ctx context.Context, repoPath, prefix, arg string, skip, limit _, err = bufReader.ReadSlice(' ') } if err == io.EOF { - return branchNames, i, nil + return i, nil } if err != nil { - return nil, 0, err + return 0, err } branchName, err := bufReader.ReadString('\n') if err == io.EOF { // This shouldn't happen... but we'll tolerate it for the sake of peace - return branchNames, i, nil + return i, nil } if err != nil { - return nil, i, err + return i, err } - branchName = strings.TrimPrefix(branchName, prefix) - if len(branchName) > 0 { - branchName = branchName[:len(branchName)-1] + + err = walkfn(branchName) + if err != nil { + return i, err } - branchNames = append(branchNames, branchName) i++ } // count all refs for limit != 0 { _, isPrefix, err := bufReader.ReadLine() if err == io.EOF { - return branchNames, i, nil + return i, nil } if err != nil { - return nil, 0, err + return 0, err } if !isPrefix { i++ } } - return branchNames, i, nil + return i, nil } diff --git a/services/repository/branch.go b/services/repository/branch.go index 6fc5260f5f6db..2231ebb4d157a 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -8,6 +8,7 @@ import ( "context" "errors" "fmt" + "strings" "code.gitea.io/gitea/models" user_model "code.gitea.io/gitea/models/user" @@ -66,40 +67,26 @@ func GetBranches(ctx context.Context, repo *models.Repository, skip, limit int) } // checkBranchName validates branch name with existing repository branches -// FIXME: There really has to be faster mechanism than this. git cat-file --batch-check will check a branchname -// and or tag name... (git.DefaultContext - as a marker to come back to this) func checkBranchName(ctx context.Context, repo *models.Repository, name string) error { - gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath()) - if err != nil { - return err - } - defer gitRepo.Close() - - branches, _, err := gitRepo.GetBranches(0, 0) - if err != nil { - return err - } - - for _, branch := range branches { - if branch == name { + _, err := git.WalkReferences(ctx, repo.RepoPath(), func(refName string) error { + switch { + case refName == git.BranchPrefix+name: return models.ErrBranchAlreadyExists{ - BranchName: branch, + BranchName: name, } - } else if (len(branch) < len(name) && branch+"/" == name[0:len(branch)+1]) || - (len(branch) > len(name) && name+"/" == branch[0:len(name)+1]) { + case strings.HasPrefix(refName, git.BranchPrefix+name+"/"): return models.ErrBranchNameConflict{ - BranchName: branch, + BranchName: name, + } + case refName == git.TagPrefix+name: + return models.ErrTagAlreadyExists{ + TagName: name, } } - } - - if _, err := gitRepo.GetTag(name); err == nil { - return models.ErrTagAlreadyExists{ - TagName: name, - } - } + return nil + }) - return nil + return err } // CreateNewBranchFromCommit creates a new repository branch @@ -109,7 +96,7 @@ func CreateNewBranchFromCommit(ctx context.Context, doer *user_model.User, repo 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), From 1d4cbbd3679e93b5f78a9f92c608d747c4f4cd01 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Wed, 1 Dec 2021 20:47:08 +0000 Subject: [PATCH 03/48] extend propagation to the temprepo Signed-off-by: Andrew Thornton --- integrations/api_repo_file_helpers.go | 3 ++- integrations/pull_update_test.go | 4 ++-- integrations/repofiles_delete_test.go | 17 +++++++------- integrations/repofiles_update_test.go | 20 ++++++++--------- modules/git/repo_compare.go | 9 ++++---- routers/api/v1/repo/file.go | 4 ++-- routers/web/repo/editor.go | 8 +++---- services/gitdiff/gitdiff.go | 4 ++-- services/repository/files/delete.go | 5 +++-- services/repository/files/diff.go | 5 +++-- services/repository/files/diff_test.go | 10 ++++----- services/repository/files/temp_repo.go | 31 +++++++++++++------------- services/repository/files/update.go | 5 +++-- services/repository/files/upload.go | 5 +++-- 14 files changed, 69 insertions(+), 61 deletions(-) diff --git a/integrations/api_repo_file_helpers.go b/integrations/api_repo_file_helpers.go index 8477d65a3ed4f..a2014d5ceb379 100644 --- a/integrations/api_repo_file_helpers.go +++ b/integrations/api_repo_file_helpers.go @@ -7,6 +7,7 @@ package integrations import ( "code.gitea.io/gitea/models" 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 *models.Repository, treePath 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 *models.Repository, treePath string) (*api.FileResponse, error) { diff --git a/integrations/pull_update_test.go b/integrations/pull_update_test.go index 9e129e7270074..c9100b174d35c 100644 --- a/integrations/pull_update_test.go +++ b/integrations/pull_update_test.go @@ -99,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", @@ -122,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", diff --git a/integrations/repofiles_delete_test.go b/integrations/repofiles_delete_test.go index 8490f8d3a7cde..fced608f4da24 100644 --- a/integrations/repofiles_delete_test.go +++ b/integrations/repofiles_delete_test.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/models" "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 fe0e2c21c6caf..ca34d4b80687d 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/modules/git/repo_compare.go b/modules/git/repo_compare.go index 4342eb4b2fefd..6e7c6487d0ffd 100644 --- a/modules/git/repo_compare.go +++ b/modules/git/repo_compare.go @@ -7,6 +7,7 @@ package git import ( "bytes" + "context" "fmt" "io" "regexp" @@ -159,15 +160,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: @@ -177,7 +178,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/routers/api/v1/repo/file.go b/routers/api/v1/repo/file.go index e84f652ed945c..af4de88992329 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -401,7 +401,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 fle in a repository @@ -488,7 +488,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 diff --git a/routers/web/repo/editor.go b/routers/web/repo/editor.go index d4fc55979c060..171f834a29a0a 100644 --- a/routers/web/repo/editor.go +++ b/routers/web/repo/editor.go @@ -244,7 +244,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, @@ -369,7 +369,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 @@ -450,7 +450,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, @@ -658,7 +658,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, diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index f0bb75643b4f2..ee68b2780dec7 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -1466,12 +1466,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/repository/files/delete.go b/services/repository/files/delete.go index f7b342db3f7a8..2217a46e1ed0c 100644 --- a/services/repository/files/delete.go +++ b/services/repository/files/delete.go @@ -5,6 +5,7 @@ package files import ( + "context" "fmt" "strings" @@ -30,7 +31,7 @@ type DeleteRepoFileOptions struct { } // DeleteRepoFile deletes a file in the given repository -func DeleteRepoFile(repo *models.Repository, doer *user_model.User, opts *DeleteRepoFileOptions) (*api.FileResponse, error) { +func DeleteRepoFile(ctx context.Context, repo *models.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 @@ -73,7 +74,7 @@ func DeleteRepoFile(repo *models.Repository, doer *user_model.User, opts *Delete author, committer := GetAuthorAndCommitterUsers(opts.Author, opts.Committer, doer) - t, err := NewTemporaryUploadRepository(repo) + t, err := NewTemporaryUploadRepository(ctx, repo) if err != nil { return nil, err } diff --git a/services/repository/files/diff.go b/services/repository/files/diff.go index fadaf2023102c..0ccf45bbbd7b7 100644 --- a/services/repository/files/diff.go +++ b/services/repository/files/diff.go @@ -5,6 +5,7 @@ package files import ( + "context" "strings" "code.gitea.io/gitea/models" @@ -12,11 +13,11 @@ import ( ) // GetDiffPreview produces and returns diff result of a file which is not yet committed. -func GetDiffPreview(repo *models.Repository, branch, treePath, content string) (*gitdiff.Diff, error) { +func GetDiffPreview(ctx context.Context, repo *models.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 be8e92033d671..7bdb98efcdb6a 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(&models.Repository{}, branch, treePath, content) + diff, err := GetDiffPreview(ctx, &models.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/temp_repo.go b/services/repository/files/temp_repo.go index 55dcd7436cb35..9c86d3ff63b54 100644 --- a/services/repository/files/temp_repo.go +++ b/services/repository/files/temp_repo.go @@ -24,18 +24,19 @@ import ( // TemporaryUploadRepository is a type to wrap our upload repositories as a shallow clone type TemporaryUploadRepository struct { + ctx context.Context repo *models.Repository gitRepo *git.Repository basePath string } // NewTemporaryUploadRepository creates a new temporary upload repository -func NewTemporaryUploadRepository(repo *models.Repository) (*TemporaryUploadRepository, error) { +func NewTemporaryUploadRepository(ctx context.Context, repo *models.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 } @@ -49,7 +50,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{ @@ -66,7 +67,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 } @@ -76,7 +77,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 @@ -94,7 +95,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 @@ -121,7 +122,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()) } @@ -133,7 +134,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()) } @@ -143,7 +144,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{ @@ -159,7 +160,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) @@ -177,7 +178,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) @@ -251,7 +252,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", @@ -264,7 +265,7 @@ func (t *TemporaryUploadRepository) CommitTreeWithDate(author, committer *user_m func (t *TemporaryUploadRepository) Push(doer *user_model.User, commitHash string, 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, @@ -300,7 +301,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, "") @@ -321,7 +322,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/update.go b/services/repository/files/update.go index eeb98f363026c..d7cfdcf0c22b2 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" @@ -123,7 +124,7 @@ func detectEncodingAndBOM(entry *git.TreeEntry, repo *models.Repository) (string } // CreateOrUpdateRepoFile adds or updates a file in the given repository -func CreateOrUpdateRepoFile(repo *models.Repository, doer *user_model.User, opts *UpdateRepoFileOptions) (*structs.FileResponse, error) { +func CreateOrUpdateRepoFile(ctx context.Context, repo *models.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 @@ -178,7 +179,7 @@ func CreateOrUpdateRepoFile(repo *models.Repository, doer *user_model.User, opts author, committer := GetAuthorAndCommitterUsers(opts.Author, opts.Committer, doer) - t, err := NewTemporaryUploadRepository(repo) + t, err := NewTemporaryUploadRepository(ctx, repo) if err != nil { log.Error("%v", err) } diff --git a/services/repository/files/upload.go b/services/repository/files/upload.go index 7a0ea31085e5b..28323df0d6a2a 100644 --- a/services/repository/files/upload.go +++ b/services/repository/files/upload.go @@ -5,6 +5,7 @@ package files import ( + "context" "fmt" "os" "path" @@ -48,7 +49,7 @@ func cleanUpAfterFailure(infos *[]uploadInfo, t *TemporaryUploadRepository, orig } // UploadRepoFiles uploads files to the given repository -func UploadRepoFiles(repo *models.Repository, doer *user_model.User, opts *UploadRepoFileOptions) error { +func UploadRepoFiles(ctx context.Context, repo *models.Repository, doer *user_model.User, opts *UploadRepoFileOptions) error { if len(opts.Files) == 0 { return nil } @@ -79,7 +80,7 @@ func UploadRepoFiles(repo *models.Repository, doer *user_model.User, opts *Uploa infos[i] = uploadInfo{upload: upload} } - t, err := NewTemporaryUploadRepository(repo) + t, err := NewTemporaryUploadRepository(ctx, repo) if err != nil { return err } From a7276d99cff75700a05c271db8d0fdc3427074e4 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Wed, 1 Dec 2021 21:21:52 +0000 Subject: [PATCH 04/48] more propagation Signed-off-by: Andrew Thornton --- modules/context/api.go | 4 +-- routers/api/v1/repo/blob.go | 2 +- routers/api/v1/repo/file.go | 2 +- routers/api/v1/repo/status.go | 2 +- routers/api/v1/repo/tree.go | 2 +- routers/api/v1/repo/wiki.go | 2 +- services/migrations/gitea_uploader.go | 2 +- services/repository/adopt.go | 2 +- services/repository/files/commit.go | 5 ++-- services/repository/files/content.go | 19 ++++++------ services/repository/files/content_test.go | 36 +++++++++++------------ services/repository/files/delete.go | 2 +- services/repository/files/file.go | 7 +++-- services/repository/files/file_test.go | 4 +-- services/repository/files/tree.go | 5 ++-- services/repository/files/tree_test.go | 2 +- services/repository/files/update.go | 2 +- 17 files changed, 52 insertions(+), 48 deletions(-) diff --git a/modules/context/api.go b/modules/context/api.go index 4a2d5d1665d3e..69a71e3f8a948 100644 --- a/modules/context/api.go +++ b/modules/context/api.go @@ -328,7 +328,7 @@ func ReferencesGitRepo(allowEmpty bool) func(http.Handler) http.Handler { // For API calls. if ctx.Repo.GitRepo == nil { repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name) - gitRepo, err := git.OpenRepository(repoPath) + gitRepo, err := git.OpenRepositoryCtx(ctx, repoPath) if err != nil { ctx.Error(http.StatusInternalServerError, "RepoRef Invalid repo "+repoPath, err) return @@ -386,7 +386,7 @@ func RepoRefForAPI(next http.Handler) http.Handler { if ctx.Repo.GitRepo == nil { repoPath := models.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/routers/api/v1/repo/blob.go b/routers/api/v1/repo/blob.go index e3ce0e1916885..19d893a68b92e 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/file.go b/routers/api/v1/repo/file.go index af4de88992329..8385336599dbb 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -554,7 +554,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/status.go b/routers/api/v1/repo/status.go index 583b89dd7ab77..8cad5a7ad6bb4 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/tree.go b/routers/api/v1/repo/tree.go index 2168a311d01a5..5e1721090d821 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, sha, ctx.FormInt("page"), ctx.FormInt("per_page"), ctx.FormBool("recursive")); err != nil { ctx.Error(http.StatusBadRequest, "", err.Error()) } else { ctx.JSON(http.StatusOK, tree) diff --git a/routers/api/v1/repo/wiki.go b/routers/api/v1/repo/wiki.go index 97fc3286a6d09..5bcf0d804f5a4 100644 --- a/routers/api/v1/repo/wiki.go +++ b/routers/api/v1/repo/wiki.go @@ -456,7 +456,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/services/migrations/gitea_uploader.go b/services/migrations/gitea_uploader.go index 4808916c1d878..805b26408c0a4 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 } diff --git a/services/repository/adopt.go b/services/repository/adopt.go index 3f4045a778310..28a58c237222f 100644 --- a/services/repository/adopt.go +++ b/services/repository/adopt.go @@ -120,7 +120,7 @@ func adoptRepository(ctx context.Context, repoPath string, u *user_model.User, r } repo.IsEmpty = false - 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/services/repository/files/commit.go b/services/repository/files/commit.go index 8f14ed8926300..2a42358bfd673 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" @@ -16,11 +17,11 @@ 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 *models.Repository, creator *user_model.User, sha string, status *models.CommitStatus) error { +func CreateCommitStatus(ctx context.Context, repo *models.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, err := git.OpenRepositoryCtx(ctx, repoPath) if err != nil { return fmt.Errorf("OpenRepository[%s]: %v", repoPath, err) } diff --git a/services/repository/files/content.go b/services/repository/files/content.go index afb775fed3aba..38af2009fb42a 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" @@ -38,7 +39,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 *models.Repository, treePath, ref string) (interface{}, error) { +func GetContentsOrList(ctx context.Context, repo *models.Repository, treePath, ref string) (interface{}, error) { if repo.IsEmpty { return make([]interface{}, 0), nil } @@ -56,7 +57,7 @@ func GetContentsOrList(repo *models.Repository, treePath, ref string) (interface } treePath = cleanTreePath - gitRepo, err := git.OpenRepository(repo.RepoPath()) + gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath()) if err != nil { return nil, err } @@ -74,7 +75,7 @@ func GetContentsOrList(repo *models.Repository, treePath, ref string) (interface } 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 @@ -90,7 +91,7 @@ func GetContentsOrList(repo *models.Repository, treePath, ref string) (interface } 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 } @@ -100,7 +101,7 @@ func GetContentsOrList(repo *models.Repository, treePath, ref string) (interface } // GetContents gets the meta data on a file's contents. Ref can be a branch, commit or tag -func GetContents(repo *models.Repository, treePath, ref string, forList bool) (*api.ContentsResponse, error) { +func GetContents(ctx context.Context, repo *models.Repository, treePath, ref string, forList bool) (*api.ContentsResponse, error) { if ref == "" { ref = repo.DefaultBranch } @@ -115,7 +116,7 @@ func GetContents(repo *models.Repository, treePath, ref string, forList bool) (* } treePath = cleanTreePath - gitRepo, err := git.OpenRepository(repo.RepoPath()) + gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath()) if err != nil { return nil, err } @@ -162,7 +163,7 @@ func GetContents(repo *models.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 @@ -218,8 +219,8 @@ func GetContents(repo *models.Repository, treePath, ref string, forList bool) (* } // GetBlobBySHA get the GitBlobResponse of a repository using a sha hash. -func GetBlobBySHA(repo *models.Repository, sha string) (*api.GitBlobResponse, error) { - gitRepo, err := git.OpenRepository(repo.RepoPath()) +func GetBlobBySHA(ctx context.Context, repo *models.Repository, sha string) (*api.GitBlobResponse, error) { + gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath()) if err != nil { return nil, err } diff --git a/services/repository/files/content_test.go b/services/repository/files/content_test.go index 68b29b1daaec7..c21aa31d32d0f 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 2217a46e1ed0c..188a539a4e881 100644 --- a/services/repository/files/delete.go +++ b/services/repository/files/delete.go @@ -191,7 +191,7 @@ func DeleteRepoFile(ctx context.Context, repo *models.Repository, doer *user_mod 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/file.go b/services/repository/files/file.go index 8de60c4cb8544..c0374196b070c 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 *models.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 *models.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 25303a6d004c0..24e4949832731 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/tree.go b/services/repository/files/tree.go index ede206274e2dc..d41d456e1ab1f 100644 --- a/services/repository/files/tree.go +++ b/services/repository/files/tree.go @@ -5,6 +5,7 @@ package files import ( + "context" "fmt" "net/url" @@ -15,8 +16,8 @@ import ( ) // GetTreeBySHA get the GitTreeResponse of a repository using a sha hash. -func GetTreeBySHA(repo *models.Repository, sha string, page, perPage int, recursive bool) (*api.GitTreeResponse, error) { - gitRepo, err := git.OpenRepository(repo.RepoPath()) +func GetTreeBySHA(ctx context.Context, repo *models.Repository, sha string, page, perPage int, recursive bool) (*api.GitTreeResponse, error) { + gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath()) if err != nil { return nil, err } diff --git a/services/repository/files/tree_test.go b/services/repository/files/tree_test.go index 37a6025d6c2cd..339214e95c7d2 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.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 d7cfdcf0c22b2..2df88fc7c9ff5 100644 --- a/services/repository/files/update.go +++ b/services/repository/files/update.go @@ -433,7 +433,7 @@ func CreateOrUpdateRepoFile(ctx context.Context, repo *models.Repository, doer * 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 } From fa0902c9949b293aacbbac08e3410d652e1d4fbd Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Fri, 3 Dec 2021 17:54:03 +0000 Subject: [PATCH 05/48] oops Signed-off-by: Andrew Thornton --- modules/git/repo_branch_nogogit.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/git/repo_branch_nogogit.go b/modules/git/repo_branch_nogogit.go index 1584a7b49d88f..73d687fa711c6 100644 --- a/modules/git/repo_branch_nogogit.go +++ b/modules/git/repo_branch_nogogit.go @@ -74,7 +74,7 @@ func WalkReferences(ctx context.Context, repoPath string, walkfn func(string) er // callShowRef return refs, if limit = 0 it will not limit func callShowRef(ctx context.Context, repoPath, prefix, arg string, skip, limit int) (branchNames []string, countAll int, err error) { - countAll, err = walkShowRef(ctx, repoPath, arg, limit, skip, func(branchName string) error { + countAll, err = walkShowRef(ctx, repoPath, arg, skip, limit, func(branchName string) error { branchName = strings.TrimPrefix(branchName, prefix) if len(branchName) > 0 { branchName = branchName[:len(branchName)-1] From ef8bee1b4536b1e0ea6c352416dd915a438ad900 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Fri, 3 Dec 2021 23:17:41 +0000 Subject: [PATCH 06/48] fix tests Signed-off-by: Andrew Thornton --- services/repository/branch.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/repository/branch.go b/services/repository/branch.go index 2231ebb4d157a..23ba25a09f7ab 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -70,7 +70,7 @@ func GetBranches(ctx context.Context, repo *models.Repository, skip, limit int) func checkBranchName(ctx context.Context, repo *models.Repository, name string) error { _, err := git.WalkReferences(ctx, repo.RepoPath(), func(refName string) error { switch { - case refName == git.BranchPrefix+name: + case refName == name || refName == git.BranchPrefix+name: return models.ErrBranchAlreadyExists{ BranchName: name, } From 271a6bf258db55af609581ec046352853d6845ba Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Fri, 3 Dec 2021 23:30:42 +0000 Subject: [PATCH 07/48] Propagate context in set_default_branch --- routers/private/default_branch.go | 29 ++++------------------------- routers/private/internal_repo.go | 2 +- 2 files changed, 5 insertions(+), 26 deletions(-) diff --git a/routers/private/default_branch.go b/routers/private/default_branch.go index ec6adc4805020..e09b43bfe7860 100644 --- a/routers/private/default_branch.go +++ b/routers/private/default_branch.go @@ -9,10 +9,8 @@ import ( "fmt" "net/http" - "code.gitea.io/gitea/models" 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 +32,19 @@ func SetDefaultBranch(ctx *gitea_context.PrivateContext) { ownerName := ctx.Params(":owner") repoName := ctx.Params(":repo") branch := ctx.Params(":branch") - repo, err := models.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.Repo.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.UpdateDefaultBranch(); err != nil { + if err := ctx.Repo.Repository.UpdateDefaultBranch(); 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/internal_repo.go b/routers/private/internal_repo.go index 60daa1dbeaae2..df4a50724547a 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{}{ From da9edc986e48be592ad5d2f02872740384dbbcf6 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Fri, 3 Dec 2021 23:31:21 +0000 Subject: [PATCH 08/48] Create parent context in push_updates --- services/repository/push.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/services/repository/push.go b/services/repository/push.go index 7b439a41f4865..30c6dc62b3f5d 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -19,6 +19,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" @@ -76,13 +77,16 @@ 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 := models.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) } From 4a711e84fd78825074102c48b905f266793985b5 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Fri, 3 Dec 2021 23:36:25 +0000 Subject: [PATCH 09/48] more api propagation Signed-off-by: Andrew Thornton --- routers/api/v1/repo/repo.go | 2 +- routers/api/v1/utils/git.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index 80a2d3d0b37d7..f3e620b567a26 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -703,7 +703,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 diff --git a/routers/api/v1/utils/git.go b/routers/api/v1/utils/git.go index ac68c3625d924..a941c1fc2c0f6 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 } From e4a37c0709bfc984e872e38189bb4d0568bb8495 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Fri, 3 Dec 2021 23:49:40 +0000 Subject: [PATCH 10/48] Use the common ctx.Repo.Gitrepo instead of reopening it for loadBranches --- modules/context/repo.go | 4 ++-- modules/git/repo_branch.go | 9 +++++++-- modules/git/repo_branch_nogogit.go | 4 ++-- modules/git/repo_branch_test.go | 8 ++++---- routers/web/repo/branch.go | 4 ++-- routers/web/repo/compare.go | 4 ++-- routers/web/repo/issue.go | 2 +- services/repository/adopt.go | 2 +- 8 files changed, 21 insertions(+), 16 deletions(-) diff --git a/modules/context/repo.go b/modules/context/repo.go index 159fd07d9d18b..b2844c04c4c15 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -584,7 +584,7 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) { } ctx.Data["Tags"] = tags - brs, _, err := ctx.Repo.GitRepo.GetBranches(0, 0) + brs, _, err := ctx.Repo.GitRepo.GetBranchNames(0, 0) if err != nil { ctx.ServerError("GetBranches", err) return @@ -810,7 +810,7 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context if len(ctx.Params("*")) == 0 { refName = ctx.Repo.Repository.DefaultBranch if !ctx.Repo.GitRepo.IsBranchExist(refName) { - brs, _, err := ctx.Repo.GitRepo.GetBranches(0, 0) + brs, _, err := ctx.Repo.GitRepo.GetBranchNames(0, 0) if err != nil { ctx.ServerError("GetBranches", err) return diff --git a/modules/git/repo_branch.go b/modules/git/repo_branch.go index fd02018b84f3e..0e9c24fe210bb 100644 --- a/modules/git/repo_branch.go +++ b/modules/git/repo_branch.go @@ -95,7 +95,12 @@ func GetBranchesByPath(ctx context.Context, path string, skip, limit int) ([]*Br } defer gitRepo.Close() - brs, countAll, err := gitRepo.GetBranches(skip, limit) + return gitRepo.GetBranches(skip, limit) +} + +// GetBranches returns a slice of *git.Branch +func (gitRepo *Repository) GetBranches(skip, limit int) ([]*Branch, int, error) { + brs, countAll, err := gitRepo.GetBranchNames(skip, limit) if err != nil { return nil, 0, err } @@ -103,7 +108,7 @@ func GetBranchesByPath(ctx context.Context, path string, skip, limit int) ([]*Br branches := make([]*Branch, len(brs)) for i := range brs { branches[i] = &Branch{ - Path: path, + Path: gitRepo.Path, Name: brs[i], gitRepo: gitRepo, } diff --git a/modules/git/repo_branch_nogogit.go b/modules/git/repo_branch_nogogit.go index 73d687fa711c6..08050005c78bb 100644 --- a/modules/git/repo_branch_nogogit.go +++ b/modules/git/repo_branch_nogogit.go @@ -61,9 +61,9 @@ func (repo *Repository) IsBranchExist(name string) bool { return repo.IsReferenceExist(BranchPrefix + name) } -// GetBranches returns branches from the repository, skipping skip initial branches and +// GetBranchNames returns branches from the repository, skipping skip initial branches and // returning at most limit branches, or all branches if limit is 0. -func (repo *Repository) GetBranches(skip, limit int) ([]string, int, error) { +func (repo *Repository) GetBranchNames(skip, limit int) ([]string, int, error) { return callShowRef(repo.Ctx, repo.Path, BranchPrefix, "--heads", skip, limit) } diff --git a/modules/git/repo_branch_test.go b/modules/git/repo_branch_test.go index 05d5237e6a65b..ac5f5deea9bea 100644 --- a/modules/git/repo_branch_test.go +++ b/modules/git/repo_branch_test.go @@ -17,21 +17,21 @@ func TestRepository_GetBranches(t *testing.T) { assert.NoError(t, err) defer bareRepo1.Close() - branches, countAll, err := bareRepo1.GetBranches(0, 2) + branches, countAll, err := bareRepo1.GetBranchNames(0, 2) assert.NoError(t, err) assert.Len(t, branches, 2) assert.EqualValues(t, 3, countAll) assert.ElementsMatch(t, []string{"branch1", "branch2"}, branches) - branches, countAll, err = bareRepo1.GetBranches(0, 0) + branches, countAll, err = bareRepo1.GetBranchNames(0, 0) assert.NoError(t, err) assert.Len(t, branches, 3) assert.EqualValues(t, 3, countAll) assert.ElementsMatch(t, []string{"branch1", "branch2", "master"}, branches) - branches, countAll, err = bareRepo1.GetBranches(5, 1) + branches, countAll, err = bareRepo1.GetBranchNames(5, 1) assert.NoError(t, err) assert.Len(t, branches, 0) @@ -48,7 +48,7 @@ func BenchmarkRepository_GetBranches(b *testing.B) { defer bareRepo1.Close() for i := 0; i < b.N; i++ { - _, _, err := bareRepo1.GetBranches(0, 0) + _, _, err := bareRepo1.GetBranchNames(0, 0) if err != nil { b.Fatal(err) } diff --git a/routers/web/repo/branch.go b/routers/web/repo/branch.go index fa9991ce64e75..f04f77712c1bc 100644 --- a/routers/web/repo/branch.go +++ b/routers/web/repo/branch.go @@ -165,14 +165,14 @@ func redirect(ctx *context.Context) { // loadBranches loads branches from the repository limited by page & pageSize. // NOTE: May write to context on error. func loadBranches(ctx *context.Context, skip, limit int) ([]*Branch, int) { - defaultBranch, err := repo_service.GetBranch(ctx.Repo.Repository, ctx.Repo.Repository.DefaultBranch) + defaultBranch, err := ctx.Repo.GitRepo.GetBranch(ctx.Repo.Repository.DefaultBranch) if err != nil { log.Error("loadBranches: get default branch: %v", err) ctx.ServerError("GetDefaultBranch", err) return nil, 0 } - rawBranches, totalNumOfBranches, err := repo_service.GetBranches(ctx, ctx.Repo.Repository, skip, limit) + rawBranches, totalNumOfBranches, err := ctx.Repo.GitRepo.GetBranches(skip, limit) if err != nil { log.Error("GetBranches: %v", err) ctx.ServerError("GetBranches", err) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 865a734caf2fb..842c0b06debaa 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -654,7 +654,7 @@ func getBranchesAndTagsForRepo(repo *models.Repository) (branches, tags []string } defer gitRepo.Close() - branches, _, err = gitRepo.GetBranches(0, 0) + branches, _, err = gitRepo.GetBranchNames(0, 0) if err != nil { return nil, nil, err } @@ -705,7 +705,7 @@ func CompareDiff(ctx *context.Context) { return } - headBranches, _, err := ci.HeadGitRepo.GetBranches(0, 0) + headBranches, _, err := ci.HeadGitRepo.GetBranchNames(0, 0) if err != nil { ctx.ServerError("GetBranches", err) return diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index f0857b18c0eb6..398aa26cc49dc 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -690,7 +690,7 @@ func RetrieveRepoMetas(ctx *context.Context, repo *models.Repository, isPull boo return nil } - brs, _, err := ctx.Repo.GitRepo.GetBranches(0, 0) + brs, _, err := ctx.Repo.GitRepo.GetBranchNames(0, 0) if err != nil { ctx.ServerError("GetBranches", err) return nil diff --git a/services/repository/adopt.go b/services/repository/adopt.go index 28a58c237222f..f195a510e11b0 100644 --- a/services/repository/adopt.go +++ b/services/repository/adopt.go @@ -142,7 +142,7 @@ func adoptRepository(ctx context.Context, repoPath string, u *user_model.User, r repo.DefaultBranch = strings.TrimPrefix(repo.DefaultBranch, git.BranchPrefix) } - branches, _, _ := gitRepo.GetBranches(0, 0) + branches, _, _ := gitRepo.GetBranchNames(0, 0) found := false hasDefault := false hasMaster := false From aa419cb079f9b27ce83a6244836d64170db15815 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sat, 4 Dec 2021 00:18:01 +0000 Subject: [PATCH 11/48] fix tests again Signed-off-by: Andrew Thornton --- modules/git/repo_branch_nogogit.go | 3 +++ services/repository/branch.go | 13 ++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/modules/git/repo_branch_nogogit.go b/modules/git/repo_branch_nogogit.go index 08050005c78bb..23c4a07f0a14b 100644 --- a/modules/git/repo_branch_nogogit.go +++ b/modules/git/repo_branch_nogogit.go @@ -149,6 +149,9 @@ func walkShowRef(ctx context.Context, repoPath, arg string, skip, limit int, wal return i, err } + if len(branchName) > 0 { + branchName = branchName[:len(branchName)-1] + } err = walkfn(branchName) if err != nil { return i, err diff --git a/services/repository/branch.go b/services/repository/branch.go index 23ba25a09f7ab..798b12b1990a1 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -69,14 +69,21 @@ func GetBranches(ctx context.Context, repo *models.Repository, skip, limit int) // checkBranchName validates branch name with existing repository branches func checkBranchName(ctx context.Context, repo *models.Repository, name string) error { _, err := git.WalkReferences(ctx, repo.RepoPath(), func(refName string) error { + branchRefName := strings.TrimPrefix(refName, git.BranchPrefix) switch { - case refName == name || refName == git.BranchPrefix+name: + case branchRefName == name: return models.ErrBranchAlreadyExists{ BranchName: name, } - case strings.HasPrefix(refName, git.BranchPrefix+name+"/"): + // If branchRefName like a/b but we want to create a branch named a then we have a conflict + case strings.HasPrefix(branchRefName, name+"/"): return models.ErrBranchNameConflict{ - BranchName: name, + BranchName: branchRefName, + } + // Conversely if branchRefName like a but we want to create a branch named a/b then we also have a conflict + case strings.HasPrefix(name, branchRefName+"/"): + return models.ErrBranchNameConflict{ + BranchName: branchRefName, } case refName == git.TagPrefix+name: return models.ErrTagAlreadyExists{ From ee3516486b4426ae431b4f2f7b588ccddba9ba9e Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sat, 4 Dec 2021 00:24:55 +0000 Subject: [PATCH 12/48] another fixup Signed-off-by: Andrew Thornton --- modules/git/repo_branch_nogogit.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/modules/git/repo_branch_nogogit.go b/modules/git/repo_branch_nogogit.go index 23c4a07f0a14b..55952acda4a3d 100644 --- a/modules/git/repo_branch_nogogit.go +++ b/modules/git/repo_branch_nogogit.go @@ -76,9 +76,6 @@ func WalkReferences(ctx context.Context, repoPath string, walkfn func(string) er func callShowRef(ctx context.Context, repoPath, prefix, arg string, skip, limit int) (branchNames []string, countAll int, err error) { countAll, err = walkShowRef(ctx, repoPath, arg, skip, limit, func(branchName string) error { branchName = strings.TrimPrefix(branchName, prefix) - if len(branchName) > 0 { - branchName = branchName[:len(branchName)-1] - } branchNames = append(branchNames, branchName) return nil From 62157c48c0a76e8307da0f3090c3563c3c1c8ea6 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sat, 4 Dec 2021 00:35:45 +0000 Subject: [PATCH 13/48] use the ctx git repo instead of reopening it --- routers/api/v1/repo/tree.go | 2 +- services/repository/files/tree.go | 7 +------ services/repository/files/tree_test.go | 2 +- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/routers/api/v1/repo/tree.go b/routers/api/v1/repo/tree.go index 5e1721090d821..1886a5f72b3bd 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, 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.JSON(http.StatusOK, tree) diff --git a/services/repository/files/tree.go b/services/repository/files/tree.go index d41d456e1ab1f..d53c178ca1b4c 100644 --- a/services/repository/files/tree.go +++ b/services/repository/files/tree.go @@ -16,12 +16,7 @@ import ( ) // GetTreeBySHA get the GitTreeResponse of a repository using a sha hash. -func GetTreeBySHA(ctx context.Context, repo *models.Repository, sha string, page, perPage int, recursive bool) (*api.GitTreeResponse, error) { - gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath()) - if err != nil { - return nil, err - } - defer gitRepo.Close() +func GetTreeBySHA(ctx context.Context, repo *models.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 339214e95c7d2..e900480d354dd 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, 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", From 6a5dd0094380a8844a02511cdd18294df50869d9 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sat, 4 Dec 2021 00:40:10 +0000 Subject: [PATCH 14/48] avoid using repo_service.GetBranch if we already have an open git repo --- routers/api/v1/repo/branch.go | 4 ++-- routers/web/repo/editor.go | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index 14384ca7e6964..3ecc22db6de5c 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) @@ -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 diff --git a/routers/web/repo/editor.go b/routers/web/repo/editor.go index 171f834a29a0a..0966a9ee72b0a 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" ) @@ -614,7 +613,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 @@ -806,7 +805,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 } From 2e821501e88cd604657e97714c184f9e77c0c6f1 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sat, 4 Dec 2021 00:41:24 +0000 Subject: [PATCH 15/48] placate lint Signed-off-by: Andrew Thornton --- modules/git/repo_branch.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/git/repo_branch.go b/modules/git/repo_branch.go index 0e9c24fe210bb..0e5c8e9b8baf2 100644 --- a/modules/git/repo_branch.go +++ b/modules/git/repo_branch.go @@ -99,8 +99,8 @@ func GetBranchesByPath(ctx context.Context, path string, skip, limit int) ([]*Br } // GetBranches returns a slice of *git.Branch -func (gitRepo *Repository) GetBranches(skip, limit int) ([]*Branch, int, error) { - brs, countAll, err := gitRepo.GetBranchNames(skip, limit) +func (repo *Repository) GetBranches(skip, limit int) ([]*Branch, int, error) { + brs, countAll, err := repo.GetBranchNames(skip, limit) if err != nil { return nil, 0, err } @@ -108,9 +108,9 @@ func (gitRepo *Repository) GetBranches(skip, limit int) ([]*Branch, int, error) branches := make([]*Branch, len(brs)) for i := range brs { branches[i] = &Branch{ - Path: gitRepo.Path, + Path: repo.Path, Name: brs[i], - gitRepo: gitRepo, + gitRepo: repo, } } From 525e495334fa5f3729d1ddd892bc157c5170622f Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sat, 4 Dec 2021 00:45:38 +0000 Subject: [PATCH 16/48] placate lint2 Signed-off-by: Andrew Thornton --- modules/git/repo_branch_gogit.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/git/repo_branch_gogit.go b/modules/git/repo_branch_gogit.go index 4b7d8421bb125..d159aafd6f3bb 100644 --- a/modules/git/repo_branch_gogit.go +++ b/modules/git/repo_branch_gogit.go @@ -53,7 +53,7 @@ func (repo *Repository) IsBranchExist(name string) bool { // GetBranches returns branches from the repository, skipping skip initial branches and // returning at most limit branches, or all branches if limit is 0. -func (repo *Repository) GetBranches(skip, limit int) ([]string, int, error) { +func (repo *Repository) GetBranchNames(skip, limit int) ([]string, int, error) { var branchNames []string branches, err := repo.gogitRepo.Branches() From aa4997d4e1ffbe59a7ba05c3353a2b49a997ffde Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sat, 4 Dec 2021 01:10:01 +0000 Subject: [PATCH 17/48] need to set the gitrepo here Signed-off-by: Andrew Thornton --- routers/api/v1/repo/branch.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index 3ecc22db6de5c..98eb9b363f95f 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -53,6 +53,23 @@ func GetBranch(ctx *context.APIContext) { branchName := ctx.Params("*") + if ctx.Repo.GitRepo == nil { + repoPath := models.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 + } + 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() + } + }() + } + branch, err := ctx.Repo.GitRepo.GetBranch(branchName) if err != nil { if git.IsErrBranchNotExist(err) { From c3c1eb1fdd6d9d57cb89f046b4a4c64ff456c1e0 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sat, 4 Dec 2021 01:21:26 +0000 Subject: [PATCH 18/48] more use of ctx.Repo.GitRepo instead of reopening it Signed-off-by: Andrew Thornton --- routers/api/v1/api.go | 4 ++-- routers/api/v1/repo/branch.go | 19 +------------------ 2 files changed, 3 insertions(+), 20 deletions(-) diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 867803f8dd5f9..c5e42021e388b 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -775,8 +775,8 @@ 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.Get("", context.ReferencesGitRepo(false), repo.ListBranches) + m.Get("/*", context.ReferencesGitRepo(false), repo.GetBranch) m.Delete("/*", context.ReferencesGitRepo(false), reqRepoWriter(unit.TypeCode), repo.DeleteBranch) m.Post("", reqRepoWriter(unit.TypeCode), bind(api.CreateBranchRepoOption{}), repo.CreateBranch) }, reqRepoReader(unit.TypeCode)) diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index 98eb9b363f95f..e06a550027852 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -53,23 +53,6 @@ func GetBranch(ctx *context.APIContext) { branchName := ctx.Params("*") - if ctx.Repo.GitRepo == nil { - repoPath := models.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 - } - 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() - } - }() - } - branch, err := ctx.Repo.GitRepo.GetBranch(branchName) if err != nil { if git.IsErrBranchNotExist(err) { @@ -274,7 +257,7 @@ func ListBranches(ctx *context.APIContext) { listOptions := utils.GetListOptions(ctx) skip, _ := listOptions.GetStartEnd() - branches, totalNumOfBranches, err := repo_service.GetBranches(ctx, 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 From f20362120205aacb20efa3b7679f98ff01acfa39 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sat, 4 Dec 2021 11:39:35 +0000 Subject: [PATCH 19/48] Stop reopening git repo with repo_service.GetBranch Signed-off-by: Andrew Thornton --- services/repository/branch.go | 14 -------------- services/repository/files/delete.go | 11 ++++++++--- services/repository/files/update.go | 11 ++++++++--- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/services/repository/branch.go b/services/repository/branch.go index 798b12b1990a1..21dac41a628bd 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -46,20 +46,6 @@ func CreateNewBranch(ctx context.Context, doer *user_model.User, repo *models.Re return nil } -// GetBranch returns a branch by its name -func GetBranch(repo *models.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(ctx context.Context, repo *models.Repository, skip, limit int) ([]*git.Branch, int, error) { diff --git a/services/repository/files/delete.go b/services/repository/files/delete.go index 188a539a4e881..09871c197da10 100644 --- a/services/repository/files/delete.go +++ b/services/repository/files/delete.go @@ -13,7 +13,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 @@ -40,8 +39,14 @@ func DeleteRepoFile(ctx context.Context, repo *models.Repository, doer *user_mod opts.NewBranch = opts.OldBranch } + gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath()) + if err != nil { + return nil, err + } + defer gitRepo.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 +54,7 @@ func DeleteRepoFile(ctx context.Context, repo *models.Repository, doer *user_mod // 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 } diff --git a/services/repository/files/update.go b/services/repository/files/update.go index 2df88fc7c9ff5..716015af02296 100644 --- a/services/repository/files/update.go +++ b/services/repository/files/update.go @@ -21,7 +21,6 @@ import ( "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" - repo_service "code.gitea.io/gitea/services/repository" stdcharset "golang.org/x/net/html/charset" "golang.org/x/text/transform" @@ -133,8 +132,14 @@ func CreateOrUpdateRepoFile(ctx context.Context, repo *models.Repository, doer * opts.NewBranch = opts.OldBranch } + gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath()) + if err != nil { + return nil, err + } + defer gitRepo.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 } @@ -142,7 +147,7 @@ func CreateOrUpdateRepoFile(ctx context.Context, repo *models.Repository, doer * // 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, From 40caafcb349ea5255e219eba39fe2b85ace2fbe2 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sun, 5 Dec 2021 13:29:49 +0000 Subject: [PATCH 20/48] fix tests Signed-off-by: Andrew Thornton --- modules/context/api.go | 45 ++++++++++++++++++++---------------------- modules/web/route.go | 10 ++++++++++ routers/api/v1/api.go | 4 ++-- 3 files changed, 33 insertions(+), 26 deletions(-) diff --git a/modules/context/api.go b/modules/context/api.go index 69a71e3f8a948..c162a604dca8f 100644 --- a/modules/context/api.go +++ b/modules/context/api.go @@ -316,35 +316,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 := models.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 := models.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 + 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 } } diff --git a/modules/web/route.go b/modules/web/route.go index ad9cea8bb6e14..0d6670c690b8a 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 c5e42021e388b..db93dd6db676f 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -777,8 +777,8 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route { m.Group("/branches", func() { m.Get("", context.ReferencesGitRepo(false), repo.ListBranches) m.Get("/*", context.ReferencesGitRepo(false), repo.GetBranch) - m.Delete("/*", context.ReferencesGitRepo(false), reqRepoWriter(unit.TypeCode), repo.DeleteBranch) - m.Post("", reqRepoWriter(unit.TypeCode), bind(api.CreateBranchRepoOption{}), repo.CreateBranch) + 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) From 360eb9654fe6f733d6177c82524e595a29661dcb Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sun, 5 Dec 2021 14:56:05 +0000 Subject: [PATCH 21/48] make it possible to get the repository from the context instead of having to pass it in Signed-off-by: Andrew Thornton --- integrations/repo_tag_test.go | 6 ++--- modules/context/context.go | 5 ++++ modules/git/blame.go | 12 ++++++---- modules/git/repo_base.go | 30 +++++++++++++++++++++++ modules/git/repo_branch_gogit.go | 12 ++++++---- routers/api/v1/repo/pull_review.go | 2 +- routers/api/v1/repo/tag.go | 2 +- routers/web/repo/branch.go | 2 +- routers/web/repo/pull_review.go | 2 +- routers/web/repo/release.go | 2 +- services/pull/review.go | 21 +++++++++------- services/release/release.go | 14 +++++++---- services/release/release_test.go | 2 +- services/repository/adopt.go | 3 +++ services/repository/files/commit.go | 12 +++++++--- services/repository/files/content.go | 36 ++++++++++++++++++---------- services/repository/files/delete.go | 12 ++++++---- services/repository/files/update.go | 12 ++++++---- services/repository/push.go | 5 ++-- 19 files changed, 137 insertions(+), 55 deletions(-) create mode 100644 modules/git/repo_base.go diff --git a/integrations/repo_tag_test.go b/integrations/repo_tag_test.go index 823e7e6589e47..fac942cd84545 100644 --- a/integrations/repo_tag_test.go +++ b/integrations/repo_tag_test.go @@ -28,7 +28,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{ @@ -43,11 +43,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/modules/context/context.go b/modules/context/context.go index 5a37f8b0bf2a9..c92e91e4aede8 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -24,6 +24,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" @@ -538,6 +539,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) } diff --git a/modules/git/blame.go b/modules/git/blame.go index b30124594dc25..208a600dc89d7 100644 --- a/modules/git/blame.go +++ b/modules/git/blame.go @@ -114,11 +114,15 @@ 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 := RepositoryFromContext(ctx, repoPath) + if gitRepo == nil { + var err error + gitRepo, err = OpenRepositoryCtx(ctx, repoPath) + if err != nil { + return nil, err + } + gitRepo.Close() } - gitRepo.Close() return createBlameReader(ctx, repoPath, GitExecutable, "blame", commitID, "--porcelain", "--", file) } diff --git a/modules/git/repo_base.go b/modules/git/repo_base.go new file mode 100644 index 0000000000000..5f2c49535444d --- /dev/null +++ b/modules/git/repo_base.go @@ -0,0 +1,30 @@ +// 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" + +// 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"} + +func RepositoryFromContext(ctx context.Context, path string) *Repository { + value := ctx.Value(RepositoryContextKey) + if value == nil { + return nil + } + + if repo, ok := value.(*Repository); ok { + if repo.Path == path { + return repo + } + } + + return nil +} diff --git a/modules/git/repo_branch_gogit.go b/modules/git/repo_branch_gogit.go index d159aafd6f3bb..57952bcc640ca 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/routers/api/v1/repo/pull_review.go b/routers/api/v1/repo/pull_review.go index ec5f0456476e0..319938618fabc 100644 --- a/routers/api/v1/repo/pull_review.go +++ b/routers/api/v1/repo/pull_review.go @@ -351,7 +351,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, diff --git a/routers/api/v1/repo/tag.go b/routers/api/v1/repo/tag.go index 8d42e63a489f0..39295f078fe39 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 diff --git a/routers/web/repo/branch.go b/routers/web/repo/branch.go index f04f77712c1bc..b8067b57bfc91 100644 --- a/routers/web/repo/branch.go +++ b/routers/web/repo/branch.go @@ -349,7 +349,7 @@ 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, ctx.User, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName) } else { diff --git a/routers/web/repo/pull_review.go b/routers/web/repo/pull_review.go index 257aa737f6527..e552328de0bf9 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, diff --git a/routers/web/repo/release.go b/routers/web/repo/release.go index 9bca4e6897840..3b8b324eed6a8 100644 --- a/routers/web/repo/release.go +++ b/routers/web/repo/release.go @@ -327,7 +327,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)) diff --git a/services/pull/review.go b/services/pull/review.go index 31c2e99782c40..ee959362670a9 100644 --- a/services/pull/review.go +++ b/services/pull/review.go @@ -6,6 +6,7 @@ package pull import ( + "context" "fmt" "io" "regexp" @@ -21,7 +22,7 @@ import ( ) // CreateCodeComment creates a comment on the code line -func CreateCodeComment(doer *user_model.User, gitRepo *git.Repository, issue *models.Issue, line int64, content string, 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 string, treePath string, isReview bool, replyReviewID int64, latestCommitID string) (*models.Comment, error) { var ( existsReview bool @@ -47,7 +48,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 +88,7 @@ func CreateCodeComment(doer *user_model.User, gitRepo *git.Repository, issue *mo } } - comment, err := createCodeComment( + comment, err := createCodeComment(ctx, doer, issue.Repo, issue, @@ -115,7 +116,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 *models.Repository, issue *models.Issue, content, treePath string, line, reviewID int64) (*models.Comment, error) { +func createCodeComment(ctx context.Context, doer *user_model.User, repo *models.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 +125,15 @@ func createCodeComment(doer *user_model.User, repo *models.Repository, issue *mo if err := pr.LoadBaseRepo(); err != nil { return nil, fmt.Errorf("LoadHeadRepo: %v", err) } - gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath()) - if err != nil { - return nil, fmt.Errorf("OpenRepository: %v", err) + gitRepo := git.RepositoryFromContext(ctx, pr.BaseRepo.RepoPath()) + if gitRepo == nil { + var err error + gitRepo, err = git.OpenRepositoryCtx(ctx, pr.BaseRepo.RepoPath()) + if err != nil { + return nil, fmt.Errorf("OpenRepository: %v", err) + } + defer gitRepo.Close() } - defer gitRepo.Close() invalidated := false head := pr.GetGitRefName() diff --git a/services/release/release.go b/services/release/release.go index ec1494d426061..9c5d1e05362d1 100644 --- a/services/release/release.go +++ b/services/release/release.go @@ -5,6 +5,7 @@ package release import ( + "context" "errors" "fmt" "strings" @@ -137,7 +138,7 @@ func CreateRelease(gitRepo *git.Repository, rel *models.Release, attachmentUUIDs } // CreateNewTag creates a new repository tag -func CreateNewTag(doer *user_model.User, repo *models.Repository, commit, tagName, msg string) error { +func CreateNewTag(ctx context.Context, doer *user_model.User, repo *models.Repository, commit, tagName, msg string) error { isExist, err := models.IsReleaseExist(repo.ID, tagName) if err != nil { return err @@ -147,11 +148,14 @@ func CreateNewTag(doer *user_model.User, repo *models.Repository, commit, tagNam } } - gitRepo, err := git.OpenRepository(repo.RepoPath()) - if err != nil { - return err + gitRepo := git.RepositoryFromContext(ctx, repo.RepoPath()) + if gitRepo == nil { + gitRepo, err = git.OpenRepositoryCtx(ctx, repo.RepoPath()) + if err != nil { + return err + } + defer gitRepo.Close() } - defer gitRepo.Close() rel := &models.Release{ RepoID: repo.ID, diff --git a/services/release/release_test.go b/services/release/release_test.go index 29ff78ca3a7df..fee945fac9e88 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, &models.Repository{ID: 1}).(*models.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 f195a510e11b0..0618ba7bf9b6a 100644 --- a/services/repository/adopt.go +++ b/services/repository/adopt.go @@ -120,11 +120,14 @@ func adoptRepository(ctx context.Context, repoPath string, u *user_model.User, r } repo.IsEmpty = false + + // 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/files/commit.go b/services/repository/files/commit.go index 2a42358bfd673..bd7771ae0ac71 100644 --- a/services/repository/files/commit.go +++ b/services/repository/files/commit.go @@ -21,10 +21,16 @@ func CreateCommitStatus(ctx context.Context, repo *models.Repository, creator *u repoPath := repo.RepoPath() // confirm that commit is exist - gitRepo, err := git.OpenRepositoryCtx(ctx, repoPath) - if err != nil { - return fmt.Errorf("OpenRepository[%s]: %v", repoPath, err) + gitRepo := git.RepositoryFromContext(ctx, repo.RepoPath()) + if gitRepo == nil { + var err error + gitRepo, err = git.OpenRepositoryCtx(ctx, repo.RepoPath()) + if err != nil { + return fmt.Errorf("OpenRepository[%s]: %v", repoPath, err) + } + defer gitRepo.Close() } + if _, err := gitRepo.GetCommit(sha); err != nil { gitRepo.Close() return fmt.Errorf("GetCommit[%s]: %v", sha, err) diff --git a/services/repository/files/content.go b/services/repository/files/content.go index 38af2009fb42a..55c7d99be6d41 100644 --- a/services/repository/files/content.go +++ b/services/repository/files/content.go @@ -57,11 +57,15 @@ func GetContentsOrList(ctx context.Context, repo *models.Repository, treePath, r } treePath = cleanTreePath - gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath()) - if err != nil { - return nil, err + gitRepo := git.RepositoryFromContext(ctx, repo.RepoPath()) + if gitRepo == nil { + var err error + gitRepo, err = git.OpenRepositoryCtx(ctx, repo.RepoPath()) + if err != nil { + return nil, err + } + defer gitRepo.Close() } - defer gitRepo.Close() // Get the commit object for the ref commit, err := gitRepo.GetCommit(ref) @@ -116,11 +120,15 @@ func GetContents(ctx context.Context, repo *models.Repository, treePath, ref str } treePath = cleanTreePath - gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath()) - if err != nil { - return nil, err + gitRepo := git.RepositoryFromContext(ctx, repo.RepoPath()) + if gitRepo == nil { + var err error + gitRepo, err = git.OpenRepositoryCtx(ctx, repo.RepoPath()) + if err != nil { + return nil, err + } + defer gitRepo.Close() } - defer gitRepo.Close() // Get the commit object for the ref commit, err := gitRepo.GetCommit(ref) @@ -220,11 +228,15 @@ func GetContents(ctx context.Context, repo *models.Repository, treePath, ref str // GetBlobBySHA get the GitBlobResponse of a repository using a sha hash. func GetBlobBySHA(ctx context.Context, repo *models.Repository, sha string) (*api.GitBlobResponse, error) { - gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath()) - if err != nil { - return nil, err + gitRepo := git.RepositoryFromContext(ctx, repo.RepoPath()) + if gitRepo == nil { + var err error + gitRepo, err = git.OpenRepositoryCtx(ctx, repo.RepoPath()) + if err != nil { + return nil, err + } + defer gitRepo.Close() } - defer gitRepo.Close() gitBlob, err := gitRepo.GetBlob(sha) if err != nil { return nil, err diff --git a/services/repository/files/delete.go b/services/repository/files/delete.go index 09871c197da10..cd2d448190829 100644 --- a/services/repository/files/delete.go +++ b/services/repository/files/delete.go @@ -39,11 +39,15 @@ func DeleteRepoFile(ctx context.Context, repo *models.Repository, doer *user_mod opts.NewBranch = opts.OldBranch } - gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath()) - if err != nil { - return nil, err + gitRepo := git.RepositoryFromContext(ctx, repo.RepoPath()) + if gitRepo == nil { + var err error + gitRepo, err = git.OpenRepositoryCtx(ctx, repo.RepoPath()) + if err != nil { + return nil, err + } + defer gitRepo.Close() } - defer gitRepo.Close() // oldBranch must exist for this operation if _, err := gitRepo.GetBranch(opts.OldBranch); err != nil { diff --git a/services/repository/files/update.go b/services/repository/files/update.go index 716015af02296..1974bb1fc2c5a 100644 --- a/services/repository/files/update.go +++ b/services/repository/files/update.go @@ -132,11 +132,15 @@ func CreateOrUpdateRepoFile(ctx context.Context, repo *models.Repository, doer * opts.NewBranch = opts.OldBranch } - gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath()) - if err != nil { - return nil, err + gitRepo := git.RepositoryFromContext(ctx, repo.RepoPath()) + if gitRepo == nil { + var err error + gitRepo, err = git.OpenRepositoryCtx(ctx, repo.RepoPath()) + if err != nil { + return nil, err + } + defer gitRepo.Close() } - defer gitRepo.Close() // oldBranch must exist for this operation if _, err := gitRepo.GetBranch(opts.OldBranch); err != nil { diff --git a/services/repository/push.go b/services/repository/push.go index 30c6dc62b3f5d..94ac78740df68 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -86,9 +86,10 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { } repoPath := repo.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() @@ -102,7 +103,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 { From 3a3e6bf4cb184ffd149cabc8a2e534612ee5e3a5 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sun, 5 Dec 2021 15:37:47 +0000 Subject: [PATCH 22/48] more contexting Signed-off-by: Andrew Thornton --- modules/git/repo_base.go | 1 + routers/api/v1/repo/pull.go | 4 +- routers/private/hook_pre_receive.go | 2 +- routers/web/repo/pull.go | 4 +- services/mirror/mirror_pull.go | 2 +- services/pull/check.go | 58 +++++++++++++++++------------ services/pull/commit_status.go | 21 +++++++---- services/pull/merge.go | 4 +- services/pull/patch.go | 15 +++++--- 9 files changed, 68 insertions(+), 43 deletions(-) diff --git a/modules/git/repo_base.go b/modules/git/repo_base.go index 5f2c49535444d..f2e10011cebb0 100644 --- a/modules/git/repo_base.go +++ b/modules/git/repo_base.go @@ -14,6 +14,7 @@ type contextKey struct { // 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 { diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index d4c9640251e03..34b2a1b26f92b 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -233,7 +233,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 } @@ -790,7 +790,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 diff --git a/routers/private/hook_pre_receive.go b/routers/private/hook_pre_receive.go index 5ab21d525b975..44e2254de0311 100644 --- a/routers/private/hook_pre_receive.go +++ b/routers/private/hook_pre_receive.go @@ -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/web/repo/pull.go b/routers/web/repo/pull.go index 795548378acf4..ac15f4d0610e1 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -879,7 +879,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 @@ -1350,7 +1350,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 } diff --git a/services/mirror/mirror_pull.go b/services/mirror/mirror_pull.go index b443cdec1414b..67c5080fd3739 100644 --- a/services/mirror/mirror_pull.go +++ b/services/mirror/mirror_pull.go @@ -249,7 +249,7 @@ func runSync(ctx context.Context, m *models.Mirror) ([]*mirrorSyncResult, bool) } 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 diff --git a/services/pull/check.go b/services/pull/check.go index 03d4a39919f19..d13c73c576a54 100644 --- a/services/pull/check.go +++ b/services/pull/check.go @@ -19,6 +19,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" @@ -67,7 +68,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 = models.GetRepositoryByID(pr.BaseRepoID) @@ -89,7 +90,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 @@ -110,7 +111,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) @@ -119,7 +120,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) } @@ -135,7 +136,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 @@ -151,7 +152,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 @@ -217,26 +218,37 @@ 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) + testPR(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 +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) } - checkAndUpdateStatus(pr) + return } + checkAndUpdateStatus(pr) } // CheckPrsForBaseBranch check all pulls with bseBrannch diff --git a/services/pull/commit_status.go b/services/pull/commit_status.go index 2b834c25f1c8b..f695dcf25ad57 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,22 @@ 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()) - if err != nil { - return "", errors.Wrap(err, "OpenRepository") + headGitRepo := git.RepositoryFromContext(ctx, pr.HeadRepo.RepoPath()) + if headGitRepo == nil { + var err error + headGitRepo, err = git.OpenRepositoryCtx(ctx, pr.HeadRepo.RepoPath()) + if err != nil { + return "", errors.Wrap(err, "OpenRepository") + } + defer headGitRepo.Close() } - defer headGitRepo.Close() if pr.Flow == models.PullRequestFlowGithub && !headGitRepo.IsBranchExist(pr.HeadBranch) { return "", errors.New("Head branch does not exist, can not merge") @@ -117,6 +123,7 @@ func GetPullRequestCommitStatusState(pr *models.PullRequest) (structs.CommitStat } var sha string + var err error if pr.Flow == models.PullRequestFlowGithub { sha, err = headGitRepo.GetBranchCommitID(pr.HeadBranch) } else { diff --git a/services/pull/merge.go b/services/pull/merge.go index ecc224330ab46..ee87c88441efc 100644 --- a/services/pull/merge.go +++ b/services/pull/merge.go @@ -576,7 +576,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) } @@ -588,7 +588,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 a1e33b72f1e6a..a24c6e74e734c 100644 --- a/services/pull/patch.go +++ b/services/pull/patch.go @@ -25,17 +25,22 @@ 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()) - if err != nil { - return fmt.Errorf("OpenRepository: %v", err) + gitRepo := git.RepositoryFromContext(ctx, pr.BaseRepo.RepoPath()) + if gitRepo == nil { + var err error + gitRepo, err = git.OpenRepositoryCtx(ctx, pr.BaseRepo.RepoPath()) + if err != nil { + return fmt.Errorf("OpenRepository: %v", err) + } + defer gitRepo.Close() } - defer gitRepo.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) From acc8de2e44b4394405ff40565cb8eae2efe9a457 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sun, 5 Dec 2021 15:37:55 +0000 Subject: [PATCH 23/48] Add context to archiver Signed-off-by: Andrew Thornton --- services/archiver/archiver.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/services/archiver/archiver.go b/services/archiver/archiver.go index 06686220f11a5..3c7f16defa33b 100644 --- a/services/archiver/archiver.go +++ b/services/archiver/archiver.go @@ -18,6 +18,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" @@ -114,11 +115,13 @@ func (aReq *ArchiveRequest) GetArchiveName() string { } func doArchive(r *ArchiveRequest) (*models.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 := models.GetRepoArchiver(ctx, r.RepoID, r.Type, r.CommitID) if err != nil { @@ -174,7 +177,7 @@ func doArchive(r *ArchiveRequest) (*models.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 } @@ -189,13 +192,13 @@ func doArchive(r *ArchiveRequest) (*models.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, From ae5b6f67f1907cabb0709a5f759b994064ee09dc Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sun, 5 Dec 2021 16:00:25 +0000 Subject: [PATCH 24/48] More propagation Signed-off-by: Andrew Thornton --- modules/git/diff.go | 18 ++++++++----- modules/repofiles/commit_status.go | 42 ------------------------------ modules/repository/init.go | 2 +- modules/repository/repo.go | 2 +- modules/test/context_tests.go | 4 +-- routers/api/v1/api.go | 4 +-- routers/api/v1/repo/commits.go | 23 +++++----------- routers/api/v1/repo/file.go | 2 +- routers/api/v1/repo/notes.go | 2 +- routers/api/v1/repo/pull.go | 18 ++++++++----- routers/api/v1/repo/pull_review.go | 13 +++++---- routers/web/repo/branch.go | 2 +- routers/web/repo/commit.go | 3 ++- routers/web/repo/lfs.go | 2 +- routers/web/repo/wiki.go | 2 +- 15 files changed, 49 insertions(+), 90 deletions(-) delete mode 100644 modules/repofiles/commit_status.go diff --git a/modules/git/diff.go b/modules/git/diff.go index 3a82cda1ce037..f912992e77572 100644 --- a/modules/git/diff.go +++ b/modules/git/diff.go @@ -30,17 +30,21 @@ 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) - if err != nil { - return fmt.Errorf("OpenRepository: %v", err) +func GetRawDiffForFile(ctx context.Context, repoPath, startCommit, endCommit string, diffType RawDiffType, file string, writer io.Writer) error { + repo := RepositoryFromContext(ctx, repoPath) + if repo == nil { + var err error + repo, err = OpenRepositoryCtx(ctx, repoPath) + if err != nil { + return fmt.Errorf("OpenRepository: %v", err) + } + defer repo.Close() } - defer repo.Close() return GetRepoRawDiffForFile(repo, startCommit, endCommit, diffType, file, writer) } diff --git a/modules/repofiles/commit_status.go b/modules/repofiles/commit_status.go deleted file mode 100644 index 2074b4b464ca6..0000000000000 --- a/modules/repofiles/commit_status.go +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright 2019 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 repofiles - -import ( - "fmt" - - "code.gitea.io/gitea/models" - user_model "code.gitea.io/gitea/models/user" - "code.gitea.io/gitea/modules/git" -) - -// 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 *models.Repository, creator *user_model.User, sha string, status *models.CommitStatus) error { - repoPath := repo.RepoPath() - - // confirm that commit is exist - gitRepo, err := git.OpenRepository(repoPath) - if err != nil { - return fmt.Errorf("OpenRepository[%s]: %v", repoPath, err) - } - if _, err := gitRepo.GetCommit(sha); err != nil { - gitRepo.Close() - return fmt.Errorf("GetCommit[%s]: %v", sha, err) - } - gitRepo.Close() - - if err := models.NewCommitStatus(models.NewCommitStatusOptions{ - Repo: repo, - Creator: creator, - SHA: sha, - CommitStatus: status, - }); err != nil { - return fmt.Errorf("NewCommitStatus[repo_id: %d, user_id: %d, sha: %s]: %v", repo.ID, creator.ID, sha, err) - } - - return nil -} diff --git a/modules/repository/init.go b/modules/repository/init.go index b6a66cb3bedf5..bd729caf05c7e 100644 --- a/modules/repository/init.go +++ b/modules/repository/init.go @@ -239,7 +239,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/repo.go b/modules/repository/repo.go index 349635d81275f..920349998804a 100644 --- a/modules/repository/repo.go +++ b/modules/repository/repo.go @@ -115,7 +115,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) } diff --git a/modules/test/context_tests.go b/modules/test/context_tests.go index 548e454c8eafc..e05e6215d7cfd 100644 --- a/modules/test/context_tests.go +++ b/modules/test/context_tests.go @@ -65,7 +65,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() @@ -87,7 +87,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()) 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/routers/api/v1/api.go b/routers/api/v1/api.go index db93dd6db676f..bbb82a077df62 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -953,7 +953,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) @@ -961,7 +961,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/repo/commits.go b/routers/api/v1/repo/commits.go index 9907054b831d5..b68c5494c748e 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) @@ -132,13 +126,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 @@ -150,23 +137,24 @@ func GetAllCommits(ctx *context.APIContext) { sha := ctx.FormString("sha") + var err error 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 @@ -251,6 +239,7 @@ func DownloadCommitDiffOrPatch(ctx *context.APIContext) { // "$ref": "#/responses/notFound" repoPath := models.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 8385336599dbb..d2f047314459a 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -121,7 +121,7 @@ func GetArchive(ctx *context.APIContext) { repoPath := models.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 diff --git a/routers/api/v1/repo/notes.go b/routers/api/v1/repo/notes.go index a5f9512983f9e..b56f828d82820 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 diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 34b2a1b26f92b..a33c6cafbc1a7 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -872,7 +872,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 @@ -966,7 +966,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) headRepo = ctx.Repo.Repository headGitRepo = ctx.Repo.GitRepo } else { - headGitRepo, err = git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name)) + headGitRepo, err = git.OpenRepositoryCtx(ctx, models.RepoPath(headUser.Name, headRepo.Name)) if err != nil { ctx.Error(http.StatusInternalServerError, "OpenRepository", err) return nil, nil, nil, nil, "", "" @@ -1188,12 +1188,16 @@ func GetPullRequestCommits(ctx *context.APIContext) { } var prInfo *git.CompareInfo - baseGitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath()) - if err != nil { - ctx.ServerError("OpenRepository", err) - return + baseGitRepo := git.RepositoryFromContext(ctx, pr.BaseRepo.RepoPath()) + if baseGitRepo == nil { + baseGitRepo, err = git.OpenRepositoryCtx(ctx, pr.BaseRepo.RepoPath()) + if err != nil { + ctx.ServerError("OpenRepository", err) + return + } + defer baseGitRepo.Close() } - defer baseGitRepo.Close() + if pr.HasMerged { prInfo, err = baseGitRepo.GetCompareInfo(pr.BaseRepo.RepoPath(), pr.MergeBase, pr.GetGitRefName(), true, false) } else { diff --git a/routers/api/v1/repo/pull_review.go b/routers/api/v1/repo/pull_review.go index 319938618fabc..a52793c3ec7ae 100644 --- a/routers/api/v1/repo/pull_review.go +++ b/routers/api/v1/repo/pull_review.go @@ -328,12 +328,15 @@ func CreatePullReview(ctx *context.APIContext) { // if CommitID is empty, set it as lastCommitID if opts.CommitID == "" { - gitRepo, err := git.OpenRepository(pr.Issue.Repo.RepoPath()) - if err != nil { - ctx.Error(http.StatusInternalServerError, "git.OpenRepository", err) - return + gitRepo := git.RepositoryFromContext(ctx, pr.Issue.Repo.RepoPath()) + if gitRepo == nil { + gitRepo, err = git.OpenRepositoryCtx(ctx, pr.Issue.Repo.RepoPath()) + if err != nil { + ctx.Error(http.StatusInternalServerError, "git.OpenRepository", err) + return + } + defer gitRepo.Close() } - defer gitRepo.Close() headCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName()) if err != nil { diff --git a/routers/web/repo/branch.go b/routers/web/repo/branch.go index b8067b57bfc91..09be0c4765240 100644 --- a/routers/web/repo/branch.go +++ b/routers/web/repo/branch.go @@ -275,7 +275,7 @@ func loadOneBranch(ctx *context.Context, rawBranch *git.Branch, protectedBranche 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 diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index 993fc1c335f8a..077860fba6a85 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -264,7 +264,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 @@ -385,6 +385,7 @@ func RawDiff(ctx *context.Context) { repoPath = models.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/lfs.go b/routers/web/repo/lfs.go index 7717499b79129..cea31c94e3adf 100644 --- a/routers/web/repo/lfs.go +++ b/routers/web/repo/lfs.go @@ -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/wiki.go b/routers/web/repo/wiki.go index dd698d64ea41b..3a282b2d4545e 100644 --- a/routers/web/repo/wiki.go +++ b/routers/web/repo/wiki.go @@ -89,7 +89,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 From af4cbdcf55c890df93f37a1330463e915246cd34 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sun, 5 Dec 2021 16:29:29 +0000 Subject: [PATCH 25/48] Pass RefID into notification to avoid having to reopen the repository unnecessarily --- modules/notification/action/action.go | 4 ++-- modules/notification/base/notifier.go | 4 ++-- modules/notification/base/null.go | 4 ++-- modules/notification/notification.go | 8 ++++---- modules/notification/webhook/webhook.go | 24 +++++------------------- services/mirror/mirror_pull.go | 2 +- services/release/release.go | 2 +- services/repository/branch.go | 6 +++++- services/repository/push.go | 4 ++-- 9 files changed, 24 insertions(+), 34 deletions(-) diff --git a/modules/notification/action/action.go b/modules/notification/action/action.go index 80f97ad9932c9..5d3ca6b6c7a81 100644 --- a/modules/notification/action/action.go +++ b/modules/notification/action/action.go @@ -329,7 +329,7 @@ func (a *actionNotifier) NotifyPushCommits(pusher *user_model.User, repo *models } } -func (a *actionNotifier) NotifyCreateRef(doer *user_model.User, repo *models.Repository, refType, refFullName string) { +func (a *actionNotifier) NotifyCreateRef(doer *user_model.User, repo *models.Repository, refType, refFullName, shaSum string) { opType := models.ActionCommitRepo if refType == "tag" { // has sent same action in `NotifyPushCommits`, so skip it. @@ -388,7 +388,7 @@ func (a *actionNotifier) NotifySyncPushCommits(pusher *user_model.User, repo *mo } } -func (a *actionNotifier) NotifySyncCreateRef(doer *user_model.User, repo *models.Repository, refType, refFullName string) { +func (a *actionNotifier) NotifySyncCreateRef(doer *user_model.User, repo *models.Repository, refType, refFullName, shaSum 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 24f6375a69711..bc20dd9075b5b 100644 --- a/modules/notification/base/notifier.go +++ b/modules/notification/base/notifier.go @@ -52,11 +52,11 @@ type Notifier interface { NotifyDeleteRelease(doer *user_model.User, rel *models.Release) NotifyPushCommits(pusher *user_model.User, repo *models.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) - NotifyCreateRef(doer *user_model.User, repo *models.Repository, refType, refFullName string) + NotifyCreateRef(doer *user_model.User, repo *models.Repository, refType, refFullName, refID string) NotifyDeleteRef(doer *user_model.User, repo *models.Repository, refType, refFullName string) NotifySyncPushCommits(pusher *user_model.User, repo *models.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) - NotifySyncCreateRef(doer *user_model.User, repo *models.Repository, refType, refFullName string) + NotifySyncCreateRef(doer *user_model.User, repo *models.Repository, refType, refFullName, refID string) NotifySyncDeleteRef(doer *user_model.User, repo *models.Repository, refType, refFullName string) NotifyRepoPendingTransfer(doer, newOwner *user_model.User, repo *models.Repository) diff --git a/modules/notification/base/null.go b/modules/notification/base/null.go index 8a977e122bdd9..7d20c21fb7fb9 100644 --- a/modules/notification/base/null.go +++ b/modules/notification/base/null.go @@ -141,7 +141,7 @@ func (*NullNotifier) NotifyPushCommits(pusher *user_model.User, repo *models.Rep } // NotifyCreateRef notifies branch or tag creation to notifiers -func (*NullNotifier) NotifyCreateRef(doer *user_model.User, repo *models.Repository, refType, refFullName string) { +func (*NullNotifier) NotifyCreateRef(doer *user_model.User, repo *models.Repository, refType, refFullName, RefID string) { } // NotifyDeleteRef notifies branch or tag deletion to notifiers @@ -161,7 +161,7 @@ func (*NullNotifier) NotifySyncPushCommits(pusher *user_model.User, repo *models } // NotifySyncCreateRef places a place holder function -func (*NullNotifier) NotifySyncCreateRef(doer *user_model.User, repo *models.Repository, refType, refFullName string) { +func (*NullNotifier) NotifySyncCreateRef(doer *user_model.User, repo *models.Repository, refType, refFullName, refID string) { } // NotifySyncDeleteRef places a place holder function diff --git a/modules/notification/notification.go b/modules/notification/notification.go index 9bea38faf6e3e..b10685dfacbde 100644 --- a/modules/notification/notification.go +++ b/modules/notification/notification.go @@ -258,9 +258,9 @@ func NotifyPushCommits(pusher *user_model.User, repo *models.Repository, opts *r } // NotifyCreateRef notifies branch or tag creation to notifiers -func NotifyCreateRef(pusher *user_model.User, repo *models.Repository, refType, refFullName string) { +func NotifyCreateRef(pusher *user_model.User, repo *models.Repository, refType, refFullName, refID string) { for _, notifier := range notifiers { - notifier.NotifyCreateRef(pusher, repo, refType, refFullName) + notifier.NotifyCreateRef(pusher, repo, refType, refFullName, refID) } } @@ -279,9 +279,9 @@ func NotifySyncPushCommits(pusher *user_model.User, repo *models.Repository, opt } // NotifySyncCreateRef notifies branch or tag creation to notifiers -func NotifySyncCreateRef(pusher *user_model.User, repo *models.Repository, refType, refFullName string) { +func NotifySyncCreateRef(pusher *user_model.User, repo *models.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 378e7fd20264d..b0feefb5a2e4e 100644 --- a/modules/notification/webhook/webhook.go +++ b/modules/notification/webhook/webhook.go @@ -697,28 +697,14 @@ func (m *webhookNotifier) NotifyPullRequestReview(pr *models.PullRequest, review } } -func (m *webhookNotifier) NotifyCreateRef(pusher *user_model.User, repo *models.Repository, refType, refFullName string) { +func (m *webhookNotifier) NotifyCreateRef(pusher *user_model.User, repo *models.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, @@ -816,8 +802,8 @@ func (m *webhookNotifier) NotifySyncPushCommits(pusher *user_model.User, repo *m } } -func (m *webhookNotifier) NotifySyncCreateRef(pusher *user_model.User, repo *models.Repository, refType, refFullName string) { - m.NotifyCreateRef(pusher, repo, refType, refFullName) +func (m *webhookNotifier) NotifySyncCreateRef(pusher *user_model.User, repo *models.Repository, refType, refFullName, refID string) { + m.NotifyCreateRef(pusher, repo, refType, refFullName, refID) } func (m *webhookNotifier) NotifySyncDeleteRef(pusher *user_model.User, repo *models.Repository, refType, refFullName string) { diff --git a/services/mirror/mirror_pull.go b/services/mirror/mirror_pull.go index 67c5080fd3739..4260a134cf410 100644 --- a/services/mirror/mirror_pull.go +++ b/services/mirror/mirror_pull.go @@ -426,7 +426,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 } diff --git a/services/release/release.go b/services/release/release.go index 9c5d1e05362d1..2162196183630 100644 --- a/services/release/release.go +++ b/services/release/release.go @@ -80,7 +80,7 @@ func createTag(gitRepo *git.Repository, rel *models.Release, msg string) (bool, OldCommitID: git.EmptySHA, NewCommitID: commit.ID.String(), }, repository.NewPushCommits()) - 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) diff --git a/services/repository/branch.go b/services/repository/branch.go index 21dac41a628bd..b6ed77fb3b2cd 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -134,9 +134,13 @@ func RenameBranch(repo *models.Repository, doer *user_model.User, gitRepo *git.R }); 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/push.go b/services/repository/push.go index 94ac78740df68..d0210232ed98f 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -134,7 +134,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { }, repo_module.NewPushCommits()) 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 { @@ -179,7 +179,7 @@ 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 { From 8a3ff743954e8c9e3483e41db56257d14a71b198 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sun, 5 Dec 2021 16:34:07 +0000 Subject: [PATCH 26/48] more propagation --- models/repo_generate.go | 4 ++-- modules/doctor/misc.go | 2 +- modules/markup/html.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/models/repo_generate.go b/models/repo_generate.go index cef5fa79289fb..6e368ce5ca501 100644 --- a/models/repo_generate.go +++ b/models/repo_generate.go @@ -81,13 +81,13 @@ func GenerateTopics(ctx context.Context, templateRepo, generateRepo *Repository) // GenerateGitHooks generates git hooks from a template repository func GenerateGitHooks(ctx context.Context, templateRepo, generateRepo *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/modules/doctor/misc.go b/modules/doctor/misc.go index 1cf8024b983b6..d72fc4196f3c4 100644 --- a/modules/doctor/misc.go +++ b/modules/doctor/misc.go @@ -86,7 +86,7 @@ func checkEnablePushOptions(logger log.Logger, autofix bool) error { if err := iterateRepositories(func(repo *models.Repository) error { numRepos++ - r, err := git.OpenRepository(repo.RepoPath()) + r, err := git.OpenRepositoryCtx(git.DefaultContext, repo.RepoPath()) if err != nil { return err } diff --git a/modules/markup/html.go b/modules/markup/html.go index 746830720da90..6bae1968ee891 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -1018,7 +1018,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 From b0cc9a110cea0110f3b2323fe9a5dee68e48d071 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sun, 5 Dec 2021 16:35:06 +0000 Subject: [PATCH 27/48] more propagation --- models/migrations/v156.go | 2 +- models/migrations/v82.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/models/migrations/v156.go b/models/migrations/v156.go index 7158d7bb6b71f..77aed3ff57074 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 67bb0cb8c9eb8..7f9f979d95f20 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 } From a0040486b9cf36791ee10aee905fae7d3b1ee427 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sun, 5 Dec 2021 19:16:46 +0000 Subject: [PATCH 28/48] No more git.OpenRepository without a context Signed-off-by: Andrew Thornton --- models/issue_comment.go | 31 ++++++---- models/pull_sign.go | 12 ++-- models/repo_activity.go | 29 ++++++--- models/repo_sign.go | 33 +++++----- modules/context/repo.go | 4 +- modules/convert/pull.go | 11 ++-- modules/convert/pull_test.go | 5 +- modules/notification/base/null.go | 2 +- modules/notification/mail/mail.go | 7 ++- modules/notification/webhook/webhook.go | 64 +++++++++++++++---- modules/process/manager.go | 16 ++--- modules/repository/init.go | 2 +- routers/api/v1/misc/signing.go | 2 +- routers/api/v1/repo/pull.go | 10 +-- routers/api/v1/repo/pull_review.go | 4 +- routers/web/repo/activity.go | 6 +- routers/web/repo/compare.go | 11 ++-- routers/web/repo/editor.go | 2 +- routers/web/repo/issue.go | 6 +- routers/web/repo/pull.go | 26 +++++--- routers/web/repo/pull_review.go | 2 +- routers/web/repo/setting.go | 2 +- routers/web/user/home.go | 2 +- services/agit/agit.go | 4 +- services/pull/merge.go | 6 +- services/pull/pull.go | 82 +++++++++++++------------ services/pull/review.go | 7 +-- services/repository/files/delete.go | 2 +- services/repository/files/temp_repo.go | 2 +- services/repository/files/update.go | 6 +- services/wiki/wiki.go | 4 +- 31 files changed, 238 insertions(+), 164 deletions(-) diff --git a/models/issue_comment.go b/models/issue_comment.go index 417caccfaf8a6..e4bc2584e85f0 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -684,7 +684,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 } @@ -706,11 +706,14 @@ func (c *Comment) LoadPushCommits() (err error) { c.NewCommit = data.CommitIDs[1] } else { repoPath := c.Issue.Repo.RepoPath() - gitRepo, err := git.OpenRepository(repoPath) - if err != nil { - return err + gitRepo := git.RepositoryFromContext(ctx, repoPath) + if gitRepo == nil { + gitRepo, err = git.OpenRepositoryCtx(ctx, repoPath) + if err != nil { + return err + } + defer gitRepo.Close() } - defer gitRepo.Close() c.Commits = ConvertFromGitCommit(gitRepo.GetCommitsFromIDs(data.CommitIDs), c.Issue.Repo) c.CommitsNum = int64(len(c.Commits)) @@ -1278,7 +1281,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 } @@ -1291,7 +1294,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 } @@ -1313,13 +1316,17 @@ 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 *Repository, oldCommitID, newCommitID, baseBranch string) (commitIDs []string, isForcePush bool, err error) { +func getCommitIDsFromRepo(ctx context.Context, repo *Repository, oldCommitID, newCommitID, baseBranch string) (commitIDs []string, isForcePush bool, err error) { repoPath := repo.RepoPath() - gitRepo, err := git.OpenRepository(repoPath) - if err != nil { - return nil, false, err + gitRepo := git.RepositoryFromContext(ctx, repoPath) + if gitRepo == nil { + var err error + gitRepo, err = git.OpenRepositoryCtx(ctx, repoPath) + if err != nil { + return nil, false, err + } + defer gitRepo.Close() } - defer gitRepo.Close() oldCommit, err := gitRepo.GetCommit(oldCommitID) if err != nil { diff --git a/models/pull_sign.go b/models/pull_sign.go index 269e8e06d2025..8104cf9d44419 100644 --- a/models/pull_sign.go +++ b/models/pull_sign.go @@ -5,6 +5,8 @@ package models import ( + "context" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/login" user_model "code.gitea.io/gitea/models/user" @@ -14,14 +16,14 @@ import ( ) // SignMerge determines if we should sign a PR merge commit to the base repository -func (pr *PullRequest) SignMerge(u *user_model.User, tmpBasePath, baseCommit, headCommit string) (bool, string, *git.Signature, error) { +func (pr *PullRequest) SignMerge(ctx context.Context, 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} } @@ -66,7 +68,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 } @@ -82,7 +84,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 } @@ -98,7 +100,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/models/repo_activity.go b/models/repo_activity.go index d3d4c7bbd714d..1f144512aa27f 100644 --- a/models/repo_activity.go +++ b/models/repo_activity.go @@ -5,6 +5,7 @@ package models import ( + "context" "fmt" "sort" "time" @@ -42,7 +43,7 @@ type ActivityStats struct { } // GetActivityStats return stats for repository at given time range -func GetActivityStats(repo *Repository, timeFrom time.Time, releases, issues, prs, code bool) (*ActivityStats, error) { +func GetActivityStats(ctx context.Context, repo *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 { @@ -63,11 +64,15 @@ func GetActivityStats(repo *Repository, timeFrom time.Time, releases, issues, pr return nil, fmt.Errorf("FillUnresolvedIssues: %v", err) } if code { - gitRepo, err := git.OpenRepository(repo.RepoPath()) - if err != nil { - return nil, fmt.Errorf("OpenRepository: %v", err) + gitRepo := git.RepositoryFromContext(ctx, repo.RepoPath()) + if gitRepo == nil { + var err error + gitRepo, err = git.OpenRepositoryCtx(ctx, repo.RepoPath()) + if err != nil { + return nil, fmt.Errorf("OpenRepository: %v", err) + } + defer gitRepo.Close() } - defer gitRepo.Close() code, err := gitRepo.GetCodeActivityStats(timeFrom, repo.DefaultBranch) if err != nil { @@ -79,12 +84,16 @@ func GetActivityStats(repo *Repository, timeFrom time.Time, releases, issues, pr } // GetActivityStatsTopAuthors returns top author stats for git commits for all branches -func GetActivityStatsTopAuthors(repo *Repository, timeFrom time.Time, count int) ([]*ActivityAuthorData, error) { - gitRepo, err := git.OpenRepository(repo.RepoPath()) - if err != nil { - return nil, fmt.Errorf("OpenRepository: %v", err) +func GetActivityStatsTopAuthors(ctx context.Context, repo *Repository, timeFrom time.Time, count int) ([]*ActivityAuthorData, error) { + gitRepo := git.RepositoryFromContext(ctx, repo.RepoPath()) + if gitRepo == nil { + var err error + gitRepo, err = git.OpenRepositoryCtx(ctx, repo.RepoPath()) + if err != nil { + return nil, fmt.Errorf("OpenRepository: %v", err) + } + defer gitRepo.Close() } - defer gitRepo.Close() code, err := gitRepo.GetCodeActivityStats(timeFrom, "") if err != nil { diff --git a/models/repo_sign.go b/models/repo_sign.go index f30ba4748c66e..309a4f9a90b68 100644 --- a/models/repo_sign.go +++ b/models/repo_sign.go @@ -5,6 +5,7 @@ package models import ( + "context" "strings" "code.gitea.io/gitea/models/db" @@ -63,22 +64,22 @@ func signingModeFromStrings(modeStrings []string) []signingMode { } // 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), @@ -92,13 +93,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) @@ -108,9 +109,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} } @@ -144,9 +145,9 @@ Loop: } // SignWikiCommit determines if we should sign the commits to this repository wiki -func (repo *Repository) SignWikiCommit(u *user_model.User) (bool, string, *git.Signature, error) { +func (repo *Repository) SignWikiCommit(ctx context.Context, u *user_model.User) (bool, string, *git.Signature, error) { rules := signingModeFromStrings(setting.Repository.Signing.Wiki) - signingKey, sig := SigningKey(repo.WikiPath()) + signingKey, sig := SigningKey(ctx, repo.WikiPath()) if signingKey == "" { return false, "", nil, &ErrWontSign{noKey} } @@ -175,7 +176,7 @@ Loop: return false, "", nil, &ErrWontSign{twofa} } case parentSigned: - gitRepo, err := git.OpenRepository(repo.WikiPath()) + gitRepo, err := git.OpenRepositoryCtx(ctx, repo.WikiPath()) if err != nil { return false, "", nil, err } @@ -197,9 +198,9 @@ Loop: } // SignCRUDAction determines if we should sign a CRUD commit to this repository -func (repo *Repository) SignCRUDAction(u *user_model.User, tmpBasePath, parentCommit string) (bool, string, *git.Signature, error) { +func (repo *Repository) SignCRUDAction(ctx context.Context, u *user_model.User, tmpBasePath, parentCommit string) (bool, string, *git.Signature, error) { rules := signingModeFromStrings(setting.Repository.Signing.CRUDActions) - signingKey, sig := SigningKey(repo.RepoPath()) + signingKey, sig := SigningKey(ctx, repo.RepoPath()) if signingKey == "" { return false, "", nil, &ErrWontSign{noKey} } @@ -228,7 +229,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 } diff --git a/modules/context/repo.go b/modules/context/repo.go index b2844c04c4c15..096b66738bcf2 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -105,7 +105,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 { @@ -118,7 +118,7 @@ func (r *Repository) CanCommitToBranch(doer *user_model.User) (CanCommitToBranch requireSigned = protectedBranch.RequireSignedCommits } - sign, keyID, _, err := r.Repository.SignCRUDAction(doer, r.Repository.RepoPath(), git.BranchPrefix+r.BranchName) + sign, keyID, _, err := r.Repository.SignCRUDAction(ctx, doer, r.Repository.RepoPath(), git.BranchPrefix+r.BranchName) canCommit := r.CanEnableEditor() && userCanPush if requireSigned { diff --git a/modules/convert/pull.go b/modules/convert/pull.go index 2e2c1b85bc6b4..1551645a51979 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_test.go b/modules/convert/pull_test.go index 1bac5d3a4a296..cd654c896848f 100644 --- a/modules/convert/pull_test.go +++ b/modules/convert/pull_test.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/perm" "code.gitea.io/gitea/models/unittest" + "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -22,7 +23,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", @@ -39,7 +40,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/notification/base/null.go b/modules/notification/base/null.go index 7d20c21fb7fb9..540ad7d7ad16a 100644 --- a/modules/notification/base/null.go +++ b/modules/notification/base/null.go @@ -141,7 +141,7 @@ func (*NullNotifier) NotifyPushCommits(pusher *user_model.User, repo *models.Rep } // NotifyCreateRef notifies branch or tag creation to notifiers -func (*NullNotifier) NotifyCreateRef(doer *user_model.User, repo *models.Repository, refType, refFullName, RefID string) { +func (*NullNotifier) NotifyCreateRef(doer *user_model.User, repo *models.Repository, refType, refFullName, refID string) { } // NotifyDeleteRef notifies branch or tag deletion to notifiers diff --git a/modules/notification/mail/mail.go b/modules/notification/mail/mail.go index c1c9e9613558c..2fb9b225ed068 100644 --- a/modules/notification/mail/mail.go +++ b/modules/notification/mail/mail.go @@ -9,8 +9,10 @@ import ( "code.gitea.io/gitea/models" 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" ) @@ -142,6 +144,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) @@ -159,7 +164,7 @@ 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) diff --git a/modules/notification/webhook/webhook.go b/modules/notification/webhook/webhook.go index b0feefb5a2e4e..2e76448144783 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" "code.gitea.io/gitea/models/unit" @@ -12,8 +14,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" @@ -34,6 +38,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 @@ -55,7 +62,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), }) @@ -139,6 +146,9 @@ func (m *webhookNotifier) NotifyMigrateRepository(doer *user_model.User, u *user } 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) @@ -149,7 +159,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), } @@ -185,6 +195,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 { @@ -201,7 +214,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), }) @@ -226,6 +239,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 { @@ -236,7 +252,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), } @@ -288,6 +304,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 @@ -305,7 +324,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 { @@ -314,6 +333,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 { @@ -326,7 +348,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), }) @@ -479,6 +501,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 { @@ -504,7 +529,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), }) @@ -523,6 +548,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 { @@ -546,7 +574,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), }) @@ -588,6 +616,9 @@ func (m *webhookNotifier) NotifyPushCommits(pusher *user_model.User, repo *model } 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) @@ -613,7 +644,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, @@ -626,6 +657,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 @@ -646,7 +680,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), }) @@ -657,6 +691,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 { @@ -685,7 +722,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{ @@ -714,6 +751,9 @@ func (m *webhookNotifier) NotifyCreateRef(pusher *user_model.User, repo *models. } 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 @@ -726,7 +766,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 { diff --git a/modules/process/manager.go b/modules/process/manager.go index 10a89d04ddb44..68e01601303e7 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/init.go b/modules/repository/init.go index bd729caf05c7e..f6afd0282731d 100644 --- a/modules/repository/init.go +++ b/modules/repository/init.go @@ -133,7 +133,7 @@ func initRepoCommit(ctx context.Context, tmpPath string, repo *models.Repository } if git.CheckGitVersionAtLeast("1.7.9") == nil { - sign, keyID, signer, _ := models.SignInitialCommit(tmpPath, u) + sign, keyID, signer, _ := models.SignInitialCommit(ctx, tmpPath, u) if sign { args = append(args, "-S"+keyID) diff --git a/routers/api/v1/misc/signing.go b/routers/api/v1/misc/signing.go index 3c3391bfe736a..17c09a0caa9ee 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 := models.PublicSigningKey(path) + content, err := models.PublicSigningKey(ctx, path) if err != nil { ctx.Error(http.StatusInternalServerError, "gpg export", err) return diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index a33c6cafbc1a7..f7f7f0dfc27ac 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -117,7 +117,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) @@ -173,7 +173,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 @@ -419,7 +419,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 @@ -626,7 +626,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 @@ -808,7 +808,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 !models.IsErrWontSign(err) { ctx.Error(http.StatusInternalServerError, "IsSignedIfRequired", err) return diff --git a/routers/api/v1/repo/pull_review.go b/routers/api/v1/repo/pull_review.go index a52793c3ec7ae..82dc174787bb2 100644 --- a/routers/api/v1/repo/pull_review.go +++ b/routers/api/v1/repo/pull_review.go @@ -371,7 +371,7 @@ 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 @@ -459,7 +459,7 @@ 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 diff --git a/routers/web/repo/activity.go b/routers/web/repo/activity.go index ae37b51403fe0..b2f25ebe724e4 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/compare.go b/routers/web/repo/compare.go index 842c0b06debaa..f459ac3ded5af 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" @@ -376,7 +377,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 @@ -436,7 +437,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 @@ -461,7 +462,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 @@ -647,8 +648,8 @@ func PrepareCompareDiff( return false } -func getBranchesAndTagsForRepo(repo *models.Repository) (branches, tags []string, err error) { - gitRepo, err := git.OpenRepository(repo.RepoPath()) +func getBranchesAndTagsForRepo(ctx gocontext.Context, repo *models.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 0966a9ee72b0a..60c22e0e326d7 100644 --- a/routers/web/repo/editor.go +++ b/routers/web/repo/editor.go @@ -40,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) } diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index 398aa26cc49dc..7c1287112e3f1 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -262,7 +262,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 @@ -1466,7 +1466,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 } @@ -1578,7 +1578,7 @@ func ViewIssue(ctx *context.Context) { } ctx.Data["WillSign"] = false if ctx.User != nil { - sign, key, _, err := pull.SignMerge(ctx.User, pull.BaseRepo.RepoPath(), pull.BaseBranch, pull.GetGitRefName()) + sign, key, _, err := pull.SignMerge(ctx, 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/pull.go b/routers/web/repo/pull.go index ac15f4d0610e1..27cd551649a50 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -375,12 +375,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 @@ -426,7 +431,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 @@ -453,12 +458,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()) @@ -1012,7 +1018,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 @@ -1251,7 +1257,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 @@ -1266,7 +1272,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 diff --git a/routers/web/repo/pull_review.go b/routers/web/repo/pull_review.go index e552328de0bf9..5bfd4cd2bb01e 100644 --- a/routers/web/repo/pull_review.go +++ b/routers/web/repo/pull_review.go @@ -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")) diff --git a/routers/web/repo/setting.go b/routers/web/repo/setting.go index 70bb845d868cc..1ebe8d9b44b7b 100644 --- a/routers/web/repo/setting.go +++ b/routers/web/repo/setting.go @@ -61,7 +61,7 @@ func Settings(ctx *context.Context) { ctx.Data["DisableNewPushMirrors"] = setting.Mirror.DisableNewPush ctx.Data["DefaultMirrorInterval"] = setting.Mirror.DefaultInterval - signing, _ := models.SigningKey(ctx.Repo.Repository.RepoPath()) + signing, _ := models.SigningKey(ctx, ctx.Repo.Repository.RepoPath()) ctx.Data["SigningKeyAvailable"] = len(signing) > 0 ctx.Data["SigningSettings"] = setting.Repository.Signing diff --git a/routers/web/user/home.go b/routers/web/user/home.go index ca2705a42fdb0..216b41a92a799 100644 --- a/routers/web/user/home.go +++ b/routers/web/user/home.go @@ -545,7 +545,7 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) { issue.Repo = showReposMap[issue.RepoID] } - 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 11b92e20b0954..357adf884d36f 100644 --- a/services/agit/agit.go +++ b/services/agit/agit.go @@ -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{ @@ -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/pull/merge.go b/services/pull/merge.go index ee87c88441efc..9e4c1917f2f0f 100644 --- a/services/pull/merge.go +++ b/services/pull/merge.go @@ -217,7 +217,7 @@ func rawMerge(ctx context.Context, pr *models.PullRequest, doer *user_model.User // Determine if we should sign signArg := "" if git.CheckGitVersionAtLeast("1.7.9") == nil { - sign, keyID, signer, _ := pr.SignMerge(doer, tmpBasePath, "HEAD", trackingBranch) + sign, keyID, signer, _ := pr.SignMerge(ctx, doer, tmpBasePath, "HEAD", trackingBranch) if sign { signArg = "-S" + keyID if pr.BaseRepo.GetTrustModel() == models.CommitterTrustModel || pr.BaseRepo.GetTrustModel() == models.CollaboratorCommitterTrustModel { @@ -543,7 +543,7 @@ func getDiffTree(ctx context.Context, repoPath, baseBranch, headBranch string) ( } // 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 } @@ -552,7 +552,7 @@ func IsSignedIfRequired(pr *models.PullRequest, doer *user_model.User) (bool, er return true, nil } - sign, _, _, err := pr.SignMerge(doer, pr.BaseRepo.RepoPath(), pr.BaseBranch, pr.GetGitRefName()) + sign, _, _, err := pr.SignMerge(ctx, doer, pr.BaseRepo.RepoPath(), pr.BaseBranch, pr.GetGitRefName()) return sign, err } diff --git a/services/pull/pull.go b/services/pull/pull.go index b3020dba2e9f1..98657dffa3231 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -74,7 +74,7 @@ func NewPullRequest(ctx context.Context, repo *models.Repository, pull *models.I } // add first push codes comment - baseGitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath()) + baseGitRepo, err := git.OpenRepositoryCtx(ctx, pr.BaseRepo.RepoPath()) if err != nil { return err } @@ -140,7 +140,7 @@ func ChangeTargetBranch(ctx context.Context, pr *models.PullRequest, doer *user_ } // Check if branches are equal - branchesEqual, err := IsHeadEqualWithBranch(pr, targetBranch) + branchesEqual, err := IsHeadEqualWithBranch(ctx, pr, targetBranch) if err != nil { return err } @@ -210,12 +210,12 @@ func ChangeTargetBranch(ctx context.Context, pr *models.PullRequest, doer *user_ 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 := models.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) } @@ -250,13 +250,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) } @@ -298,7 +298,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) } @@ -331,7 +331,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 { @@ -343,7 +343,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) } @@ -562,7 +562,7 @@ func CloseRepoBranchesPulls(ctx context.Context, doer *user_model.User, repo *mo 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 "" @@ -582,14 +582,20 @@ func GetSquashMergeCommitMessages(pr *models.PullRequest) string { } } - gitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath()) - if err != nil { - log.Error("Unable to open head repository: Error: %v", err) - return "" + gitRepo := git.RepositoryFromContext(ctx, pr.HeadRepo.RepoPath()) + if gitRepo == nil { + var err error + gitRepo, err = git.OpenRepositoryCtx(ctx, pr.HeadRepo.RepoPath()) + if err != nil { + log.Error("Unable to open head repository: Error: %v", err) + return "" + } + defer gitRepo.Close() } - defer gitRepo.Close() var headCommit *git.Commit + var err error + if pr.Flow == models.PullRequestFlowGithub { headCommit, err = gitRepo.GetBranchCommit(pr.HeadBranch) } else { @@ -718,7 +724,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 } @@ -743,7 +749,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 { return nil, err } @@ -760,20 +766,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()) @@ -789,16 +781,20 @@ 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()) - if err != nil { - return false, err + baseGitRepo := git.RepositoryFromContext(ctx, pr.BaseRepo.RepoPath()) + if baseGitRepo == nil { + var err error + baseGitRepo, err = git.OpenRepositoryCtx(ctx, pr.BaseRepo.RepoPath()) + if err != nil { + return false, err + } + defer baseGitRepo.Close() } - defer baseGitRepo.Close() baseCommit, err := baseGitRepo.GetBranchCommit(branchName) if err != nil { @@ -808,11 +804,17 @@ 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 if headGitRepo := git.RepositoryFromContext(ctx, pr.HeadRepo.RepoPath()); headGitRepo == nil { + var err error + headGitRepo, err = git.OpenRepositoryCtx(ctx, pr.HeadRepo.RepoPath()) + if err != nil { + return false, err + } + defer headGitRepo.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 ee959362670a9..a4e899a94aae9 100644 --- a/services/pull/review.go +++ b/services/pull/review.go @@ -103,7 +103,7 @@ func CreateCodeComment(ctx context.Context, doer *user_model.User, gitRepo *git. 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 } } @@ -222,7 +222,7 @@ func createCodeComment(ctx context.Context, doer *user_model.User, repo *models. } // 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 @@ -240,7 +240,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 } @@ -252,7 +252,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 diff --git a/services/repository/files/delete.go b/services/repository/files/delete.go index cd2d448190829..9ef8a64e9335c 100644 --- a/services/repository/files/delete.go +++ b/services/repository/files/delete.go @@ -67,7 +67,7 @@ func DeleteRepoFile(ctx context.Context, repo *models.Repository, doer *user_mod 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 } diff --git a/services/repository/files/temp_repo.go b/services/repository/files/temp_repo.go index 9c86d3ff63b54..ff6b1eb8ed41e 100644 --- a/services/repository/files/temp_repo.go +++ b/services/repository/files/temp_repo.go @@ -217,7 +217,7 @@ func (t *TemporaryUploadRepository) CommitTreeWithDate(author, committer *user_m // Determine if we should sign if git.CheckGitVersionAtLeast("1.7.9") == nil { - sign, keyID, signer, _ := t.repo.SignCRUDAction(author, t.basePath, "HEAD") + sign, keyID, signer, _ := t.repo.SignCRUDAction(t.ctx, author, t.basePath, "HEAD") if sign { args = append(args, "-S"+keyID) if t.repo.GetTrustModel() == models.CommitterTrustModel || t.repo.GetTrustModel() == models.CollaboratorCommitterTrustModel { diff --git a/services/repository/files/update.go b/services/repository/files/update.go index 1974bb1fc2c5a..2571e5e2c9a52 100644 --- a/services/repository/files/update.go +++ b/services/repository/files/update.go @@ -160,7 +160,7 @@ func CreateOrUpdateRepoFile(ctx context.Context, repo *models.Repository, doer * 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 } @@ -450,7 +450,7 @@ func CreateOrUpdateRepoFile(ctx context.Context, repo *models.Repository, doer * } // VerifyBranchProtection verify the branch protection for modifying the given treePath on the given branch -func VerifyBranchProtection(repo *models.Repository, doer *user_model.User, branchName string, treePath string) error { +func VerifyBranchProtection(ctx context.Context, repo *models.Repository, doer *user_model.User, branchName string, treePath string) error { protectedBranch, err := repo.GetBranchProtection(branchName) if err != nil { return err @@ -467,7 +467,7 @@ func VerifyBranchProtection(repo *models.Repository, doer *user_model.User, bran } } if protectedBranch.RequireSignedCommits { - _, _, _, err := repo.SignCRUDAction(doer, repo.RepoPath(), branchName) + _, _, _, err := repo.SignCRUDAction(ctx, doer, repo.RepoPath(), branchName) if err != nil { if !models.IsErrWontSign(err) { return err diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go index 82d19725bf665..db6b20f15550f 100644 --- a/services/wiki/wiki.go +++ b/services/wiki/wiki.go @@ -224,7 +224,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *models.Rep committer := doer.NewGitSig() - sign, signingKey, signer, _ := repo.SignWikiCommit(doer) + sign, signingKey, signer, _ := repo.SignWikiCommit(ctx, doer) if sign { commitTreeOpts.KeyID = signingKey if repo.GetTrustModel() == models.CommitterTrustModel || repo.GetTrustModel() == models.CollaboratorCommitterTrustModel { @@ -342,7 +342,7 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *models.Rep committer := doer.NewGitSig() - sign, signingKey, signer, _ := repo.SignWikiCommit(doer) + sign, signingKey, signer, _ := repo.SignWikiCommit(ctx, doer) if sign { commitTreeOpts.KeyID = signingKey if repo.GetTrustModel() == models.CommitterTrustModel || repo.GetTrustModel() == models.CollaboratorCommitterTrustModel { From 2191ed8ba5989e3f95c1e494b450ed8b59746849 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sun, 5 Dec 2021 19:59:38 +0000 Subject: [PATCH 29/48] Begin removing calls to NewCommand and replace with NewCommandContext Signed-off-by: Andrew Thornton --- modules/context/repo.go | 6 ++--- modules/convert/git_commit.go | 4 ++-- modules/git/commit.go | 31 +++++++++++++------------ modules/git/commit_test.go | 6 ++--- modules/git/diff.go | 4 ++-- modules/git/repo.go | 8 +++---- modules/git/repo_commit.go | 6 ++--- modules/git/repo_compare.go | 6 ++--- modules/git/tree_entry_nogogit.go | 16 ++++++++----- modules/notification/webhook/webhook.go | 10 ++++++-- modules/repository/commits.go | 11 +++++---- modules/repository/commits_test.go | 2 +- modules/repository/repo.go | 6 ++--- routers/api/v1/repo/commits.go | 4 ++-- routers/api/v1/repo/notes.go | 2 +- routers/api/v1/repo/pull.go | 2 +- routers/private/hook_pre_receive.go | 4 ++-- routers/web/repo/commit.go | 4 ++-- routers/web/repo/setting.go | 6 ++--- services/migrations/dump.go | 2 +- services/mirror/mirror_pull.go | 16 ++++++------- services/mirror/mirror_push.go | 14 +++++------ services/pull/merge.go | 6 ++--- services/pull/patch.go | 10 ++++---- 24 files changed, 99 insertions(+), 87 deletions(-) diff --git a/modules/context/repo.go b/modules/context/repo.go index 096b66738bcf2..2fa613c31b53b 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -176,14 +176,14 @@ func (r *Repository) GetCommitsCount() (int64, error) { } // GetCommitGraphsCount returns cached commit count for current view -func (r *Repository) GetCommitGraphsCount(hidePRRefs bool, branches []string, files []string) (int64, error) { +func (r *Repository) GetCommitGraphsCount(ctx context.Context, hidePRRefs bool, branches []string, 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 3148d880ff4eb..951dc7938309f 100644 --- a/modules/convert/git_commit.go +++ b/modules/convert/git_commit.go @@ -72,7 +72,7 @@ func ToPayloadCommit(repo *models.Repository, c *git.Commit) *api.PayloadCommit } // ToCommit convert a git.Commit to api.Commit -func ToCommit(repo *models.Repository, commit *git.Commit, userCache map[string]*user_model.User) (*api.Commit, error) { +func ToCommit(repo *models.Repository, gitRepo *git.Repository, commit *git.Commit, userCache map[string]*user_model.User) (*api.Commit, error) { var apiAuthor, apiCommitter *api.User @@ -134,7 +134,7 @@ func ToCommit(repo *models.Repository, commit *git.Commit, userCache map[string] } // 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/git/commit.go b/modules/git/commit.go index 26c5445e2a953..fb51a762a194b 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_test.go b/modules/git/commit_test.go index 57132c00dc69c..2b3365342d37f 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 f912992e77572..c42cb7878ae55 100644 --- a/modules/git/diff.go +++ b/modules/git/diff.go @@ -278,7 +278,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) @@ -292,7 +292,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/repo.go b/modules/git/repo.go index 3a005866c1e42..456f2264b27ab 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,8 +57,8 @@ 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 } @@ -80,7 +80,7 @@ func InitRepository(ctx context.Context, 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 diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index 3afabac27b51a..27e6a92b621a0 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 6e7c6487d0ffd..91b3fe053e0cd 100644 --- a/modules/git/repo_compare.go +++ b/modules/git/repo_compare.go @@ -69,14 +69,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 } @@ -102,7 +102,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 } diff --git a/modules/git/tree_entry_nogogit.go b/modules/git/tree_entry_nogogit.go index 288ec4db6e194..076062e1d678f 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/notification/webhook/webhook.go b/modules/notification/webhook/webhook.go index 2e76448144783..7a88153c6e580 100644 --- a/modules/notification/webhook/webhook.go +++ b/modules/notification/webhook/webhook.go @@ -593,8 +593,11 @@ func (m *webhookNotifier) NotifyIssueChangeMilestone(doer *user_model.User, issu } func (m *webhookNotifier) NotifyPushCommits(pusher *user_model.User, repo *models.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 @@ -820,8 +823,11 @@ func (m *webhookNotifier) NotifyDeleteRelease(doer *user_model.User, rel *models } func (m *webhookNotifier) NotifySyncPushCommits(pusher *user_model.User, repo *models.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 diff --git a/modules/repository/commits.go b/modules/repository/commits.go index 8e727c95d0e7b..05d0062ba2503 100644 --- a/modules/repository/commits.go +++ b/modules/repository/commits.go @@ -5,6 +5,7 @@ package repository import ( + "context" "fmt" "net/url" "time" @@ -47,7 +48,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] @@ -74,7 +75,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) } @@ -102,7 +103,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 @@ -110,7 +111,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 } @@ -122,7 +123,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 b6a63dc12765f..6ba24604f91dd 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, &models.Repository{ID: 16}).(*models.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/repo.go b/modules/repository/repo.go index 920349998804a..2874065b191e6 100644 --- a/modules/repository/repo.go +++ b/modules/repository/repo.go @@ -35,11 +35,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 } } @@ -80,7 +80,7 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User, if opts.Wiki { wikiPath := models.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) diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index b68c5494c748e..5fe55dad189d5 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -72,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 @@ -182,7 +182,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 diff --git a/routers/api/v1/repo/notes.go b/routers/api/v1/repo/notes.go index b56f828d82820..edcb27b1ec908 100644 --- a/routers/api/v1/repo/notes.go +++ b/routers/api/v1/repo/notes.go @@ -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 f7f7f0dfc27ac..69875f3e5f082 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -1224,7 +1224,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/private/hook_pre_receive.go b/routers/private/hook_pre_receive.go index 44e2254de0311..f119d38b6e141 100644 --- a/routers/private/hook_pre_receive.go +++ b/routers/private/hook_pre_receive.go @@ -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{ diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index 077860fba6a85..d3888793fb369 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -116,12 +116,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 diff --git a/routers/web/repo/setting.go b/routers/web/repo/setting.go index 1ebe8d9b44b7b..5ee3b5113938f 100644 --- a/routers/web/repo/setting.go +++ b/routers/web/repo/setting.go @@ -193,7 +193,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 } @@ -269,7 +269,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 } @@ -326,7 +326,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 := models.DeletePushMirrorByID(m.ID); err != nil { log.Error("DeletePushMirrorByID %v", err) } diff --git a/services/migrations/dump.go b/services/migrations/dump.go index e1181c85ebbf8..158235220a9c4 100644 --- a/services/migrations/dump.go +++ b/services/migrations/dump.go @@ -161,7 +161,7 @@ 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) diff --git a/services/mirror/mirror_pull.go b/services/mirror/mirror_pull.go index 4260a134cf410..d1914698d3704 100644 --- a/services/mirror/mirror_pull.go +++ b/services/mirror/mirror_pull.go @@ -29,30 +29,30 @@ import ( const gitShortEmptySha = "0000000" // UpdateAddress writes new address to Git repository and database -func UpdateAddress(m *models.Mirror, addr string) error { +func UpdateAddress(ctx context.Context, m *models.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 } @@ -437,12 +437,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 diff --git a/services/mirror/mirror_push.go b/services/mirror/mirror_push.go index cf205e7b51d8a..acbec81a12524 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 *models.PushMirror, addr string) error { +func AddPushMirrorRemote(ctx context.Context, m *models.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 *models.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 *models.PushMirror, addr string) error { } // RemovePushMirrorRemote removes the push mirror remote. -func RemovePushMirrorRemote(m *models.PushMirror) error { - cmd := git.NewCommand("remote", "rm", m.RemoteName) +func RemovePushMirrorRemote(ctx context.Context, m *models.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/merge.go b/services/pull/merge.go index 9e4c1917f2f0f..1fd47a8c9c000 100644 --- a/services/pull/merge.go +++ b/services/pull/merge.go @@ -376,15 +376,15 @@ func rawMerge(ctx context.Context, pr *models.PullRequest, doer *user_model.User } // 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) } diff --git a/services/pull/patch.go b/services/pull/patch.go index a24c6e74e734c..b842514f27acb 100644 --- a/services/pull/patch.go +++ b/services/pull/patch.go @@ -254,11 +254,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 } @@ -284,11 +284,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 } @@ -320,7 +320,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 } From a649d3e990c6944507a1539285d39b0431ede290 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Mon, 6 Dec 2021 17:46:47 +0000 Subject: [PATCH 30/48] Add context to RenderContext Signed-off-by: Andrew Thornton --- models/issue_comment.go | 20 +++++++++++--------- models/issue_comment_test.go | 5 +++-- models/repo.go | 3 ++- models/review.go | 21 ++++++++++++--------- models/review_test.go | 11 ++++++----- modules/context/context.go | 1 + modules/convert/pull_review.go | 13 +++++++------ modules/notification/action/action.go | 7 ++++++- routers/api/v1/repo/pull_review.go | 18 +++++++++--------- routers/web/repo/issue.go | 4 ++-- routers/web/repo/pull.go | 2 +- routers/web/repo/pull_review.go | 4 ++-- services/gitdiff/gitdiff.go | 4 ++-- services/gitdiff/gitdiff_test.go | 3 ++- services/pull/review.go | 4 ++-- templates/explore/repo_list.tmpl | 3 ++- templates/repo/home.tmpl | 3 ++- 17 files changed, 72 insertions(+), 54 deletions(-) diff --git a/models/issue_comment.go b/models/issue_comment.go index e4bc2584e85f0..cc9f10c705027 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -1147,11 +1147,11 @@ func deleteComment(e db.Engine, comment *Comment) error { // CodeComments represents comments on code by using this structure: FILENAME -> LINE (+ == proposed; - == previous) -> COMMENTS type CodeComments map[string]map[int64][]*Comment -func fetchCodeComments(e db.Engine, issue *Issue, currentUser *user_model.User) (CodeComments, error) { - return fetchCodeCommentsByReview(e, issue, currentUser, nil) +func fetchCodeComments(ctx context.Context, issue *Issue, currentUser *user_model.User) (CodeComments, error) { + return fetchCodeCommentsByReview(ctx, issue, currentUser, nil) } -func fetchCodeCommentsByReview(e db.Engine, issue *Issue, currentUser *user_model.User, review *Review) (CodeComments, error) { +func fetchCodeCommentsByReview(ctx context.Context, issue *Issue, currentUser *user_model.User, review *Review) (CodeComments, error) { pathToLineToComment := make(CodeComments) if review == nil { review = &Review{ID: 0} @@ -1162,7 +1162,7 @@ func fetchCodeCommentsByReview(e db.Engine, issue *Issue, currentUser *user_mode ReviewID: review.ID, } - comments, err := findCodeComments(e, opts, issue, currentUser, review) + comments, err := findCodeComments(ctx, opts, issue, currentUser, review) if err != nil { return nil, err } @@ -1176,7 +1176,8 @@ func fetchCodeCommentsByReview(e db.Engine, issue *Issue, currentUser *user_mode return pathToLineToComment, nil } -func findCodeComments(e db.Engine, opts FindCommentsOptions, issue *Issue, currentUser *user_model.User, review *Review) ([]*Comment, error) { +func findCodeComments(ctx context.Context, opts FindCommentsOptions, issue *Issue, currentUser *user_model.User, review *Review) ([]*Comment, error) { + e := db.GetEngine(ctx) var comments []*Comment if review == nil { review = &Review{ID: 0} @@ -1235,6 +1236,7 @@ func findCodeComments(e db.Engine, opts FindCommentsOptions, issue *Issue, curre 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 { @@ -1245,19 +1247,19 @@ func findCodeComments(e db.Engine, opts FindCommentsOptions, issue *Issue, curre } // 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.GetEngine(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.GetEngine(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 diff --git a/models/issue_comment_test.go b/models/issue_comment_test.go index 5ab4892bc7cd8..55baec2095440 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" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" @@ -48,7 +49,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)) @@ -56,7 +57,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/repo.go b/models/repo.go index 67879fefdaa52..b43e2e74bb584 100644 --- a/models/repo.go +++ b/models/repo.go @@ -866,8 +866,9 @@ func (repo *Repository) getUsersWithAccessMode(e db.Engine, mode perm.AccessMode } // 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/review.go b/models/review.go index eebf796876423..2fa21a52cbb6c 100644 --- a/models/review.go +++ b/models/review.go @@ -5,6 +5,7 @@ package models import ( + "context" "fmt" "strings" @@ -85,20 +86,21 @@ func init() { db.RegisterModel(new(Review)) } -func (r *Review) loadCodeComments(e db.Engine) (err error) { +func (r *Review) loadCodeComments(ctx context.Context) (err error) { + e := db.GetEngine(ctx) if r.CodeComments != nil { return } if err = r.loadIssue(e); err != nil { return } - r.CodeComments, err = fetchCodeCommentsByReview(e, r.Issue, nil, r) + r.CodeComments, err = fetchCodeCommentsByReview(ctx, r.Issue, nil, r) return } // LoadCodeComments loads CodeComments -func (r *Review) LoadCodeComments() error { - return r.loadCodeComments(db.GetEngine(db.DefaultContext)) +func (r *Review) LoadCodeComments(ctx context.Context) error { + return r.loadCodeComments(ctx) } func (r *Review) loadIssue(e db.Engine) (err error) { @@ -136,11 +138,12 @@ func (r *Review) LoadReviewerTeam() error { return r.loadReviewerTeam(db.GetEngine(db.DefaultContext)) } -func (r *Review) loadAttributes(e db.Engine) (err error) { +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(e); err != nil { + if err = r.loadCodeComments(ctx); err != nil { return } if err = r.loadReviewer(e); err != nil { @@ -153,8 +156,8 @@ func (r *Review) loadAttributes(e db.Engine) (err error) { } // LoadAttributes loads all attributes except CodeComments -func (r *Review) LoadAttributes() error { - return r.loadAttributes(db.GetEngine(db.DefaultContext)) +func (r *Review) LoadAttributes(ctx context.Context) error { + return r.loadAttributes(ctx) } func getReviewByID(e db.Engine, id int64) (*Review, error) { @@ -403,7 +406,7 @@ func SubmitReview(doer *user_model.User, issue *Issue, reviewType ReviewType, co return nil, nil, err } } else { - if err := review.loadCodeComments(sess); 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 b9d156ceb18cb..20810dac4ac2b 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/context.go b/modules/context/context.go index c92e91e4aede8..c9274ccb68d18 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -670,6 +670,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/convert/pull_review.go b/modules/convert/pull_review.go index b99d7bead6ccd..962aae58bb34f 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/notification/action/action.go b/modules/notification/action/action.go index 5d3ca6b6c7a81..abed2e05379e8 100644 --- a/modules/notification/action/action.go +++ b/modules/notification/action/action.go @@ -11,9 +11,11 @@ import ( "code.gitea.io/gitea/models" 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" ) @@ -205,11 +207,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 } diff --git a/routers/api/v1/repo/pull_review.go b/routers/api/v1/repo/pull_review.go index 82dc174787bb2..bb42fe3376f8f 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 @@ -378,7 +378,7 @@ func CreatePullReview(ctx *context.APIContext) { } // 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 @@ -466,7 +466,7 @@ func SubmitPullReview(ctx *context.APIContext) { } // 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 @@ -558,7 +558,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 } @@ -768,7 +768,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 @@ -886,7 +886,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 @@ -898,7 +898,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/web/repo/issue.go b/routers/web/repo/issue.go index 7c1287112e3f1..f01b045c0cfe1 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -1429,14 +1429,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 } diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index 27cd551649a50..e51a0f657f7f5 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -663,7 +663,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 } diff --git a/routers/web/repo/pull_review.go b/routers/web/repo/pull_review.go index 5bfd4cd2bb01e..c92f08a88a7b3 100644 --- a/routers/web/repo/pull_review.go +++ b/routers/web/repo/pull_review.go @@ -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 @@ -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/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index ee68b2780dec7..98357659df380 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -667,8 +667,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 } diff --git a/services/gitdiff/gitdiff_test.go b/services/gitdiff/gitdiff_test.go index 21afdb4cac446..488127abcd274 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/pull/review.go b/services/pull/review.go index a4e899a94aae9..d6df3d75f046d 100644 --- a/services/pull/review.go +++ b/services/pull/review.go @@ -275,7 +275,7 @@ func SubmitReview(ctx context.Context, doer *user_model.User, gitRepo *git.Repos } // 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 @@ -294,7 +294,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/templates/explore/repo_list.tmpl b/templates/explore/repo_list.tmpl index 4ff48b60761d8..a757ed6368939 100644 --- a/templates/explore/repo_list.tmpl +++ b/templates/explore/repo_list.tmpl @@ -49,7 +49,8 @@
- {{if .DescriptionHTML}}

{{.DescriptionHTML}}

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

{{$description}}

{{end}} {{if .Topics }}
{{range .Topics}} diff --git a/templates/repo/home.tmpl b/templates/repo/home.tmpl index 5ee6cc2df74cf..139cf8e4edaea 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 := call .Repository.DescriptionHTML $.Context}} + {{if $description}}{{$description}}{{else if .IsRepositoryAdmin}}{{.i18n.Tr "repo.no_desc"}}{{end}} {{.Repository.Website}}
{{if .RepoSearchEnabled}} From 4bcc196278392abe438cd0b965c18d3b28f95f29 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Mon, 6 Dec 2021 18:03:33 +0000 Subject: [PATCH 31/48] prevent panic in tests Signed-off-by: Andrew Thornton --- modules/markup/html_test.go | 2 ++ modules/markup/markdown/markdown_test.go | 3 +++ 2 files changed, 5 insertions(+) diff --git a/modules/markup/html_test.go b/modules/markup/html_test.go index 3eb2df00a942d..735ff8baea1fe 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" @@ -30,6 +31,7 @@ func TestRender_Commits(t *testing.T) { test := func(input, expected string) { buffer, err := RenderString(&RenderContext{ + Ctx: git.DefaultContext, Filename: ".md", URLPrefix: setting.AppSubURL, Metas: localMetas, diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index 936e4a39fdc5d..4f8e068ffde71 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]) From ce0ea53d963368da5c2765249dd88fd544716079 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Mon, 6 Dec 2021 18:33:11 +0000 Subject: [PATCH 32/48] more fixes Signed-off-by: Andrew Thornton --- modules/context/api.go | 1 + modules/context/context.go | 2 +- modules/context/private.go | 1 + modules/git/repo_base.go | 2 +- templates/explore/repo_list.tmpl | 2 +- templates/repo/home.tmpl | 2 +- 6 files changed, 6 insertions(+), 4 deletions(-) diff --git a/modules/context/api.go b/modules/context/api.go index c162a604dca8f..95281dfc80780 100644 --- a/modules/context/api.go +++ b/modules/context/api.go @@ -298,6 +298,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) diff --git a/modules/context/context.go b/modules/context/context.go index c9274ccb68d18..99a5885fddc6b 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -670,7 +670,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.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 dfefa1d2f0192..909eb589db806 100644 --- a/modules/context/private.go +++ b/modules/context/private.go @@ -39,6 +39,7 @@ func PrivateContexter() func(http.Handler) http.Handler { }, } ctx.Req = WithPrivateContext(req, ctx) + ctx.Data["Context"] = ctx next.ServeHTTP(ctx.Resp, ctx.Req) }) } diff --git a/modules/git/repo_base.go b/modules/git/repo_base.go index f2e10011cebb0..bfa9cb17944af 100644 --- a/modules/git/repo_base.go +++ b/modules/git/repo_base.go @@ -21,7 +21,7 @@ func RepositoryFromContext(ctx context.Context, path string) *Repository { return nil } - if repo, ok := value.(*Repository); ok { + if repo, ok := value.(*Repository); ok && repo != nil { if repo.Path == path { return repo } diff --git a/templates/explore/repo_list.tmpl b/templates/explore/repo_list.tmpl index a757ed6368939..196c947213863 100644 --- a/templates/explore/repo_list.tmpl +++ b/templates/explore/repo_list.tmpl @@ -49,7 +49,7 @@
- {{ $description := call .DescriptionHTML $.Context}} + {{ $description := .DescriptionHTML $.Context}} {{if $description}}

{{$description}}

{{end}} {{if .Topics }}
diff --git a/templates/repo/home.tmpl b/templates/repo/home.tmpl index 139cf8e4edaea..9924ae547dcf0 100644 --- a/templates/repo/home.tmpl +++ b/templates/repo/home.tmpl @@ -5,7 +5,7 @@ {{template "base/alert" .}}
- {{ $description := call .Repository.DescriptionHTML $.Context}} + {{ $description := .Repository.DescriptionHTML $.Context}} {{if $description}}{{$description}}{{else if .IsRepositoryAdmin}}{{.i18n.Tr "repo.no_desc"}}{{end}} {{.Repository.Website}}
From 98e2eefe90186406add4e05b671979937a9b1c28 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Mon, 6 Dec 2021 19:36:27 +0000 Subject: [PATCH 33/48] more fixes Signed-off-by: Andrew Thornton --- modules/notification/mail/mail.go | 25 ++++++++++++++++++++----- modules/templates/helper.go | 20 +++++++++++++------- routers/api/v1/misc/markdown.go | 6 +++++- routers/web/feed/convert.go | 2 +- routers/web/org/home.go | 1 + routers/web/repo/compare.go | 4 ++-- routers/web/repo/wiki.go | 1 + services/mailer/mail.go | 1 + services/mailer/mail_comment.go | 8 ++++++-- services/mailer/mail_issue.go | 2 ++ services/mailer/mail_release.go | 8 +++++--- 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/issue/view_title.tmpl | 2 +- templates/repo/view_list.tmpl | 6 +++--- templates/user/dashboard/feeds.tmpl | 2 +- tools/fuzz.go | 2 ++ 20 files changed, 74 insertions(+), 36 deletions(-) diff --git a/modules/notification/mail/mail.go b/modules/notification/mail/mail.go index 2fb9b225ed068..4d3b861438d0e 100644 --- a/modules/notification/mail/mail.go +++ b/modules/notification/mail/mail.go @@ -31,6 +31,9 @@ func NewNotifier() base.Notifier { func (m *mailNotifier) NotifyCreateIssueComment(doer *user_model.User, repo *models.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 @@ -44,7 +47,7 @@ func (m *mailNotifier) NotifyCreateIssueComment(doer *user_model.User, repo *mod 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) } } @@ -95,6 +98,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 @@ -103,13 +109,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) } } @@ -171,12 +180,18 @@ func (m *mailNotifier) NotifyPullRequestPushCommits(doer *user_model.User, pr *m } 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 @@ -186,7 +201,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 *models.Repository) { diff --git a/modules/templates/helper.go b/modules/templates/helper.go index eb0c7f7e714c9..151870e320364 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -7,6 +7,7 @@ package templates import ( "bytes" + "context" "errors" "fmt" "html" @@ -634,17 +635,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, @@ -662,7 +664,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 { @@ -676,6 +678,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, @@ -688,7 +691,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 { @@ -702,6 +705,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)) @@ -713,8 +717,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)) @@ -749,9 +754,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) diff --git a/routers/api/v1/misc/markdown.go b/routers/api/v1/misc/markdown.go index f1007b7ee2db2..eb0b78a5bab0e 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/web/feed/convert.go b/routers/web/feed/convert.go index 4743668621dd4..4dbd9c9d0e71a 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 52f0282b5cf5a..6889d8870c873 100644 --- a/routers/web/org/home.go +++ b/routers/web/org/home.go @@ -39,6 +39,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/compare.go b/routers/web/repo/compare.go index f459ac3ded5af..5e961338100df 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -135,14 +135,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() } diff --git a/routers/web/repo/wiki.go b/routers/web/repo/wiki.go index 3a282b2d4545e..3ed90d4f04ea5 100644 --- a/routers/web/repo/wiki.go +++ b/routers/web/repo/wiki.go @@ -219,6 +219,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, diff --git a/services/mailer/mail.go b/services/mailer/mail.go index a9978be8b190a..1d37816b8e4f0 100644 --- a/services/mailer/mail.go +++ b/services/mailer/mail.go @@ -234,6 +234,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 3662164092c4b..a42458b2c2c82 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 1fbe68e0222ee..f0d09c959d79e 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" @@ -19,6 +20,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 72476fe8be0cb..c30827ac1c5f8 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" user_model "code.gitea.io/gitea/models/user" @@ -23,7 +24,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 @@ -49,15 +50,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/templates/repo/branch/list.tmpl b/templates/repo/branch/list.tmpl index 3580808aac188..fc8bb340786dd 100644 --- a/templates/repo/branch/list.tmpl +++ b/templates/repo/branch/list.tmpl @@ -19,7 +19,7 @@ {{svg "octicon-shield-lock"}} {{end}} {{$.DefaultBranch}} -

{{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}} {{end}} @@ -61,7 +61,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 244bc9c121672..ea096de046c95 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}} @@ -150,7 +150,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 ad2e825e23bd6..e7666c35e97b4 100644 --- a/templates/repo/commits_list.tmpl +++ b/templates/repo/commits_list.tmpl @@ -63,7 +63,7 @@ {{.Summary | RenderEmoji}} {{else }} {{ $commitLink:= printf "%s/commit/%s" $.RepoLink (PathEscape .ID.String) }} - {{RenderCommitMessageLinkSubject .Message $.RepoLink $commitLink $.Repository.ComposeMetas}} + {{RenderCommitMessageLinkSubject $.Context .Message $.RepoLink $commitLink $.Repository.ComposeMetas}} {{end}} {{if IsMultilineCommitMessage .Message}} @@ -71,7 +71,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 1593ac81295d1..ddcccf0db57f3 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 d054462ebcedd..fa4da1614cc5e 100644 --- a/templates/repo/graph/commits.tmpl +++ b/templates/repo/graph/commits.tmpl @@ -28,7 +28,7 @@ {{- end -}} - {{RenderCommitMessage $commit.Subject $.RepoLink $.Repository.ComposeMetas}} + {{RenderCommitMessage $.Context $commit.Subject $.RepoLink $.Repository.ComposeMetas}} {{range $commit.Refs}} {{$refGroup := .RefGroup}} diff --git a/templates/repo/issue/view_title.tmpl b/templates/repo/issue/view_title.tmpl index b43015f1d24a8..07c700afea29b 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}}

- {{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/settings/options.tmpl b/templates/repo/settings/options.tmpl index c911a83c9823a..77b2c239bcec8 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -91,7 +91,7 @@ {{if .Repository.IsMirror}} - {{(MirrorRemoteAddress .Mirror).Address}} + {{(MirrorRemoteAddress $.Context .Mirror).Address}} {{$.i18n.Tr "repo.settings.mirror_settings.direction.pull"}} {{.Mirror.UpdatedUnix.AsTime}} @@ -119,7 +119,7 @@ - {{$address := MirrorRemoteAddress .Mirror}} + {{$address := MirrorRemoteAddress $.Context .Mirror}}
@@ -168,7 +168,7 @@ {{range .Repository.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}} From 363d9bd30daff46ad8582fd2e1a667e15c8ee981 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Tue, 7 Dec 2021 18:13:01 +0000 Subject: [PATCH 39/48] Prevent per request cancellation of hook/proc-receive and hook/post-receive Signed-off-by: Andrew Thornton --- modules/context/private.go | 40 +++++++++++++++++++++++++++++++++++++ routers/private/internal.go | 4 ++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/modules/context/private.go b/modules/context/private.go index 909eb589db806..3e31a7e7d89fc 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 ( @@ -44,3 +74,13 @@ func PrivateContexter() func(http.Handler) http.Handler { }) } } + +// 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/routers/private/internal.go b/routers/private/internal.go index 183ab5e98ac28..4960f561c12b5 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) From 7f620816e1528883fdf5811c912af2fc014f3c9e Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Wed, 8 Dec 2021 17:57:29 +0000 Subject: [PATCH 40/48] Ensure NewPullRequest works correctly even if cancelled Signed-off-by: Andrew Thornton --- models/db/context.go | 7 ++++++- models/db/engine.go | 1 + models/pull.go | 4 +++- services/pull/pull.go | 15 +++++++++++---- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/models/db/context.go b/models/db/context.go index 55e38ba7e5047..833c26ff6c6fe 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 63e1d5547a275..665808d701768 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/pull.go b/models/pull.go index aa58322bcebcf..df971708fd6db 100644 --- a/models/pull.go +++ b/models/pull.go @@ -6,6 +6,7 @@ package models import ( + "context" "fmt" "io" "strings" @@ -452,7 +453,7 @@ func (pr *PullRequest) SetMerged() (bool, error) { } // NewPullRequest creates new pull request with labels for repository. -func NewPullRequest(repo *Repository, issue *Issue, labelIDs []int64, uuids []string, pr *PullRequest) (err error) { +func NewPullRequest(outerCtx context.Context, repo *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) @@ -465,6 +466,7 @@ func NewPullRequest(repo *Repository, issue *Issue, labelIDs []int64, uuids []st return err } defer committer.Close() + ctx.WithContext(outerCtx) if err = newIssue(ctx, issue.Poster, NewIssueOptions{ Repo: repo, diff --git a/services/pull/pull.go b/services/pull/pull.go index 7430a5a32b025..690733dd2f6b8 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -21,6 +21,7 @@ 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" ) @@ -38,7 +39,7 @@ func NewPullRequest(ctx context.Context, repo *models.Repository, pull *models.I 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 } @@ -51,10 +52,16 @@ func NewPullRequest(ctx context.Context, repo *models.Repository, pull *models.I 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(ctx, pr) + err = PushToBaseRepo(prCtx, pr) } else { - err = UpdateRef(ctx, pr) + err = UpdateRef(prCtx, pr) } if err != nil { return err @@ -74,7 +81,7 @@ func NewPullRequest(ctx context.Context, repo *models.Repository, pull *models.I } // add first push codes comment - baseGitRepo, err := git.OpenRepositoryCtx(ctx, pr.BaseRepo.RepoPath()) + baseGitRepo, err := git.OpenRepositoryCtx(prCtx, pr.BaseRepo.RepoPath()) if err != nil { return err } From 8ab620eb5ca167e81ed9480e1d2a5ee82300cbf8 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Wed, 8 Dec 2021 18:09:40 +0000 Subject: [PATCH 41/48] fix text Signed-off-by: Andrew Thornton --- models/issue_xref_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/issue_xref_test.go b/models/issue_xref_test.go index 06f937c80d719..a16e10c184ef3 100644 --- a/models/issue_xref_test.go +++ b/models/issue_xref_test.go @@ -161,7 +161,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 } From 5b894d2c2bbdfcbdacd336d7f3d3fe17bc92f8b5 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Mon, 13 Dec 2021 19:13:50 +0000 Subject: [PATCH 42/48] missed one Signed-off-by: Andrew Thornton --- models/repo/repo.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/models/repo/repo.go b/models/repo/repo.go index 8907691dde3c1..bc47f0accb4b4 100644 --- a/models/repo/repo.go +++ b/models/repo/repo.go @@ -477,8 +477,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) From 56764de3249f67ce55dc009d8075fdc050a72b22 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Wed, 15 Dec 2021 17:17:37 +0000 Subject: [PATCH 43/48] remove review.loadAttributes as it is no longer needed Signed-off-by: Andrew Thornton --- models/review.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/models/review.go b/models/review.go index 0f920082ac307..aa9611ca99f50 100644 --- a/models/review.go +++ b/models/review.go @@ -137,7 +137,8 @@ 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 @@ -154,11 +155,6 @@ func (r *Review) loadAttributes(ctx context.Context) (err error) { return } -// LoadAttributes loads all attributes except CodeComments -func (r *Review) LoadAttributes(ctx context.Context) error { - return r.loadAttributes(ctx) -} - func getReviewByID(e db.Engine, id int64) (*Review, error) { review := new(Review) if has, err := e.ID(id).Get(review); err != nil { From c82263e56095e4c166e828a23c36522a01e67576 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Tue, 21 Dec 2021 11:01:16 +0000 Subject: [PATCH 44/48] replace review.loadCodeComments with LoadCodeComments Signed-off-by: Andrew Thornton --- models/review.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/models/review.go b/models/review.go index aa9611ca99f50..e34fca59f8506 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(ctx context.Context) error { - return r.loadCodeComments(ctx) -} - func (r *Review) loadIssue(e db.Engine) (err error) { if r.Issue != nil { return @@ -143,7 +139,7 @@ func (r *Review) LoadAttributes(ctx context.Context) (err error) { 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 { @@ -401,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 { From 7563316d0095ecdf29a7b5e5660efebb85ad6904 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Tue, 21 Dec 2021 18:22:50 +0000 Subject: [PATCH 45/48] as per review Signed-off-by: Andrew Thornton --- models/issue_comment.go | 23 ++++++----------- models/repo_activity.go | 24 ++++++------------ modules/git/diff.go | 12 +++------ modules/git/repo_base.go | 20 ++++++++++++++- modules/git/repo_base_gogit.go | 3 ++- modules/git/repo_base_nogogit.go | 3 ++- routers/api/v1/repo/pull.go | 13 ++++------ routers/api/v1/repo/pull_review.go | 14 +++++----- services/pull/commit_status.go | 13 +++------- services/pull/patch.go | 12 +++------ services/pull/pull.go | 38 +++++++++++----------------- services/pull/review.go | 12 +++------ services/release/release.go | 11 +++----- services/repository/files/commit.go | 12 +++------ services/repository/files/content.go | 36 +++++++++----------------- services/repository/files/delete.go | 12 +++------ services/repository/files/update.go | 12 +++------ 17 files changed, 109 insertions(+), 161 deletions(-) diff --git a/models/issue_comment.go b/models/issue_comment.go index b3d968d0076a8..603f2b62111e5 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -706,14 +706,11 @@ func (c *Comment) LoadPushCommits(ctx context.Context) (err error) { c.NewCommit = data.CommitIDs[1] } else { repoPath := c.Issue.Repo.RepoPath() - gitRepo := git.RepositoryFromContext(ctx, repoPath) - if gitRepo == nil { - gitRepo, err = git.OpenRepositoryCtx(ctx, repoPath) - if err != nil { - return err - } - defer gitRepo.Close() + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repoPath) + if err != nil { + return err } + defer closer.Close() c.Commits = ConvertFromGitCommit(gitRepo.GetCommitsFromIDs(data.CommitIDs), c.Issue.Repo) c.CommitsNum = int64(len(c.Commits)) @@ -1320,15 +1317,11 @@ func CreatePushPullComment(ctx context.Context, pusher *user_model.User, pr *Pul // Commit on baseBranch will skip func getCommitIDsFromRepo(ctx context.Context, repo *repo_model.Repository, oldCommitID, newCommitID, baseBranch string) (commitIDs []string, isForcePush bool, err error) { repoPath := repo.RepoPath() - gitRepo := git.RepositoryFromContext(ctx, repoPath) - if gitRepo == nil { - var err error - gitRepo, err = git.OpenRepositoryCtx(ctx, repoPath) - if err != nil { - return nil, false, err - } - defer gitRepo.Close() + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repoPath) + if err != nil { + return nil, false, err } + defer closer.Close() oldCommit, err := gitRepo.GetCommit(oldCommitID) if err != nil { diff --git a/models/repo_activity.go b/models/repo_activity.go index e683ffcec1a62..7475be2b116d3 100644 --- a/models/repo_activity.go +++ b/models/repo_activity.go @@ -65,15 +65,11 @@ func GetActivityStats(ctx context.Context, repo *repo_model.Repository, timeFrom return nil, fmt.Errorf("FillUnresolvedIssues: %v", err) } if code { - gitRepo := git.RepositoryFromContext(ctx, repo.RepoPath()) - if gitRepo == nil { - var err error - gitRepo, err = git.OpenRepositoryCtx(ctx, repo.RepoPath()) - if err != nil { - return nil, fmt.Errorf("OpenRepository: %v", err) - } - defer gitRepo.Close() + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) + if err != nil { + return nil, fmt.Errorf("OpenRepository: %v", err) } + defer closer.Close() code, err := gitRepo.GetCodeActivityStats(timeFrom, repo.DefaultBranch) if err != nil { @@ -86,15 +82,11 @@ func GetActivityStats(ctx context.Context, repo *repo_model.Repository, timeFrom // GetActivityStatsTopAuthors returns top author stats for git commits for all branches func GetActivityStatsTopAuthors(ctx context.Context, repo *repo_model.Repository, timeFrom time.Time, count int) ([]*ActivityAuthorData, error) { - gitRepo := git.RepositoryFromContext(ctx, repo.RepoPath()) - if gitRepo == nil { - var err error - gitRepo, err = git.OpenRepositoryCtx(ctx, repo.RepoPath()) - if err != nil { - return nil, fmt.Errorf("OpenRepository: %v", err) - } - defer gitRepo.Close() + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) + if err != nil { + return nil, fmt.Errorf("OpenRepository: %v", err) } + defer closer.Close() code, err := gitRepo.GetCodeActivityStats(timeFrom, "") if err != nil { diff --git a/modules/git/diff.go b/modules/git/diff.go index 03756c9fcc0bd..02ed2c60c42da 100644 --- a/modules/git/diff.go +++ b/modules/git/diff.go @@ -36,15 +36,11 @@ func GetRawDiff(ctx context.Context, repoPath, commitID string, diffType RawDiff // GetRawDiffForFile dumps diff results of file in given commit ID to io.Writer. func GetRawDiffForFile(ctx context.Context, repoPath, startCommit, endCommit string, diffType RawDiffType, file string, writer io.Writer) error { - repo := RepositoryFromContext(ctx, repoPath) - if repo == nil { - var err error - repo, err = OpenRepositoryCtx(ctx, repoPath) - if err != nil { - return fmt.Errorf("OpenRepository: %v", err) - } - defer repo.Close() + repo, closer, err := RepositoryFromContextOrOpen(ctx, repoPath) + if err != nil { + return fmt.Errorf("OpenRepository: %v", err) } + defer closer.Close() return GetRepoRawDiffForFile(repo, startCommit, endCommit, diffType, file, writer) } diff --git a/modules/git/repo_base.go b/modules/git/repo_base.go index bfa9cb17944af..8c2f19f6d7be4 100644 --- a/modules/git/repo_base.go +++ b/modules/git/repo_base.go @@ -4,7 +4,10 @@ package git -import "context" +import ( + "context" + "io" +) // contextKey is a value for use with context.WithValue. type contextKey struct { @@ -29,3 +32,18 @@ func RepositoryFromContext(ctx context.Context, path string) *Repository { 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 72995265622e5..23009d80f7d58 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 e264fd4a14132..ad4a00be1f82f 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/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 297fcaded1950..379fd07c477d4 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -1193,15 +1193,12 @@ func GetPullRequestCommits(ctx *context.APIContext) { } var prInfo *git.CompareInfo - baseGitRepo := git.RepositoryFromContext(ctx, pr.BaseRepo.RepoPath()) - if baseGitRepo == nil { - baseGitRepo, err = git.OpenRepositoryCtx(ctx, pr.BaseRepo.RepoPath()) - if err != nil { - ctx.ServerError("OpenRepository", err) - return - } - defer baseGitRepo.Close() + baseGitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, pr.BaseRepo.RepoPath()) + if err != nil { + ctx.ServerError("OpenRepository", err) + return } + defer closer.Close() if pr.HasMerged { prInfo, err = baseGitRepo.GetCompareInfo(pr.BaseRepo.RepoPath(), pr.MergeBase, pr.GetGitRefName(), true, false) diff --git a/routers/api/v1/repo/pull_review.go b/routers/api/v1/repo/pull_review.go index bb42fe3376f8f..9eb63bafadb11 100644 --- a/routers/api/v1/repo/pull_review.go +++ b/routers/api/v1/repo/pull_review.go @@ -328,15 +328,13 @@ func CreatePullReview(ctx *context.APIContext) { // if CommitID is empty, set it as lastCommitID if opts.CommitID == "" { - gitRepo := git.RepositoryFromContext(ctx, pr.Issue.Repo.RepoPath()) - if gitRepo == nil { - gitRepo, err = git.OpenRepositoryCtx(ctx, pr.Issue.Repo.RepoPath()) - if err != nil { - ctx.Error(http.StatusInternalServerError, "git.OpenRepository", err) - return - } - defer gitRepo.Close() + + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, pr.Issue.Repo.RepoPath()) + if err != nil { + ctx.Error(http.StatusInternalServerError, "git.OpenRepository", err) + return } + defer closer.Close() headCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName()) if err != nil { diff --git a/services/pull/commit_status.go b/services/pull/commit_status.go index 1ccb9493e5399..d605f8e301119 100644 --- a/services/pull/commit_status.go +++ b/services/pull/commit_status.go @@ -105,15 +105,11 @@ func GetPullRequestCommitStatusState(ctx context.Context, pr *models.PullRequest } // check if all required status checks are successful - headGitRepo := git.RepositoryFromContext(ctx, pr.HeadRepo.RepoPath()) - if headGitRepo == nil { - var err error - headGitRepo, err = git.OpenRepositoryCtx(ctx, pr.HeadRepo.RepoPath()) - if err != nil { - return "", errors.Wrap(err, "OpenRepository") - } - defer headGitRepo.Close() + headGitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, pr.HeadRepo.RepoPath()) + if err != nil { + return "", errors.Wrap(err, "OpenRepository") } + defer closer.Close() if pr.Flow == models.PullRequestFlowGithub && !headGitRepo.IsBranchExist(pr.HeadBranch) { return "", errors.New("Head branch does not exist, can not merge") @@ -123,7 +119,6 @@ func GetPullRequestCommitStatusState(ctx context.Context, pr *models.PullRequest } var sha string - var err error if pr.Flow == models.PullRequestFlowGithub { sha, err = headGitRepo.GetBranchCommitID(pr.HeadBranch) } else { diff --git a/services/pull/patch.go b/services/pull/patch.go index 4af650395f25c..cd12e63124aab 100644 --- a/services/pull/patch.go +++ b/services/pull/patch.go @@ -32,15 +32,11 @@ func DownloadDiffOrPatch(ctx context.Context, pr *models.PullRequest, w io.Write return err } - gitRepo := git.RepositoryFromContext(ctx, pr.BaseRepo.RepoPath()) - if gitRepo == nil { - var err error - gitRepo, err = git.OpenRepositoryCtx(ctx, pr.BaseRepo.RepoPath()) - if err != nil { - return fmt.Errorf("OpenRepository: %v", err) - } - defer gitRepo.Close() + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, pr.BaseRepo.RepoPath()) + if err != nil { + return fmt.Errorf("OpenRepository: %v", err) } + 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) diff --git a/services/pull/pull.go b/services/pull/pull.go index d3ec8440ba089..a4de235ba9251 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -9,6 +9,7 @@ import ( "bytes" "context" "fmt" + "io" "regexp" "strings" "time" @@ -590,20 +591,14 @@ func GetSquashMergeCommitMessages(ctx context.Context, pr *models.PullRequest) s } } - gitRepo := git.RepositoryFromContext(ctx, pr.HeadRepo.RepoPath()) - if gitRepo == nil { - var err error - gitRepo, err = git.OpenRepositoryCtx(ctx, pr.HeadRepo.RepoPath()) - if err != nil { - log.Error("Unable to open head repository: Error: %v", err) - return "" - } - defer gitRepo.Close() + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, pr.HeadRepo.RepoPath()) + if err != nil { + log.Error("Unable to open head repository: Error: %v", err) + return "" } + defer closer.Close() var headCommit *git.Commit - var err error - if pr.Flow == models.PullRequestFlowGithub { headCommit, err = gitRepo.GetBranchCommit(pr.HeadBranch) } else { @@ -795,15 +790,11 @@ func IsHeadEqualWithBranch(ctx context.Context, pr *models.PullRequest, branchNa if err = pr.LoadBaseRepo(); err != nil { return false, err } - baseGitRepo := git.RepositoryFromContext(ctx, pr.BaseRepo.RepoPath()) - if baseGitRepo == nil { - var err error - baseGitRepo, err = git.OpenRepositoryCtx(ctx, pr.BaseRepo.RepoPath()) - if err != nil { - return false, err - } - defer baseGitRepo.Close() + baseGitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, pr.BaseRepo.RepoPath()) + if err != nil { + return false, err } + defer closer.Close() baseCommit, err := baseGitRepo.GetBranchCommit(branchName) if err != nil { @@ -816,13 +807,14 @@ func IsHeadEqualWithBranch(ctx context.Context, pr *models.PullRequest, branchNa var headGitRepo *git.Repository if pr.HeadRepoID == pr.BaseRepoID { headGitRepo = baseGitRepo - } else if headGitRepo := git.RepositoryFromContext(ctx, pr.HeadRepo.RepoPath()); headGitRepo == nil { - var err error - headGitRepo, err = git.OpenRepositoryCtx(ctx, pr.HeadRepo.RepoPath()) + } else { + var closer io.Closer + + headGitRepo, closer, err = git.RepositoryFromContextOrOpen(ctx, pr.HeadRepo.RepoPath()) if err != nil { return false, err } - defer headGitRepo.Close() + defer closer.Close() } var headCommit *git.Commit diff --git a/services/pull/review.go b/services/pull/review.go index 3b98e9b095415..0db3168895c39 100644 --- a/services/pull/review.go +++ b/services/pull/review.go @@ -126,15 +126,11 @@ func createCodeComment(ctx context.Context, doer *user_model.User, repo *repo_mo if err := pr.LoadBaseRepo(); err != nil { return nil, fmt.Errorf("LoadHeadRepo: %v", err) } - gitRepo := git.RepositoryFromContext(ctx, pr.BaseRepo.RepoPath()) - if gitRepo == nil { - var err error - gitRepo, err = git.OpenRepositoryCtx(ctx, pr.BaseRepo.RepoPath()) - if err != nil { - return nil, fmt.Errorf("OpenRepository: %v", err) - } - defer gitRepo.Close() + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, pr.BaseRepo.RepoPath()) + if err != nil { + return nil, fmt.Errorf("OpenRepository: %v", err) } + defer closer.Close() invalidated := false head := pr.GetGitRefName() diff --git a/services/release/release.go b/services/release/release.go index ff146d7f66ff4..11795a1243bd9 100644 --- a/services/release/release.go +++ b/services/release/release.go @@ -148,14 +148,11 @@ func CreateNewTag(ctx context.Context, doer *user_model.User, repo *repo_model.R } } - gitRepo := git.RepositoryFromContext(ctx, repo.RepoPath()) - if gitRepo == nil { - gitRepo, err = git.OpenRepositoryCtx(ctx, repo.RepoPath()) - if err != nil { - return err - } - defer gitRepo.Close() + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) + if err != nil { + return err } + defer closer.Close() rel := &models.Release{ RepoID: repo.ID, diff --git a/services/repository/files/commit.go b/services/repository/files/commit.go index 2a6f3a8d0435c..e7604e3f924d7 100644 --- a/services/repository/files/commit.go +++ b/services/repository/files/commit.go @@ -23,15 +23,11 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creato repoPath := repo.RepoPath() // confirm that commit is exist - gitRepo := git.RepositoryFromContext(ctx, repo.RepoPath()) - if gitRepo == nil { - var err error - gitRepo, err = git.OpenRepositoryCtx(ctx, repo.RepoPath()) - if err != nil { - return fmt.Errorf("OpenRepository[%s]: %v", repoPath, err) - } - defer gitRepo.Close() + 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() diff --git a/services/repository/files/content.go b/services/repository/files/content.go index 9416ae325bddf..9037a84349e5a 100644 --- a/services/repository/files/content.go +++ b/services/repository/files/content.go @@ -58,15 +58,11 @@ func GetContentsOrList(ctx context.Context, repo *repo_model.Repository, treePat } treePath = cleanTreePath - gitRepo := git.RepositoryFromContext(ctx, repo.RepoPath()) - if gitRepo == nil { - var err error - gitRepo, err = git.OpenRepositoryCtx(ctx, repo.RepoPath()) - if err != nil { - return nil, err - } - defer gitRepo.Close() + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) + if err != nil { + return nil, err } + defer closer.Close() // Get the commit object for the ref commit, err := gitRepo.GetCommit(ref) @@ -121,15 +117,11 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref } treePath = cleanTreePath - gitRepo := git.RepositoryFromContext(ctx, repo.RepoPath()) - if gitRepo == nil { - var err error - gitRepo, err = git.OpenRepositoryCtx(ctx, repo.RepoPath()) - if err != nil { - return nil, err - } - defer gitRepo.Close() + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) + if err != nil { + return nil, err } + defer closer.Close() // Get the commit object for the ref commit, err := gitRepo.GetCommit(ref) @@ -229,15 +221,11 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref // GetBlobBySHA get the GitBlobResponse of a repository using a sha hash. func GetBlobBySHA(ctx context.Context, repo *repo_model.Repository, sha string) (*api.GitBlobResponse, error) { - gitRepo := git.RepositoryFromContext(ctx, repo.RepoPath()) - if gitRepo == nil { - var err error - gitRepo, err = git.OpenRepositoryCtx(ctx, repo.RepoPath()) - if err != nil { - return nil, err - } - defer gitRepo.Close() + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) + if err != nil { + return nil, err } + defer closer.Close() gitBlob, err := gitRepo.GetBlob(sha) if err != nil { return nil, err diff --git a/services/repository/files/delete.go b/services/repository/files/delete.go index 6889a30216fe2..95d05b5202352 100644 --- a/services/repository/files/delete.go +++ b/services/repository/files/delete.go @@ -40,15 +40,11 @@ func DeleteRepoFile(ctx context.Context, repo *repo_model.Repository, doer *user opts.NewBranch = opts.OldBranch } - gitRepo := git.RepositoryFromContext(ctx, repo.RepoPath()) - if gitRepo == nil { - var err error - gitRepo, err = git.OpenRepositoryCtx(ctx, repo.RepoPath()) - if err != nil { - return nil, err - } - defer gitRepo.Close() + 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 := gitRepo.GetBranch(opts.OldBranch); err != nil { diff --git a/services/repository/files/update.go b/services/repository/files/update.go index 06fa05f06a76e..5a77969e257ba 100644 --- a/services/repository/files/update.go +++ b/services/repository/files/update.go @@ -134,15 +134,11 @@ func CreateOrUpdateRepoFile(ctx context.Context, repo *repo_model.Repository, do opts.NewBranch = opts.OldBranch } - gitRepo := git.RepositoryFromContext(ctx, repo.RepoPath()) - if gitRepo == nil { - var err error - gitRepo, err = git.OpenRepositoryCtx(ctx, repo.RepoPath()) - if err != nil { - return nil, err - } - defer gitRepo.Close() + 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 := gitRepo.GetBranch(opts.OldBranch); err != nil { From 1bbbcaea221802844abc55b9db60c45032f7f941 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Tue, 21 Dec 2021 18:26:36 +0000 Subject: [PATCH 46/48] remove openrepo as git repo exists check Signed-off-by: Andrew Thornton --- modules/git/blame.go | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/modules/git/blame.go b/modules/git/blame.go index 208a600dc89d7..40e3d4e885d29 100644 --- a/modules/git/blame.go +++ b/modules/git/blame.go @@ -114,16 +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 := RepositoryFromContext(ctx, repoPath) - if gitRepo == nil { - var err error - gitRepo, err = OpenRepositoryCtx(ctx, repoPath) - if err != nil { - return nil, err - } - gitRepo.Close() - } - return createBlameReader(ctx, repoPath, GitExecutable, "blame", commitID, "--porcelain", "--", file) } From ccd0b97a0868ddd6743e5dff790a7fa32ebe80ea Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sat, 8 Jan 2022 11:12:55 +0000 Subject: [PATCH 47/48] rename shasum refid Signed-off-by: Andrew Thornton --- modules/notification/action/action.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/notification/action/action.go b/modules/notification/action/action.go index 2a358ba3a8f2a..bdf4a509951a5 100644 --- a/modules/notification/action/action.go +++ b/modules/notification/action/action.go @@ -335,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, shaSum 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. @@ -394,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, shaSum 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(), From d387d52400d2c82e81003099b92dd6ba8b54ad85 Mon Sep 17 00:00:00 2001 From: zeripath Date: Thu, 13 Jan 2022 20:05:02 +0000 Subject: [PATCH 48/48] Update routers/private/default_branch.go Co-authored-by: 6543 <6543@obermui.de> --- routers/private/default_branch.go | 1 - 1 file changed, 1 deletion(-) diff --git a/routers/private/default_branch.go b/routers/private/default_branch.go index 605539a7d1171..55ffef43f3f0c 100644 --- a/routers/private/default_branch.go +++ b/routers/private/default_branch.go @@ -37,7 +37,6 @@ func SetDefaultBranch(ctx *gitea_context.PrivateContext) { ctx.Repo.Repository.DefaultBranch = branch if err := ctx.Repo.GitRepo.SetDefaultBranch(ctx.Repo.Repository.DefaultBranch); err != nil { if !git.IsErrUnsupportedVersion(err) { - ctx.Repo.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), })