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 patch.LineStats func to get patch additions/deletions summary #949

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ func (patch *Patch) Free() error {
return nil
}

func (patch *Patch) LineStats() (uint, uint, error) {
if patch.ptr == nil {
return 0, 0, ErrInvalid
}

runtime.LockOSThread()
defer runtime.UnlockOSThread()

var context, additions, deletions C.size_t
ecode := C.git_patch_line_stats(&context, &additions, &deletions, patch.ptr)
runtime.KeepAlive(patch)
if ecode < 0 {
return 0, 0, MakeGitError(ecode)
}

return uint(additions), uint(deletions), nil
}

func (patch *Patch) String() (string, error) {
if patch.ptr == nil {
return "", ErrInvalid
Expand Down
9 changes: 9 additions & 0 deletions patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,13 @@ func TestPatch(t *testing.T) {
if strings.Index(patchStr, "diff --git a/README b/README\nindex 257cc56..820734a 100644\n--- a/README\n+++ b/README\n@@ -1 +1 @@\n-foo\n+file changed") == -1 {
t.Fatalf("patch was bad")
}

numAdditions, numDeletions, err := patch.LineStats()
checkFatal(t, err)
if numAdditions != 1 {
t.Fatal("Incorrect number of additions in line stats")
}
if numDeletions != 1 {
t.Fatal("Incorrect number of deletions in line stats")
}
}