Skip to content

Commit 108e751

Browse files
Mura Litypeless
authored andcommitted
Use modules/git for git commands
1 parent 5ffc965 commit 108e751

File tree

1 file changed

+40
-82
lines changed

1 file changed

+40
-82
lines changed

models/pull.go

Lines changed: 40 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,13 @@ 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+
if err := git.NewCommand("remote", "add", remoteRepoName, headRepoPath).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
456+
return fmt.Errorf("git remote add [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
462457
}
463458

464459
// 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)
460+
if err := git.NewCommand("fetch", remoteRepoName).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
461+
return fmt.Errorf("git fetch [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
469462
}
470463

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

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)
482+
if err := git.NewCommand("config", "--local", "core.sparseCheckout", "true").RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
483+
return fmt.Errorf("git config [core.sparsecheckout -> true]: %v", errbuf.String())
493484
}
494485

495486
// 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)
487+
if err := git.NewCommand("read-tree", "HEAD").RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
488+
return fmt.Errorf("git read-tree HEAD: %s", errbuf.String())
500489
}
501490

502491
// Merge commits.
503492
switch mergeStyle {
504493
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)
494+
if err := git.NewCommand("merge", "--no-ff", "--no-commit", trackingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
495+
return fmt.Errorf("git merge --no-ff --no-commit [%s]: %v - %s", tmpBasePath, err, errbuf.String())
509496
}
510497

511498
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)
499+
if err := git.NewCommand("commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", message).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
500+
return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, errbuf.String())
517501
}
518502
case MergeStyleRebase:
519503
// 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)
504+
if err := git.NewCommand("checkout", "-b", stagingBranch, trackingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
505+
return fmt.Errorf("git checkout: %s", errbuf.String())
524506
}
525507
// 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)
508+
if err := git.NewCommand("rebase", "-q", pr.BaseBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
509+
return fmt.Errorf("git rebase [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
530510
}
531511
// 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)
512+
if err := git.NewCommand("checkout", pr.BaseBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
513+
return fmt.Errorf("git checkout: %s", errbuf.String())
536514
}
537515
// 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)
516+
if err := git.NewCommand("merge", "--ff-only", "-q", stagingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
517+
return fmt.Errorf("git merge --ff-only [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
542518
}
543519
case MergeStyleRebaseMerge:
544520
// 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)
521+
if err := git.NewCommand("checkout", "-b", stagingBranch, trackingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
522+
return fmt.Errorf("git checkout: %s", errbuf.String())
549523
}
550524
// 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)
525+
if err := git.NewCommand("rebase", "-q", pr.BaseBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
526+
return fmt.Errorf("git rebase [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
555527
}
556528
// 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)
529+
if err := git.NewCommand("checkout", pr.BaseBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
530+
return fmt.Errorf("git checkout: %s", errbuf.String())
561531
}
562532
// 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)
533+
if err := git.NewCommand("merge", "--no-ff", "--no-commit", "-q", stagingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
534+
return fmt.Errorf("git merge --no-ff [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
567535
}
568536

569537
// Set custom message and author and create merge commit
570538
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)
539+
if err := git.NewCommand("commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", message).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
540+
return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, errbuf.String())
576541
}
577542

578543
case MergeStyleSquash:
579544
// 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)
545+
if err := git.NewCommand("merge", "-q", "--squash", trackingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
546+
return fmt.Errorf("git merge --squash [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
584547
}
585548
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)
549+
if err := git.NewCommand("commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", message).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
550+
return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, errbuf.String())
591551
}
592552
default:
593553
return ErrInvalidMergeStyle{pr.BaseRepo.ID, mergeStyle}
@@ -596,10 +556,8 @@ func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository, mergeStyle
596556
env := PushingEnvironment(doer, pr.BaseRepo)
597557

598558
// 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)
559+
if err := git.NewCommand("push", baseGitRepo.Path, pr.BaseBranch).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, nil, &errbuf); err != nil {
560+
return fmt.Errorf("git push: %s", errbuf.String())
603561
}
604562

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

0 commit comments

Comments
 (0)