66package models
77
88import (
9- "bufio"
109 "fmt"
1110 "os"
1211 "path"
13- "path/filepath"
14- "strconv"
1512 "strings"
16- "time"
1713
1814 "code.gitea.io/gitea/modules/git"
1915 "code.gitea.io/gitea/modules/log"
2016 "code.gitea.io/gitea/modules/setting"
2117 api "code.gitea.io/gitea/modules/structs"
2218 "code.gitea.io/gitea/modules/timeutil"
23-
24- "github.com/unknwon/com"
2519)
2620
2721// PullRequestType defines pull request type
@@ -481,125 +475,12 @@ func (pr *PullRequest) SetMerged() (err error) {
481475 return nil
482476}
483477
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-
597478// 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 ) {
599480 // Retry several times in case INSERT fails due to duplicate key for (repo_id, index); see #7887
600481 i := 0
601482 for {
602- if err = newPullRequestAttempt (repo , pull , labelIDs , uuids , pr , patch ); err == nil {
483+ if err = newPullRequestAttempt (repo , pull , labelIDs , uuids , pr ); err == nil {
603484 return nil
604485 }
605486 if ! IsErrNewIssueInsert (err ) {
@@ -613,7 +494,7 @@ func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []str
613494 return fmt .Errorf ("NewPullRequest: too many errors attempting to insert the new issue. Last error was: %v" , err )
614495}
615496
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 ) {
617498 sess := x .NewSession ()
618499 defer sess .Close ()
619500 if err = sess .Begin (); err != nil {
@@ -635,20 +516,6 @@ func newPullRequestAttempt(repo *Repository, pull *Issue, labelIDs []int64, uuid
635516
636517 pr .Index = pull .Index
637518 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- }
652519
653520 pr .IssueID = pull .ID
654521 if _ , err = sess .Insert (pr ); err != nil {
@@ -764,54 +631,6 @@ func (pr *PullRequest) UpdateCols(cols ...string) error {
764631 return err
765632}
766633
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-
815634// PushToBaseRepo pushes commits from branches of head repository to
816635// corresponding branches of base repository.
817636// FIXME: Only push branches that are actually updates?
0 commit comments