Skip to content

Commit 5ac857f

Browse files
qwerty2876543
andauthored
Add API to get commit diff/patch (#17095)
* Add API to get commit diff/patch * Add Tests Co-authored-by: 6543 <6543@obermui.de>
1 parent d4bb8e0 commit 5ac857f

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

Diff for: integrations/api_repo_git_commits_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,26 @@ func TestAPIReposGitCommitListDifferentBranch(t *testing.T) {
109109
assert.Equal(t, "f27c2b2b03dcab38beaf89b0ab4ff61f6de63441", apiData[0].CommitMeta.SHA)
110110
compareCommitFiles(t, []string{"readme.md"}, apiData[0].Files)
111111
}
112+
113+
func TestDownloadCommitDiffOrPatch(t *testing.T) {
114+
defer prepareTestEnv(t)()
115+
user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
116+
// Login as User2.
117+
session := loginUser(t, user.Name)
118+
token := getTokenForLoggedInUser(t, session)
119+
120+
// Test getting diff
121+
reqDiff := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/git/commits/f27c2b2b03dcab38beaf89b0ab4ff61f6de63441.diff?token="+token, user.Name)
122+
resp := session.MakeRequest(t, reqDiff, http.StatusOK)
123+
assert.EqualValues(t,
124+
"commit f27c2b2b03dcab38beaf89b0ab4ff61f6de63441\nAuthor: User2 <user2@example.com>\nDate: Sun Aug 6 19:55:01 2017 +0200\n\n good signed commit\n\ndiff --git a/readme.md b/readme.md\nnew file mode 100644\nindex 0000000..458121c\n--- /dev/null\n+++ b/readme.md\n@@ -0,0 +1 @@\n+good sign\n",
125+
resp.Body.String())
126+
127+
// Test getting patch
128+
reqPatch := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/git/commits/f27c2b2b03dcab38beaf89b0ab4ff61f6de63441.patch?token="+token, user.Name)
129+
resp = session.MakeRequest(t, reqPatch, http.StatusOK)
130+
assert.EqualValues(t,
131+
"From f27c2b2b03dcab38beaf89b0ab4ff61f6de63441 Mon Sep 17 00:00:00 2001\nFrom: User2 <user2@example.com>\nDate: Sun, 6 Aug 2017 19:55:01 +0200\nSubject: [PATCH] good signed commit\n\n---\n readme.md | 1 +\n 1 file changed, 1 insertion(+)\n create mode 100644 readme.md\n\ndiff --git a/readme.md b/readme.md\nnew file mode 100644\nindex 0000000..458121c\n--- /dev/null\n+++ b/readme.md\n@@ -0,0 +1 @@\n+good sign\n",
132+
resp.Body.String())
133+
134+
}

Diff for: routers/api/v1/api.go

+1
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,7 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route {
937937
m.Group("/git", func() {
938938
m.Group("/commits", func() {
939939
m.Get("/{sha}", repo.GetSingleCommit)
940+
m.Get("/{sha}.{diffType:diff|patch}", repo.DownloadCommitDiffOrPatch)
940941
})
941942
m.Get("/refs", repo.GetGitAllRefs)
942943
m.Get("/refs/*", repo.GetGitRefs)

Diff for: routers/api/v1/repo/commits.go

+50
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,53 @@ func GetAllCommits(ctx *context.APIContext) {
213213

214214
ctx.JSON(http.StatusOK, &apiCommits)
215215
}
216+
217+
// DownloadCommitDiffOrPatch render a commit's raw diff or patch
218+
func DownloadCommitDiffOrPatch(ctx *context.APIContext) {
219+
// swagger:operation GET /repos/{owner}/{repo}/git/commits/{sha}.{diffType} repository repoDownloadCommitDiffOrPatch
220+
// ---
221+
// summary: Get a commit's diff or patch
222+
// produces:
223+
// - text/plain
224+
// parameters:
225+
// - name: owner
226+
// in: path
227+
// description: owner of the repo
228+
// type: string
229+
// required: true
230+
// - name: repo
231+
// in: path
232+
// description: name of the repo
233+
// type: string
234+
// required: true
235+
// - name: sha
236+
// in: path
237+
// description: SHA of the commit to get
238+
// type: string
239+
// required: true
240+
// - name: diffType
241+
// in: path
242+
// description: whether the output is diff or patch
243+
// type: string
244+
// enum: [diff, patch]
245+
// required: true
246+
// responses:
247+
// "200":
248+
// "$ref": "#/responses/string"
249+
// "404":
250+
// "$ref": "#/responses/notFound"
251+
repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
252+
if err := git.GetRawDiff(
253+
repoPath,
254+
ctx.Params(":sha"),
255+
git.RawDiffType(ctx.Params(":diffType")),
256+
ctx.Resp,
257+
); err != nil {
258+
if git.IsErrNotExist(err) {
259+
ctx.NotFound(ctx.Params(":sha"))
260+
return
261+
}
262+
ctx.Error(http.StatusInternalServerError, "DownloadCommitDiffOrPatch", err)
263+
return
264+
}
265+
}

Diff for: templates/swagger/v1_json.tmpl

+54
Original file line numberDiff line numberDiff line change
@@ -3581,6 +3581,60 @@
35813581
}
35823582
}
35833583
},
3584+
"/repos/{owner}/{repo}/git/commits/{sha}.{diffType}": {
3585+
"get": {
3586+
"produces": [
3587+
"text/plain"
3588+
],
3589+
"tags": [
3590+
"repository"
3591+
],
3592+
"summary": "Get a commit's diff or patch",
3593+
"operationId": "repoDownloadCommitDiffOrPatch",
3594+
"parameters": [
3595+
{
3596+
"type": "string",
3597+
"description": "owner of the repo",
3598+
"name": "owner",
3599+
"in": "path",
3600+
"required": true
3601+
},
3602+
{
3603+
"type": "string",
3604+
"description": "name of the repo",
3605+
"name": "repo",
3606+
"in": "path",
3607+
"required": true
3608+
},
3609+
{
3610+
"type": "string",
3611+
"description": "SHA of the commit to get",
3612+
"name": "sha",
3613+
"in": "path",
3614+
"required": true
3615+
},
3616+
{
3617+
"enum": [
3618+
"diff",
3619+
"patch"
3620+
],
3621+
"type": "string",
3622+
"description": "whether the output is diff or patch",
3623+
"name": "diffType",
3624+
"in": "path",
3625+
"required": true
3626+
}
3627+
],
3628+
"responses": {
3629+
"200": {
3630+
"$ref": "#/responses/string"
3631+
},
3632+
"404": {
3633+
"$ref": "#/responses/notFound"
3634+
}
3635+
}
3636+
}
3637+
},
35843638
"/repos/{owner}/{repo}/git/notes/{sha}": {
35853639
"get": {
35863640
"produces": [

0 commit comments

Comments
 (0)