Skip to content

Commit 7b7d382

Browse files
lunnysapk
authored andcommitted
Fix datarace on git.GlobalCommandArgs on tests (#9162)
* fix datarace on git.GlobalCommandArgs on tests * fix tests * fix tests * fix tests
1 parent 9d9e6ac commit 7b7d382

File tree

5 files changed

+40
-23
lines changed

5 files changed

+40
-23
lines changed

integrations/git_helper_for_declarative_test.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ func createSSHUrl(gitPath string, u *url.URL) *url.URL {
6363

6464
func allowLFSFilters() []string {
6565
// Now here we should explicitly allow lfs filters to run
66-
globalArgs := git.GlobalCommandArgs
6766
filteredLFSGlobalArgs := make([]string, len(git.GlobalCommandArgs))
6867
j := 0
6968
for _, arg := range git.GlobalCommandArgs {
@@ -74,9 +73,7 @@ func allowLFSFilters() []string {
7473
j++
7574
}
7675
}
77-
filteredLFSGlobalArgs = filteredLFSGlobalArgs[:j]
78-
git.GlobalCommandArgs = filteredLFSGlobalArgs
79-
return globalArgs
76+
return filteredLFSGlobalArgs[:j]
8077
}
8178

8279
func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL), prepare ...bool) {
@@ -107,9 +104,7 @@ func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL), prepare ...bo
107104

108105
func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
109106
return func(t *testing.T) {
110-
oldGlobals := allowLFSFilters()
111-
assert.NoError(t, git.Clone(u.String(), dstLocalPath, git.CloneRepoOptions{}))
112-
git.GlobalCommandArgs = oldGlobals
107+
assert.NoError(t, git.CloneWithArgs(u.String(), dstLocalPath, allowLFSFilters(), git.CloneRepoOptions{}))
113108
assert.True(t, com.IsExist(filepath.Join(dstLocalPath, "README.md")))
114109
}
115110
}
@@ -170,9 +165,7 @@ func doGitCreateBranch(dstPath, branch string) func(*testing.T) {
170165

171166
func doGitCheckoutBranch(dstPath string, args ...string) func(*testing.T) {
172167
return func(t *testing.T) {
173-
oldGlobals := allowLFSFilters()
174-
_, err := git.NewCommand(append([]string{"checkout"}, args...)...).RunInDir(dstPath)
175-
git.GlobalCommandArgs = oldGlobals
168+
_, err := git.NewCommandNoGlobals(append(append(allowLFSFilters(), "checkout"), args...)...).RunInDir(dstPath)
176169
assert.NoError(t, err)
177170
}
178171
}
@@ -186,9 +179,7 @@ func doGitMerge(dstPath string, args ...string) func(*testing.T) {
186179

187180
func doGitPull(dstPath string, args ...string) func(*testing.T) {
188181
return func(t *testing.T) {
189-
oldGlobals := allowLFSFilters()
190-
_, err := git.NewCommand(append([]string{"pull"}, args...)...).RunInDir(dstPath)
191-
git.GlobalCommandArgs = oldGlobals
182+
_, err := git.NewCommandNoGlobals(append(append(allowLFSFilters(), "pull"), args...)...).RunInDir(dstPath)
192183
assert.NoError(t, err)
193184
}
194185
}

integrations/git_test.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ func lfsCommitAndPushTest(t *testing.T, dstPath string) (littleLFS, bigLFS strin
148148
assert.NoError(t, err)
149149
err = git.AddChanges(dstPath, false, ".gitattributes")
150150
assert.NoError(t, err)
151-
oldGlobals := allowLFSFilters()
152-
err = git.CommitChanges(dstPath, git.CommitChangesOptions{
151+
152+
err = git.CommitChangesWithArgs(dstPath, allowLFSFilters(), git.CommitChangesOptions{
153153
Committer: &git.Signature{
154154
155155
Name: "User Two",
@@ -163,7 +163,6 @@ func lfsCommitAndPushTest(t *testing.T, dstPath string) (littleLFS, bigLFS strin
163163
Message: fmt.Sprintf("Testing commit @ %v", time.Now()),
164164
})
165165
assert.NoError(t, err)
166-
git.GlobalCommandArgs = oldGlobals
167166

168167
littleLFS, bigLFS = commitAndPushTest(t, dstPath, prefix)
169168

@@ -307,12 +306,12 @@ func generateCommitWithNewData(size int, repoPath, email, fullName, prefix strin
307306

308307
//Commit
309308
// Now here we should explicitly allow lfs filters to run
310-
oldGlobals := allowLFSFilters()
311-
err = git.AddChanges(repoPath, false, filepath.Base(tmpFile.Name()))
309+
globalArgs := allowLFSFilters()
310+
err = git.AddChangesWithArgs(repoPath, globalArgs, false, filepath.Base(tmpFile.Name()))
312311
if err != nil {
313312
return "", err
314313
}
315-
err = git.CommitChanges(repoPath, git.CommitChangesOptions{
314+
err = git.CommitChangesWithArgs(repoPath, globalArgs, git.CommitChangesOptions{
316315
Committer: &git.Signature{
317316
Email: email,
318317
Name: fullName,
@@ -325,7 +324,6 @@ func generateCommitWithNewData(size int, repoPath, email, fullName, prefix strin
325324
},
326325
Message: fmt.Sprintf("Testing commit @ %v", time.Now()),
327326
})
328-
git.GlobalCommandArgs = oldGlobals
329327
return filepath.Base(tmpFile.Name()), err
330328
}
331329

modules/git/command.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ func NewCommand(args ...string) *Command {
5252
}
5353
}
5454

55+
// NewCommandNoGlobals creates and returns a new Git Command based on given command and arguments only with the specify args and don't care global command args
56+
func NewCommandNoGlobals(args ...string) *Command {
57+
return &Command{
58+
name: GitExecutable,
59+
args: args,
60+
}
61+
}
62+
5563
// AddArguments adds new argument(s) to the command.
5664
func (c *Command) AddArguments(args ...string) *Command {
5765
c.args = append(c.args, args...)

modules/git/commit.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,12 @@ func (c *Commit) GetCommitByPath(relpath string) (*Commit, error) {
207207

208208
// AddChanges marks local changes to be ready for commit.
209209
func AddChanges(repoPath string, all bool, files ...string) error {
210-
cmd := NewCommand("add")
210+
return AddChangesWithArgs(repoPath, GlobalCommandArgs, all, files...)
211+
}
212+
213+
// AddChangesWithArgs marks local changes to be ready for commit.
214+
func AddChangesWithArgs(repoPath string, gloablArgs []string, all bool, files ...string) error {
215+
cmd := NewCommandNoGlobals(append(gloablArgs, "add")...)
211216
if all {
212217
cmd.AddArguments("--all")
213218
}
@@ -226,7 +231,15 @@ type CommitChangesOptions struct {
226231
// CommitChanges commits local changes with given committer, author and message.
227232
// If author is nil, it will be the same as committer.
228233
func CommitChanges(repoPath string, opts CommitChangesOptions) error {
229-
cmd := NewCommand()
234+
cargs := make([]string, len(GlobalCommandArgs))
235+
copy(cargs, GlobalCommandArgs)
236+
return CommitChangesWithArgs(repoPath, cargs, opts)
237+
}
238+
239+
// CommitChangesWithArgs commits local changes with given committer, author and message.
240+
// If author is nil, it will be the same as committer.
241+
func CommitChangesWithArgs(repoPath string, args []string, opts CommitChangesOptions) error {
242+
cmd := NewCommandNoGlobals(args...)
230243
if opts.Committer != nil {
231244
cmd.AddArguments("-c", "user.name="+opts.Committer.Name, "-c", "user.email="+opts.Committer.Email)
232245
}

modules/git/repo.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,19 @@ type CloneRepoOptions struct {
165165

166166
// Clone clones original repository to target path.
167167
func Clone(from, to string, opts CloneRepoOptions) (err error) {
168+
cargs := make([]string, len(GlobalCommandArgs))
169+
copy(cargs, GlobalCommandArgs)
170+
return CloneWithArgs(from, to, cargs, opts)
171+
}
172+
173+
// CloneWithArgs original repository to target path.
174+
func CloneWithArgs(from, to string, args []string, opts CloneRepoOptions) (err error) {
168175
toDir := path.Dir(to)
169176
if err = os.MkdirAll(toDir, os.ModePerm); err != nil {
170177
return err
171178
}
172179

173-
cmd := NewCommand("clone")
180+
cmd := NewCommandNoGlobals(args...).AddArguments("clone")
174181
if opts.Mirror {
175182
cmd.AddArguments("--mirror")
176183
}

0 commit comments

Comments
 (0)