Skip to content

Serve .patch for pull requests #3305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion integrations/pull_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ func TestPullCreate(t *testing.T) {
// check .diff can be accessed and matches performed change
req := NewRequest(t, "GET", url+".diff")
resp = session.MakeRequest(t, req, http.StatusOK)
assert.Regexp(t, "\\+Hello, World \\(Edited\\)", resp.Body)
assert.Regexp(t, `\+Hello, World \(Edited\)`, resp.Body)
assert.Regexp(t, "^diff", resp.Body)
assert.NotRegexp(t, "diff.*diff", resp.Body) // not two diffs, just one

// check .patch can be accessed and matches performed change
req = NewRequest(t, "GET", url+".patch")
resp = session.MakeRequest(t, req, http.StatusOK)
assert.Regexp(t, `\+Hello, World \(Edited\)`, resp.Body)
assert.Regexp(t, "diff", resp.Body)
assert.Regexp(t, `Subject: \[PATCH\] Update 'README.md'`, resp.Body)
assert.NotRegexp(t, "diff.*diff", resp.Body) // not two diffs, just one
}
48 changes: 47 additions & 1 deletion routers/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package repo
import (
"container/list"
"fmt"
"io"
"path"
"strings"

Expand Down Expand Up @@ -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"))
Expand All @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@strk You need to set the approriate Content-Type before this :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR is merged, if you can handle to test Content-Type in the existing integration test that'd be very useful (Content-Type was automatically added in previous implementation via ServeContent)

if err != nil {
ctx.Handle(500, "io.Copy", err)
return
}
}
1 change: 1 addition & 0 deletions routers/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions vendor/code.gitea.io/git/MAINTAINERS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion vendor/code.gitea.io/git/command.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions vendor/code.gitea.io/git/repo_pull.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"ignore": "test appengine",
"package": [
{
"checksumSHA1": "Em29XiKkOh5rFFXdkCjqqsQ7fe4=",
"checksumSHA1": "1WHdGmDRsFRTD5N69l+MEbZr+nM=",
"path": "code.gitea.io/git",
"revision": "4ec3654064ef7eef4f05f891073a38039ad8d0f7",
"revisionTime": "2017-12-22T02:43:26Z"
"revision": "f4a91053671bee69f1995e456c1541668717c19d",
"revisionTime": "2018-01-07T06:11:05Z"
},
{
"checksumSHA1": "Qtq0kW+BnpYMOriaoCjMa86WGG8=",
Expand Down