diff --git a/patch.go b/patch.go index 9b67687..9cd2c50 100644 --- a/patch.go +++ b/patch.go @@ -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 diff --git a/patch_test.go b/patch_test.go index 291c705..6b4b186 100644 --- a/patch_test.go +++ b/patch_test.go @@ -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") + } }