Add patch.LineStats func to get patch additions/deletions summary #949

Open
sosedoff wants to merge 2 commits from sosedoff/add-patch-stats into main
2 changed files with 27 additions and 0 deletions

View File

@ -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

View File

@ -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")
}
}