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