aboutsummaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
authorSandro Santilli <strk@kbt.io>2018-01-07 14:10:20 +0100
committerLauris BH <lauris@nix.lv>2018-01-07 15:10:20 +0200
commit44053532bb7c5b7fcd65fd5246780db8cf446f7e (patch)
tree421763fd45a3d50e3a6cd67d07bb563fddf35418 /routers
parent18bb0f8f1344771bbbc44fd37eee772c5c3b44d6 (diff)
downloadgitea-44053532bb7c5b7fcd65fd5246780db8cf446f7e.tar.gz
gitea-44053532bb7c5b7fcd65fd5246780db8cf446f7e.zip
Serve .patch for pull requests (#3305)
* Serve .patch for pull requests Closes #3259 Updates "git" module, for GetFormatPatch * Handle io.Copy error
Diffstat (limited to 'routers')
-rw-r--r--routers/repo/pull.go48
-rw-r--r--routers/routes/routes.go1
2 files changed, 48 insertions, 1 deletions
diff --git a/routers/repo/pull.go b/routers/repo/pull.go
index 9de8268957..515b6a91f8 100644
--- a/routers/repo/pull.go
+++ b/routers/repo/pull.go
@@ -9,6 +9,7 @@ package repo
import (
"container/list"
"fmt"
+ "io"
"path"
"strings"
@@ -1033,7 +1034,7 @@ func DownloadPullDiff(ctx *context.Context) {
return
}
- // Redirect elsewhere if it's not a pull request
+ // Return not found if it's not a pull request
if !issue.IsPull {
ctx.Handle(404, "DownloadPullDiff",
fmt.Errorf("Issue is not a pull request"))
@@ -1054,3 +1055,48 @@ func DownloadPullDiff(ctx *context.Context) {
ctx.ServeFileContent(patch)
}
+
+// DownloadPullPatch render a pull's raw patch
+func DownloadPullPatch(ctx *context.Context) {
+ issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
+ if err != nil {
+ if models.IsErrIssueNotExist(err) {
+ ctx.Handle(404, "GetIssueByIndex", err)
+ } else {
+ ctx.Handle(500, "GetIssueByIndex", err)
+ }
+ return
+ }
+
+ // Return not found if it's not a pull request
+ if !issue.IsPull {
+ ctx.Handle(404, "DownloadPullDiff",
+ fmt.Errorf("Issue is not a pull request"))
+ return
+ }
+
+ pr := issue.PullRequest
+
+ if err = pr.GetHeadRepo(); err != nil {
+ ctx.Handle(500, "GetHeadRepo", err)
+ return
+ }
+
+ headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
+ if err != nil {
+ ctx.Handle(500, "OpenRepository", err)
+ return
+ }
+
+ patch, err := headGitRepo.GetFormatPatch(pr.MergeBase, pr.HeadBranch)
+ if err != nil {
+ ctx.Handle(500, "GetFormatPatch", err)
+ return
+ }
+
+ _, err = io.Copy(ctx, patch)
+ if err != nil {
+ ctx.Handle(500, "io.Copy", err)
+ return
+ }
+}
diff --git a/routers/routes/routes.go b/routers/routes/routes.go
index 3e00f55ebf..fc7401fc9a 100644
--- a/routers/routes/routes.go
+++ b/routers/routes/routes.go
@@ -625,6 +625,7 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Group("/pulls/:index", func() {
m.Get(".diff", repo.DownloadPullDiff)
+ m.Get(".patch", repo.DownloadPullPatch)
m.Get("/commits", context.RepoRef(), repo.ViewPullCommits)
m.Get("/files", context.RepoRef(), repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.ViewPullFiles)
m.Post("/merge", reqRepoWriter, bindIgnErr(auth.MergePullRequestForm{}), repo.MergePullRequest)