@@ -363,16 +363,13 @@ func (pr *PullRequest) CheckUserAllowedToMerge(doer *User) (err error) {
363
363
364
364
func getDiffTree (repoPath , baseBranch , headBranch string ) (string , error ) {
365
365
getDiffTreeFromBranch := func (repoPath , baseBranch , headBranch string ) (string , error ) {
366
- var stdout , stderr string
366
+ var outbuf , errbuf strings. Builder
367
367
// Compute the diff-tree for sparse-checkout
368
368
// 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 ())
374
371
}
375
- return stdout , nil
372
+ return outbuf . String () , nil
376
373
}
377
374
378
375
list , err := getDiffTreeFromBranch (repoPath , baseBranch , headBranch )
@@ -455,17 +452,13 @@ func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository, mergeStyle
455
452
if err := addCacheRepo (tmpBasePath , headRepoPath ); err != nil {
456
453
return fmt .Errorf ("addCacheRepo [%s -> %s]: %v" , headRepoPath , tmpBasePath , err )
457
454
}
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 ())
462
457
}
463
458
464
459
// 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 ())
469
462
}
470
463
471
464
trackingBranch := path .Join (remoteRepoName , pr .HeadBranch )
@@ -486,108 +479,75 @@ func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository, mergeStyle
486
479
return fmt .Errorf ("Writing sparse-checkout file to %s: %v" , sparseCheckoutListPath , err )
487
480
}
488
481
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 ())
493
484
}
494
485
495
486
// 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 ())
500
489
}
501
490
502
491
// Merge commits.
503
492
switch mergeStyle {
504
493
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 ())
509
496
}
510
497
511
498
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 ())
517
501
}
518
502
case MergeStyleRebase :
519
503
// 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 ())
524
506
}
525
507
// 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 ())
530
510
}
531
511
// 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 ())
536
514
}
537
515
// 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 ())
542
518
}
543
519
case MergeStyleRebaseMerge :
544
520
// 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 ())
549
523
}
550
524
// 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 ())
555
527
}
556
528
// 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 ())
561
531
}
562
532
// 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 ())
567
535
}
568
536
569
537
// Set custom message and author and create merge commit
570
538
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 ())
576
541
}
577
542
578
543
case MergeStyleSquash :
579
544
// 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 ())
584
547
}
585
548
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 ())
591
551
}
592
552
default :
593
553
return ErrInvalidMergeStyle {pr .BaseRepo .ID , mergeStyle }
@@ -596,10 +556,8 @@ func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository, mergeStyle
596
556
env := PushingEnvironment (doer , pr .BaseRepo )
597
557
598
558
// 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 ())
603
561
}
604
562
605
563
pr .MergedCommitID , err = baseGitRepo .GetBranchCommitID (pr .BaseBranch )
0 commit comments