From 83584f0fef80e71ad529235ddd5de9e2a2af6e6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20R=2E=20de=20Miranda?= Date: Wed, 25 Jan 2023 13:06:51 -0300 Subject: [PATCH 1/2] Add git_patch_num_hunks, git_patch_get_hunk, git_patch_get_line_in_hunk --- patch.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/patch.go b/patch.go index 9b676870..a3275e69 100644 --- a/patch.go +++ b/patch.go @@ -57,6 +57,54 @@ func (patch *Patch) String() (string, error) { return C.GoString(buf.ptr), nil } +func (patch *Patch) NumHunks() (int, error) { + if patch.ptr == nil { + return -1, ErrInvalid + } + ret := int(C.git_patch_num_hunks()) + runtime.KeepAlive(patch) + return ret, nil +} + +func (patch *Patch) Hunk(hunkIndex int) (DiffHunk, int, error) { + if patch.ptr == nil { + return DiffHunk{}, 0, ErrInvalid + } + + var diffHunkPtr *C.git_diff_hunk + var cLinesInHunk C.size_t + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + ecode := C.git_patch_get_hunk(&diffHunkPtr, &cLinesInHunk, patch.ptr, C.size_t(hunkIndex)) + runtime.KeepAlive(patch) + if ecode < 0 { + return DiffHunk{}, 0, MakeGitError(ecode) + } + + return diffHunkFromC(diffHunkPtr), int(cLinesInHunk), nil +} + +func (patch *Patch) HunkLine(hunkIndex, hunkLine int) (DiffLine, error) { + if patch.ptr == nil { + return DiffLine{}, ErrInvalid + } + + var diffLinePtr *C.git_diff_line + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + ecode := C.git_patch_get_line_in_hunk(&diffLinePtr, patch.ptr, C.size_t(hunkIndex), C.size_t(hunkLine)) + runtime.KeepAlive(patch) + if ecode < 0 { + return DiffLine{}, MakeGitError(ecode) + } + + return diffLineFromC(diffLinePtr), nil +} + func toPointer(data []byte) (ptr unsafe.Pointer) { if len(data) > 0 { ptr = unsafe.Pointer(&data[0]) From f1ece8650b60f13fa633fe94fb93dd221f65cdaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20R=2E=20de=20Miranda?= Date: Mon, 30 Jan 2023 10:08:25 -0300 Subject: [PATCH 2/2] Fix patch num hunk without patch.ptr --- patch.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/patch.go b/patch.go index a3275e69..cf79b495 100644 --- a/patch.go +++ b/patch.go @@ -61,7 +61,7 @@ func (patch *Patch) NumHunks() (int, error) { if patch.ptr == nil { return -1, ErrInvalid } - ret := int(C.git_patch_num_hunks()) + ret := int(C.git_patch_num_hunks(patch.ptr)) runtime.KeepAlive(patch) return ret, nil }