mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-25 07:34:01 +01:00
Add API to get commit diff/patch (#17095)
* Add API to get commit diff/patch * Add Tests Co-authored-by: 6543 <6543@obermui.de>
This commit is contained in:
parent
d4bb8e0ae7
commit
5ac857f4d4
4 changed files with 128 additions and 0 deletions
|
@ -109,3 +109,26 @@ func TestAPIReposGitCommitListDifferentBranch(t *testing.T) {
|
||||||
assert.Equal(t, "f27c2b2b03dcab38beaf89b0ab4ff61f6de63441", apiData[0].CommitMeta.SHA)
|
assert.Equal(t, "f27c2b2b03dcab38beaf89b0ab4ff61f6de63441", apiData[0].CommitMeta.SHA)
|
||||||
compareCommitFiles(t, []string{"readme.md"}, apiData[0].Files)
|
compareCommitFiles(t, []string{"readme.md"}, apiData[0].Files)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDownloadCommitDiffOrPatch(t *testing.T) {
|
||||||
|
defer prepareTestEnv(t)()
|
||||||
|
user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
|
||||||
|
// Login as User2.
|
||||||
|
session := loginUser(t, user.Name)
|
||||||
|
token := getTokenForLoggedInUser(t, session)
|
||||||
|
|
||||||
|
// Test getting diff
|
||||||
|
reqDiff := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/git/commits/f27c2b2b03dcab38beaf89b0ab4ff61f6de63441.diff?token="+token, user.Name)
|
||||||
|
resp := session.MakeRequest(t, reqDiff, http.StatusOK)
|
||||||
|
assert.EqualValues(t,
|
||||||
|
"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",
|
||||||
|
resp.Body.String())
|
||||||
|
|
||||||
|
// Test getting patch
|
||||||
|
reqPatch := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/git/commits/f27c2b2b03dcab38beaf89b0ab4ff61f6de63441.patch?token="+token, user.Name)
|
||||||
|
resp = session.MakeRequest(t, reqPatch, http.StatusOK)
|
||||||
|
assert.EqualValues(t,
|
||||||
|
"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",
|
||||||
|
resp.Body.String())
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -937,6 +937,7 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route {
|
||||||
m.Group("/git", func() {
|
m.Group("/git", func() {
|
||||||
m.Group("/commits", func() {
|
m.Group("/commits", func() {
|
||||||
m.Get("/{sha}", repo.GetSingleCommit)
|
m.Get("/{sha}", repo.GetSingleCommit)
|
||||||
|
m.Get("/{sha}.{diffType:diff|patch}", repo.DownloadCommitDiffOrPatch)
|
||||||
})
|
})
|
||||||
m.Get("/refs", repo.GetGitAllRefs)
|
m.Get("/refs", repo.GetGitAllRefs)
|
||||||
m.Get("/refs/*", repo.GetGitRefs)
|
m.Get("/refs/*", repo.GetGitRefs)
|
||||||
|
|
|
@ -213,3 +213,53 @@ func GetAllCommits(ctx *context.APIContext) {
|
||||||
|
|
||||||
ctx.JSON(http.StatusOK, &apiCommits)
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -3581,6 +3581,60 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"/repos/{owner}/{repo}/git/commits/{sha}.{diffType}": {
|
||||||
|
"get": {
|
||||||
|
"produces": [
|
||||||
|
"text/plain"
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
"repository"
|
||||||
|
],
|
||||||
|
"summary": "Get a commit's diff or patch",
|
||||||
|
"operationId": "repoDownloadCommitDiffOrPatch",
|
||||||
|
"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
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"enum": [
|
||||||
|
"diff",
|
||||||
|
"patch"
|
||||||
|
],
|
||||||
|
"type": "string",
|
||||||
|
"description": "whether the output is diff or patch",
|
||||||
|
"name": "diffType",
|
||||||
|
"in": "path",
|
||||||
|
"required": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"$ref": "#/responses/string"
|
||||||
|
},
|
||||||
|
"404": {
|
||||||
|
"$ref": "#/responses/notFound"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"/repos/{owner}/{repo}/git/notes/{sha}": {
|
"/repos/{owner}/{repo}/git/notes/{sha}": {
|
||||||
"get": {
|
"get": {
|
||||||
"produces": [
|
"produces": [
|
||||||
|
|
Loading…
Reference in a new issue