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 git_patch_num_hunks, git_patch_get_hunk, git_patch_get_line_in_hunk #944

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all 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
48 changes: 48 additions & 0 deletions patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(patch.ptr))
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])
Expand Down