summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorUnknwon <u@gogs.io>2016-02-19 22:00:25 -0500
committerUnknwon <u@gogs.io>2016-02-19 22:00:25 -0500
commitf6c98465c79b87f717e7687af16939d3305461f5 (patch)
tree6086e45c96d7894b42202874b5192351c356f0b6 /models
parentaa12135b975ff2ebf04acb12cf3b3fd52b6c024a (diff)
parenta3bdede2ce0701e1ffcfa7510cea404d6d74e2cb (diff)
downloadgitea-f6c98465c79b87f717e7687af16939d3305461f5.tar.gz
gitea-f6c98465c79b87f717e7687af16939d3305461f5.zip
Merge pull request #2524 from mhartkorn/pullrefs
Enable a way to checkout Pull Requests from remote refs
Diffstat (limited to 'models')
-rw-r--r--models/pull.go38
1 files changed, 38 insertions, 0 deletions
diff --git a/models/pull.go b/models/pull.go
index 330319f9a8..67f103e8d5 100644
--- a/models/pull.go
+++ b/models/pull.go
@@ -20,6 +20,7 @@ import (
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/process"
"github.com/gogits/gogs/modules/setting"
+ "strconv"
)
type PullRequestType int
@@ -482,6 +483,40 @@ func (pr *PullRequest) UpdatePatch() (err error) {
return nil
}
+func (pr *PullRequest) PushToBaseRepo() (err error) {
+ log.Trace("PushToBase[%d]: pushing commits to base repo refs/pull/%d/head", pr.ID, pr.ID)
+
+ branch := pr.HeadBranch
+
+ if err = pr.BaseRepo.GetOwner(); err != nil {
+ return fmt.Errorf("Could not get base repo owner data: %v", err)
+ } else if err = pr.HeadRepo.GetOwner(); err != nil {
+ return fmt.Errorf("Could not get head repo owner data: %v", err)
+ }
+
+ headRepoPath := RepoPath(pr.HeadRepo.Owner.Name, pr.HeadRepo.Name)
+
+ prIdStr := strconv.FormatInt(pr.ID, 10)
+ tmpRemoteName := "tmp-pull-" + branch + "-" + prIdStr
+ repo, err := git.OpenRepository(headRepoPath)
+ if err != nil {
+ return fmt.Errorf("Unable to open head repository: %v", err)
+ }
+
+ if err = repo.AddRemote(tmpRemoteName, RepoPath(pr.BaseRepo.Owner.Name, pr.BaseRepo.Name), false); err != nil {
+ return fmt.Errorf("Unable to add remote to head repository: %v", err)
+ }
+ // Make sure to remove the remote even if the push fails
+ defer repo.RemoveRemote(tmpRemoteName)
+
+ pushRef := branch+":"+"refs/pull/"+prIdStr+"/head"
+ if err = git.Push(headRepoPath, tmpRemoteName, pushRef); err != nil {
+ return fmt.Errorf("Error pushing: %v", err)
+ }
+
+ return nil
+}
+
// AddToTaskQueue adds itself to pull request test task queue.
func (pr *PullRequest) AddToTaskQueue() {
go PullRequestQueue.AddFunc(pr.ID, func() {
@@ -498,6 +533,9 @@ func addHeadRepoTasks(prs []*PullRequest) {
if err := pr.UpdatePatch(); err != nil {
log.Error(4, "UpdatePatch: %v", err)
continue
+ } else if err := pr.PushToBaseRepo(); err != nil {
+ log.Error(4, "PushToBaseRepo: %v", err)
+ continue
}
pr.AddToTaskQueue()