Skip to content

Commit 6024673

Browse files
authored
Remove wrong "git.DefaultContext" (#35364)
1 parent e837c99 commit 6024673

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+384
-476
lines changed

models/git/commit_status_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"code.gitea.io/gitea/models/unittest"
1616
user_model "code.gitea.io/gitea/models/user"
1717
"code.gitea.io/gitea/modules/commitstatus"
18-
"code.gitea.io/gitea/modules/git"
1918
"code.gitea.io/gitea/modules/gitrepo"
2019

2120
"github.com/stretchr/testify/assert"
@@ -187,7 +186,7 @@ func TestFindRepoRecentCommitStatusContexts(t *testing.T) {
187186

188187
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
189188
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
190-
gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo2)
189+
gitRepo, err := gitrepo.OpenRepository(t.Context(), repo2)
191190
assert.NoError(t, err)
192191
defer gitRepo.Close()
193192

models/migrations/migrations.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,24 @@ const minDBVersion = 70 // Gitea 1.5.3
4242
type migration struct {
4343
idNumber int64 // DB version is "the last migration's idNumber" + 1
4444
description string
45-
migrate func(*xorm.Engine) error
45+
migrate func(context.Context, *xorm.Engine) error
4646
}
4747

4848
// newMigration creates a new migration
49-
func newMigration(idNumber int64, desc string, fn func(*xorm.Engine) error) *migration {
50-
return &migration{idNumber, desc, fn}
49+
func newMigration[T func(*xorm.Engine) error | func(context.Context, *xorm.Engine) error](idNumber int64, desc string, fn T) *migration {
50+
m := &migration{idNumber: idNumber, description: desc}
51+
var ok bool
52+
if m.migrate, ok = any(fn).(func(context.Context, *xorm.Engine) error); !ok {
53+
m.migrate = func(ctx context.Context, x *xorm.Engine) error {
54+
return any(fn).(func(*xorm.Engine) error)(x)
55+
}
56+
}
57+
return m
5158
}
5259

5360
// Migrate executes the migration
54-
func (m *migration) Migrate(x *xorm.Engine) error {
55-
return m.migrate(x)
61+
func (m *migration) Migrate(ctx context.Context, x *xorm.Engine) error {
62+
return m.migrate(ctx, x)
5663
}
5764

5865
// Version describes the version table. Should have only one row with id==1
@@ -456,7 +463,7 @@ func migrationIDNumberToDBVersion(idNumber int64) int64 {
456463
}
457464

458465
// Migrate database to current version
459-
func Migrate(x *xorm.Engine) error {
466+
func Migrate(ctx context.Context, x *xorm.Engine) error {
460467
migrations := prepareMigrationTasks()
461468
maxDBVer := calcDBVersion(migrations)
462469

@@ -500,18 +507,16 @@ Please try upgrading to a lower version first (suggested v1.6.4), then upgrade t
500507
}
501508

502509
// Some migration tasks depend on the git command
503-
if git.DefaultContext == nil {
504-
if err = git.InitSimple(); err != nil {
505-
return err
506-
}
510+
if err = git.InitSimple(); err != nil {
511+
return err
507512
}
508513

509514
// Migrate
510515
for _, m := range getPendingMigrations(curDBVer, migrations) {
511516
log.Info("Migration[%d]: %s", m.idNumber, m.description)
512517
// Reset the mapper between each migration - migrations are not supposed to depend on each other
513518
x.SetMapper(names.GonicMapper{})
514-
if err = m.Migrate(x); err != nil {
519+
if err = m.Migrate(ctx, x); err != nil {
515520
return fmt.Errorf("migration[%d]: %s failed: %w", m.idNumber, m.description, err)
516521
}
517522
currentVersion.Version = migrationIDNumberToDBVersion(m.idNumber)

models/migrations/v1_12/v128.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package v1_12
55

66
import (
7+
"context"
78
"fmt"
89
"math"
910
"path/filepath"
@@ -17,7 +18,7 @@ import (
1718
"xorm.io/xorm"
1819
)
1920

20-
func FixMergeBase(x *xorm.Engine) error {
21+
func FixMergeBase(ctx context.Context, x *xorm.Engine) error {
2122
type Repository struct {
2223
ID int64 `xorm:"pk autoincr"`
2324
OwnerID int64 `xorm:"UNIQUE(s) index"`
@@ -82,17 +83,17 @@ func FixMergeBase(x *xorm.Engine) error {
8283

8384
if !pr.HasMerged {
8485
var err error
85-
pr.MergeBase, _, err = git.NewCommand("merge-base").AddDashesAndList(pr.BaseBranch, gitRefName).RunStdString(git.DefaultContext, &git.RunOpts{Dir: repoPath})
86+
pr.MergeBase, _, err = git.NewCommand("merge-base").AddDashesAndList(pr.BaseBranch, gitRefName).RunStdString(ctx, &git.RunOpts{Dir: repoPath})
8687
if err != nil {
8788
var err2 error
88-
pr.MergeBase, _, err2 = git.NewCommand("rev-parse").AddDynamicArguments(git.BranchPrefix+pr.BaseBranch).RunStdString(git.DefaultContext, &git.RunOpts{Dir: repoPath})
89+
pr.MergeBase, _, err2 = git.NewCommand("rev-parse").AddDynamicArguments(git.BranchPrefix+pr.BaseBranch).RunStdString(ctx, &git.RunOpts{Dir: repoPath})
8990
if err2 != nil {
9091
log.Error("Unable to get merge base for PR ID %d, Index %d in %s/%s. Error: %v & %v", pr.ID, pr.Index, baseRepo.OwnerName, baseRepo.Name, err, err2)
9192
continue
9293
}
9394
}
9495
} else {
95-
parentsString, _, err := git.NewCommand("rev-list", "--parents", "-n", "1").AddDynamicArguments(pr.MergedCommitID).RunStdString(git.DefaultContext, &git.RunOpts{Dir: repoPath})
96+
parentsString, _, err := git.NewCommand("rev-list", "--parents", "-n", "1").AddDynamicArguments(pr.MergedCommitID).RunStdString(ctx, &git.RunOpts{Dir: repoPath})
9697
if err != nil {
9798
log.Error("Unable to get parents for merged PR ID %d, Index %d in %s/%s. Error: %v", pr.ID, pr.Index, baseRepo.OwnerName, baseRepo.Name, err)
9899
continue
@@ -106,7 +107,7 @@ func FixMergeBase(x *xorm.Engine) error {
106107
refs = append(refs, gitRefName)
107108
cmd := git.NewCommand("merge-base").AddDashesAndList(refs...)
108109

109-
pr.MergeBase, _, err = cmd.RunStdString(git.DefaultContext, &git.RunOpts{Dir: repoPath})
110+
pr.MergeBase, _, err = cmd.RunStdString(ctx, &git.RunOpts{Dir: repoPath})
110111
if err != nil {
111112
log.Error("Unable to get merge base for merged PR ID %d, Index %d in %s/%s. Error: %v", pr.ID, pr.Index, baseRepo.OwnerName, baseRepo.Name, err)
112113
continue

models/migrations/v1_12/v134.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package v1_12
55

66
import (
7+
"context"
78
"fmt"
89
"math"
910
"path/filepath"
@@ -17,7 +18,7 @@ import (
1718
"xorm.io/xorm"
1819
)
1920

20-
func RefixMergeBase(x *xorm.Engine) error {
21+
func RefixMergeBase(ctx context.Context, x *xorm.Engine) error {
2122
type Repository struct {
2223
ID int64 `xorm:"pk autoincr"`
2324
OwnerID int64 `xorm:"UNIQUE(s) index"`
@@ -79,7 +80,7 @@ func RefixMergeBase(x *xorm.Engine) error {
7980

8081
gitRefName := fmt.Sprintf("refs/pull/%d/head", pr.Index)
8182

82-
parentsString, _, err := git.NewCommand("rev-list", "--parents", "-n", "1").AddDynamicArguments(pr.MergedCommitID).RunStdString(git.DefaultContext, &git.RunOpts{Dir: repoPath})
83+
parentsString, _, err := git.NewCommand("rev-list", "--parents", "-n", "1").AddDynamicArguments(pr.MergedCommitID).RunStdString(ctx, &git.RunOpts{Dir: repoPath})
8384
if err != nil {
8485
log.Error("Unable to get parents for merged PR ID %d, Index %d in %s/%s. Error: %v", pr.ID, pr.Index, baseRepo.OwnerName, baseRepo.Name, err)
8586
continue
@@ -94,7 +95,7 @@ func RefixMergeBase(x *xorm.Engine) error {
9495
refs = append(refs, gitRefName)
9596
cmd := git.NewCommand("merge-base").AddDashesAndList(refs...)
9697

97-
pr.MergeBase, _, err = cmd.RunStdString(git.DefaultContext, &git.RunOpts{Dir: repoPath})
98+
pr.MergeBase, _, err = cmd.RunStdString(ctx, &git.RunOpts{Dir: repoPath})
9899
if err != nil {
99100
log.Error("Unable to get merge base for merged PR ID %d, Index %d in %s/%s. Error: %v", pr.ID, pr.Index, baseRepo.OwnerName, baseRepo.Name, err)
100101
continue

models/migrations/v1_14/v156.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package v1_14
55

66
import (
7+
"context"
78
"fmt"
89
"path/filepath"
910
"strings"
@@ -24,7 +25,7 @@ func userPath(userName string) string {
2425
return filepath.Join(setting.RepoRootPath, strings.ToLower(userName))
2526
}
2627

27-
func FixPublisherIDforTagReleases(x *xorm.Engine) error {
28+
func FixPublisherIDforTagReleases(ctx context.Context, x *xorm.Engine) error {
2829
type Release struct {
2930
ID int64
3031
RepoID int64
@@ -108,7 +109,7 @@ func FixPublisherIDforTagReleases(x *xorm.Engine) error {
108109
return err
109110
}
110111
}
111-
gitRepo, err = git.OpenRepository(git.DefaultContext, repoPath(repo.OwnerName, repo.Name))
112+
gitRepo, err = git.OpenRepository(ctx, repoPath(repo.OwnerName, repo.Name))
112113
if err != nil {
113114
log.Error("Error whilst opening git repo for [%d]%s/%s. Error: %v", repo.ID, repo.OwnerName, repo.Name, err)
114115
return err

models/migrations/v1_9/v82.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package v1_9
55

66
import (
7+
"context"
78
"fmt"
89
"path/filepath"
910
"strings"
@@ -14,7 +15,7 @@ import (
1415
"xorm.io/xorm"
1516
)
1617

17-
func FixReleaseSha1OnReleaseTable(x *xorm.Engine) error {
18+
func FixReleaseSha1OnReleaseTable(ctx context.Context, x *xorm.Engine) error {
1819
type Release struct {
1920
ID int64
2021
RepoID int64
@@ -98,7 +99,7 @@ func FixReleaseSha1OnReleaseTable(x *xorm.Engine) error {
9899
userCache[repo.OwnerID] = user
99100
}
100101

101-
gitRepo, err = git.OpenRepository(git.DefaultContext, RepoPath(user.Name, repo.Name))
102+
gitRepo, err = git.OpenRepository(ctx, RepoPath(user.Name, repo.Name))
102103
if err != nil {
103104
return err
104105
}

modules/git/blame.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func CreateBlameReader(ctx context.Context, objectFormat ObjectFormat, repoPath
141141
}
142142
}()
143143

144-
cmd := NewCommandNoGlobals("blame", "--porcelain")
144+
cmd := NewCommand("blame", "--porcelain")
145145

146146
if DefaultFeatures().CheckVersionAtLeast("2.23") && !bypassBlameIgnore {
147147
ignoreRevsFileName, ignoreRevsFileCleanup, err = tryCreateBlameIgnoreRevsFile(commit)

modules/git/blob_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
func TestBlob_Data(t *testing.T) {
1717
output := "file2\n"
1818
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
19-
repo, err := openRepositoryWithDefaultContext(bareRepo1Path)
19+
repo, err := OpenRepository(t.Context(), bareRepo1Path)
2020
require.NoError(t, err)
2121
defer repo.Close()
2222

@@ -36,7 +36,7 @@ func TestBlob_Data(t *testing.T) {
3636

3737
func Benchmark_Blob_Data(b *testing.B) {
3838
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
39-
repo, err := openRepositoryWithDefaultContext(bareRepo1Path)
39+
repo, err := OpenRepository(b.Context(), bareRepo1Path)
4040
if err != nil {
4141
b.Fatal(err)
4242
}

modules/git/command.go

Lines changed: 8 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,19 @@ import (
2929
// In most cases, it shouldn't be used. Use AddXxx function instead
3030
type TrustedCmdArgs []internal.CmdArg
3131

32-
var (
33-
// globalCommandArgs global command args for external package setting
34-
globalCommandArgs TrustedCmdArgs
35-
36-
// defaultCommandExecutionTimeout default command execution timeout duration
37-
defaultCommandExecutionTimeout = 360 * time.Second
38-
)
32+
// defaultCommandExecutionTimeout default command execution timeout duration
33+
var defaultCommandExecutionTimeout = 360 * time.Second
3934

4035
// DefaultLocale is the default LC_ALL to run git commands in.
4136
const DefaultLocale = "C"
4237

4338
// Command represents a command with its subcommands or arguments.
4439
type Command struct {
45-
prog string
46-
args []string
47-
globalArgsLength int
48-
brokenArgs []string
49-
cmd *exec.Cmd // for debug purpose only
50-
configArgs []string
40+
prog string
41+
args []string
42+
brokenArgs []string
43+
cmd *exec.Cmd // for debug purpose only
44+
configArgs []string
5145
}
5246

5347
func logArgSanitize(arg string) string {
@@ -72,10 +66,7 @@ func (c *Command) LogString() string {
7266
}
7367
a := make([]string, 0, len(c.args)+1)
7468
a = append(a, debugQuote(c.prog))
75-
if c.globalArgsLength > 0 {
76-
a = append(a, "...global...")
77-
}
78-
for i := c.globalArgsLength; i < len(c.args); i++ {
69+
for i := 0; i < len(c.args); i++ {
7970
a = append(a, debugQuote(logArgSanitize(c.args[i])))
8071
}
8172
return strings.Join(a, " ")
@@ -91,24 +82,6 @@ func (c *Command) ProcessState() string {
9182
// NewCommand creates and returns a new Git Command based on given command and arguments.
9283
// Each argument should be safe to be trusted. User-provided arguments should be passed to AddDynamicArguments instead.
9384
func NewCommand(args ...internal.CmdArg) *Command {
94-
// Make an explicit copy of globalCommandArgs, otherwise append might overwrite it
95-
cargs := make([]string, 0, len(globalCommandArgs)+len(args))
96-
for _, arg := range globalCommandArgs {
97-
cargs = append(cargs, string(arg))
98-
}
99-
for _, arg := range args {
100-
cargs = append(cargs, string(arg))
101-
}
102-
return &Command{
103-
prog: GitExecutable,
104-
args: cargs,
105-
globalArgsLength: len(globalCommandArgs),
106-
}
107-
}
108-
109-
// NewCommandNoGlobals creates and returns a new Git Command based on given command and arguments only with the specified args and don't use global command args
110-
// Each argument should be safe to be trusted. User-provided arguments should be passed to AddDynamicArguments instead.
111-
func NewCommandNoGlobals(args ...internal.CmdArg) *Command {
11285
cargs := make([]string, 0, len(args))
11386
for _, arg := range args {
11487
cargs = append(cargs, string(arg))
@@ -468,19 +441,3 @@ func (c *Command) runStdBytes(ctx context.Context, opts *RunOpts) (stdout, stder
468441
// even if there is no err, there could still be some stderr output
469442
return stdoutBuf.Bytes(), stderr, nil
470443
}
471-
472-
// AllowLFSFiltersArgs return globalCommandArgs with lfs filter, it should only be used for tests
473-
func AllowLFSFiltersArgs() TrustedCmdArgs {
474-
// Now here we should explicitly allow lfs filters to run
475-
filteredLFSGlobalArgs := make(TrustedCmdArgs, len(globalCommandArgs))
476-
j := 0
477-
for _, arg := range globalCommandArgs {
478-
if strings.Contains(string(arg), "lfs") {
479-
j--
480-
} else {
481-
filteredLFSGlobalArgs[j] = arg
482-
j++
483-
}
484-
}
485-
return filteredLFSGlobalArgs[:j]
486-
}

modules/git/command_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ func TestGitArgument(t *testing.T) {
5353
}
5454

5555
func TestCommandString(t *testing.T) {
56-
cmd := NewCommandNoGlobals("a", "-m msg", "it's a test", `say "hello"`)
56+
cmd := NewCommand("a", "-m msg", "it's a test", `say "hello"`)
5757
assert.Equal(t, cmd.prog+` a "-m msg" "it's a test" "say \"hello\""`, cmd.LogString())
5858

59-
cmd = NewCommandNoGlobals("url: https://a:b@c/", "/root/dir-a/dir-b")
59+
cmd = NewCommand("url: https://a:b@c/", "/root/dir-a/dir-b")
6060
assert.Equal(t, cmd.prog+` "url: https://sanitized-credential@c/" .../dir-a/dir-b`, cmd.LogString())
6161
}

0 commit comments

Comments
 (0)