diff options
Diffstat (limited to 'services/repository')
-rw-r--r-- | services/repository/adopt.go | 7 | ||||
-rw-r--r-- | services/repository/archiver/archiver.go | 11 | ||||
-rw-r--r-- | services/repository/branch.go | 38 | ||||
-rw-r--r-- | services/repository/check.go | 2 | ||||
-rw-r--r-- | services/repository/files/commit.go | 11 | ||||
-rw-r--r-- | services/repository/files/content.go | 25 | ||||
-rw-r--r-- | services/repository/files/content_test.go | 36 | ||||
-rw-r--r-- | services/repository/files/delete.go | 20 | ||||
-rw-r--r-- | services/repository/files/diff.go | 5 | ||||
-rw-r--r-- | services/repository/files/diff_test.go | 10 | ||||
-rw-r--r-- | services/repository/files/file.go | 7 | ||||
-rw-r--r-- | services/repository/files/file_test.go | 4 | ||||
-rw-r--r-- | services/repository/files/temp_repo.go | 33 | ||||
-rw-r--r-- | services/repository/files/tree.go | 8 | ||||
-rw-r--r-- | services/repository/files/tree_test.go | 2 | ||||
-rw-r--r-- | services/repository/files/update.go | 24 | ||||
-rw-r--r-- | services/repository/files/upload.go | 5 | ||||
-rw-r--r-- | services/repository/hooks.go | 4 | ||||
-rw-r--r-- | services/repository/push.go | 17 | ||||
-rw-r--r-- | services/repository/repository.go | 5 |
20 files changed, 146 insertions, 128 deletions
diff --git a/services/repository/adopt.go b/services/repository/adopt.go index fc3fdc608f..adc5942a3c 100644 --- a/services/repository/adopt.go +++ b/services/repository/adopt.go @@ -84,7 +84,7 @@ func AdoptRepository(doer, u *user_model.User, opts models.CreateRepoOptions) (* } } - if stdout, err := git.NewCommand("update-server-info"). + if stdout, err := git.NewCommandContext(ctx, "update-server-info"). SetDescription(fmt.Sprintf("CreateRepository(git update-server-info): %s", repoPath)). RunInDir(repoPath); err != nil { log.Error("CreateRepository(git update-server-info) in %v: Stdout: %s\nError: %v", repo, stdout, err) @@ -121,11 +121,14 @@ func adoptRepository(ctx context.Context, repoPath string, u *user_model.User, r } repo.IsEmpty = false - gitRepo, err := git.OpenRepository(repo.RepoPath()) + + // Don't bother looking this repo in the context it won't be there + gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath()) if err != nil { return fmt.Errorf("openRepository: %v", err) } defer gitRepo.Close() + if len(opts.DefaultBranch) > 0 { repo.DefaultBranch = opts.DefaultBranch diff --git a/services/repository/archiver/archiver.go b/services/repository/archiver/archiver.go index 16ee532f78..93ec8bcca2 100644 --- a/services/repository/archiver/archiver.go +++ b/services/repository/archiver/archiver.go @@ -19,6 +19,7 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/queue" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/storage" @@ -115,11 +116,13 @@ func (aReq *ArchiveRequest) GetArchiveName() string { } func doArchive(r *ArchiveRequest) (*repo_model.RepoArchiver, error) { - ctx, committer, err := db.TxContext() + txCtx, committer, err := db.TxContext() if err != nil { return nil, err } defer committer.Close() + ctx, _, finished := process.GetManager().AddContext(txCtx, fmt.Sprintf("ArchiveRequest[%d]: %s", r.RepoID, r.GetArchiveName())) + defer finished() archiver, err := repo_model.GetRepoArchiver(ctx, r.RepoID, r.Type, r.CommitID) if err != nil { @@ -175,7 +178,7 @@ func doArchive(r *ArchiveRequest) (*repo_model.RepoArchiver, error) { return nil, fmt.Errorf("archiver.LoadRepo failed: %v", err) } - gitRepo, err := git.OpenRepository(repo.RepoPath()) + gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath()) if err != nil { return nil, err } @@ -190,13 +193,13 @@ func doArchive(r *ArchiveRequest) (*repo_model.RepoArchiver, error) { if archiver.Type == git.BUNDLE { err = gitRepo.CreateBundle( - graceful.GetManager().ShutdownContext(), + ctx, archiver.CommitID, w, ) } else { err = gitRepo.CreateArchive( - graceful.GetManager().ShutdownContext(), + ctx, archiver.Type, w, setting.Repository.PrefixArchiveFiles, diff --git a/services/repository/branch.go b/services/repository/branch.go index e1775fc12b..b1a6dafb58 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -21,19 +21,19 @@ import ( ) // CreateNewBranch creates a new repository branch -func CreateNewBranch(doer *user_model.User, repo *repo_model.Repository, oldBranchName, branchName string) (err error) { +func CreateNewBranch(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, oldBranchName, branchName string) (err error) { // Check if branch name can be used - if err := checkBranchName(git.DefaultContext, repo, branchName); err != nil { + if err := checkBranchName(ctx, repo, branchName); err != nil { return err } - if !git.IsBranchExist(git.DefaultContext, repo.RepoPath(), oldBranchName) { + if !git.IsBranchExist(ctx, repo.RepoPath(), oldBranchName) { return models.ErrBranchDoesNotExist{ BranchName: oldBranchName, } } - if err := git.Push(git.DefaultContext, repo.RepoPath(), git.PushOptions{ + if err := git.Push(ctx, repo.RepoPath(), git.PushOptions{ Remote: repo.RepoPath(), Branch: fmt.Sprintf("%s:%s%s", oldBranchName, git.BranchPrefix, branchName), Env: models.PushingEnvironment(doer, repo), @@ -47,24 +47,10 @@ func CreateNewBranch(doer *user_model.User, repo *repo_model.Repository, oldBran return nil } -// GetBranch returns a branch by its name -func GetBranch(repo *repo_model.Repository, branch string) (*git.Branch, error) { - if len(branch) == 0 { - return nil, fmt.Errorf("GetBranch: empty string for branch") - } - gitRepo, err := git.OpenRepository(repo.RepoPath()) - if err != nil { - return nil, err - } - defer gitRepo.Close() - - return gitRepo.GetBranch(branch) -} - // GetBranches returns branches from the repository, skipping skip initial branches and // returning at most limit branches, or all branches if limit is 0. -func GetBranches(repo *repo_model.Repository, skip, limit int) ([]*git.Branch, int, error) { - return git.GetBranchesByPath(repo.RepoPath(), skip, limit) +func GetBranches(ctx context.Context, repo *repo_model.Repository, skip, limit int) ([]*git.Branch, int, error) { + return git.GetBranchesByPath(ctx, repo.RepoPath(), skip, limit) } // checkBranchName validates branch name with existing repository branches @@ -98,13 +84,13 @@ func checkBranchName(ctx context.Context, repo *repo_model.Repository, name stri } // CreateNewBranchFromCommit creates a new repository branch -func CreateNewBranchFromCommit(doer *user_model.User, repo *repo_model.Repository, commit, branchName string) (err error) { +func CreateNewBranchFromCommit(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, commit, branchName string) (err error) { // Check if branch name can be used - if err := checkBranchName(git.DefaultContext, repo, branchName); err != nil { + if err := checkBranchName(ctx, repo, branchName); err != nil { return err } - if err := git.Push(git.DefaultContext, repo.RepoPath(), git.PushOptions{ + if err := git.Push(ctx, repo.RepoPath(), git.PushOptions{ Remote: repo.RepoPath(), Branch: fmt.Sprintf("%s:%s%s", commit, git.BranchPrefix, branchName), Env: models.PushingEnvironment(doer, repo), @@ -149,9 +135,13 @@ func RenameBranch(repo *repo_model.Repository, doer *user_model.User, gitRepo *g }); err != nil { return "", err } + refID, err := gitRepo.GetRefCommitID(git.BranchPrefix + to) + if err != nil { + return "", err + } notification.NotifyDeleteRef(doer, repo, "branch", git.BranchPrefix+from) - notification.NotifyCreateRef(doer, repo, "branch", git.BranchPrefix+to) + notification.NotifyCreateRef(doer, repo, "branch", git.BranchPrefix+to, refID) return "", nil } diff --git a/services/repository/check.go b/services/repository/check.go index 7118b006bd..59b8626b46 100644 --- a/services/repository/check.go +++ b/services/repository/check.go @@ -196,7 +196,7 @@ func ReinitMissingRepositories(ctx context.Context) error { default: } log.Trace("Initializing %d/%d...", repo.OwnerID, repo.ID) - if err := git.InitRepository(repo.RepoPath(), true); err != nil { + if err := git.InitRepository(ctx, repo.RepoPath(), true); err != nil { log.Error("Unable (re)initialize repository %d at %s. Error: %v", repo.ID, repo.RepoPath(), err) if err2 := admin_model.CreateRepositoryNotice("InitRepository [%d]: %v", repo.ID, err); err2 != nil { log.Error("CreateRepositoryNotice: %v", err2) diff --git a/services/repository/files/commit.go b/services/repository/files/commit.go index 4e391ff343..e7604e3f92 100644 --- a/services/repository/files/commit.go +++ b/services/repository/files/commit.go @@ -5,6 +5,7 @@ package files import ( + "context" "fmt" "code.gitea.io/gitea/models" @@ -18,14 +19,16 @@ import ( // CreateCommitStatus creates a new CommitStatus given a bunch of parameters // NOTE: All text-values will be trimmed from whitespaces. // Requires: Repo, Creator, SHA -func CreateCommitStatus(repo *repo_model.Repository, creator *user_model.User, sha string, status *models.CommitStatus) error { +func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creator *user_model.User, sha string, status *models.CommitStatus) error { repoPath := repo.RepoPath() // confirm that commit is exist - gitRepo, err := git.OpenRepository(repoPath) + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) if err != nil { return fmt.Errorf("OpenRepository[%s]: %v", repoPath, err) } + defer closer.Close() + if _, err := gitRepo.GetCommit(sha); err != nil { gitRepo.Close() return fmt.Errorf("GetCommit[%s]: %v", sha, err) @@ -45,8 +48,8 @@ func CreateCommitStatus(repo *repo_model.Repository, creator *user_model.User, s } // CountDivergingCommits determines how many commits a branch is ahead or behind the repository's base branch -func CountDivergingCommits(repo *repo_model.Repository, branch string) (*git.DivergeObject, error) { - divergence, err := git.GetDivergingCommits(repo.RepoPath(), repo.DefaultBranch, branch) +func CountDivergingCommits(ctx context.Context, repo *repo_model.Repository, branch string) (*git.DivergeObject, error) { + divergence, err := git.GetDivergingCommits(ctx, repo.RepoPath(), repo.DefaultBranch, branch) if err != nil { return nil, err } diff --git a/services/repository/files/content.go b/services/repository/files/content.go index 10461c82a7..9037a84349 100644 --- a/services/repository/files/content.go +++ b/services/repository/files/content.go @@ -5,6 +5,7 @@ package files import ( + "context" "fmt" "net/url" "path" @@ -39,7 +40,7 @@ func (ct *ContentType) String() string { // GetContentsOrList gets the meta data of a file's contents (*ContentsResponse) if treePath not a tree // directory, otherwise a listing of file contents ([]*ContentsResponse). Ref can be a branch, commit or tag -func GetContentsOrList(repo *repo_model.Repository, treePath, ref string) (interface{}, error) { +func GetContentsOrList(ctx context.Context, repo *repo_model.Repository, treePath, ref string) (interface{}, error) { if repo.IsEmpty { return make([]interface{}, 0), nil } @@ -57,11 +58,11 @@ func GetContentsOrList(repo *repo_model.Repository, treePath, ref string) (inter } treePath = cleanTreePath - gitRepo, err := git.OpenRepository(repo.RepoPath()) + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) if err != nil { return nil, err } - defer gitRepo.Close() + defer closer.Close() // Get the commit object for the ref commit, err := gitRepo.GetCommit(ref) @@ -75,7 +76,7 @@ func GetContentsOrList(repo *repo_model.Repository, treePath, ref string) (inter } if entry.Type() != "tree" { - return GetContents(repo, treePath, origRef, false) + return GetContents(ctx, repo, treePath, origRef, false) } // We are in a directory, so we return a list of FileContentResponse objects @@ -91,7 +92,7 @@ func GetContentsOrList(repo *repo_model.Repository, treePath, ref string) (inter } for _, e := range entries { subTreePath := path.Join(treePath, e.Name()) - fileContentResponse, err := GetContents(repo, subTreePath, origRef, true) + fileContentResponse, err := GetContents(ctx, repo, subTreePath, origRef, true) if err != nil { return nil, err } @@ -101,7 +102,7 @@ func GetContentsOrList(repo *repo_model.Repository, treePath, ref string) (inter } // GetContents gets the meta data on a file's contents. Ref can be a branch, commit or tag -func GetContents(repo *repo_model.Repository, treePath, ref string, forList bool) (*api.ContentsResponse, error) { +func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref string, forList bool) (*api.ContentsResponse, error) { if ref == "" { ref = repo.DefaultBranch } @@ -116,11 +117,11 @@ func GetContents(repo *repo_model.Repository, treePath, ref string, forList bool } treePath = cleanTreePath - gitRepo, err := git.OpenRepository(repo.RepoPath()) + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) if err != nil { return nil, err } - defer gitRepo.Close() + defer closer.Close() // Get the commit object for the ref commit, err := gitRepo.GetCommit(ref) @@ -163,7 +164,7 @@ func GetContents(repo *repo_model.Repository, treePath, ref string, forList bool // Now populate the rest of the ContentsResponse based on entry type if entry.IsRegular() || entry.IsExecutable() { contentsResponse.Type = string(ContentTypeRegular) - if blobResponse, err := GetBlobBySHA(repo, entry.ID.String()); err != nil { + if blobResponse, err := GetBlobBySHA(ctx, repo, entry.ID.String()); err != nil { return nil, err } else if !forList { // We don't show the content if we are getting a list of FileContentResponses @@ -219,12 +220,12 @@ func GetContents(repo *repo_model.Repository, treePath, ref string, forList bool } // GetBlobBySHA get the GitBlobResponse of a repository using a sha hash. -func GetBlobBySHA(repo *repo_model.Repository, sha string) (*api.GitBlobResponse, error) { - gitRepo, err := git.OpenRepository(repo.RepoPath()) +func GetBlobBySHA(ctx context.Context, repo *repo_model.Repository, sha string) (*api.GitBlobResponse, error) { + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) if err != nil { return nil, err } - defer gitRepo.Close() + defer closer.Close() gitBlob, err := gitRepo.GetBlob(sha) if err != nil { return nil, err diff --git a/services/repository/files/content_test.go b/services/repository/files/content_test.go index 68b29b1daa..c21aa31d32 100644 --- a/services/repository/files/content_test.go +++ b/services/repository/files/content_test.go @@ -63,14 +63,14 @@ func TestGetContents(t *testing.T) { expectedContentsResponse := getExpectedReadmeContentsResponse() - t.Run("Get README.md contents with GetContents()", func(t *testing.T) { - fileContentResponse, err := GetContents(ctx.Repo.Repository, treePath, ref, false) + t.Run("Get README.md contents with GetContents(ctx, )", func(t *testing.T) { + fileContentResponse, err := GetContents(ctx, ctx.Repo.Repository, treePath, ref, false) assert.EqualValues(t, expectedContentsResponse, fileContentResponse) assert.NoError(t, err) }) - t.Run("Get README.md contents with ref as empty string (should then use the repo's default branch) with GetContents()", func(t *testing.T) { - fileContentResponse, err := GetContents(ctx.Repo.Repository, treePath, "", false) + t.Run("Get README.md contents with ref as empty string (should then use the repo's default branch) with GetContents(ctx, )", func(t *testing.T) { + fileContentResponse, err := GetContents(ctx, ctx.Repo.Repository, treePath, "", false) assert.EqualValues(t, expectedContentsResponse, fileContentResponse) assert.NoError(t, err) }) @@ -98,14 +98,14 @@ func TestGetContentsOrListForDir(t *testing.T) { readmeContentsResponse, } - t.Run("Get root dir contents with GetContentsOrList()", func(t *testing.T) { - fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, ref) + t.Run("Get root dir contents with GetContentsOrList(ctx, )", func(t *testing.T) { + fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref) assert.EqualValues(t, expectedContentsListResponse, fileContentResponse) assert.NoError(t, err) }) - t.Run("Get root dir contents with ref as empty string (should then use the repo's default branch) with GetContentsOrList()", func(t *testing.T) { - fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, "") + t.Run("Get root dir contents with ref as empty string (should then use the repo's default branch) with GetContentsOrList(ctx, )", func(t *testing.T) { + fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, "") assert.EqualValues(t, expectedContentsListResponse, fileContentResponse) assert.NoError(t, err) }) @@ -126,14 +126,14 @@ func TestGetContentsOrListForFile(t *testing.T) { expectedContentsResponse := getExpectedReadmeContentsResponse() - t.Run("Get README.md contents with GetContentsOrList()", func(t *testing.T) { - fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, ref) + t.Run("Get README.md contents with GetContentsOrList(ctx, )", func(t *testing.T) { + fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref) assert.EqualValues(t, expectedContentsResponse, fileContentResponse) assert.NoError(t, err) }) - t.Run("Get README.md contents with ref as empty string (should then use the repo's default branch) with GetContentsOrList()", func(t *testing.T) { - fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, "") + t.Run("Get README.md contents with ref as empty string (should then use the repo's default branch) with GetContentsOrList(ctx, )", func(t *testing.T) { + fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, "") assert.EqualValues(t, expectedContentsResponse, fileContentResponse) assert.NoError(t, err) }) @@ -155,7 +155,7 @@ func TestGetContentsErrors(t *testing.T) { t.Run("bad treePath", func(t *testing.T) { badTreePath := "bad/tree.md" - fileContentResponse, err := GetContents(repo, badTreePath, ref, false) + fileContentResponse, err := GetContents(ctx, repo, badTreePath, ref, false) assert.Error(t, err) assert.EqualError(t, err, "object does not exist [id: , rel_path: bad]") assert.Nil(t, fileContentResponse) @@ -163,7 +163,7 @@ func TestGetContentsErrors(t *testing.T) { t.Run("bad ref", func(t *testing.T) { badRef := "bad_ref" - fileContentResponse, err := GetContents(repo, treePath, badRef, false) + fileContentResponse, err := GetContents(ctx, repo, treePath, badRef, false) assert.Error(t, err) assert.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]") assert.Nil(t, fileContentResponse) @@ -186,7 +186,7 @@ func TestGetContentsOrListErrors(t *testing.T) { t.Run("bad treePath", func(t *testing.T) { badTreePath := "bad/tree.md" - fileContentResponse, err := GetContentsOrList(repo, badTreePath, ref) + fileContentResponse, err := GetContentsOrList(ctx, repo, badTreePath, ref) assert.Error(t, err) assert.EqualError(t, err, "object does not exist [id: , rel_path: bad]") assert.Nil(t, fileContentResponse) @@ -194,7 +194,7 @@ func TestGetContentsOrListErrors(t *testing.T) { t.Run("bad ref", func(t *testing.T) { badRef := "bad_ref" - fileContentResponse, err := GetContentsOrList(repo, treePath, badRef) + fileContentResponse, err := GetContentsOrList(ctx, repo, treePath, badRef) assert.Error(t, err) assert.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]") assert.Nil(t, fileContentResponse) @@ -213,7 +213,7 @@ func TestGetContentsOrListOfEmptyRepos(t *testing.T) { repo := ctx.Repo.Repository t.Run("empty repo", func(t *testing.T) { - contents, err := GetContentsOrList(repo, "", "") + contents, err := GetContentsOrList(ctx, repo, "", "") assert.NoError(t, err) assert.Empty(t, contents) }) @@ -232,7 +232,7 @@ func TestGetBlobBySHA(t *testing.T) { ctx.SetParams(":id", "1") ctx.SetParams(":sha", sha) - gbr, err := GetBlobBySHA(ctx.Repo.Repository, ctx.Params(":sha")) + gbr, err := GetBlobBySHA(ctx, ctx.Repo.Repository, ctx.Params(":sha")) expectedGBR := &api.GitBlobResponse{ Content: "dHJlZSAyYTJmMWQ0NjcwNzI4YTJlMTAwNDllMzQ1YmQ3YTI3NjQ2OGJlYWI2CmF1dGhvciB1c2VyMSA8YWRkcmVzczFAZXhhbXBsZS5jb20+IDE0ODk5NTY0NzkgLTA0MDAKY29tbWl0dGVyIEV0aGFuIEtvZW5pZyA8ZXRoYW50a29lbmlnQGdtYWlsLmNvbT4gMTQ4OTk1NjQ3OSAtMDQwMAoKSW5pdGlhbCBjb21taXQK", Encoding: "base64", diff --git a/services/repository/files/delete.go b/services/repository/files/delete.go index 15208addda..95d05b5202 100644 --- a/services/repository/files/delete.go +++ b/services/repository/files/delete.go @@ -5,6 +5,7 @@ package files import ( + "context" "fmt" "strings" @@ -13,7 +14,6 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" api "code.gitea.io/gitea/modules/structs" - repo_service "code.gitea.io/gitea/services/repository" ) // DeleteRepoFileOptions holds the repository delete file options @@ -31,7 +31,7 @@ type DeleteRepoFileOptions struct { } // DeleteRepoFile deletes a file in the given repository -func DeleteRepoFile(repo *repo_model.Repository, doer *user_model.User, opts *DeleteRepoFileOptions) (*api.FileResponse, error) { +func DeleteRepoFile(ctx context.Context, repo *repo_model.Repository, doer *user_model.User, opts *DeleteRepoFileOptions) (*api.FileResponse, error) { // If no branch name is set, assume the repo's default branch if opts.OldBranch == "" { opts.OldBranch = repo.DefaultBranch @@ -40,8 +40,14 @@ func DeleteRepoFile(repo *repo_model.Repository, doer *user_model.User, opts *De opts.NewBranch = opts.OldBranch } + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) + if err != nil { + return nil, err + } + defer closer.Close() + // oldBranch must exist for this operation - if _, err := repo_service.GetBranch(repo, opts.OldBranch); err != nil { + if _, err := gitRepo.GetBranch(opts.OldBranch); err != nil { return nil, err } @@ -49,7 +55,7 @@ func DeleteRepoFile(repo *repo_model.Repository, doer *user_model.User, opts *De // Check to make sure the branch does not already exist, otherwise we can't proceed. // If we aren't branching to a new branch, make sure user can commit to the given branch if opts.NewBranch != opts.OldBranch { - newBranch, err := repo_service.GetBranch(repo, opts.NewBranch) + newBranch, err := gitRepo.GetBranch(opts.NewBranch) if err != nil && !git.IsErrBranchNotExist(err) { return nil, err } @@ -58,7 +64,7 @@ func DeleteRepoFile(repo *repo_model.Repository, doer *user_model.User, opts *De BranchName: opts.NewBranch, } } - } else if err := VerifyBranchProtection(repo, doer, opts.OldBranch, opts.TreePath); err != nil { + } else if err := VerifyBranchProtection(ctx, repo, doer, opts.OldBranch, opts.TreePath); err != nil { return nil, err } @@ -74,7 +80,7 @@ func DeleteRepoFile(repo *repo_model.Repository, doer *user_model.User, opts *De author, committer := GetAuthorAndCommitterUsers(opts.Author, opts.Committer, doer) - t, err := NewTemporaryUploadRepository(repo) + t, err := NewTemporaryUploadRepository(ctx, repo) if err != nil { return nil, err } @@ -191,7 +197,7 @@ func DeleteRepoFile(repo *repo_model.Repository, doer *user_model.User, opts *De return nil, err } - file, err := GetFileResponseFromCommit(repo, commit, opts.NewBranch, treePath) + file, err := GetFileResponseFromCommit(ctx, repo, commit, opts.NewBranch, treePath) if err != nil { return nil, err } diff --git a/services/repository/files/diff.go b/services/repository/files/diff.go index fb41237e13..dbe1bef52b 100644 --- a/services/repository/files/diff.go +++ b/services/repository/files/diff.go @@ -5,6 +5,7 @@ package files import ( + "context" "strings" repo_model "code.gitea.io/gitea/models/repo" @@ -12,11 +13,11 @@ import ( ) // GetDiffPreview produces and returns diff result of a file which is not yet committed. -func GetDiffPreview(repo *repo_model.Repository, branch, treePath, content string) (*gitdiff.Diff, error) { +func GetDiffPreview(ctx context.Context, repo *repo_model.Repository, branch, treePath, content string) (*gitdiff.Diff, error) { if branch == "" { branch = repo.DefaultBranch } - t, err := NewTemporaryUploadRepository(repo) + t, err := NewTemporaryUploadRepository(ctx, repo) if err != nil { return nil, err } diff --git a/services/repository/files/diff_test.go b/services/repository/files/diff_test.go index 2f256e888c..c0a378dc4b 100644 --- a/services/repository/files/diff_test.go +++ b/services/repository/files/diff_test.go @@ -117,7 +117,7 @@ func TestGetDiffPreview(t *testing.T) { expectedDiff.NumFiles = len(expectedDiff.Files) t.Run("with given branch", func(t *testing.T) { - diff, err := GetDiffPreview(ctx.Repo.Repository, branch, treePath, content) + diff, err := GetDiffPreview(ctx, ctx.Repo.Repository, branch, treePath, content) assert.NoError(t, err) expectedBs, err := json.Marshal(expectedDiff) assert.NoError(t, err) @@ -127,7 +127,7 @@ func TestGetDiffPreview(t *testing.T) { }) t.Run("empty branch, same results", func(t *testing.T) { - diff, err := GetDiffPreview(ctx.Repo.Repository, "", treePath, content) + diff, err := GetDiffPreview(ctx, ctx.Repo.Repository, "", treePath, content) assert.NoError(t, err) expectedBs, err := json.Marshal(expectedDiff) assert.NoError(t, err) @@ -152,20 +152,20 @@ func TestGetDiffPreviewErrors(t *testing.T) { content := "# repo1\n\nDescription for repo1\nthis is a new line" t.Run("empty repo", func(t *testing.T) { - diff, err := GetDiffPreview(&repo_model.Repository{}, branch, treePath, content) + diff, err := GetDiffPreview(ctx, &repo_model.Repository{}, branch, treePath, content) assert.Nil(t, diff) assert.EqualError(t, err, "repository does not exist [id: 0, uid: 0, owner_name: , name: ]") }) t.Run("bad branch", func(t *testing.T) { badBranch := "bad_branch" - diff, err := GetDiffPreview(ctx.Repo.Repository, badBranch, treePath, content) + diff, err := GetDiffPreview(ctx, ctx.Repo.Repository, badBranch, treePath, content) assert.Nil(t, diff) assert.EqualError(t, err, "branch does not exist [name: "+badBranch+"]") }) t.Run("empty treePath", func(t *testing.T) { - diff, err := GetDiffPreview(ctx.Repo.Repository, branch, "", content) + diff, err := GetDiffPreview(ctx, ctx.Repo.Repository, branch, "", content) assert.Nil(t, diff) assert.EqualError(t, err, "path is invalid [path: ]") }) diff --git a/services/repository/files/file.go b/services/repository/files/file.go index ddbced809e..c6c626c9ce 100644 --- a/services/repository/files/file.go +++ b/services/repository/files/file.go @@ -5,6 +5,7 @@ package files import ( + "context" "fmt" "net/url" "path" @@ -18,9 +19,9 @@ import ( ) // GetFileResponseFromCommit Constructs a FileResponse from a Commit object -func GetFileResponseFromCommit(repo *repo_model.Repository, commit *git.Commit, branch, treeName string) (*api.FileResponse, error) { - fileContents, _ := GetContents(repo, treeName, branch, false) // ok if fails, then will be nil - fileCommitResponse, _ := GetFileCommitResponse(repo, commit) // ok if fails, then will be nil +func GetFileResponseFromCommit(ctx context.Context, repo *repo_model.Repository, commit *git.Commit, branch, treeName string) (*api.FileResponse, error) { + fileContents, _ := GetContents(ctx, repo, treeName, branch, false) // ok if fails, then will be nil + fileCommitResponse, _ := GetFileCommitResponse(repo, commit) // ok if fails, then will be nil verification := GetPayloadCommitVerification(commit) fileResponse := &api.FileResponse{ Content: fileContents, diff --git a/services/repository/files/file_test.go b/services/repository/files/file_test.go index 25303a6d00..24e4949832 100644 --- a/services/repository/files/file_test.go +++ b/services/repository/files/file_test.go @@ -109,12 +109,12 @@ func TestGetFileResponseFromCommit(t *testing.T) { repo := ctx.Repo.Repository branch := repo.DefaultBranch treePath := "README.md" - gitRepo, _ := git.OpenRepository(repo.RepoPath()) + gitRepo, _ := git.OpenRepositoryCtx(ctx, repo.RepoPath()) defer gitRepo.Close() commit, _ := gitRepo.GetBranchCommit(branch) expectedFileResponse := getExpectedFileResponse() - fileResponse, err := GetFileResponseFromCommit(repo, commit, branch, treePath) + fileResponse, err := GetFileResponseFromCommit(ctx, repo, commit, branch, treePath) assert.NoError(t, err) assert.EqualValues(t, expectedFileResponse, fileResponse) } diff --git a/services/repository/files/temp_repo.go b/services/repository/files/temp_repo.go index bc761e3101..229b9ffe80 100644 --- a/services/repository/files/temp_repo.go +++ b/services/repository/files/temp_repo.go @@ -26,18 +26,19 @@ import ( // TemporaryUploadRepository is a type to wrap our upload repositories as a shallow clone type TemporaryUploadRepository struct { + ctx context.Context repo *repo_model.Repository gitRepo *git.Repository basePath string } // NewTemporaryUploadRepository creates a new temporary upload repository -func NewTemporaryUploadRepository(repo *repo_model.Repository) (*TemporaryUploadRepository, error) { +func NewTemporaryUploadRepository(ctx context.Context, repo *repo_model.Repository) (*TemporaryUploadRepository, error) { basePath, err := models.CreateTemporaryPath("upload") if err != nil { return nil, err } - t := &TemporaryUploadRepository{repo: repo, basePath: basePath} + t := &TemporaryUploadRepository{ctx: ctx, repo: repo, basePath: basePath} return t, nil } @@ -51,7 +52,7 @@ func (t *TemporaryUploadRepository) Close() { // Clone the base repository to our path and set branch as the HEAD func (t *TemporaryUploadRepository) Clone(branch string) error { - if _, err := git.NewCommand("clone", "-s", "--bare", "-b", branch, t.repo.RepoPath(), t.basePath).Run(); err != nil { + if _, err := git.NewCommandContext(t.ctx, "clone", "-s", "--bare", "-b", branch, t.repo.RepoPath(), t.basePath).Run(); err != nil { stderr := err.Error() if matched, _ := regexp.MatchString(".*Remote branch .* not found in upstream origin.*", stderr); matched { return git.ErrBranchNotExist{ @@ -68,7 +69,7 @@ func (t *TemporaryUploadRepository) Clone(branch string) error { return fmt.Errorf("Clone: %v %s", err, stderr) } } - gitRepo, err := git.OpenRepository(t.basePath) + gitRepo, err := git.OpenRepositoryCtx(t.ctx, t.basePath) if err != nil { return err } @@ -78,7 +79,7 @@ func (t *TemporaryUploadRepository) Clone(branch string) error { // SetDefaultIndex sets the git index to our HEAD func (t *TemporaryUploadRepository) SetDefaultIndex() error { - if _, err := git.NewCommand("read-tree", "HEAD").RunInDir(t.basePath); err != nil { + if _, err := git.NewCommandContext(t.ctx, "read-tree", "HEAD").RunInDir(t.basePath); err != nil { return fmt.Errorf("SetDefaultIndex: %v", err) } return nil @@ -96,7 +97,7 @@ func (t *TemporaryUploadRepository) LsFiles(filenames ...string) ([]string, erro } } - if err := git.NewCommand(cmdArgs...).RunInDirPipeline(t.basePath, stdOut, stdErr); err != nil { + if err := git.NewCommandContext(t.ctx, cmdArgs...).RunInDirPipeline(t.basePath, stdOut, stdErr); err != nil { log.Error("Unable to run git ls-files for temporary repo: %s (%s) Error: %v\nstdout: %s\nstderr: %s", t.repo.FullName(), t.basePath, err, stdOut.String(), stdErr.String()) err = fmt.Errorf("Unable to run git ls-files for temporary repo of: %s Error: %v\nstdout: %s\nstderr: %s", t.repo.FullName(), err, stdOut.String(), stdErr.String()) return nil, err @@ -123,7 +124,7 @@ func (t *TemporaryUploadRepository) RemoveFilesFromIndex(filenames ...string) er } } - if err := git.NewCommand("update-index", "--remove", "-z", "--index-info").RunInDirFullPipeline(t.basePath, stdOut, stdErr, stdIn); err != nil { + if err := git.NewCommandContext(t.ctx, "update-index", "--remove", "-z", "--index-info").RunInDirFullPipeline(t.basePath, stdOut, stdErr, stdIn); err != nil { log.Error("Unable to update-index for temporary repo: %s (%s) Error: %v\nstdout: %s\nstderr: %s", t.repo.FullName(), t.basePath, err, stdOut.String(), stdErr.String()) return fmt.Errorf("Unable to update-index for temporary repo: %s Error: %v\nstdout: %s\nstderr: %s", t.repo.FullName(), err, stdOut.String(), stdErr.String()) } @@ -135,7 +136,7 @@ func (t *TemporaryUploadRepository) HashObject(content io.Reader) (string, error stdOut := new(bytes.Buffer) stdErr := new(bytes.Buffer) - if err := git.NewCommand("hash-object", "-w", "--stdin").RunInDirFullPipeline(t.basePath, stdOut, stdErr, content); err != nil { + if err := git.NewCommandContext(t.ctx, "hash-object", "-w", "--stdin").RunInDirFullPipeline(t.basePath, stdOut, stdErr, content); err != nil { log.Error("Unable to hash-object to temporary repo: %s (%s) Error: %v\nstdout: %s\nstderr: %s", t.repo.FullName(), t.basePath, err, stdOut.String(), stdErr.String()) return "", fmt.Errorf("Unable to hash-object to temporary repo: %s Error: %v\nstdout: %s\nstderr: %s", t.repo.FullName(), err, stdOut.String(), stdErr.String()) } @@ -145,7 +146,7 @@ func (t *TemporaryUploadRepository) HashObject(content io.Reader) (string, error // AddObjectToIndex adds the provided object hash to the index with the provided mode and path func (t *TemporaryUploadRepository) AddObjectToIndex(mode, objectHash, objectPath string) error { - if _, err := git.NewCommand("update-index", "--add", "--replace", "--cacheinfo", mode, objectHash, objectPath).RunInDir(t.basePath); err != nil { + if _, err := git.NewCommandContext(t.ctx, "update-index", "--add", "--replace", "--cacheinfo", mode, objectHash, objectPath).RunInDir(t.basePath); err != nil { stderr := err.Error() if matched, _ := regexp.MatchString(".*Invalid path '.*", stderr); matched { return models.ErrFilePathInvalid{ @@ -161,7 +162,7 @@ func (t *TemporaryUploadRepository) AddObjectToIndex(mode, objectHash, objectPat // WriteTree writes the current index as a tree to the object db and returns its hash func (t *TemporaryUploadRepository) WriteTree() (string, error) { - stdout, err := git.NewCommand("write-tree").RunInDir(t.basePath) + stdout, err := git.NewCommandContext(t.ctx, "write-tree").RunInDir(t.basePath) if err != nil { log.Error("Unable to write tree in temporary repo: %s(%s): Error: %v", t.repo.FullName(), t.basePath, err) return "", fmt.Errorf("Unable to write-tree in temporary repo for: %s Error: %v", t.repo.FullName(), err) @@ -179,7 +180,7 @@ func (t *TemporaryUploadRepository) GetLastCommitByRef(ref string) (string, erro if ref == "" { ref = "HEAD" } - stdout, err := git.NewCommand("rev-parse", ref).RunInDir(t.basePath) + stdout, err := git.NewCommandContext(t.ctx, "rev-parse", ref).RunInDir(t.basePath) if err != nil { log.Error("Unable to get last ref for %s in temporary repo: %s(%s): Error: %v", ref, t.repo.FullName(), t.basePath, err) return "", fmt.Errorf("Unable to rev-parse %s in temporary repo for: %s Error: %v", ref, t.repo.FullName(), err) @@ -218,7 +219,7 @@ func (t *TemporaryUploadRepository) CommitTreeWithDate(author, committer *user_m // Determine if we should sign if git.CheckGitVersionAtLeast("1.7.9") == nil { - sign, keyID, signer, _ := asymkey_service.SignCRUDAction(t.repo.RepoPath(), author, t.basePath, "HEAD") + sign, keyID, signer, _ := asymkey_service.SignCRUDAction(t.ctx, t.repo.RepoPath(), author, t.basePath, "HEAD") if sign { args = append(args, "-S"+keyID) if t.repo.GetTrustModel() == repo_model.CommitterTrustModel || t.repo.GetTrustModel() == repo_model.CollaboratorCommitterTrustModel { @@ -253,7 +254,7 @@ func (t *TemporaryUploadRepository) CommitTreeWithDate(author, committer *user_m stdout := new(bytes.Buffer) stderr := new(bytes.Buffer) - if err := git.NewCommand(args...).RunInDirTimeoutEnvFullPipeline(env, -1, t.basePath, stdout, stderr, messageBytes); err != nil { + if err := git.NewCommandContext(t.ctx, args...).RunInDirTimeoutEnvFullPipeline(env, -1, t.basePath, stdout, stderr, messageBytes); err != nil { log.Error("Unable to commit-tree in temporary repo: %s (%s) Error: %v\nStdout: %s\nStderr: %s", t.repo.FullName(), t.basePath, err, stdout, stderr) return "", fmt.Errorf("Unable to commit-tree in temporary repo: %s Error: %v\nStdout: %s\nStderr: %s", @@ -266,7 +267,7 @@ func (t *TemporaryUploadRepository) CommitTreeWithDate(author, committer *user_m func (t *TemporaryUploadRepository) Push(doer *user_model.User, commitHash, branch string) error { // Because calls hooks we need to pass in the environment env := models.PushingEnvironment(doer, t.repo) - if err := git.Push(t.gitRepo.Ctx, t.basePath, git.PushOptions{ + if err := git.Push(t.ctx, t.basePath, git.PushOptions{ Remote: t.repo.RepoPath(), Branch: strings.TrimSpace(commitHash) + ":" + git.BranchPrefix + strings.TrimSpace(branch), Env: env, @@ -302,7 +303,7 @@ func (t *TemporaryUploadRepository) DiffIndex() (*gitdiff.Diff, error) { var diff *gitdiff.Diff var finalErr error - if err := git.NewCommand("diff-index", "--src-prefix=\\a/", "--dst-prefix=\\b/", "--cached", "-p", "HEAD"). + if err := git.NewCommandContext(t.ctx, "diff-index", "--src-prefix=\\a/", "--dst-prefix=\\b/", "--cached", "-p", "HEAD"). RunInDirTimeoutEnvFullPipelineFunc(nil, 30*time.Second, t.basePath, stdoutWriter, stderr, nil, func(ctx context.Context, cancel context.CancelFunc) error { _ = stdoutWriter.Close() diff, finalErr = gitdiff.ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, stdoutReader, "") @@ -323,7 +324,7 @@ func (t *TemporaryUploadRepository) DiffIndex() (*gitdiff.Diff, error) { t.repo.FullName(), err, stderr) } - diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(t.basePath, "--cached", "HEAD") + diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(t.ctx, t.basePath, "--cached", "HEAD") if err != nil { return nil, err } diff --git a/services/repository/files/tree.go b/services/repository/files/tree.go index c6b77355ed..caad732887 100644 --- a/services/repository/files/tree.go +++ b/services/repository/files/tree.go @@ -5,6 +5,7 @@ package files import ( + "context" "fmt" "net/url" @@ -16,12 +17,7 @@ import ( ) // GetTreeBySHA get the GitTreeResponse of a repository using a sha hash. -func GetTreeBySHA(repo *repo_model.Repository, sha string, page, perPage int, recursive bool) (*api.GitTreeResponse, error) { - gitRepo, err := git.OpenRepository(repo.RepoPath()) - if err != nil { - return nil, err - } - defer gitRepo.Close() +func GetTreeBySHA(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, sha string, page, perPage int, recursive bool) (*api.GitTreeResponse, error) { gitTree, err := gitRepo.GetTree(sha) if err != nil || gitTree == nil { return nil, models.ErrSHANotFound{ diff --git a/services/repository/files/tree_test.go b/services/repository/files/tree_test.go index 37a6025d6c..e900480d35 100644 --- a/services/repository/files/tree_test.go +++ b/services/repository/files/tree_test.go @@ -29,7 +29,7 @@ func TestGetTreeBySHA(t *testing.T) { ctx.SetParams(":id", "1") ctx.SetParams(":sha", sha) - tree, err := GetTreeBySHA(ctx.Repo.Repository, ctx.Params(":sha"), page, perPage, true) + tree, err := GetTreeBySHA(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, ctx.Params(":sha"), page, perPage, true) assert.NoError(t, err) expectedTree := &api.GitTreeResponse{ SHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d", diff --git a/services/repository/files/update.go b/services/repository/files/update.go index 9fb074ed39..4b8653a7f7 100644 --- a/services/repository/files/update.go +++ b/services/repository/files/update.go @@ -6,6 +6,7 @@ package files import ( "bytes" + "context" "fmt" "path" "strings" @@ -22,7 +23,6 @@ import ( "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" asymkey_service "code.gitea.io/gitea/services/asymkey" - repo_service "code.gitea.io/gitea/services/repository" stdcharset "golang.org/x/net/html/charset" "golang.org/x/text/transform" @@ -125,7 +125,7 @@ func detectEncodingAndBOM(entry *git.TreeEntry, repo *repo_model.Repository) (st } // CreateOrUpdateRepoFile adds or updates a file in the given repository -func CreateOrUpdateRepoFile(repo *repo_model.Repository, doer *user_model.User, opts *UpdateRepoFileOptions) (*structs.FileResponse, error) { +func CreateOrUpdateRepoFile(ctx context.Context, repo *repo_model.Repository, doer *user_model.User, opts *UpdateRepoFileOptions) (*structs.FileResponse, error) { // If no branch name is set, assume default branch if opts.OldBranch == "" { opts.OldBranch = repo.DefaultBranch @@ -134,8 +134,14 @@ func CreateOrUpdateRepoFile(repo *repo_model.Repository, doer *user_model.User, opts.NewBranch = opts.OldBranch } + gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) + if err != nil { + return nil, err + } + defer closer.Close() + // oldBranch must exist for this operation - if _, err := repo_service.GetBranch(repo, opts.OldBranch); err != nil { + if _, err := gitRepo.GetBranch(opts.OldBranch); err != nil { return nil, err } @@ -143,7 +149,7 @@ func CreateOrUpdateRepoFile(repo *repo_model.Repository, doer *user_model.User, // Check to make sure the branch does not already exist, otherwise we can't proceed. // If we aren't branching to a new branch, make sure user can commit to the given branch if opts.NewBranch != opts.OldBranch { - existingBranch, err := repo_service.GetBranch(repo, opts.NewBranch) + existingBranch, err := gitRepo.GetBranch(opts.NewBranch) if existingBranch != nil { return nil, models.ErrBranchAlreadyExists{ BranchName: opts.NewBranch, @@ -152,7 +158,7 @@ func CreateOrUpdateRepoFile(repo *repo_model.Repository, doer *user_model.User, if err != nil && !git.IsErrBranchNotExist(err) { return nil, err } - } else if err := VerifyBranchProtection(repo, doer, opts.OldBranch, opts.TreePath); err != nil { + } else if err := VerifyBranchProtection(ctx, repo, doer, opts.OldBranch, opts.TreePath); err != nil { return nil, err } @@ -180,7 +186,7 @@ func CreateOrUpdateRepoFile(repo *repo_model.Repository, doer *user_model.User, author, committer := GetAuthorAndCommitterUsers(opts.Author, opts.Committer, doer) - t, err := NewTemporaryUploadRepository(repo) + t, err := NewTemporaryUploadRepository(ctx, repo) if err != nil { log.Error("%v", err) } @@ -435,7 +441,7 @@ func CreateOrUpdateRepoFile(repo *repo_model.Repository, doer *user_model.User, return nil, err } - file, err := GetFileResponseFromCommit(repo, commit, opts.NewBranch, treePath) + file, err := GetFileResponseFromCommit(ctx, repo, commit, opts.NewBranch, treePath) if err != nil { return nil, err } @@ -443,7 +449,7 @@ func CreateOrUpdateRepoFile(repo *repo_model.Repository, doer *user_model.User, } // VerifyBranchProtection verify the branch protection for modifying the given treePath on the given branch -func VerifyBranchProtection(repo *repo_model.Repository, doer *user_model.User, branchName, treePath string) error { +func VerifyBranchProtection(ctx context.Context, repo *repo_model.Repository, doer *user_model.User, branchName, treePath string) error { protectedBranch, err := models.GetProtectedBranchBy(repo.ID, branchName) if err != nil { return err @@ -460,7 +466,7 @@ func VerifyBranchProtection(repo *repo_model.Repository, doer *user_model.User, } } if protectedBranch.RequireSignedCommits { - _, _, _, err := asymkey_service.SignCRUDAction(repo.RepoPath(), doer, repo.RepoPath(), branchName) + _, _, _, err := asymkey_service.SignCRUDAction(ctx, repo.RepoPath(), doer, repo.RepoPath(), branchName) if err != nil { if !asymkey_service.IsErrWontSign(err) { return err diff --git a/services/repository/files/upload.go b/services/repository/files/upload.go index 28ed461fdc..79fca3ead7 100644 --- a/services/repository/files/upload.go +++ b/services/repository/files/upload.go @@ -5,6 +5,7 @@ package files import ( + "context" "fmt" "os" "path" @@ -49,7 +50,7 @@ func cleanUpAfterFailure(infos *[]uploadInfo, t *TemporaryUploadRepository, orig } // UploadRepoFiles uploads files to the given repository -func UploadRepoFiles(repo *repo_model.Repository, doer *user_model.User, opts *UploadRepoFileOptions) error { +func UploadRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *user_model.User, opts *UploadRepoFileOptions) error { if len(opts.Files) == 0 { return nil } @@ -80,7 +81,7 @@ func UploadRepoFiles(repo *repo_model.Repository, doer *user_model.User, opts *U infos[i] = uploadInfo{upload: upload} } - t, err := NewTemporaryUploadRepository(repo) + t, err := NewTemporaryUploadRepository(ctx, repo) if err != nil { return err } diff --git a/services/repository/hooks.go b/services/repository/hooks.go index f567702e9d..3905249499 100644 --- a/services/repository/hooks.go +++ b/services/repository/hooks.go @@ -54,13 +54,13 @@ func SyncRepositoryHooks(ctx context.Context) error { // GenerateGitHooks generates git hooks from a template repository func GenerateGitHooks(ctx context.Context, templateRepo, generateRepo *repo_model.Repository) error { - generateGitRepo, err := git.OpenRepository(generateRepo.RepoPath()) + generateGitRepo, err := git.OpenRepositoryCtx(ctx, generateRepo.RepoPath()) if err != nil { return err } defer generateGitRepo.Close() - templateGitRepo, err := git.OpenRepository(templateRepo.RepoPath()) + templateGitRepo, err := git.OpenRepositoryCtx(ctx, templateRepo.RepoPath()) if err != nil { return err } diff --git a/services/repository/push.go b/services/repository/push.go index 11854ccb39..62e2104432 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -20,6 +20,7 @@ import ( "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification" + "code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/queue" repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" @@ -77,15 +78,19 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { return nil } + ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("PushUpdates: %s/%s", optsList[0].RepoUserName, optsList[0].RepoName)) + defer finished() + repo, err := repo_model.GetRepositoryByOwnerAndName(optsList[0].RepoUserName, optsList[0].RepoName) if err != nil { return fmt.Errorf("GetRepositoryByOwnerAndName failed: %v", err) } repoPath := repo.RepoPath() - gitRepo, err := git.OpenRepository(repoPath) + + gitRepo, err := git.OpenRepositoryCtx(ctx, repoPath) if err != nil { - return fmt.Errorf("OpenRepository: %v", err) + return fmt.Errorf("OpenRepository[%s]: %v", repoPath, err) } defer gitRepo.Close() @@ -99,7 +104,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { for _, opts := range optsList { if opts.IsNewRef() && opts.IsDelRef() { - return fmt.Errorf("Old and new revisions are both %s", git.EmptySHA) + return fmt.Errorf("old and new revisions are both %s", git.EmptySHA) } if opts.IsTag() { // If is tag reference if pusher == nil || pusher.ID != opts.PusherID { @@ -139,7 +144,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { }, commits) addTags = append(addTags, tagName) - notification.NotifyCreateRef(pusher, repo, "tag", opts.RefFullName) + notification.NotifyCreateRef(pusher, repo, "tag", opts.RefFullName, opts.NewCommitID) } } else if opts.IsBranch() { // If is branch reference if pusher == nil || pusher.ID != opts.PusherID { @@ -184,14 +189,14 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { if err != nil { return fmt.Errorf("newCommit.CommitsBeforeLimit: %v", err) } - notification.NotifyCreateRef(pusher, repo, "branch", opts.RefFullName) + notification.NotifyCreateRef(pusher, repo, "branch", opts.RefFullName, opts.NewCommitID) } else { l, err = newCommit.CommitsBeforeUntil(opts.OldCommitID) if err != nil { return fmt.Errorf("newCommit.CommitsBeforeUntil: %v", err) } - isForce, err := repo_module.IsForcePush(opts) + isForce, err := repo_module.IsForcePush(ctx, opts) if err != nil { log.Error("isForcePush %s:%s failed: %v", repo.FullName(), branch, err) } diff --git a/services/repository/repository.go b/services/repository/repository.go index 17fab57e19..c3ca867187 100644 --- a/services/repository/repository.go +++ b/services/repository/repository.go @@ -5,6 +5,7 @@ package repository import ( + "context" "fmt" "code.gitea.io/gitea/models" @@ -31,8 +32,8 @@ func CreateRepository(doer, owner *user_model.User, opts models.CreateRepoOption } // DeleteRepository deletes a repository for a user or organization. -func DeleteRepository(doer *user_model.User, repo *repo_model.Repository, notify bool) error { - if err := pull_service.CloseRepoBranchesPulls(doer, repo); err != nil { +func DeleteRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, notify bool) error { + if err := pull_service.CloseRepoBranchesPulls(ctx, doer, repo); err != nil { log.Error("CloseRepoBranchesPulls failed: %v", err) } |