aboutsummaryrefslogtreecommitdiffstats
path: root/routers/api
diff options
context:
space:
mode:
authorqwerty287 <80460567+qwerty287@users.noreply.github.com>2021-09-20 18:14:29 +0200
committerGitHub <noreply@github.com>2021-09-20 18:14:29 +0200
commit5ac857f4d43a8452089e22c820bd1f0ddb001b7a (patch)
tree83fdf358fd8faea906fe08bc18486dfa5c12973d /routers/api
parentd4bb8e0ae7aa9bdd769a935770dfe7046d519313 (diff)
downloadgitea-5ac857f4d43a8452089e22c820bd1f0ddb001b7a.tar.gz
gitea-5ac857f4d43a8452089e22c820bd1f0ddb001b7a.zip
Add API to get commit diff/patch (#17095)
* Add API to get commit diff/patch * Add Tests Co-authored-by: 6543 <6543@obermui.de>
Diffstat (limited to 'routers/api')
-rw-r--r--routers/api/v1/api.go1
-rw-r--r--routers/api/v1/repo/commits.go50
2 files changed, 51 insertions, 0 deletions
diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go
index d859642c42..90189701c0 100644
--- a/routers/api/v1/api.go
+++ b/routers/api/v1/api.go
@@ -937,6 +937,7 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route {
m.Group("/git", func() {
m.Group("/commits", func() {
m.Get("/{sha}", repo.GetSingleCommit)
+ m.Get("/{sha}.{diffType:diff|patch}", repo.DownloadCommitDiffOrPatch)
})
m.Get("/refs", repo.GetGitAllRefs)
m.Get("/refs/*", repo.GetGitRefs)
diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go
index 975b9cab2a..639100757b 100644
--- a/routers/api/v1/repo/commits.go
+++ b/routers/api/v1/repo/commits.go
@@ -213,3 +213,53 @@ func GetAllCommits(ctx *context.APIContext) {
ctx.JSON(http.StatusOK, &apiCommits)
}
+
+// DownloadCommitDiffOrPatch render a commit's raw diff or patch
+func DownloadCommitDiffOrPatch(ctx *context.APIContext) {
+ // swagger:operation GET /repos/{owner}/{repo}/git/commits/{sha}.{diffType} repository repoDownloadCommitDiffOrPatch
+ // ---
+ // summary: Get a commit's diff or patch
+ // produces:
+ // - text/plain
+ // parameters:
+ // - name: owner
+ // in: path
+ // description: owner of the repo
+ // type: string
+ // required: true
+ // - name: repo
+ // in: path
+ // description: name of the repo
+ // type: string
+ // required: true
+ // - name: sha
+ // in: path
+ // description: SHA of the commit to get
+ // type: string
+ // required: true
+ // - name: diffType
+ // in: path
+ // description: whether the output is diff or patch
+ // type: string
+ // enum: [diff, patch]
+ // required: true
+ // responses:
+ // "200":
+ // "$ref": "#/responses/string"
+ // "404":
+ // "$ref": "#/responses/notFound"
+ repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
+ if err := git.GetRawDiff(
+ repoPath,
+ ctx.Params(":sha"),
+ git.RawDiffType(ctx.Params(":diffType")),
+ ctx.Resp,
+ ); err != nil {
+ if git.IsErrNotExist(err) {
+ ctx.NotFound(ctx.Params(":sha"))
+ return
+ }
+ ctx.Error(http.StatusInternalServerError, "DownloadCommitDiffOrPatch", err)
+ return
+ }
+}