Skip to content
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

Add API to get commit diff/patch #17095

Merged
merged 11 commits into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,10 @@ 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}.diff",
6543 marked this conversation as resolved.
Show resolved Hide resolved
repo.DownloadCommitDiff)
m.Get("/{sha}.patch",
repo.DownloadCommitPatch)
})
m.Get("/refs", repo.GetGitAllRefs)
m.Get("/refs/*", repo.GetGitRefs)
Expand Down
80 changes: 80 additions & 0 deletions routers/api/v1/repo/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,83 @@ func GetAllCommits(ctx *context.APIContext) {

ctx.JSON(http.StatusOK, &apiCommits)
}

// DownloadCommitDiff render a commit's raw diff
func DownloadCommitDiff(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/git/commits/{sha}.diff repository repoDownloadCommitDiff
// ---
// summary: Get a commit diff
// 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
// responses:
6543 marked this conversation as resolved.
Show resolved Hide resolved
// "200":
// "$ref": "#/responses/string"
// "404":
// "$ref": "#/responses/notFound"
DownloadCommitDiffOrPatch(ctx, "diff")
}

// DownloadCommitPatch render a commit's raw patch
func DownloadCommitPatch(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/git/commits/{sha}.patch repository repoDownloadCommitPatch
// ---
// summary: Get a commit 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
// responses:
// "200":
// "$ref": "#/responses/string"
// "404":
// "$ref": "#/responses/notFound"
DownloadCommitDiffOrPatch(ctx, "patch")
}

// DownloadCommitDiffOrPatch render a commit's raw diff
func DownloadCommitDiffOrPatch(ctx *context.APIContext, diffType string) {
repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
if err := git.GetRawDiff(
repoPath,
ctx.Params(":sha"),
git.RawDiffType(diffType),
ctx.Resp,
); err != nil {
if git.IsErrNotExist(err) {
ctx.NotFound(ctx.Params(":sha"))
return
}
ctx.Error(http.StatusInternalServerError, "DownloadCommitDiffOrPatch", err)
return
}
}
86 changes: 86 additions & 0 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3581,6 +3581,92 @@
}
}
},
"/repos/{owner}/{repo}/git/commits/{sha}.diff": {
"get": {
"produces": [
"text/plain"
],
"tags": [
"repository"
],
"summary": "Get a commit diff",
"operationId": "repoDownloadCommitDiff",
"parameters": [
{
"type": "string",
"description": "owner of the repo",
"name": "owner",
"in": "path",
"required": true
},
{
"type": "string",
"description": "name of the repo",
"name": "repo",
"in": "path",
"required": true
},
{
"type": "string",
"description": "SHA of the commit to get",
"name": "sha",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"$ref": "#/responses/string"
},
"404": {
"$ref": "#/responses/notFound"
}
}
}
},
"/repos/{owner}/{repo}/git/commits/{sha}.patch": {
"get": {
"produces": [
"text/plain"
],
"tags": [
"repository"
],
"summary": "Get a commit patch",
"operationId": "repoDownloadCommitPatch",
"parameters": [
{
"type": "string",
"description": "owner of the repo",
"name": "owner",
"in": "path",
"required": true
},
{
"type": "string",
"description": "name of the repo",
"name": "repo",
"in": "path",
"required": true
},
{
"type": "string",
"description": "SHA of the commit to get",
"name": "sha",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"$ref": "#/responses/string"
},
"404": {
"$ref": "#/responses/notFound"
}
}
}
},
"/repos/{owner}/{repo}/git/notes/{sha}": {
"get": {
"produces": [
Expand Down