* fix datarace on git.GlobalCommandArgs on tests * fix tests * fix tests * fix teststags/v1.11.0-rc1
func allowLFSFilters() []string { | func allowLFSFilters() []string { | ||||
// Now here we should explicitly allow lfs filters to run | // Now here we should explicitly allow lfs filters to run | ||||
globalArgs := git.GlobalCommandArgs | |||||
filteredLFSGlobalArgs := make([]string, len(git.GlobalCommandArgs)) | filteredLFSGlobalArgs := make([]string, len(git.GlobalCommandArgs)) | ||||
j := 0 | j := 0 | ||||
for _, arg := range git.GlobalCommandArgs { | for _, arg := range git.GlobalCommandArgs { | ||||
j++ | j++ | ||||
} | } | ||||
} | } | ||||
filteredLFSGlobalArgs = filteredLFSGlobalArgs[:j] | |||||
git.GlobalCommandArgs = filteredLFSGlobalArgs | |||||
return globalArgs | |||||
return filteredLFSGlobalArgs[:j] | |||||
} | } | ||||
func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL), prepare ...bool) { | func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL), prepare ...bool) { | ||||
func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) { | func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) { | ||||
return func(t *testing.T) { | return func(t *testing.T) { | ||||
oldGlobals := allowLFSFilters() | |||||
assert.NoError(t, git.Clone(u.String(), dstLocalPath, git.CloneRepoOptions{})) | |||||
git.GlobalCommandArgs = oldGlobals | |||||
assert.NoError(t, git.CloneWithArgs(u.String(), dstLocalPath, allowLFSFilters(), git.CloneRepoOptions{})) | |||||
assert.True(t, com.IsExist(filepath.Join(dstLocalPath, "README.md"))) | assert.True(t, com.IsExist(filepath.Join(dstLocalPath, "README.md"))) | ||||
} | } | ||||
} | } | ||||
func doGitCheckoutBranch(dstPath string, args ...string) func(*testing.T) { | func doGitCheckoutBranch(dstPath string, args ...string) func(*testing.T) { | ||||
return func(t *testing.T) { | return func(t *testing.T) { | ||||
oldGlobals := allowLFSFilters() | |||||
_, err := git.NewCommand(append([]string{"checkout"}, args...)...).RunInDir(dstPath) | |||||
git.GlobalCommandArgs = oldGlobals | |||||
_, err := git.NewCommandNoGlobals(append(append(allowLFSFilters(), "checkout"), args...)...).RunInDir(dstPath) | |||||
assert.NoError(t, err) | assert.NoError(t, err) | ||||
} | } | ||||
} | } | ||||
func doGitPull(dstPath string, args ...string) func(*testing.T) { | func doGitPull(dstPath string, args ...string) func(*testing.T) { | ||||
return func(t *testing.T) { | return func(t *testing.T) { | ||||
oldGlobals := allowLFSFilters() | |||||
_, err := git.NewCommand(append([]string{"pull"}, args...)...).RunInDir(dstPath) | |||||
git.GlobalCommandArgs = oldGlobals | |||||
_, err := git.NewCommandNoGlobals(append(append(allowLFSFilters(), "pull"), args...)...).RunInDir(dstPath) | |||||
assert.NoError(t, err) | assert.NoError(t, err) | ||||
} | } | ||||
} | } |
assert.NoError(t, err) | assert.NoError(t, err) | ||||
err = git.AddChanges(dstPath, false, ".gitattributes") | err = git.AddChanges(dstPath, false, ".gitattributes") | ||||
assert.NoError(t, err) | assert.NoError(t, err) | ||||
oldGlobals := allowLFSFilters() | |||||
err = git.CommitChanges(dstPath, git.CommitChangesOptions{ | |||||
err = git.CommitChangesWithArgs(dstPath, allowLFSFilters(), git.CommitChangesOptions{ | |||||
Committer: &git.Signature{ | Committer: &git.Signature{ | ||||
Email: "user2@example.com", | Email: "user2@example.com", | ||||
Name: "User Two", | Name: "User Two", | ||||
Message: fmt.Sprintf("Testing commit @ %v", time.Now()), | Message: fmt.Sprintf("Testing commit @ %v", time.Now()), | ||||
}) | }) | ||||
assert.NoError(t, err) | assert.NoError(t, err) | ||||
git.GlobalCommandArgs = oldGlobals | |||||
littleLFS, bigLFS = commitAndPushTest(t, dstPath, prefix) | littleLFS, bigLFS = commitAndPushTest(t, dstPath, prefix) | ||||
//Commit | //Commit | ||||
// Now here we should explicitly allow lfs filters to run | // Now here we should explicitly allow lfs filters to run | ||||
oldGlobals := allowLFSFilters() | |||||
err = git.AddChanges(repoPath, false, filepath.Base(tmpFile.Name())) | |||||
globalArgs := allowLFSFilters() | |||||
err = git.AddChangesWithArgs(repoPath, globalArgs, false, filepath.Base(tmpFile.Name())) | |||||
if err != nil { | if err != nil { | ||||
return "", err | return "", err | ||||
} | } | ||||
err = git.CommitChanges(repoPath, git.CommitChangesOptions{ | |||||
err = git.CommitChangesWithArgs(repoPath, globalArgs, git.CommitChangesOptions{ | |||||
Committer: &git.Signature{ | Committer: &git.Signature{ | ||||
Email: email, | Email: email, | ||||
Name: fullName, | Name: fullName, | ||||
}, | }, | ||||
Message: fmt.Sprintf("Testing commit @ %v", time.Now()), | Message: fmt.Sprintf("Testing commit @ %v", time.Now()), | ||||
}) | }) | ||||
git.GlobalCommandArgs = oldGlobals | |||||
return filepath.Base(tmpFile.Name()), err | return filepath.Base(tmpFile.Name()), err | ||||
} | } | ||||
} | } | ||||
} | } | ||||
// 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 | |||||
func NewCommandNoGlobals(args ...string) *Command { | |||||
return &Command{ | |||||
name: GitExecutable, | |||||
args: args, | |||||
} | |||||
} | |||||
// AddArguments adds new argument(s) to the command. | // AddArguments adds new argument(s) to the command. | ||||
func (c *Command) AddArguments(args ...string) *Command { | func (c *Command) AddArguments(args ...string) *Command { | ||||
c.args = append(c.args, args...) | c.args = append(c.args, args...) |
// AddChanges marks local changes to be ready for commit. | // AddChanges marks local changes to be ready for commit. | ||||
func AddChanges(repoPath string, all bool, files ...string) error { | func AddChanges(repoPath string, all bool, files ...string) error { | ||||
cmd := NewCommand("add") | |||||
return AddChangesWithArgs(repoPath, GlobalCommandArgs, all, files...) | |||||
} | |||||
// AddChangesWithArgs marks local changes to be ready for commit. | |||||
func AddChangesWithArgs(repoPath string, gloablArgs []string, all bool, files ...string) error { | |||||
cmd := NewCommandNoGlobals(append(gloablArgs, "add")...) | |||||
if all { | if all { | ||||
cmd.AddArguments("--all") | cmd.AddArguments("--all") | ||||
} | } | ||||
// CommitChanges commits local changes with given committer, author and message. | // CommitChanges commits local changes with given committer, author and message. | ||||
// If author is nil, it will be the same as committer. | // If author is nil, it will be the same as committer. | ||||
func CommitChanges(repoPath string, opts CommitChangesOptions) error { | func CommitChanges(repoPath string, opts CommitChangesOptions) error { | ||||
cmd := NewCommand() | |||||
cargs := make([]string, len(GlobalCommandArgs)) | |||||
copy(cargs, GlobalCommandArgs) | |||||
return CommitChangesWithArgs(repoPath, cargs, opts) | |||||
} | |||||
// CommitChangesWithArgs commits local changes with given committer, author and message. | |||||
// If author is nil, it will be the same as committer. | |||||
func CommitChangesWithArgs(repoPath string, args []string, opts CommitChangesOptions) error { | |||||
cmd := NewCommandNoGlobals(args...) | |||||
if opts.Committer != nil { | if opts.Committer != nil { | ||||
cmd.AddArguments("-c", "user.name="+opts.Committer.Name, "-c", "user.email="+opts.Committer.Email) | cmd.AddArguments("-c", "user.name="+opts.Committer.Name, "-c", "user.email="+opts.Committer.Email) | ||||
} | } |
// Clone clones original repository to target path. | // Clone clones original repository to target path. | ||||
func Clone(from, to string, opts CloneRepoOptions) (err error) { | func Clone(from, to string, opts CloneRepoOptions) (err error) { | ||||
cargs := make([]string, len(GlobalCommandArgs)) | |||||
copy(cargs, GlobalCommandArgs) | |||||
return CloneWithArgs(from, to, cargs, opts) | |||||
} | |||||
// CloneWithArgs original repository to target path. | |||||
func CloneWithArgs(from, to string, args []string, opts CloneRepoOptions) (err error) { | |||||
toDir := path.Dir(to) | toDir := path.Dir(to) | ||||
if err = os.MkdirAll(toDir, os.ModePerm); err != nil { | if err = os.MkdirAll(toDir, os.ModePerm); err != nil { | ||||
return err | return err | ||||
} | } | ||||
cmd := NewCommand("clone") | |||||
cmd := NewCommandNoGlobals(args...).AddArguments("clone") | |||||
if opts.Mirror { | if opts.Mirror { | ||||
cmd.AddArguments("--mirror") | cmd.AddArguments("--mirror") | ||||
} | } |