refactor: remove Ctx field from git.Repository (#38500)

This commit is contained in:
wxiaoguang
2026-07-17 18:44:31 +08:00
committed by GitHub
parent 2c8e99bbf7
commit 5b078f72aa
235 changed files with 1234 additions and 1258 deletions
+5 -5
View File
@@ -63,7 +63,7 @@ func CherryPick(ctx context.Context, repo *repo_model.Repository, doer *user_mod
}
// Get the commit of the original branch
commit, err := t.GetBranchCommit(opts.OldBranch)
commit, err := t.GetBranchCommit(ctx, opts.OldBranch)
if err != nil {
return nil, err // Couldn't get a commit for the branch
}
@@ -72,7 +72,7 @@ func CherryPick(ctx context.Context, repo *repo_model.Repository, doer *user_mod
if opts.LastCommitID == "" {
opts.LastCommitID = commit.ID.String()
} else {
lastCommitID, err := t.gitRepo.ConvertToGitID(opts.LastCommitID)
lastCommitID, err := t.gitRepo.ConvertToGitID(ctx, opts.LastCommitID)
if err != nil {
return nil, fmt.Errorf("CherryPick: Invalid last commit ID: %w", err)
}
@@ -85,7 +85,7 @@ func CherryPick(ctx context.Context, repo *repo_model.Repository, doer *user_mod
}
}
commit, err = t.GetCommit(strings.TrimSpace(opts.Content))
commit, err = t.GetCommit(ctx, strings.TrimSpace(opts.Content))
if err != nil {
return nil, err
}
@@ -142,12 +142,12 @@ func CherryPick(ctx context.Context, repo *repo_model.Repository, doer *user_mod
return nil, err
}
commit, err = t.GetCommit(commitHash)
commit, err = t.GetCommit(ctx, commitHash)
if err != nil {
return nil, err
}
fileCommitResponse, _ := GetFileCommitResponse(repo, gitRepo, commit) // ok if fails, then will be nil
fileCommitResponse, _ := GetFileCommitResponse(ctx, repo, gitRepo, commit) // ok if fails, then will be nil
verification := GetPayloadCommitVerification(ctx, commit)
fileResponse := &structs.FileResponse{
Commit: fileCommitResponse,
+11 -11
View File
@@ -162,7 +162,7 @@ func getFileContentsByEntryInternal(ctx context.Context, repo *repo_model.Reposi
return nil, err
}
lastCommit, err := refCommit.Commit.GetCommitByPath(gitRepo, opts.TreePath)
lastCommit, err := refCommit.Commit.GetCommitByPath(ctx, gitRepo, opts.TreePath)
if err != nil {
return nil, err
}
@@ -188,14 +188,14 @@ func getFileContentsByEntryInternal(ctx context.Context, repo *repo_model.Reposi
contentsResponse.Type = string(ContentTypeRegular)
// if it is listing the repo root dir, don't waste system resources on reading content
if opts.IncludeSingleFileContent {
blobResponse, err := GetBlobBySHA(repo, gitRepo, entry.ID.String())
blobResponse, err := GetBlobBySHA(ctx, repo, gitRepo, entry.ID.String())
if err != nil {
return nil, err
}
contentsResponse.Encoding, contentsResponse.Content = blobResponse.Encoding, blobResponse.Content
contentsResponse.LfsOid, contentsResponse.LfsSize = blobResponse.LfsOid, blobResponse.LfsSize
} else if opts.IncludeLfsMetadata {
contentsResponse.LfsOid, contentsResponse.LfsSize, err = parsePossibleLfsPointerBlob(gitRepo, entry.ID.String())
contentsResponse.LfsOid, contentsResponse.LfsSize, err = parsePossibleLfsPointerBlob(ctx, gitRepo, entry.ID.String())
if err != nil {
return nil, err
}
@@ -205,7 +205,7 @@ func getFileContentsByEntryInternal(ctx context.Context, repo *repo_model.Reposi
} else if entry.IsLink() {
contentsResponse.Type = string(ContentTypeLink)
// The target of a symlink file is the content of the file
targetFromContent, err := entry.Blob(gitRepo).GetBlobContent(1024)
targetFromContent, err := entry.Blob(gitRepo).GetBlobContent(ctx, 1024)
if err != nil {
return nil, err
}
@@ -250,7 +250,7 @@ func getFileContentsByEntryInternal(ctx context.Context, repo *repo_model.Reposi
return contentsResponse, nil
}
func GetBlobBySHA(repo *repo_model.Repository, gitRepo *git.Repository, sha string) (*api.GitBlobResponse, error) {
func GetBlobBySHA(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, sha string) (*api.GitBlobResponse, error) {
gitBlob, err := gitRepo.GetBlob(sha)
if err != nil {
return nil, err
@@ -258,10 +258,10 @@ func GetBlobBySHA(repo *repo_model.Repository, gitRepo *git.Repository, sha stri
ret := &api.GitBlobResponse{
SHA: gitBlob.ID.String(),
URL: repo.APIURL() + "/git/blobs/" + url.PathEscape(gitBlob.ID.String()),
Size: gitBlob.Size(),
Size: gitBlob.Size(ctx),
}
blobSize := gitBlob.Size()
blobSize := gitBlob.Size(ctx)
if blobSize > setting.API.DefaultMaxBlobSize {
return ret, nil
}
@@ -271,7 +271,7 @@ func GetBlobBySHA(repo *repo_model.Repository, gitRepo *git.Repository, sha stri
originContent = &strings.Builder{}
}
content, err := gitBlob.GetBlobContentBase64(originContent)
content, err := gitBlob.GetBlobContentBase64(ctx, originContent)
if err != nil {
return nil, err
}
@@ -291,15 +291,15 @@ func parsePossibleLfsPointerBuffer(r io.Reader) (*string, *int64) {
return nil, nil
}
func parsePossibleLfsPointerBlob(gitRepo *git.Repository, sha string) (*string, *int64, error) {
func parsePossibleLfsPointerBlob(ctx context.Context, gitRepo *git.Repository, sha string) (*string, *int64, error) {
gitBlob, err := gitRepo.GetBlob(sha)
if err != nil {
return nil, nil, err
}
if gitBlob.Size() > lfs.MetaFileMaxSize {
if gitBlob.Size(ctx) > lfs.MetaFileMaxSize {
return nil, nil, nil // not a LFS pointer
}
buf, err := gitBlob.GetBlobContent(lfs.MetaFileMaxSize)
buf, err := gitBlob.GetBlobContent(ctx, lfs.MetaFileMaxSize)
if err != nil {
return nil, nil, err
}
+1 -1
View File
@@ -34,7 +34,7 @@ func TestGetContents(t *testing.T) {
sha := "65f1bf27bc3bf70f64657658635e66094edbcb4d"
ctx.SetPathParam("id", "1")
ctx.SetPathParam("sha", sha)
gbr, err := GetBlobBySHA(ctx.Repo.Repository, ctx.Repo.GitRepo, ctx.PathParam("sha"))
gbr, err := GetBlobBySHA(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, ctx.PathParam("sha"))
expectedGBR := &api.GitBlobResponse{
Content: new("dHJlZSAyYTJmMWQ0NjcwNzI4YTJlMTAwNDllMzQ1YmQ3YTI3NjQ2OGJlYWI2CmF1dGhvciB1c2VyMSA8YWRkcmVzczFAZXhhbXBsZS5jb20+IDE0ODk5NTY0NzkgLTA0MDAKY29tbWl0dGVyIEV0aGFuIEtvZW5pZyA8ZXRoYW50a29lbmlnQGdtYWlsLmNvbT4gMTQ4OTk1NjQ3OSAtMDQwMAoKSW5pdGlhbCBjb21taXQK"),
Encoding: new("base64"),
+3 -3
View File
@@ -45,7 +45,7 @@ func GetContentsListFromTreePaths(ctx context.Context, repo *repo_model.Reposito
func GetFilesResponseFromCommit(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, refCommit *utils.RefCommit, treeNames []string) (*api.FilesResponse, error) {
files := GetContentsListFromTreePaths(ctx, repo, gitRepo, refCommit, treeNames)
fileCommitResponse, _ := GetFileCommitResponse(repo, gitRepo, refCommit.Commit) // ok if fails, then will be nil
fileCommitResponse, _ := GetFileCommitResponse(ctx, repo, gitRepo, refCommit.Commit) // ok if fails, then will be nil
verification := GetPayloadCommitVerification(ctx, refCommit.Commit)
filesResponse := &api.FilesResponse{
Files: files,
@@ -70,7 +70,7 @@ func GetFileResponseFromFilesResponse(filesResponse *api.FilesResponse, index in
}
// GetFileCommitResponse Constructs a FileCommitResponse from a Commit object
func GetFileCommitResponse(repo *repo_model.Repository, gitRepo *git.Repository, commit *git.Commit) (*api.FileCommitResponse, error) {
func GetFileCommitResponse(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, commit *git.Commit) (*api.FileCommitResponse, error) {
if repo == nil {
return nil, errors.New("repo cannot be nil")
}
@@ -81,7 +81,7 @@ func GetFileCommitResponse(repo *repo_model.Repository, gitRepo *git.Repository,
commitTreeURL, _ := url.Parse(repo.APIURL() + "/git/trees/" + url.PathEscape(commit.TreeID.String()))
parents := make([]*api.CommitMeta, commit.ParentCount())
for i := 0; i < commit.ParentCount(); i++ {
if parent, err := commit.Parent(gitRepo, i); err == nil && parent != nil {
if parent, err := commit.Parent(ctx, gitRepo, i); err == nil && parent != nil {
parentCommitURL, _ := url.Parse(repo.APIURL() + "/git/commits/" + url.PathEscape(parent.ID.String()))
parents[i] = &api.CommitMeta{
SHA: parent.ID.String(),
+4 -4
View File
@@ -142,7 +142,7 @@ func ApplyDiffPatch(ctx context.Context, repo *repo_model.Repository, doer *user
}
// Get the commit of the original branch
commit, err := t.GetBranchCommit(opts.OldBranch)
commit, err := t.GetBranchCommit(ctx, opts.OldBranch)
if err != nil {
return nil, err // Couldn't get a commit for the branch
}
@@ -151,7 +151,7 @@ func ApplyDiffPatch(ctx context.Context, repo *repo_model.Repository, doer *user
if opts.LastCommitID == "" {
opts.LastCommitID = commit.ID.String()
} else {
lastCommitID, err := t.gitRepo.ConvertToGitID(opts.LastCommitID)
lastCommitID, err := t.gitRepo.ConvertToGitID(ctx, opts.LastCommitID)
if err != nil {
return nil, fmt.Errorf("ApplyPatch: Invalid last commit ID: %w", err)
}
@@ -206,12 +206,12 @@ func ApplyDiffPatch(ctx context.Context, repo *repo_model.Repository, doer *user
return nil, err
}
commit, err = t.GetCommit(commitHash)
commit, err = t.GetCommit(ctx, commitHash)
if err != nil {
return nil, err
}
fileCommitResponse, _ := GetFileCommitResponse(repo, gitRepo, commit) // ok if fails, then will be nil
fileCommitResponse, _ := GetFileCommitResponse(ctx, repo, gitRepo, commit) // ok if fails, then will be nil
verification := GetPayloadCommitVerification(ctx, commit)
fileResponse := &structs.FileResponse{
Commit: fileCommitResponse,
+11 -8
View File
@@ -48,7 +48,10 @@ func NewTemporaryUploadRepository(repo *repo_model.Repository) (*TemporaryUpload
// Close the repository cleaning up all files
func (t *TemporaryUploadRepository) Close() {
// must stop the repo access before removal, otherwise Windows can't remove the directory occupied by other processes
t.gitRepo.Close()
if t.gitRepo != nil {
_ = t.gitRepo.Close()
t.gitRepo = nil
}
if t.cleanup != nil {
t.cleanup()
}
@@ -76,7 +79,7 @@ func (t *TemporaryUploadRepository) Clone(ctx context.Context, branch string, ba
}
return fmt.Errorf("Clone: %w %s", err, stderr)
}
gitRepo, err := git.OpenRepository(ctx, t.basePath)
gitRepo, err := git.OpenRepository(t.basePath)
if err != nil {
return err
}
@@ -89,7 +92,7 @@ func (t *TemporaryUploadRepository) Init(ctx context.Context, objectFormatName s
if err := git.InitRepository(ctx, t.basePath, false, objectFormatName); err != nil {
return err
}
gitRepo, err := git.OpenRepository(ctx, t.basePath)
gitRepo, err := git.OpenRepository(t.basePath)
if err != nil {
return err
}
@@ -141,7 +144,7 @@ func (t *TemporaryUploadRepository) RemoveRecursivelyFromIndex(ctx context.Conte
// RemoveFilesFromIndex removes the given files from the index
func (t *TemporaryUploadRepository) RemoveFilesFromIndex(ctx context.Context, filenames ...string) error {
objFmt, err := t.gitRepo.GetObjectFormat()
objFmt, err := t.gitRepo.GetObjectFormat(ctx)
if err != nil {
return fmt.Errorf("unable to get object format for temporary repo: %q, error: %w", t.repo.FullName(), err)
}
@@ -387,17 +390,17 @@ func (t *TemporaryUploadRepository) DiffIndex(ctx context.Context, oldContent, n
}
// GetBranchCommit Gets the commit object of the given branch
func (t *TemporaryUploadRepository) GetBranchCommit(branch string) (*git.Commit, error) {
func (t *TemporaryUploadRepository) GetBranchCommit(ctx context.Context, branch string) (*git.Commit, error) {
if t.gitRepo == nil {
return nil, errors.New("repository has not been cloned")
}
return t.gitRepo.GetBranchCommit(branch)
return t.gitRepo.GetBranchCommit(ctx, branch)
}
// GetCommit Gets the commit object of the given commit ID
func (t *TemporaryUploadRepository) GetCommit(commitID string) (*git.Commit, error) {
func (t *TemporaryUploadRepository) GetCommit(ctx context.Context, commitID string) (*git.Commit, error) {
if t.gitRepo == nil {
return nil, errors.New("repository has not been cloned")
}
return t.gitRepo.GetCommit(commitID)
return t.gitRepo.GetCommit(ctx, commitID)
}
+3 -3
View File
@@ -24,7 +24,7 @@ import (
// GetTreeBySHA get the GitTreeResponse of a repository using a sha hash (id of a commit or a tree)
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)
gitTree, err := gitRepo.GetTree(ctx, sha)
if err != nil {
return nil, util.NewInvalidArgumentErrorf("sha not found [%s]", sha)
}
@@ -170,7 +170,7 @@ func listTreeNodes(ctx context.Context, repoLink string, renderedIconPool *filei
if subTreePath[0] == '/' {
subTreePath = subTreePath[1:]
}
subNodes, err := listTreeNodes(ctx, repoLink, renderedIconPool, gitRepo, commit, entry.Tree(gitRepo), subTreePath, subPathRemaining)
subNodes, err := listTreeNodes(ctx, repoLink, renderedIconPool, gitRepo, commit, entry.Tree(ctx, gitRepo), subTreePath, subPathRemaining)
if err != nil {
log.Error("listTreeNodes: %v", err)
} else {
@@ -187,5 +187,5 @@ func GetTreeViewNodes(ctx context.Context, repoLink string, renderedIconPool *fi
if err != nil {
return nil, err
}
return listTreeNodes(ctx, repoLink, renderedIconPool, gitRepo, commit, entry.Tree(gitRepo), treePath, subPath)
return listTreeNodes(ctx, repoLink, renderedIconPool, gitRepo, commit, entry.Tree(ctx, gitRepo), treePath, subPath)
}
+8 -8
View File
@@ -202,7 +202,7 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
if hasOldBranch {
// Get the commit of the original branch
commit, err := t.GetBranchCommit(opts.OldBranch)
commit, err := t.GetBranchCommit(ctx, opts.OldBranch)
if err != nil {
return nil, err // Couldn't get a commit for the branch
}
@@ -211,7 +211,7 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
if opts.LastCommitID == "" {
opts.LastCommitID = commit.ID.String()
} else {
lastCommitID, err := t.gitRepo.ConvertToGitID(opts.LastCommitID)
lastCommitID, err := t.gitRepo.ConvertToGitID(ctx, opts.LastCommitID)
if err != nil {
return nil, fmt.Errorf("ConvertToSHA1: Invalid last commit ID: %w", err)
}
@@ -282,7 +282,7 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
return nil, err
}
commit, err := t.GetCommit(commitHash)
commit, err := t.GetCommit(ctx, commitHash)
if err != nil {
return nil, err
}
@@ -295,7 +295,7 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
}
if repo.IsEmpty {
if isEmpty, err := gitRepo.IsEmpty(); err == nil && !isEmpty {
if isEmpty, err := gitRepo.IsEmpty(ctx); err == nil && !isEmpty {
_ = repo_model.UpdateRepositoryColsWithAutoTime(ctx, &repo_model.Repository{ID: repo.ID, IsEmpty: false, DefaultBranch: opts.NewBranch}, "is_empty", "default_branch")
}
}
@@ -391,7 +391,7 @@ func handleCheckErrors(ctx context.Context, file *ChangeRepoFile, gitRepo *git.R
// If a lastCommitID given doesn't match the branch head's commitID throw
// an error, but only if we aren't creating a new branch.
if commit.ID.String() != opts.LastCommitID && opts.OldBranch == opts.NewBranch {
if changed, err := commit.FileChangedSinceCommit(gitRepo, file.Options.treePath, opts.LastCommitID); err != nil {
if changed, err := commit.FileChangedSinceCommit(ctx, gitRepo, file.Options.treePath, opts.LastCommitID); err != nil {
return err
} else if changed {
return ErrCommitIDDoesNotMatch{
@@ -592,7 +592,7 @@ func writeRepoObjectForRename(ctx context.Context, t *TemporaryUploadRepository,
if err != nil {
return nil, err
}
commit, err := t.GetCommit(lastCommitID)
commit, err := t.GetCommit(ctx, lastCommitID)
if err != nil {
return nil, err
}
@@ -619,7 +619,7 @@ func writeRepoObjectForRename(ctx context.Context, t *TemporaryUploadRepository,
}
oldEntryBlobPointerBy := func(f func(r io.Reader) (lfs.Pointer, error)) (lfsPointer lfs.Pointer, err error) {
r, err := oldEntry.Blob(t.gitRepo).DataAsync()
r, err := oldEntry.Blob(t.gitRepo).DataAsync(ctx)
if err != nil {
return lfsPointer, err
}
@@ -645,7 +645,7 @@ func writeRepoObjectForRename(ctx context.Context, t *TemporaryUploadRepository,
if err != nil {
return nil, err
}
ret.LfsContent, err = oldEntry.Blob(t.gitRepo).DataAsync()
ret.LfsContent, err = oldEntry.Blob(t.gitRepo).DataAsync(ctx)
if err != nil {
return nil, err
}