@@ -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,15 @@ 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
+
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 () )
462
459
}
463
460
464
461
// 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 ())
469
464
}
470
465
471
466
trackingBranch := path .Join (remoteRepoName , pr .HeadBranch )
@@ -486,108 +481,75 @@ func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository, mergeStyle
486
481
return fmt .Errorf ("Writing sparse-checkout file to %s: %v" , sparseCheckoutListPath , err )
487
482
}
488
483
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 ())
493
486
}
494
487
495
488
// 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 ())
500
491
}
501
492
502
493
// Merge commits.
503
494
switch mergeStyle {
504
495
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 ())
509
498
}
510
499
511
500
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 ())
517
503
}
518
504
case MergeStyleRebase :
519
505
// 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 ())
524
508
}
525
509
// 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 ())
530
512
}
531
513
// 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 ())
536
516
}
537
517
// 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 ())
542
520
}
543
521
case MergeStyleRebaseMerge :
544
522
// 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 ())
549
525
}
550
526
// 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 ())
555
529
}
556
530
// 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 ())
561
533
}
562
534
// 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 ())
567
537
}
568
538
569
539
// Set custom message and author and create merge commit
570
540
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 ())
576
543
}
577
544
578
545
case MergeStyleSquash :
579
546
// 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 ())
584
549
}
585
550
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 ())
591
553
}
592
554
default :
593
555
return ErrInvalidMergeStyle {pr .BaseRepo .ID , mergeStyle }
@@ -596,10 +558,8 @@ func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository, mergeStyle
596
558
env := PushingEnvironment (doer , pr .BaseRepo )
597
559
598
560
// 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 ())
603
563
}
604
564
605
565
pr .MergedCommitID , err = baseGitRepo .GetBranchCommitID (pr .BaseBranch )
0 commit comments