Skip to content

Commit 80adbeb

Browse files
authored
Unexport git.GlobalCommandArgs (#18376)
Unexport the git.GlobalCommandArgs variable.
1 parent 93250bf commit 80adbeb

File tree

8 files changed

+39
-39
lines changed

8 files changed

+39
-39
lines changed

integrations/git_clone_wiki_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestRepoCloneWiki(t *testing.T) {
4141
u, _ = url.Parse(r)
4242
u.User = url.UserPassword("user2", userPassword)
4343
t.Run("Clone", func(t *testing.T) {
44-
assert.NoError(t, git.CloneWithArgs(context.Background(), u.String(), dstPath, allowLFSFilters(), git.CloneRepoOptions{}))
44+
assert.NoError(t, git.CloneWithArgs(context.Background(), u.String(), dstPath, git.AllowLFSFiltersArgs(), git.CloneRepoOptions{}))
4545
assertFileEqual(t, filepath.Join(dstPath, "Home.md"), []byte("# Home page\n\nThis is the home page!\n"))
4646
assertFileExist(t, filepath.Join(dstPath, "Page-With-Image.md"))
4747
assertFileExist(t, filepath.Join(dstPath, "Page-With-Spaced-Name.md"))

integrations/git_helper_for_declarative_test.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"path"
1515
"path/filepath"
1616
"strconv"
17-
"strings"
1817
"testing"
1918
"time"
2019

@@ -60,21 +59,6 @@ func createSSHUrl(gitPath string, u *url.URL) *url.URL {
6059
return &u2
6160
}
6261

63-
func allowLFSFilters() []string {
64-
// Now here we should explicitly allow lfs filters to run
65-
filteredLFSGlobalArgs := make([]string, len(git.GlobalCommandArgs))
66-
j := 0
67-
for _, arg := range git.GlobalCommandArgs {
68-
if strings.Contains(arg, "lfs") {
69-
j--
70-
} else {
71-
filteredLFSGlobalArgs[j] = arg
72-
j++
73-
}
74-
}
75-
return filteredLFSGlobalArgs[:j]
76-
}
77-
7862
func onGiteaRunTB(t testing.TB, callback func(testing.TB, *url.URL), prepare ...bool) {
7963
if len(prepare) == 0 || prepare[0] {
8064
defer prepareTestEnv(t, 1)()
@@ -115,7 +99,7 @@ func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL), prepare ...bo
11599

116100
func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
117101
return func(t *testing.T) {
118-
assert.NoError(t, git.CloneWithArgs(context.Background(), u.String(), dstLocalPath, allowLFSFilters(), git.CloneRepoOptions{}))
102+
assert.NoError(t, git.CloneWithArgs(context.Background(), u.String(), dstLocalPath, git.AllowLFSFiltersArgs(), git.CloneRepoOptions{}))
119103
exist, err := util.IsExist(filepath.Join(dstLocalPath, "README.md"))
120104
assert.NoError(t, err)
121105
assert.True(t, exist)
@@ -124,7 +108,7 @@ func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
124108

125109
func doPartialGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
126110
return func(t *testing.T) {
127-
assert.NoError(t, git.CloneWithArgs(context.Background(), u.String(), dstLocalPath, allowLFSFilters(), git.CloneRepoOptions{
111+
assert.NoError(t, git.CloneWithArgs(context.Background(), u.String(), dstLocalPath, git.AllowLFSFiltersArgs(), git.CloneRepoOptions{
128112
Filter: "blob:none",
129113
}))
130114
exist, err := util.IsExist(filepath.Join(dstLocalPath, "README.md"))
@@ -197,7 +181,7 @@ func doGitCreateBranch(dstPath, branch string) func(*testing.T) {
197181

198182
func doGitCheckoutBranch(dstPath string, args ...string) func(*testing.T) {
199183
return func(t *testing.T) {
200-
_, err := git.NewCommandNoGlobals(append(append(allowLFSFilters(), "checkout"), args...)...).RunInDir(dstPath)
184+
_, err := git.NewCommandNoGlobals(append(append(git.AllowLFSFiltersArgs(), "checkout"), args...)...).RunInDir(dstPath)
201185
assert.NoError(t, err)
202186
}
203187
}
@@ -211,7 +195,7 @@ func doGitMerge(dstPath string, args ...string) func(*testing.T) {
211195

212196
func doGitPull(dstPath string, args ...string) func(*testing.T) {
213197
return func(t *testing.T) {
214-
_, err := git.NewCommandNoGlobals(append(append(allowLFSFilters(), "pull"), args...)...).RunInDir(dstPath)
198+
_, err := git.NewCommandNoGlobals(append(append(git.AllowLFSFiltersArgs(), "pull"), args...)...).RunInDir(dstPath)
215199
assert.NoError(t, err)
216200
}
217201
}

integrations/git_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func lfsCommitAndPushTest(t *testing.T, dstPath string) (littleLFS, bigLFS strin
167167
err = git.AddChanges(dstPath, false, ".gitattributes")
168168
assert.NoError(t, err)
169169

170-
err = git.CommitChangesWithArgs(dstPath, allowLFSFilters(), git.CommitChangesOptions{
170+
err = git.CommitChangesWithArgs(dstPath, git.AllowLFSFiltersArgs(), git.CommitChangesOptions{
171171
Committer: &git.Signature{
172172
173173
Name: "User Two",
@@ -346,7 +346,7 @@ func generateCommitWithNewData(size int, repoPath, email, fullName, prefix strin
346346

347347
// Commit
348348
// Now here we should explicitly allow lfs filters to run
349-
globalArgs := allowLFSFilters()
349+
globalArgs := git.AllowLFSFiltersArgs()
350350
err = git.AddChangesWithArgs(repoPath, globalArgs, false, filepath.Base(tmpFile.Name()))
351351
if err != nil {
352352
return "", err

modules/git/command.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import (
2020
)
2121

2222
var (
23-
// GlobalCommandArgs global command args for external package setting
24-
GlobalCommandArgs []string
23+
// globalCommandArgs global command args for external package setting
24+
globalCommandArgs []string
2525

2626
// defaultCommandExecutionTimeout default command execution timeout duration
2727
defaultCommandExecutionTimeout = 360 * time.Second
@@ -52,9 +52,9 @@ func NewCommand(args ...string) *Command {
5252

5353
// NewCommandContext creates and returns a new Git Command based on given command and arguments.
5454
func NewCommandContext(ctx context.Context, args ...string) *Command {
55-
// Make an explicit copy of GlobalCommandArgs, otherwise append might overwrite it
56-
cargs := make([]string, len(GlobalCommandArgs))
57-
copy(cargs, GlobalCommandArgs)
55+
// Make an explicit copy of globalCommandArgs, otherwise append might overwrite it
56+
cargs := make([]string, len(globalCommandArgs))
57+
copy(cargs, globalCommandArgs)
5858
return &Command{
5959
name: GitExecutable,
6060
args: append(cargs, args...),
@@ -278,3 +278,19 @@ func (c *Command) RunTimeout(timeout time.Duration) (string, error) {
278278
func (c *Command) Run() (string, error) {
279279
return c.RunTimeout(-1)
280280
}
281+
282+
// AllowLFSFiltersArgs return globalCommandArgs with lfs filter, it should only be used for tests
283+
func AllowLFSFiltersArgs() []string {
284+
// Now here we should explicitly allow lfs filters to run
285+
filteredLFSGlobalArgs := make([]string, len(globalCommandArgs))
286+
j := 0
287+
for _, arg := range globalCommandArgs {
288+
if strings.Contains(arg, "lfs") {
289+
j--
290+
} else {
291+
filteredLFSGlobalArgs[j] = arg
292+
j++
293+
}
294+
}
295+
return filteredLFSGlobalArgs[:j]
296+
}

modules/git/commit.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (c *Commit) GetCommitByPath(relpath string) (*Commit, error) {
8484

8585
// AddChanges marks local changes to be ready for commit.
8686
func AddChanges(repoPath string, all bool, files ...string) error {
87-
return AddChangesWithArgs(repoPath, GlobalCommandArgs, all, files...)
87+
return AddChangesWithArgs(repoPath, globalCommandArgs, all, files...)
8888
}
8989

9090
// AddChangesWithArgs marks local changes to be ready for commit.
@@ -108,8 +108,8 @@ type CommitChangesOptions struct {
108108
// CommitChanges commits local changes with given committer, author and message.
109109
// If author is nil, it will be the same as committer.
110110
func CommitChanges(repoPath string, opts CommitChangesOptions) error {
111-
cargs := make([]string, len(GlobalCommandArgs))
112-
copy(cargs, GlobalCommandArgs)
111+
cargs := make([]string, len(globalCommandArgs))
112+
copy(cargs, globalCommandArgs)
113113
return CommitChangesWithArgs(repoPath, cargs, opts)
114114
}
115115

modules/git/git.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,21 +134,21 @@ func Init(ctx context.Context) error {
134134
}
135135

136136
// force cleanup args
137-
GlobalCommandArgs = []string{}
137+
globalCommandArgs = []string{}
138138

139139
if CheckGitVersionAtLeast("2.9") == nil {
140140
// Explicitly disable credential helper, otherwise Git credentials might leak
141-
GlobalCommandArgs = append(GlobalCommandArgs, "-c", "credential.helper=")
141+
globalCommandArgs = append(globalCommandArgs, "-c", "credential.helper=")
142142
}
143143

144144
// Since git wire protocol has been released from git v2.18
145145
if setting.Git.EnableAutoGitWireProtocol && CheckGitVersionAtLeast("2.18") == nil {
146-
GlobalCommandArgs = append(GlobalCommandArgs, "-c", "protocol.version=2")
146+
globalCommandArgs = append(globalCommandArgs, "-c", "protocol.version=2")
147147
}
148148

149149
// By default partial clones are disabled, enable them from git v2.22
150150
if !setting.Git.DisablePartialClone && CheckGitVersionAtLeast("2.22") == nil {
151-
GlobalCommandArgs = append(GlobalCommandArgs, "-c", "uploadpack.allowfilter=true")
151+
globalCommandArgs = append(globalCommandArgs, "-c", "uploadpack.allowfilter=true")
152152
}
153153

154154
// Save current git version on init to gitVersion otherwise it would require an RWMutex
@@ -213,7 +213,7 @@ func Init(ctx context.Context) error {
213213
if err := checkAndSetConfig("core.protectntfs", "false", true); err != nil {
214214
return err
215215
}
216-
GlobalCommandArgs = append(GlobalCommandArgs, "-c", "core.protectntfs=false")
216+
globalCommandArgs = append(globalCommandArgs, "-c", "core.protectntfs=false")
217217
}
218218
return nil
219219
}

modules/git/lfs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func CheckLFSVersion() {
2929
logger.Error("LFS server support needs at least Git v2.1.2")
3030
} else {
3131
once.Do(func() {
32-
GlobalCommandArgs = append(GlobalCommandArgs, "-c", "filter.lfs.required=",
32+
globalCommandArgs = append(globalCommandArgs, "-c", "filter.lfs.required=",
3333
"-c", "filter.lfs.smudge=", "-c", "filter.lfs.clean=")
3434
})
3535
}

modules/git/repo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ type CloneRepoOptions struct {
106106

107107
// Clone clones original repository to target path.
108108
func Clone(ctx context.Context, from, to string, opts CloneRepoOptions) error {
109-
cargs := make([]string, len(GlobalCommandArgs))
110-
copy(cargs, GlobalCommandArgs)
109+
cargs := make([]string, len(globalCommandArgs))
110+
copy(cargs, globalCommandArgs)
111111
return CloneWithArgs(ctx, from, to, cargs, opts)
112112
}
113113

0 commit comments

Comments
 (0)