Skip to content

Commit 0b78548

Browse files
typelesstechknowlogick
authored andcommitted
Use modules/git for git commands (#6775)
1 parent 5ffc965 commit 0b78548

File tree

1 file changed

+42
-82
lines changed

1 file changed

+42
-82
lines changed

models/pull.go

Lines changed: 42 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -363,16 +363,13 @@ func (pr *PullRequest) CheckUserAllowedToMerge(doer *User) (err error) {
363363

364364
func getDiffTree(repoPath, baseBranch, headBranch string) (string, error) {
365365
getDiffTreeFromBranch := func(repoPath, baseBranch, headBranch string) (string, error) {
366-
var stdout, stderr string
366+
var outbuf, errbuf strings.Builder
367367
// Compute the diff-tree for sparse-checkout
368368
// The branch argument must be enclosed with double-quotes ("") in case it contains slashes (e.g "feature/test")
369-
stdout, stderr, err := process.GetManager().ExecDir(-1, repoPath,
370-
fmt.Sprintf("PullRequest.Merge (git diff-tree): %s", repoPath),
371-
"git", "diff-tree", "--no-commit-id", "--name-only", "-r", "--root", baseBranch, headBranch)
372-
if err != nil {
373-
return "", fmt.Errorf("git diff-tree [%s base:%s head:%s]: %s", repoPath, baseBranch, headBranch, stderr)
369+
if err := git.NewCommand("diff-tree", "--no-commit-id", "--name-only", "-r", "--root", baseBranch, headBranch).RunInDirPipeline(repoPath, &outbuf, &errbuf); err != nil {
370+
return "", fmt.Errorf("git diff-tree [%s base:%s head:%s]: %s", repoPath, baseBranch, headBranch, errbuf.String())
374371
}
375-
return stdout, nil
372+
return outbuf.String(), nil
376373
}
377374

378375
list, err := getDiffTreeFromBranch(repoPath, baseBranch, headBranch)
@@ -455,17 +452,15 @@ func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository, mergeStyle
455452
if err := addCacheRepo(tmpBasePath, headRepoPath); err != nil {
456453
return fmt.Errorf("addCacheRepo [%s -> %s]: %v", headRepoPath, tmpBasePath, err)
457454
}
458-
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
459-
fmt.Sprintf("PullRequest.Merge (git remote add): %s", tmpBasePath),
460-
"git", "remote", "add", remoteRepoName, headRepoPath); err != nil {
461-
return fmt.Errorf("git remote add [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr)
455+
456+
var errbuf strings.Builder
457+
if err := git.NewCommand("remote", "add", remoteRepoName, headRepoPath).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
458+
return fmt.Errorf("git remote add [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
462459
}
463460

464461
// Fetch head branch
465-
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
466-
fmt.Sprintf("PullRequest.Merge (git fetch): %s", tmpBasePath),
467-
"git", "fetch", remoteRepoName); err != nil {
468-
return fmt.Errorf("git fetch [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr)
462+
if err := git.NewCommand("fetch", remoteRepoName).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
463+
return fmt.Errorf("git fetch [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
469464
}
470465

471466
trackingBranch := path.Join(remoteRepoName, pr.HeadBranch)
@@ -486,108 +481,75 @@ func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository, mergeStyle
486481
return fmt.Errorf("Writing sparse-checkout file to %s: %v", sparseCheckoutListPath, err)
487482
}
488483

489-
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
490-
fmt.Sprintf("PullRequest.Merge (git config): %s", tmpBasePath),
491-
"git", "config", "--local", "core.sparseCheckout", "true"); err != nil {
492-
return fmt.Errorf("git config [core.sparsecheckout -> true]: %v", stderr)
484+
if err := git.NewCommand("config", "--local", "core.sparseCheckout", "true").RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
485+
return fmt.Errorf("git config [core.sparsecheckout -> true]: %v", errbuf.String())
493486
}
494487

495488
// Read base branch index
496-
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
497-
fmt.Sprintf("PullRequest.Merge (git read-tree): %s", tmpBasePath),
498-
"git", "read-tree", "HEAD"); err != nil {
499-
return fmt.Errorf("git read-tree HEAD: %s", stderr)
489+
if err := git.NewCommand("read-tree", "HEAD").RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
490+
return fmt.Errorf("git read-tree HEAD: %s", errbuf.String())
500491
}
501492

502493
// Merge commits.
503494
switch mergeStyle {
504495
case MergeStyleMerge:
505-
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
506-
fmt.Sprintf("PullRequest.Merge (git merge --no-ff --no-commit): %s", tmpBasePath),
507-
"git", "merge", "--no-ff", "--no-commit", trackingBranch); err != nil {
508-
return fmt.Errorf("git merge --no-ff --no-commit [%s]: %v - %s", tmpBasePath, err, stderr)
496+
if err := git.NewCommand("merge", "--no-ff", "--no-commit", trackingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
497+
return fmt.Errorf("git merge --no-ff --no-commit [%s]: %v - %s", tmpBasePath, err, errbuf.String())
509498
}
510499

511500
sig := doer.NewGitSig()
512-
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
513-
fmt.Sprintf("PullRequest.Merge (git merge): %s", tmpBasePath),
514-
"git", "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email),
515-
"-m", message); err != nil {
516-
return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, stderr)
501+
if err := git.NewCommand("commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", message).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
502+
return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, errbuf.String())
517503
}
518504
case MergeStyleRebase:
519505
// Checkout head branch
520-
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
521-
fmt.Sprintf("PullRequest.Merge (git checkout): %s", tmpBasePath),
522-
"git", "checkout", "-b", stagingBranch, trackingBranch); err != nil {
523-
return fmt.Errorf("git checkout: %s", stderr)
506+
if err := git.NewCommand("checkout", "-b", stagingBranch, trackingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
507+
return fmt.Errorf("git checkout: %s", errbuf.String())
524508
}
525509
// Rebase before merging
526-
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
527-
fmt.Sprintf("PullRequest.Merge (git rebase): %s", tmpBasePath),
528-
"git", "rebase", "-q", pr.BaseBranch); err != nil {
529-
return fmt.Errorf("git rebase [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr)
510+
if err := git.NewCommand("rebase", "-q", pr.BaseBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
511+
return fmt.Errorf("git rebase [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
530512
}
531513
// Checkout base branch again
532-
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
533-
fmt.Sprintf("PullRequest.Merge (git checkout): %s", tmpBasePath),
534-
"git", "checkout", pr.BaseBranch); err != nil {
535-
return fmt.Errorf("git checkout: %s", stderr)
514+
if err := git.NewCommand("checkout", pr.BaseBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
515+
return fmt.Errorf("git checkout: %s", errbuf.String())
536516
}
537517
// Merge fast forward
538-
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
539-
fmt.Sprintf("PullRequest.Merge (git rebase): %s", tmpBasePath),
540-
"git", "merge", "--ff-only", "-q", stagingBranch); err != nil {
541-
return fmt.Errorf("git merge --ff-only [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr)
518+
if err := git.NewCommand("merge", "--ff-only", "-q", stagingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
519+
return fmt.Errorf("git merge --ff-only [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
542520
}
543521
case MergeStyleRebaseMerge:
544522
// Checkout head branch
545-
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
546-
fmt.Sprintf("PullRequest.Merge (git checkout): %s", tmpBasePath),
547-
"git", "checkout", "-b", stagingBranch, trackingBranch); err != nil {
548-
return fmt.Errorf("git checkout: %s", stderr)
523+
if err := git.NewCommand("checkout", "-b", stagingBranch, trackingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
524+
return fmt.Errorf("git checkout: %s", errbuf.String())
549525
}
550526
// Rebase before merging
551-
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
552-
fmt.Sprintf("PullRequest.Merge (git rebase): %s", tmpBasePath),
553-
"git", "rebase", "-q", pr.BaseBranch); err != nil {
554-
return fmt.Errorf("git rebase [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr)
527+
if err := git.NewCommand("rebase", "-q", pr.BaseBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
528+
return fmt.Errorf("git rebase [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
555529
}
556530
// Checkout base branch again
557-
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
558-
fmt.Sprintf("PullRequest.Merge (git checkout): %s", tmpBasePath),
559-
"git", "checkout", pr.BaseBranch); err != nil {
560-
return fmt.Errorf("git checkout: %s", stderr)
531+
if err := git.NewCommand("checkout", pr.BaseBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
532+
return fmt.Errorf("git checkout: %s", errbuf.String())
561533
}
562534
// Prepare merge with commit
563-
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
564-
fmt.Sprintf("PullRequest.Merge (git merge): %s", tmpBasePath),
565-
"git", "merge", "--no-ff", "--no-commit", "-q", stagingBranch); err != nil {
566-
return fmt.Errorf("git merge --no-ff [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr)
535+
if err := git.NewCommand("merge", "--no-ff", "--no-commit", "-q", stagingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
536+
return fmt.Errorf("git merge --no-ff [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
567537
}
568538

569539
// Set custom message and author and create merge commit
570540
sig := doer.NewGitSig()
571-
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
572-
fmt.Sprintf("PullRequest.Merge (git commit): %s", tmpBasePath),
573-
"git", "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email),
574-
"-m", message); err != nil {
575-
return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, stderr)
541+
if err := git.NewCommand("commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", message).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
542+
return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, errbuf.String())
576543
}
577544

578545
case MergeStyleSquash:
579546
// Merge with squash
580-
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
581-
fmt.Sprintf("PullRequest.Merge (git squash): %s", tmpBasePath),
582-
"git", "merge", "-q", "--squash", trackingBranch); err != nil {
583-
return fmt.Errorf("git merge --squash [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr)
547+
if err := git.NewCommand("merge", "-q", "--squash", trackingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
548+
return fmt.Errorf("git merge --squash [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
584549
}
585550
sig := pr.Issue.Poster.NewGitSig()
586-
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
587-
fmt.Sprintf("PullRequest.Merge (git squash): %s", tmpBasePath),
588-
"git", "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email),
589-
"-m", message); err != nil {
590-
return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, stderr)
551+
if err := git.NewCommand("commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", message).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
552+
return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, errbuf.String())
591553
}
592554
default:
593555
return ErrInvalidMergeStyle{pr.BaseRepo.ID, mergeStyle}
@@ -596,10 +558,8 @@ func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository, mergeStyle
596558
env := PushingEnvironment(doer, pr.BaseRepo)
597559

598560
// Push back to upstream.
599-
if _, stderr, err := process.GetManager().ExecDirEnv(-1, tmpBasePath,
600-
fmt.Sprintf("PullRequest.Merge (git push): %s", tmpBasePath),
601-
env, "git", "push", baseGitRepo.Path, pr.BaseBranch); err != nil {
602-
return fmt.Errorf("git push: %s", stderr)
561+
if err := git.NewCommand("push", baseGitRepo.Path, pr.BaseBranch).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, nil, &errbuf); err != nil {
562+
return fmt.Errorf("git push: %s", errbuf.String())
603563
}
604564

605565
pr.MergedCommitID, err = baseGitRepo.GetBranchCommitID(pr.BaseBranch)

0 commit comments

Comments
 (0)