@@ -8,6 +8,7 @@ package models
88import (
99 "bufio"
1010 "fmt"
11+ "io/ioutil"
1112 "os"
1213 "path"
1314 "path/filepath"
@@ -595,11 +596,11 @@ func (pr *PullRequest) testPatch(e Engine) (err error) {
595596}
596597
597598// 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 ) {
599+ func NewPullRequest (repo * Repository , pull * Issue , labelIDs []int64 , uuids []string , pr * PullRequest , patchFileSize int64 , patchFileName string ) (err error ) {
599600 // Retry several times in case INSERT fails due to duplicate key for (repo_id, index); see #7887
600601 i := 0
601602 for {
602- if err = newPullRequestAttempt (repo , pull , labelIDs , uuids , pr , patch ); err == nil {
603+ if err = newPullRequestAttempt (repo , pull , labelIDs , uuids , pr , patchFileSize , patchFileName ); err == nil {
603604 return nil
604605 }
605606 if ! IsErrNewIssueInsert (err ) {
@@ -613,7 +614,7 @@ func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []str
613614 return fmt .Errorf ("NewPullRequest: too many errors attempting to insert the new issue. Last error was: %v" , err )
614615}
615616
616- func newPullRequestAttempt (repo * Repository , pull * Issue , labelIDs []int64 , uuids []string , pr * PullRequest , patch [] byte ) (err error ) {
617+ func newPullRequestAttempt (repo * Repository , pull * Issue , labelIDs []int64 , uuids []string , pr * PullRequest , patchFileSize int64 , patchFileName string ) (err error ) {
617618 sess := x .NewSession ()
618619 defer sess .Close ()
619620 if err = sess .Begin (); err != nil {
@@ -636,8 +637,8 @@ func newPullRequestAttempt(repo *Repository, pull *Issue, labelIDs []int64, uuid
636637 pr .Index = pull .Index
637638 pr .BaseRepo = repo
638639 pr .Status = PullRequestStatusChecking
639- if len ( patch ) > 0 {
640- if err = repo .savePatch (sess , pr .Index , patch ); err != nil {
640+ if patchFileSize > 0 {
641+ if err = repo .savePatch (sess , pr .Index , patchFileName ); err != nil {
641642 return fmt .Errorf ("SavePatch: %v" , err )
642643 }
643644
@@ -800,12 +801,23 @@ func (pr *PullRequest) UpdatePatch() (err error) {
800801 return fmt .Errorf ("Update: %v" , err )
801802 }
802803
803- patch , err := headGitRepo . GetPatch ( pr . MergeBase , pr . HeadBranch )
804+ tmpPatchFile , err := ioutil . TempFile ( "" , "patch" )
804805 if err != nil {
805- return fmt .Errorf ("GetPatch: %v" , err )
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 )
806817 }
807818
808- if err = pr .BaseRepo .SavePatch (pr .Index , patch ); err != nil {
819+ tmpPatchFile .Close ()
820+ if err = pr .BaseRepo .SavePatch (pr .Index , tmpPatchFile .Name ()); err != nil {
809821 return fmt .Errorf ("BaseRepo.SavePatch: %v" , err )
810822 }
811823
0 commit comments