]> source.dussan.org Git - gitea.git/commitdiff
Add API to get commit diff/patch (#17095)
authorqwerty287 <80460567+qwerty287@users.noreply.github.com>
Mon, 20 Sep 2021 16:14:29 +0000 (18:14 +0200)
committerGitHub <noreply@github.com>
Mon, 20 Sep 2021 16:14:29 +0000 (18:14 +0200)
* Add API to get commit diff/patch
* Add Tests

Co-authored-by: 6543 <6543@obermui.de>
integrations/api_repo_git_commits_test.go
routers/api/v1/api.go
routers/api/v1/repo/commits.go
templates/swagger/v1_json.tmpl

index 19dab433d468a3906b6b10ce91ca19ef4094d3a3..314416d2646e56b1a623a07ab479d1e7c2ef3b81 100644 (file)
@@ -109,3 +109,26 @@ func TestAPIReposGitCommitListDifferentBranch(t *testing.T) {
        assert.Equal(t, "f27c2b2b03dcab38beaf89b0ab4ff61f6de63441", apiData[0].CommitMeta.SHA)
        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())
+
+}
index d859642c42a1fbfea9bf696eaf3c5a0748df9756..90189701c01a638c3cc0ead36ffbd0f790952152 100644 (file)
@@ -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)
index 975b9cab2a9ce4aae1aabb0a1317c236092cf0ae..639100757bdd7c0ffb19866faa3b82797328bc93 100644 (file)
@@ -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
+       }
+}
index 69081777dc7444788031cd6226c3f5d86092d1af..77c89aea3af9f5d3f5f2b9afd34dfdb6670e9d12 100644 (file)
         }
       }
     },
+    "/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}": {
       "get": {
         "produces": [