Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace RunInDir with RunWithContext #18978

Closed
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
7126b04
Replace RunInDir with RunWithContext
Mar 2, 2022
a4fe955
Change stdout buffer from bytes.Buffer to strings.Builder
Mar 3, 2022
f9b1be2
Add missing replacement
Mar 3, 2022
21cd5d1
Run make fmt
Mar 3, 2022
f94294a
Fix build
Mar 3, 2022
d5a1f7c
Merge branch 'main' into feature/RunInPipline
6543 Mar 3, 2022
a145a34
Fix arm64 build
Mar 3, 2022
a1ee6ce
Merge branch 'main' into feature/RunInPipline
6543 Mar 3, 2022
e6dcb76
Merge branch 'main' into feature/RunInPipline
6543 Mar 13, 2022
888637d
please ling
6543 Mar 13, 2022
b9b3cb7
fix bug
6543 Mar 13, 2022
3305f5a
fix & optimize
6543 Mar 13, 2022
0ba03ec
Merge branch 'main' into feature/RunInPipline
6543 Mar 13, 2022
ac596b1
fix integration test
6543 Mar 13, 2022
7f368c6
Fix bug
Mar 14, 2022
7058b6f
Apply suggestions from code review
6543 Mar 14, 2022
1437dfa
Merge branch 'main' into feature/RunInPipline
6543 Mar 14, 2022
54d405d
Merge branch 'main' into feature/RunInPipline
6543 Mar 14, 2022
044e53e
Fix bug
Mar 17, 2022
8c61875
Make fmt
Mar 17, 2022
4cb5ed4
Merge branch 'master' into feature/RunInPipline
6543 Mar 17, 2022
b620078
Merge branch 'main' into feature/RunInPipline
6543 Mar 17, 2022
76be496
Merge branch 'main' into feature/RunInPipline
zeripath Mar 19, 2022
eb8be76
Merge branch 'main' into feature/RunInPipline
wxiaoguang Mar 21, 2022
3a75f8e
Merge branch 'main' into feature/RunInPipline
zeripath Mar 22, 2022
6459475
Merge branch 'main' into feature/RunInPipline
wxiaoguang Mar 23, 2022
6bc20f8
Merge branch 'main' into feature/RunInPipline
wxiaoguang Mar 23, 2022
39c978b
Merge branch 'main' into feature/RunInPipline
zeripath Mar 23, 2022
0d07427
Merge branch 'main' into feature/RunInPipline
zeripath Mar 23, 2022
c4c2325
Merge branch 'main' into feature/RunInPipline
zeripath Mar 23, 2022
1f80598
Merge branch 'main' into feature/RunInPipline
zeripath Mar 26, 2022
2f2a1ad
Merge branch 'main' into feature/RunInPipline
wxiaoguang Mar 27, 2022
dd6b4b8
Merge branch 'main' into feature/RunInPipline
wxiaoguang Mar 28, 2022
c3521dc
fix merge conflicts
wxiaoguang Mar 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions integrations/api_repo_git_tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func TestAPIGitTags(t *testing.T) {
token := getTokenForLoggedInUser(t, session)

// Set up git config for the tagger
git.NewCommand(git.DefaultContext, "config", "user.name", user.Name).RunInDir(repo.RepoPath())
git.NewCommand(git.DefaultContext, "config", "user.email", user.Email).RunInDir(repo.RepoPath())
git.NewCommand(git.DefaultContext, "config", "user.name", user.Name).RunWithContext(&git.RunContext{Dir: repo.RepoPath(), Timeout: -1})
git.NewCommand(git.DefaultContext, "config", "user.email", user.Email).RunWithContext(&git.RunContext{Dir: repo.RepoPath(), Timeout: -1})

gitRepo, _ := git.OpenRepository(repo.RepoPath())
defer gitRepo.Close()
Expand Down
16 changes: 8 additions & 8 deletions integrations/git_helper_for_declarative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func doGitInitTestRepository(dstPath string) func(*testing.T) {
// Init repository in dstPath
assert.NoError(t, git.InitRepository(git.DefaultContext, dstPath, false))
// forcibly set default branch to master
_, err := git.NewCommand(git.DefaultContext, "symbolic-ref", "HEAD", git.BranchPrefix+"master").RunInDir(dstPath)
err := git.NewCommand(git.DefaultContext, "symbolic-ref", "HEAD", git.BranchPrefix+"master").RunWithContext(&git.RunContext{Dir: dstPath, Timeout: -1})
assert.NoError(t, err)
assert.NoError(t, os.WriteFile(filepath.Join(dstPath, "README.md"), []byte(fmt.Sprintf("# Testing Repository\n\nOriginally created in: %s", dstPath)), 0o644))
assert.NoError(t, git.AddChanges(dstPath, true))
Expand All @@ -153,49 +153,49 @@ func doGitInitTestRepository(dstPath string) func(*testing.T) {

func doGitAddRemote(dstPath, remoteName string, u *url.URL) func(*testing.T) {
return func(t *testing.T) {
_, err := git.NewCommand(git.DefaultContext, "remote", "add", remoteName, u.String()).RunInDir(dstPath)
err := git.NewCommand(git.DefaultContext, "remote", "add", remoteName, u.String()).RunWithContext(&git.RunContext{Dir: dstPath, Timeout: -1})
assert.NoError(t, err)
}
}

func doGitPushTestRepository(dstPath string, args ...string) func(*testing.T) {
return func(t *testing.T) {
_, err := git.NewCommand(git.DefaultContext, append([]string{"push", "-u"}, args...)...).RunInDir(dstPath)
err := git.NewCommand(git.DefaultContext, append([]string{"push", "-u"}, args...)...).RunWithContext(&git.RunContext{Dir: dstPath, Timeout: -1})
assert.NoError(t, err)
}
}

func doGitPushTestRepositoryFail(dstPath string, args ...string) func(*testing.T) {
return func(t *testing.T) {
_, err := git.NewCommand(git.DefaultContext, append([]string{"push"}, args...)...).RunInDir(dstPath)
err := git.NewCommand(git.DefaultContext, append([]string{"push"}, args...)...).RunWithContext(&git.RunContext{Dir: dstPath, Timeout: -1})
assert.Error(t, err)
}
}

func doGitCreateBranch(dstPath, branch string) func(*testing.T) {
return func(t *testing.T) {
_, err := git.NewCommand(git.DefaultContext, "checkout", "-b", branch).RunInDir(dstPath)
err := git.NewCommand(git.DefaultContext, "checkout", "-b", branch).RunWithContext(&git.RunContext{Dir: dstPath, Timeout: -1})
assert.NoError(t, err)
}
}

func doGitCheckoutBranch(dstPath string, args ...string) func(*testing.T) {
return func(t *testing.T) {
_, err := git.NewCommandNoGlobals(append(append(git.AllowLFSFiltersArgs(), "checkout"), args...)...).RunInDir(dstPath)
err := git.NewCommandNoGlobals(append(append(git.AllowLFSFiltersArgs(), "checkout"), args...)...).RunWithContext(&git.RunContext{Dir: dstPath, Timeout: -1})
assert.NoError(t, err)
}
}

func doGitMerge(dstPath string, args ...string) func(*testing.T) {
return func(t *testing.T) {
_, err := git.NewCommand(git.DefaultContext, append([]string{"merge"}, args...)...).RunInDir(dstPath)
err := git.NewCommand(git.DefaultContext, append([]string{"merge"}, args...)...).RunWithContext(&git.RunContext{Dir: dstPath, Timeout: -1})
assert.NoError(t, err)
}
}

func doGitPull(dstPath string, args ...string) func(*testing.T) {
return func(t *testing.T) {
_, err := git.NewCommandNoGlobals(append(append(git.AllowLFSFiltersArgs(), "pull"), args...)...).RunInDir(dstPath)
err := git.NewCommandNoGlobals(append(append(git.AllowLFSFiltersArgs(), "pull"), args...)...).RunWithContext(&git.RunContext{Dir: dstPath, Timeout: -1})
assert.NoError(t, err)
}
}
22 changes: 11 additions & 11 deletions integrations/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ func lfsCommitAndPushTest(t *testing.T, dstPath string) (littleLFS, bigLFS strin
return
}
prefix := "lfs-data-file-"
_, err := git.NewCommand(git.DefaultContext, "lfs").AddArguments("install").RunInDir(dstPath)
err := git.NewCommand(git.DefaultContext, "lfs").AddArguments("install").RunWithContext(&git.RunContext{Dir: dstPath, Timeout: -1})
assert.NoError(t, err)
_, err = git.NewCommand(git.DefaultContext, "lfs").AddArguments("track", prefix+"*").RunInDir(dstPath)
err = git.NewCommand(git.DefaultContext, "lfs").AddArguments("track", prefix+"*").RunWithContext(&git.RunContext{Dir: dstPath, Timeout: -1})
assert.NoError(t, err)
err = git.AddChanges(dstPath, false, ".gitattributes")
assert.NoError(t, err)
Expand Down Expand Up @@ -292,20 +292,20 @@ func lockTest(t *testing.T, repoPath string) {
}

func lockFileTest(t *testing.T, filename, repoPath string) {
_, err := git.NewCommand(git.DefaultContext, "lfs").AddArguments("locks").RunInDir(repoPath)
err := git.NewCommand(git.DefaultContext, "lfs").AddArguments("locks").RunWithContext(&git.RunContext{Dir: repoPath, Timeout: -1})
assert.NoError(t, err)
_, err = git.NewCommand(git.DefaultContext, "lfs").AddArguments("lock", filename).RunInDir(repoPath)
err = git.NewCommand(git.DefaultContext, "lfs").AddArguments("lock", filename).RunWithContext(&git.RunContext{Dir: repoPath, Timeout: -1})
assert.NoError(t, err)
_, err = git.NewCommand(git.DefaultContext, "lfs").AddArguments("locks").RunInDir(repoPath)
err = git.NewCommand(git.DefaultContext, "lfs").AddArguments("locks").RunWithContext(&git.RunContext{Dir: repoPath, Timeout: -1})
assert.NoError(t, err)
_, err = git.NewCommand(git.DefaultContext, "lfs").AddArguments("unlock", filename).RunInDir(repoPath)
err = git.NewCommand(git.DefaultContext, "lfs").AddArguments("unlock", filename).RunWithContext(&git.RunContext{Dir: repoPath, Timeout: -1})
assert.NoError(t, err)
}

func doCommitAndPush(t *testing.T, size int, repoPath, prefix string) string {
name, err := generateCommitWithNewData(size, repoPath, "user2@example.com", "User Two", prefix)
assert.NoError(t, err)
_, err = git.NewCommand(git.DefaultContext, "push", "origin", "master").RunInDir(repoPath) // Push
err = git.NewCommand(git.DefaultContext, "push", "origin", "master").RunWithContext(&git.RunContext{Dir: repoPath, Timeout: -1}) // Push
assert.NoError(t, err)
return name
}
Expand Down Expand Up @@ -671,7 +671,7 @@ func doCreateAgitFlowPull(dstPath string, ctx *APITestContext, baseBranch, headB
})

t.Run("Push", func(t *testing.T) {
_, err := git.NewCommand(git.DefaultContext, "push", "origin", "HEAD:refs/for/master", "-o", "topic="+headBranch).RunInDir(dstPath)
err := git.NewCommand(git.DefaultContext, "push", "origin", "HEAD:refs/for/master", "-o", "topic="+headBranch).RunWithContext(&git.RunContext{Dir: dstPath, Timeout: -1})
if !assert.NoError(t, err) {
return
}
Expand All @@ -692,7 +692,7 @@ func doCreateAgitFlowPull(dstPath string, ctx *APITestContext, baseBranch, headB
assert.Contains(t, "Testing commit 1", prMsg.Body)
assert.Equal(t, commit, prMsg.Head.Sha)

_, err = git.NewCommand(git.DefaultContext, "push", "origin", "HEAD:refs/for/master/test/"+headBranch).RunInDir(dstPath)
err = git.NewCommand(git.DefaultContext, "push", "origin", "HEAD:refs/for/master/test/"+headBranch).RunWithContext(&git.RunContext{Dir: dstPath, Timeout: -1})
if !assert.NoError(t, err) {
return
}
Expand Down Expand Up @@ -745,7 +745,7 @@ func doCreateAgitFlowPull(dstPath string, ctx *APITestContext, baseBranch, headB
})

t.Run("Push2", func(t *testing.T) {
_, err := git.NewCommand(git.DefaultContext, "push", "origin", "HEAD:refs/for/master", "-o", "topic="+headBranch).RunInDir(dstPath)
err := git.NewCommand(git.DefaultContext, "push", "origin", "HEAD:refs/for/master", "-o", "topic="+headBranch).RunWithContext(&git.RunContext{Dir: dstPath, Timeout: -1})
if !assert.NoError(t, err) {
return
}
Expand All @@ -757,7 +757,7 @@ func doCreateAgitFlowPull(dstPath string, ctx *APITestContext, baseBranch, headB
assert.Equal(t, false, prMsg.HasMerged)
assert.Equal(t, commit, prMsg.Head.Sha)

_, err = git.NewCommand(git.DefaultContext, "push", "origin", "HEAD:refs/for/master/test/"+headBranch).RunInDir(dstPath)
err = git.NewCommand(git.DefaultContext, "push", "origin", "HEAD:refs/for/master/test/"+headBranch).RunWithContext(&git.RunContext{Dir: dstPath, Timeout: -1})
if !assert.NoError(t, err) {
return
}
Expand Down
11 changes: 6 additions & 5 deletions integrations/pull_merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func TestCantMergeUnrelated(t *testing.T) {
}).(*repo_model.Repository)
path := repo_model.RepoPath(user1.Name, repo1.Name)

_, err := git.NewCommand(git.DefaultContext, "read-tree", "--empty").RunInDir(path)
err := git.NewCommand(git.DefaultContext, "read-tree", "--empty").RunWithContext(&git.RunContext{Dir: path, Timeout: -1})
assert.NoError(t, err)

stdin := bytes.NewBufferString("Unrelated File")
Expand All @@ -284,12 +284,13 @@ func TestCantMergeUnrelated(t *testing.T) {
assert.NoError(t, err)
sha := strings.TrimSpace(stdout.String())

_, err = git.NewCommand(git.DefaultContext, "update-index", "--add", "--replace", "--cacheinfo", "100644", sha, "somewher-over-the-rainbow").RunInDir(path)
err = git.NewCommand(git.DefaultContext, "update-index", "--add", "--replace", "--cacheinfo", "100644", sha, "somewher-over-the-rainbow").RunWithContext(&git.RunContext{Dir: path, Timeout: -1})
assert.NoError(t, err)

treeSha, err := git.NewCommand(git.DefaultContext, "write-tree").RunInDir(path)
stdoutTreeSha := new(strings.Builder)
err = git.NewCommand(git.DefaultContext, "write-tree").RunWithContext(&git.RunContext{Dir: path, Timeout: -1, Stdout: stdoutTreeSha})
assert.NoError(t, err)
treeSha = strings.TrimSpace(treeSha)
treeSha := strings.TrimSpace(stdoutTreeSha.String())

commitTimeStr := time.Now().Format(time.RFC3339)
doerSig := user1.NewGitSig()
Expand Down Expand Up @@ -318,7 +319,7 @@ func TestCantMergeUnrelated(t *testing.T) {
assert.NoError(t, err)
commitSha := strings.TrimSpace(stdout.String())

_, err = git.NewCommand(git.DefaultContext, "branch", "unrelated", commitSha).RunInDir(path)
err = git.NewCommand(git.DefaultContext, "branch", "unrelated", commitSha).RunWithContext(&git.RunContext{Dir: path, Timeout: -1})
assert.NoError(t, err)

testEditFileToNewBranch(t, session, "user1", "repo1", "master", "conflict", "README.md", "Hello, World (Edited Once)\n")
Expand Down
4 changes: 2 additions & 2 deletions integrations/repo_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ func TestCreateNewTagProtected(t *testing.T) {

doGitClone(dstPath, u)(t)

_, err = git.NewCommand(git.DefaultContext, "tag", "v-2").RunInDir(dstPath)
err = git.NewCommand(git.DefaultContext, "tag", "v-2").RunWithContext(&git.RunContext{Dir: dstPath, Timeout: -1})
assert.NoError(t, err)

_, err = git.NewCommand(git.DefaultContext, "push", "--tags").RunInDir(dstPath)
err = git.NewCommand(git.DefaultContext, "push", "--tags").RunWithContext(&git.RunContext{Dir: dstPath, Timeout: -1})
assert.Error(t, err)
assert.Contains(t, err.Error(), "Tag v-2 is protected")
})
Expand Down
15 changes: 11 additions & 4 deletions models/migrations/v128.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,25 @@ func fixMergeBase(x *xorm.Engine) error {
repoPath := filepath.Join(userPath, strings.ToLower(baseRepo.Name)+".git")

gitRefName := fmt.Sprintf("refs/pull/%d/head", pr.Index)
stdout := new(strings.Builder)

if !pr.HasMerged {
var err error
pr.MergeBase, err = git.NewCommand(git.DefaultContext, "merge-base", "--", pr.BaseBranch, gitRefName).RunInDir(repoPath)
err = git.NewCommand(git.DefaultContext, "merge-base", "--", pr.BaseBranch, gitRefName).RunWithContext(&git.RunContext{Dir: repoPath, Timeout: -1, Stdout: stdout})
pr.MergeBase = stdout.String()
if err != nil {
var err2 error
pr.MergeBase, err2 = git.NewCommand(git.DefaultContext, "rev-parse", git.BranchPrefix+pr.BaseBranch).RunInDir(repoPath)
stdout.Reset()
err2 = git.NewCommand(git.DefaultContext, "rev-parse", git.BranchPrefix+pr.BaseBranch).RunWithContext(&git.RunContext{Dir: repoPath, Timeout: -1, Stdout: stdout})
pr.MergeBase = stdout.String()
if err2 != nil {
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)
continue
}
}
} else {
parentsString, err := git.NewCommand(git.DefaultContext, "rev-list", "--parents", "-n", "1", pr.MergedCommitID).RunInDir(repoPath)
err := git.NewCommand(git.DefaultContext, "rev-list", "--parents", "-n", "1", pr.MergedCommitID).RunWithContext(&git.RunContext{Dir: repoPath, Timeout: -1, Stdout: stdout})
parentsString := stdout.String()
if err != nil {
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)
continue
Expand All @@ -106,7 +111,9 @@ func fixMergeBase(x *xorm.Engine) error {
args := append([]string{"merge-base", "--"}, parents[1:]...)
args = append(args, gitRefName)

pr.MergeBase, err = git.NewCommand(git.DefaultContext, args...).RunInDir(repoPath)
stdout.Reset()
err = git.NewCommand(git.DefaultContext, args...).RunWithContext(&git.RunContext{Dir: repoPath, Timeout: -1, Stdout: stdout})
pr.MergeBase = stdout.String()
if err != nil {
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)
continue
Expand Down
8 changes: 6 additions & 2 deletions models/migrations/v134.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ func refixMergeBase(x *xorm.Engine) error {

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

parentsString, err := git.NewCommand(git.DefaultContext, "rev-list", "--parents", "-n", "1", pr.MergedCommitID).RunInDir(repoPath)
stdout := new(strings.Builder)
err = git.NewCommand(git.DefaultContext, "rev-list", "--parents", "-n", "1", pr.MergedCommitID).RunWithContext(&git.RunContext{Dir: repoPath, Timeout: -1, Stdout: stdout})
parentsString := stdout.String()
if err != nil {
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)
continue
Expand All @@ -94,7 +96,9 @@ func refixMergeBase(x *xorm.Engine) error {
args := append([]string{"merge-base", "--"}, parents[1:]...)
args = append(args, gitRefName)

pr.MergeBase, err = git.NewCommand(git.DefaultContext, args...).RunInDir(repoPath)
stdout.Reset()
err = git.NewCommand(git.DefaultContext, args...).RunWithContext(&git.RunContext{Dir: repoPath, Timeout: -1, Stdout: stdout})
pr.MergeBase = stdout.String()
if err != nil {
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)
continue
Expand Down
15 changes: 11 additions & 4 deletions modules/doctor/mergebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,25 @@ func checkPRMergeBase(ctx context.Context, logger log.Logger, autofix bool) erro
repoPath := repo.RepoPath()

oldMergeBase := pr.MergeBase
stdout := new(strings.Builder)

if !pr.HasMerged {
var err error
pr.MergeBase, err = git.NewCommand(ctx, "merge-base", "--", pr.BaseBranch, pr.GetGitRefName()).RunInDir(repoPath)
err = git.NewCommand(ctx, "merge-base", "--", pr.BaseBranch, pr.GetGitRefName()).RunWithContext(&git.RunContext{Dir: repoPath, Timeout: -1, Stdout: stdout})
pr.MergeBase = stdout.String()
if err != nil {
var err2 error
pr.MergeBase, err2 = git.NewCommand(ctx, "rev-parse", git.BranchPrefix+pr.BaseBranch).RunInDir(repoPath)
stdout.Reset()
err2 = git.NewCommand(ctx, "rev-parse", git.BranchPrefix+pr.BaseBranch).RunWithContext(&git.RunContext{Dir: repoPath, Timeout: -1, Stdout: stdout})
pr.MergeBase = stdout.String()
if err2 != nil {
logger.Warn("Unable to get merge base for PR ID %d, #%d onto %s in %s/%s. Error: %v & %v", pr.ID, pr.Index, pr.BaseBranch, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, err, err2)
return nil
}
}
} else {
parentsString, err := git.NewCommand(ctx, "rev-list", "--parents", "-n", "1", pr.MergedCommitID).RunInDir(repoPath)
err := git.NewCommand(ctx, "rev-list", "--parents", "-n", "1", pr.MergedCommitID).RunWithContext(&git.RunContext{Dir: repoPath, Timeout: -1, Stdout: stdout})
parentsString := stdout.String()
if err != nil {
logger.Warn("Unable to get parents for merged PR ID %d, #%d onto %s in %s/%s. Error: %v", pr.ID, pr.Index, pr.BaseBranch, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, err)
return nil
Expand All @@ -67,7 +72,9 @@ func checkPRMergeBase(ctx context.Context, logger log.Logger, autofix bool) erro
args := append([]string{"merge-base", "--"}, parents[1:]...)
args = append(args, pr.GetGitRefName())

pr.MergeBase, err = git.NewCommand(ctx, args...).RunInDir(repoPath)
stdout.Reset()
err = git.NewCommand(ctx, args...).RunWithContext(&git.RunContext{Dir: repoPath, Timeout: -1, Stdout: stdout})
pr.MergeBase = stdout.String()
if err != nil {
logger.Warn("Unable to get merge base for merged PR ID %d, #%d onto %s in %s/%s. Error: %v", pr.ID, pr.Index, pr.BaseBranch, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, err)
return nil
Expand Down
6 changes: 4 additions & 2 deletions modules/doctor/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,13 @@ func checkEnablePushOptions(ctx context.Context, logger log.Logger, autofix bool
defer r.Close()

if autofix {
_, err := git.NewCommand(ctx, "config", "receive.advertisePushOptions", "true").RunInDir(r.Path)
err := git.NewCommand(ctx, "config", "receive.advertisePushOptions", "true").RunWithContext(&git.RunContext{Dir: r.Path, Timeout: -1})
return err
}

value, err := git.NewCommand(ctx, "config", "receive.advertisePushOptions").RunInDir(r.Path)
stdout := new(strings.Builder)
err = git.NewCommand(ctx, "config", "receive.advertisePushOptions").RunWithContext(&git.RunContext{Dir: r.Path, Timeout: -1, Stdout: stdout})
value := stdout.String()
if err != nil {
return err
}
Expand Down
18 changes: 0 additions & 18 deletions modules/git/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,30 +222,12 @@ func (c *Command) RunInDirTimeoutEnv(env []string, timeout time.Duration, dir st
return stdout.Bytes(), nil
}

// RunInDirPipeline executes the command in given directory,
// it pipes stdout and stderr to given io.Writer.
func (c *Command) RunInDirPipeline(dir string, stdout, stderr io.Writer) error {
return c.RunInDirFullPipeline(dir, stdout, stderr, nil)
}

// RunInDirFullPipeline executes the command in given directory,
// it pipes stdout and stderr to given io.Writer.
func (c *Command) RunInDirFullPipeline(dir string, stdout, stderr io.Writer, stdin io.Reader) error {
return c.RunInDirTimeoutFullPipeline(-1, dir, stdout, stderr, stdin)
}

// RunInDirBytes executes the command in given directory
// and returns stdout in []byte and error (combined with stderr).
func (c *Command) RunInDirBytes(dir string) ([]byte, error) {
return c.RunInDirTimeout(-1, dir)
}

// RunInDir executes the command in given directory
// and returns stdout in string and error (combined with stderr).
func (c *Command) RunInDir(dir string) (string, error) {
Comment on lines -260 to -262
Copy link
Contributor

@wxiaoguang wxiaoguang Mar 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The key point is here, it returns "stdout in string and error (combined with stderr)"

So the RunInXxx can not be replaced by RunWithContext directly

return c.RunInDirWithEnv(dir, nil)
}

// RunInDirWithEnv executes the command in given directory
// and returns stdout in string and error (combined with stderr).
func (c *Command) RunInDirWithEnv(dir string, env []string) (string, error) {
Expand Down
Loading