6
6
package models
7
7
8
8
import (
9
- "bufio"
10
9
"fmt"
11
- "io/ioutil"
12
10
"os"
13
11
"path"
14
- "path/filepath"
15
- "strconv"
16
12
"strings"
17
- "time"
18
13
19
14
"code.gitea.io/gitea/modules/git"
20
15
"code.gitea.io/gitea/modules/log"
21
16
"code.gitea.io/gitea/modules/setting"
22
17
api "code.gitea.io/gitea/modules/structs"
23
18
"code.gitea.io/gitea/modules/timeutil"
24
-
25
- "github.com/unknwon/com"
26
19
)
27
20
28
21
// PullRequestType defines pull request type
@@ -482,125 +475,12 @@ func (pr *PullRequest) SetMerged() (err error) {
482
475
return nil
483
476
}
484
477
485
- // patchConflicts is a list of conflict description from Git.
486
- var patchConflicts = []string {
487
- "patch does not apply" ,
488
- "already exists in working directory" ,
489
- "unrecognized input" ,
490
- "error:" ,
491
- }
492
-
493
- // TestPatch checks if patch can be merged to base repository without conflict.
494
- func (pr * PullRequest ) TestPatch () error {
495
- return pr .testPatch (x )
496
- }
497
-
498
- // testPatch checks if patch can be merged to base repository without conflict.
499
- func (pr * PullRequest ) testPatch (e Engine ) (err error ) {
500
- if pr .BaseRepo == nil {
501
- pr .BaseRepo , err = getRepositoryByID (e , pr .BaseRepoID )
502
- if err != nil {
503
- return fmt .Errorf ("GetRepositoryByID: %v" , err )
504
- }
505
- }
506
-
507
- patchPath , err := pr .BaseRepo .patchPath (e , pr .Index )
508
- if err != nil {
509
- return fmt .Errorf ("BaseRepo.PatchPath: %v" , err )
510
- }
511
-
512
- // Fast fail if patch does not exist, this assumes data is corrupted.
513
- if ! com .IsFile (patchPath ) {
514
- log .Trace ("PullRequest[%d].testPatch: ignored corrupted data" , pr .ID )
515
- return nil
516
- }
517
-
518
- RepoWorkingPool .CheckIn (com .ToStr (pr .BaseRepoID ))
519
- defer RepoWorkingPool .CheckOut (com .ToStr (pr .BaseRepoID ))
520
-
521
- log .Trace ("PullRequest[%d].testPatch (patchPath): %s" , pr .ID , patchPath )
522
-
523
- pr .Status = PullRequestStatusChecking
524
-
525
- indexTmpPath := filepath .Join (os .TempDir (), "gitea-" + pr .BaseRepo .Name + "-" + strconv .Itoa (time .Now ().Nanosecond ()))
526
- defer os .Remove (indexTmpPath )
527
-
528
- _ , err = git .NewCommand ("read-tree" , pr .BaseBranch ).RunInDirWithEnv ("" , []string {"GIT_DIR=" + pr .BaseRepo .RepoPath (), "GIT_INDEX_FILE=" + indexTmpPath })
529
- if err != nil {
530
- return fmt .Errorf ("git read-tree --index-output=%s %s: %v" , indexTmpPath , pr .BaseBranch , err )
531
- }
532
-
533
- prUnit , err := pr .BaseRepo .getUnit (e , UnitTypePullRequests )
534
- if err != nil {
535
- return err
536
- }
537
- prConfig := prUnit .PullRequestsConfig ()
538
-
539
- args := []string {"apply" , "--check" , "--cached" }
540
- if prConfig .IgnoreWhitespaceConflicts {
541
- args = append (args , "--ignore-whitespace" )
542
- }
543
- args = append (args , patchPath )
544
- pr .ConflictedFiles = []string {}
545
-
546
- stderrBuilder := new (strings.Builder )
547
- err = git .NewCommand (args ... ).RunInDirTimeoutEnvPipeline (
548
- []string {"GIT_INDEX_FILE=" + indexTmpPath , "GIT_DIR=" + pr .BaseRepo .RepoPath ()},
549
- - 1 ,
550
- "" ,
551
- nil ,
552
- stderrBuilder )
553
- stderr := stderrBuilder .String ()
554
-
555
- if err != nil {
556
- for i := range patchConflicts {
557
- if strings .Contains (stderr , patchConflicts [i ]) {
558
- log .Trace ("PullRequest[%d].testPatch (apply): has conflict: %s" , pr .ID , stderr )
559
- const prefix = "error: patch failed:"
560
- pr .Status = PullRequestStatusConflict
561
- pr .ConflictedFiles = make ([]string , 0 , 5 )
562
- scanner := bufio .NewScanner (strings .NewReader (stderr ))
563
- for scanner .Scan () {
564
- line := scanner .Text ()
565
-
566
- if strings .HasPrefix (line , prefix ) {
567
- var found bool
568
- var filepath = strings .TrimSpace (strings .Split (line [len (prefix ):], ":" )[0 ])
569
- for _ , f := range pr .ConflictedFiles {
570
- if f == filepath {
571
- found = true
572
- break
573
- }
574
- }
575
- if ! found {
576
- pr .ConflictedFiles = append (pr .ConflictedFiles , filepath )
577
- }
578
- }
579
- // only list 10 conflicted files
580
- if len (pr .ConflictedFiles ) >= 10 {
581
- break
582
- }
583
- }
584
-
585
- if len (pr .ConflictedFiles ) > 0 {
586
- log .Trace ("Found %d files conflicted: %v" , len (pr .ConflictedFiles ), pr .ConflictedFiles )
587
- }
588
-
589
- return nil
590
- }
591
- }
592
-
593
- return fmt .Errorf ("git apply --check: %v - %s" , err , stderr )
594
- }
595
- return nil
596
- }
597
-
598
478
// NewPullRequest creates new pull request with labels for repository.
599
- func NewPullRequest (repo * Repository , pull * Issue , labelIDs []int64 , uuids []string , pr * PullRequest , patchFileSize int64 , patchFileName string ) (err error ) {
479
+ func NewPullRequest (repo * Repository , pull * Issue , labelIDs []int64 , uuids []string , pr * PullRequest ) (err error ) {
600
480
// Retry several times in case INSERT fails due to duplicate key for (repo_id, index); see #7887
601
481
i := 0
602
482
for {
603
- if err = newPullRequestAttempt (repo , pull , labelIDs , uuids , pr , patchFileSize , patchFileName ); err == nil {
483
+ if err = newPullRequestAttempt (repo , pull , labelIDs , uuids , pr ); err == nil {
604
484
return nil
605
485
}
606
486
if ! IsErrNewIssueInsert (err ) {
@@ -614,7 +494,7 @@ func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []str
614
494
return fmt .Errorf ("NewPullRequest: too many errors attempting to insert the new issue. Last error was: %v" , err )
615
495
}
616
496
617
- func newPullRequestAttempt (repo * Repository , pull * Issue , labelIDs []int64 , uuids []string , pr * PullRequest , patchFileSize int64 , patchFileName string ) (err error ) {
497
+ func newPullRequestAttempt (repo * Repository , pull * Issue , labelIDs []int64 , uuids []string , pr * PullRequest ) (err error ) {
618
498
sess := x .NewSession ()
619
499
defer sess .Close ()
620
500
if err = sess .Begin (); err != nil {
@@ -636,20 +516,6 @@ func newPullRequestAttempt(repo *Repository, pull *Issue, labelIDs []int64, uuid
636
516
637
517
pr .Index = pull .Index
638
518
pr .BaseRepo = repo
639
- pr .Status = PullRequestStatusChecking
640
- if patchFileSize > 0 {
641
- if err = repo .savePatch (sess , pr .Index , patchFileName ); err != nil {
642
- return fmt .Errorf ("SavePatch: %v" , err )
643
- }
644
-
645
- if err = pr .testPatch (sess ); err != nil {
646
- return fmt .Errorf ("testPatch: %v" , err )
647
- }
648
- }
649
- // No conflict appears after test means mergeable.
650
- if pr .Status == PullRequestStatusChecking {
651
- pr .Status = PullRequestStatusMergeable
652
- }
653
519
654
520
pr .IssueID = pull .ID
655
521
if _ , err = sess .Insert (pr ); err != nil {
@@ -765,65 +631,6 @@ func (pr *PullRequest) UpdateCols(cols ...string) error {
765
631
return err
766
632
}
767
633
768
- // UpdatePatch generates and saves a new patch.
769
- func (pr * PullRequest ) UpdatePatch () (err error ) {
770
- if err = pr .GetHeadRepo (); err != nil {
771
- return fmt .Errorf ("GetHeadRepo: %v" , err )
772
- } else if pr .HeadRepo == nil {
773
- log .Trace ("PullRequest[%d].UpdatePatch: ignored corrupted data" , pr .ID )
774
- return nil
775
- }
776
-
777
- if err = pr .GetBaseRepo (); err != nil {
778
- return fmt .Errorf ("GetBaseRepo: %v" , err )
779
- }
780
-
781
- headGitRepo , err := git .OpenRepository (pr .HeadRepo .RepoPath ())
782
- if err != nil {
783
- return fmt .Errorf ("OpenRepository: %v" , err )
784
- }
785
- defer headGitRepo .Close ()
786
-
787
- // Add a temporary remote.
788
- tmpRemote := com .ToStr (time .Now ().UnixNano ())
789
- if err = headGitRepo .AddRemote (tmpRemote , RepoPath (pr .BaseRepo .MustOwner ().Name , pr .BaseRepo .Name ), true ); err != nil {
790
- return fmt .Errorf ("AddRemote: %v" , err )
791
- }
792
- defer func () {
793
- if err := headGitRepo .RemoveRemote (tmpRemote ); err != nil {
794
- log .Error ("UpdatePatch: RemoveRemote: %s" , err )
795
- }
796
- }()
797
- pr .MergeBase , _ , err = headGitRepo .GetMergeBase (tmpRemote , pr .BaseBranch , pr .HeadBranch )
798
- if err != nil {
799
- return fmt .Errorf ("GetMergeBase: %v" , err )
800
- } else if err = pr .Update (); err != nil {
801
- return fmt .Errorf ("Update: %v" , err )
802
- }
803
-
804
- tmpPatchFile , err := ioutil .TempFile ("" , "patch" )
805
- if err != nil {
806
- log .Error ("Unable to create temporary patch file! Error: %v" , err )
807
- return fmt .Errorf ("Unable to create temporary patch file! Error: %v" , err )
808
- }
809
- defer func () {
810
- _ = os .Remove (tmpPatchFile .Name ())
811
- }()
812
-
813
- if err := headGitRepo .GetPatch (pr .MergeBase , pr .HeadBranch , tmpPatchFile ); err != nil {
814
- tmpPatchFile .Close ()
815
- log .Error ("Unable to get patch file from %s to %s in %s/%s Error: %v" , pr .MergeBase , pr .HeadBranch , pr .BaseRepo .MustOwner ().Name , pr .BaseRepo .Name , err )
816
- return fmt .Errorf ("Unable to get patch file from %s to %s in %s/%s Error: %v" , pr .MergeBase , pr .HeadBranch , pr .BaseRepo .MustOwner ().Name , pr .BaseRepo .Name , err )
817
- }
818
-
819
- tmpPatchFile .Close ()
820
- if err = pr .BaseRepo .SavePatch (pr .Index , tmpPatchFile .Name ()); err != nil {
821
- return fmt .Errorf ("BaseRepo.SavePatch: %v" , err )
822
- }
823
-
824
- return nil
825
- }
826
-
827
634
// PushToBaseRepo pushes commits from branches of head repository to
828
635
// corresponding branches of base repository.
829
636
// FIXME: Only push branches that are actually updates?
0 commit comments