Skip to content

Commit 9d3cbb4

Browse files
committed
Add API to get commit diff/patch
1 parent a4bfef2 commit 9d3cbb4

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

routers/api/v1/api.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,10 @@ 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}.diff",
941+
repo.DownloadCommitDiff)
942+
m.Get("/{sha}.patch",
943+
repo.DownloadCommitPatch)
940944
})
941945
m.Get("/refs", repo.GetGitAllRefs)
942946
m.Get("/refs/*", repo.GetGitRefs)

routers/api/v1/repo/commits.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,81 @@ func GetAllCommits(ctx *context.APIContext) {
213213

214214
ctx.JSON(http.StatusOK, &apiCommits)
215215
}
216+
217+
func DownloadCommitDiff(ctx *context.APIContext) {
218+
// swagger:operation GET /repos/{owner}/{repo}/git/commits/{sha}.diff repository repoDownloadCommitDiff
219+
// ---
220+
// summary: Get a commit diff
221+
// produces:
222+
// - text/plain
223+
// parameters:
224+
// - name: owner
225+
// in: path
226+
// description: owner of the repo
227+
// type: string
228+
// required: true
229+
// - name: repo
230+
// in: path
231+
// description: name of the repo
232+
// type: string
233+
// required: true
234+
// - name: sha
235+
// in: path
236+
// description: SHA of the commit to get
237+
// type: string
238+
// required: true
239+
// responses:
240+
// "200":
241+
// "$ref": "#/responses/string"
242+
// "404":
243+
// "$ref": "#/responses/notFound"
244+
DownloadCommitDiffOrPatch(ctx, "diff")
245+
}
246+
247+
func DownloadCommitPatch(ctx *context.APIContext) {
248+
// swagger:operation GET /repos/{owner}/{repo}/git/commits/{sha}.patch repository repoDownloadCommitPatch
249+
// ---
250+
// summary: Get a commit patch
251+
// produces:
252+
// - text/plain
253+
// parameters:
254+
// - name: owner
255+
// in: path
256+
// description: owner of the repo
257+
// type: string
258+
// required: true
259+
// - name: repo
260+
// in: path
261+
// description: name of the repo
262+
// type: string
263+
// required: true
264+
// - name: sha
265+
// in: path
266+
// description: SHA of the commit to get
267+
// type: string
268+
// required: true
269+
// responses:
270+
// "200":
271+
// "$ref": "#/responses/string"
272+
// "404":
273+
// "$ref": "#/responses/notFound"
274+
DownloadCommitDiffOrPatch(ctx, "patch")
275+
}
276+
277+
func DownloadCommitDiffOrPatch(ctx *context.APIContext, diffType string) {
278+
var repoPath string
279+
repoPath = models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
280+
if err := git.GetRawDiff(
281+
repoPath,
282+
ctx.Params(":sha"),
283+
git.RawDiffType(diffType),
284+
ctx.Resp,
285+
); err != nil {
286+
if git.IsErrNotExist(err) {
287+
ctx.NotFound(ctx.Params(":sha"))
288+
return
289+
}
290+
ctx.Error(http.StatusInternalServerError, "DownloadCommitDiffOrPatch", err)
291+
return
292+
}
293+
}

templates/swagger/v1_json.tmpl

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3581,6 +3581,92 @@
35813581
}
35823582
}
35833583
},
3584+
"/repos/{owner}/{repo}/git/commits/{sha}.diff": {
3585+
"get": {
3586+
"produces": [
3587+
"text/plain"
3588+
],
3589+
"tags": [
3590+
"repository"
3591+
],
3592+
"summary": "Get a commit diff",
3593+
"operationId": "repoDownloadCommitDiff",
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+
"responses": {
3618+
"200": {
3619+
"$ref": "#/responses/string"
3620+
},
3621+
"404": {
3622+
"$ref": "#/responses/notFound"
3623+
}
3624+
}
3625+
}
3626+
},
3627+
"/repos/{owner}/{repo}/git/commits/{sha}.patch": {
3628+
"get": {
3629+
"produces": [
3630+
"text/plain"
3631+
],
3632+
"tags": [
3633+
"repository"
3634+
],
3635+
"summary": "Get a commit patch",
3636+
"operationId": "repoDownloadCommitPatch",
3637+
"parameters": [
3638+
{
3639+
"type": "string",
3640+
"description": "owner of the repo",
3641+
"name": "owner",
3642+
"in": "path",
3643+
"required": true
3644+
},
3645+
{
3646+
"type": "string",
3647+
"description": "name of the repo",
3648+
"name": "repo",
3649+
"in": "path",
3650+
"required": true
3651+
},
3652+
{
3653+
"type": "string",
3654+
"description": "SHA of the commit to get",
3655+
"name": "sha",
3656+
"in": "path",
3657+
"required": true
3658+
}
3659+
],
3660+
"responses": {
3661+
"200": {
3662+
"$ref": "#/responses/string"
3663+
},
3664+
"404": {
3665+
"$ref": "#/responses/notFound"
3666+
}
3667+
}
3668+
}
3669+
},
35843670
"/repos/{owner}/{repo}/git/notes/{sha}": {
35853671
"get": {
35863672
"produces": [

0 commit comments

Comments
 (0)