Add function to get commit diff/patch (#589)
- Adds function the gets a commit's diff or patch. - Ref: https://github.com/go-gitea/gitea/pull/17095 Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/589 Reviewed-by: John Olheiser <john.olheiser@gmail.com> Reviewed-by: 6543 <6543@obermui.de> Co-authored-by: Gusted <williamzijl7@hotmail.com> Co-committed-by: Gusted <williamzijl7@hotmail.com>
This commit is contained in:
parent
319a978c6c
commit
f3ebdb8afe
2 changed files with 59 additions and 0 deletions
|
@ -113,3 +113,29 @@ func (c *Client) ListRepoCommits(user, repo string, opt ListCommitOptions) ([]*C
|
|||
resp, err := c.getParsedResponse("GET", link.String(), nil, nil, &commits)
|
||||
return commits, resp, err
|
||||
}
|
||||
|
||||
// GetCommitDiff returns the commit's raw diff.
|
||||
func (c *Client) GetCommitDiff(user, repo, commitID string) ([]byte, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_16_0); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if err := escapeValidatePathSegments(&user, &repo); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return c.getResponse("GET", fmt.Sprintf("/repos/%s/%s/git/commits/%s.%s", user, repo, commitID, pullRequestDiffTypeDiff), nil, nil)
|
||||
}
|
||||
|
||||
// GetCommitPatch returns the commit's raw patch.
|
||||
func (c *Client) GetCommitPatch(user, repo, commitID string) ([]byte, *Response, error) {
|
||||
if err := c.checkServerVersionGreaterThanOrEqual(version1_16_0); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if err := escapeValidatePathSegments(&user, &repo); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return c.getResponse("GET", fmt.Sprintf("/repos/%s/%s/git/commits/%s.%s", user, repo, commitID, pullRequestDiffTypePatch), nil, nil)
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
package gitea
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
|
@ -25,3 +26,35 @@ func TestListRepoCommits(t *testing.T) {
|
|||
assert.EqualValues(t, "gpg.error.not_signed_commit", l[0].RepoCommit.Verification.Reason)
|
||||
assert.EqualValues(t, 100, l[0].Stats.Additions)
|
||||
}
|
||||
|
||||
func TestGetCommitDiffOrPatch(t *testing.T) {
|
||||
log.Println("== TestGetCommitDiffOrPatch ==")
|
||||
c := newTestClient()
|
||||
|
||||
repo, err := createTestRepo(t, "TestGetCommitDiffOrPatch", c)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Add a new simple small commit to the repository.
|
||||
fileResponse, _, err := c.CreateFile(repo.Owner.UserName, repo.Name, "NOT_A_LICENSE", CreateFileOptions{
|
||||
Content: base64.StdEncoding.EncodeToString([]byte("But is it?\n")),
|
||||
FileOptions: FileOptions{
|
||||
Message: "Ensure people know it's not a license!",
|
||||
Committer: Identity{
|
||||
Name: "Sup3rCookie",
|
||||
Email: "Sup3rCookie@example.com",
|
||||
},
|
||||
},
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Test the diff output.
|
||||
diffOutput, _, err := c.GetCommitDiff(repo.Owner.UserName, repo.Name, fileResponse.Commit.SHA)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, "diff --git a/NOT_A_LICENSE b/NOT_A_LICENSE\nnew file mode 100644\nindex 0000000..f27a20a\n--- /dev/null\n+++ b/NOT_A_LICENSE\n@@ -0,0 +1 @@\n+But is it?\n", string(diffOutput))
|
||||
|
||||
// Test the patch output.
|
||||
patchOutput, _, err := c.GetCommitPatch(repo.Owner.UserName, repo.Name, fileResponse.Commit.SHA)
|
||||
assert.NoError(t, err)
|
||||
// Use contains, because we cannot include the first part, because of dates + non-static CommitID..
|
||||
assert.Contains(t, string(patchOutput), "Subject: [PATCH] Ensure people know it's not a license!\n\n---\n NOT_A_LICENSE | 1 +\n 1 file changed, 1 insertion(+)\n create mode 100644 NOT_A_LICENSE\n\ndiff --git a/NOT_A_LICENSE b/NOT_A_LICENSE\nnew file mode 100644\nindex 0000000..f27a20a\n--- /dev/null\n+++ b/NOT_A_LICENSE\n@@ -0,0 +1 @@\n+But is it?\n")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue