@@ -8,7 +8,6 @@ package models
88import (
99 "bufio"
1010 "fmt"
11- "io/ioutil"
1211 "os"
1312 "path"
1413 "path/filepath"
@@ -20,14 +19,11 @@ import (
2019 "code.gitea.io/gitea/modules/log"
2120 "code.gitea.io/gitea/modules/setting"
2221 api "code.gitea.io/gitea/modules/structs"
23- "code.gitea.io/gitea/modules/sync"
2422 "code.gitea.io/gitea/modules/timeutil"
2523
2624 "github.com/unknwon/com"
2725)
2826
29- var pullRequestQueue = sync .NewUniqueQueue (setting .Repository .PullRequestQueueLength )
30-
3127// PullRequestType defines pull request type
3228type PullRequestType int
3329
@@ -485,102 +481,6 @@ func (pr *PullRequest) SetMerged() (err error) {
485481 return nil
486482}
487483
488- // manuallyMerged checks if a pull request got manually merged
489- // When a pull request got manually merged mark the pull request as merged
490- func (pr * PullRequest ) manuallyMerged () bool {
491- commit , err := pr .getMergeCommit ()
492- if err != nil {
493- log .Error ("PullRequest[%d].getMergeCommit: %v" , pr .ID , err )
494- return false
495- }
496- if commit != nil {
497- pr .MergedCommitID = commit .ID .String ()
498- pr .MergedUnix = timeutil .TimeStamp (commit .Author .When .Unix ())
499- pr .Status = PullRequestStatusManuallyMerged
500- merger , _ := GetUserByEmail (commit .Author .Email )
501-
502- // When the commit author is unknown set the BaseRepo owner as merger
503- if merger == nil {
504- if pr .BaseRepo .Owner == nil {
505- if err = pr .BaseRepo .getOwner (x ); err != nil {
506- log .Error ("BaseRepo.getOwner[%d]: %v" , pr .ID , err )
507- return false
508- }
509- }
510- merger = pr .BaseRepo .Owner
511- }
512- pr .Merger = merger
513- pr .MergerID = merger .ID
514-
515- if err = pr .SetMerged (); err != nil {
516- log .Error ("PullRequest[%d].setMerged : %v" , pr .ID , err )
517- return false
518- }
519- log .Info ("manuallyMerged[%d]: Marked as manually merged into %s/%s by commit id: %s" , pr .ID , pr .BaseRepo .Name , pr .BaseBranch , commit .ID .String ())
520- return true
521- }
522- return false
523- }
524-
525- // getMergeCommit checks if a pull request got merged
526- // Returns the git.Commit of the pull request if merged
527- func (pr * PullRequest ) getMergeCommit () (* git.Commit , error ) {
528- if pr .BaseRepo == nil {
529- var err error
530- pr .BaseRepo , err = GetRepositoryByID (pr .BaseRepoID )
531- if err != nil {
532- return nil , fmt .Errorf ("GetRepositoryByID: %v" , err )
533- }
534- }
535-
536- indexTmpPath := filepath .Join (os .TempDir (), "gitea-" + pr .BaseRepo .Name + "-" + strconv .Itoa (time .Now ().Nanosecond ()))
537- defer os .Remove (indexTmpPath )
538-
539- headFile := pr .GetGitRefName ()
540-
541- // Check if a pull request is merged into BaseBranch
542- _ , err := git .NewCommand ("merge-base" , "--is-ancestor" , headFile , pr .BaseBranch ).RunInDirWithEnv (pr .BaseRepo .RepoPath (), []string {"GIT_INDEX_FILE=" + indexTmpPath , "GIT_DIR=" + pr .BaseRepo .RepoPath ()})
543- if err != nil {
544- // Errors are signaled by a non-zero status that is not 1
545- if strings .Contains (err .Error (), "exit status 1" ) {
546- return nil , nil
547- }
548- return nil , fmt .Errorf ("git merge-base --is-ancestor: %v" , err )
549- }
550-
551- commitIDBytes , err := ioutil .ReadFile (pr .BaseRepo .RepoPath () + "/" + headFile )
552- if err != nil {
553- return nil , fmt .Errorf ("ReadFile(%s): %v" , headFile , err )
554- }
555- commitID := string (commitIDBytes )
556- if len (commitID ) < 40 {
557- return nil , fmt .Errorf (`ReadFile(%s): invalid commit-ID "%s"` , headFile , commitID )
558- }
559- cmd := commitID [:40 ] + ".." + pr .BaseBranch
560-
561- // Get the commit from BaseBranch where the pull request got merged
562- mergeCommit , err := git .NewCommand ("rev-list" , "--ancestry-path" , "--merges" , "--reverse" , cmd ).RunInDirWithEnv ("" , []string {"GIT_INDEX_FILE=" + indexTmpPath , "GIT_DIR=" + pr .BaseRepo .RepoPath ()})
563- if err != nil {
564- return nil , fmt .Errorf ("git rev-list --ancestry-path --merges --reverse: %v" , err )
565- } else if len (mergeCommit ) < 40 {
566- // PR was fast-forwarded, so just use last commit of PR
567- mergeCommit = commitID [:40 ]
568- }
569-
570- gitRepo , err := git .OpenRepository (pr .BaseRepo .RepoPath ())
571- if err != nil {
572- return nil , fmt .Errorf ("OpenRepository: %v" , err )
573- }
574- defer gitRepo .Close ()
575-
576- commit , err := gitRepo .GetCommit (mergeCommit [:40 ])
577- if err != nil {
578- return nil , fmt .Errorf ("GetCommit: %v" , err )
579- }
580-
581- return commit , nil
582- }
583-
584484// patchConflicts is a list of conflict description from Git.
585485var patchConflicts = []string {
586486 "patch does not apply" ,
@@ -589,6 +489,11 @@ var patchConflicts = []string{
589489 "error:" ,
590490}
591491
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+
592497// testPatch checks if patch can be merged to base repository without conflict.
593498func (pr * PullRequest ) testPatch (e Engine ) (err error ) {
594499 if pr .BaseRepo == nil {
@@ -949,32 +854,6 @@ func (pr *PullRequest) PushToBaseRepo() (err error) {
949854 return nil
950855}
951856
952- // AddToTaskQueue adds itself to pull request test task queue.
953- func (pr * PullRequest ) AddToTaskQueue () {
954- go pullRequestQueue .AddFunc (pr .ID , func () {
955- pr .Status = PullRequestStatusChecking
956- if err := pr .UpdateCols ("status" ); err != nil {
957- log .Error ("AddToTaskQueue.UpdateCols[%d].(add to queue): %v" , pr .ID , err )
958- }
959- })
960- }
961-
962- // checkAndUpdateStatus checks if pull request is possible to leaving checking status,
963- // and set to be either conflict or mergeable.
964- func (pr * PullRequest ) checkAndUpdateStatus () {
965- // Status is not changed to conflict means mergeable.
966- if pr .Status == PullRequestStatusChecking {
967- pr .Status = PullRequestStatusMergeable
968- }
969-
970- // Make sure there is no waiting test to process before leaving the checking status.
971- if ! pullRequestQueue .Exist (pr .ID ) {
972- if err := pr .UpdateCols ("status, conflicted_files" ); err != nil {
973- log .Error ("Update[%d]: %v" , pr .ID , err )
974- }
975- }
976- }
977-
978857// IsWorkInProgress determine if the Pull Request is a Work In Progress by its title
979858func (pr * PullRequest ) IsWorkInProgress () bool {
980859 if err := pr .LoadIssue (); err != nil {
0 commit comments