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
+3 -3
View File
@@ -29,15 +29,15 @@ type BatchChecker struct {
// NewBatchChecker creates a check attribute reader for the current repository and provided commit ID
// If treeish is empty, then it will use current working directory, otherwise it will use the provided treeish on the bare repo
func NewBatchChecker(repo *git.Repository, treeish string, attributes []string) (checker *BatchChecker, returnedErr error) {
ctx, cancel := context.WithCancel(repo.Ctx)
func NewBatchChecker(ctx context.Context, repo *git.Repository, treeish string, attributes []string) (checker *BatchChecker, returnedErr error) {
ctx, cancel := context.WithCancel(ctx)
defer func() {
if returnedErr != nil {
cancel()
}
}()
cmd, envs, cleanup, err := checkAttrCommand(repo, treeish, nil, attributes)
cmd, envs, cleanup, err := checkAttrCommand(ctx, repo, treeish, nil, attributes)
if err != nil {
return nil, err
}
+6 -5
View File
@@ -118,7 +118,8 @@ func expectedAttrs() *Attributes {
func Test_BatchChecker(t *testing.T) {
setting.AppDataPath = t.TempDir()
repoPath := "../tests/repos/language_stats_repo"
gitRepo, err := git.OpenRepository(t.Context(), repoPath)
ctx := t.Context()
gitRepo, err := git.OpenRepository(repoPath)
require.NoError(t, err)
defer gitRepo.Close()
@@ -126,7 +127,7 @@ func Test_BatchChecker(t *testing.T) {
t.Run("Create index file to run git check-attr", func(t *testing.T) {
defer test.MockVariableValue(&git.DefaultFeatures().SupportCheckAttrOnBare, false)()
checker, err := NewBatchChecker(gitRepo, commitID, LinguistAttributes)
checker, err := NewBatchChecker(ctx, gitRepo, commitID, LinguistAttributes)
assert.NoError(t, err)
defer checker.Close()
attributes, err := checker.CheckPath("i-am-a-python.p")
@@ -143,11 +144,11 @@ func Test_BatchChecker(t *testing.T) {
})
assert.NoError(t, err)
tempRepo, err := git.OpenRepository(t.Context(), dir)
tempRepo, err := git.OpenRepository(dir)
assert.NoError(t, err)
defer tempRepo.Close()
checker, err := NewBatchChecker(tempRepo, "", LinguistAttributes)
checker, err := NewBatchChecker(t.Context(), tempRepo, "", LinguistAttributes)
assert.NoError(t, err)
defer checker.Close()
attributes, err := checker.CheckPath("i-am-a-python.p")
@@ -161,7 +162,7 @@ func Test_BatchChecker(t *testing.T) {
}
t.Run("Run git check-attr in bare repository", func(t *testing.T) {
checker, err := NewBatchChecker(gitRepo, commitID, LinguistAttributes)
checker, err := NewBatchChecker(ctx, gitRepo, commitID, LinguistAttributes)
assert.NoError(t, err)
defer checker.Close()
+3 -3
View File
@@ -14,7 +14,7 @@ import (
"gitea.dev/modules/git/gitcmd"
)
func checkAttrCommand(gitRepo *git.Repository, treeish string, filenames, attributes []string) (*gitcmd.Command, []string, func(), error) {
func checkAttrCommand(ctx context.Context, gitRepo *git.Repository, treeish string, filenames, attributes []string) (*gitcmd.Command, []string, func(), error) {
cancel := func() {}
envs := []string{"GIT_FLUSH=1"}
cmd := gitcmd.NewCommand("check-attr", "-z")
@@ -28,7 +28,7 @@ func checkAttrCommand(gitRepo *git.Repository, treeish string, filenames, attrib
cmd.AddArguments("--source")
cmd.AddDynamicArguments(treeish)
} else {
indexFilename, worktree, deleteTemporaryFile, err := gitRepo.ReadTreeToTemporaryIndex(treeish)
indexFilename, worktree, deleteTemporaryFile, err := gitRepo.ReadTreeToTemporaryIndex(ctx, treeish)
if err != nil {
return nil, nil, nil, err
}
@@ -62,7 +62,7 @@ type CheckAttributeOpts struct {
// CheckAttributes return the attributes of the given filenames and attributes in the given treeish.
// If treeish is empty, then it will use current working directory, otherwise it will use the provided treeish on the bare repo
func CheckAttributes(ctx context.Context, gitRepo *git.Repository, treeish string, opts CheckAttributeOpts) (map[string]*Attributes, error) {
cmd, envs, cancel, err := checkAttrCommand(gitRepo, treeish, opts.Filenames, opts.Attributes)
cmd, envs, cancel, err := checkAttrCommand(ctx, gitRepo, treeish, opts.Filenames, opts.Attributes)
if err != nil {
return nil, err
}
+2 -2
View File
@@ -18,7 +18,7 @@ import (
func Test_Checker(t *testing.T) {
setting.AppDataPath = t.TempDir()
repoPath := "../tests/repos/language_stats_repo"
gitRepo, err := git.OpenRepository(t.Context(), repoPath)
gitRepo, err := git.OpenRepository(repoPath)
require.NoError(t, err)
defer gitRepo.Close()
@@ -44,7 +44,7 @@ func Test_Checker(t *testing.T) {
})
assert.NoError(t, err)
tempRepo, err := git.OpenRepository(t.Context(), dir)
tempRepo, err := git.OpenRepository(dir)
assert.NoError(t, err)
defer tempRepo.Close()