From 59b46241d3cdb915763578d7f2919cce8c069b93 Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Wed, 15 Mar 2023 18:44:50 -0500 Subject: [PATCH 1/2] Add patch.Stats func to get patch additions/deletions summary --- patch.go | 18 ++++++++++++++++++ patch_test.go | 9 +++++++++ 2 files changed, 27 insertions(+) diff --git a/patch.go b/patch.go index 9b676870e..a494fbad4 100644 --- a/patch.go +++ b/patch.go @@ -37,6 +37,24 @@ func (patch *Patch) Free() error { return nil } +func (patch *Patch) Stats() (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 291c7059a..b91011db5 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.Stats() + checkFatal(t, err) + if numAdditions != 1 { + t.Fatal("Incorrect number of additions in stats") + } + if numDeletions != 1 { + t.Fatal("Incorrect number of deletions in stats") + } } From e24dc55e1ba8119f70170c6f55c89e1199120501 Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Wed, 15 Mar 2023 18:49:00 -0500 Subject: [PATCH 2/2] Rename patch.Stats to LineStats --- patch.go | 2 +- patch_test.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/patch.go b/patch.go index a494fbad4..9cd2c5088 100644 --- a/patch.go +++ b/patch.go @@ -37,7 +37,7 @@ func (patch *Patch) Free() error { return nil } -func (patch *Patch) Stats() (uint, uint, error) { +func (patch *Patch) LineStats() (uint, uint, error) { if patch.ptr == nil { return 0, 0, ErrInvalid } diff --git a/patch_test.go b/patch_test.go index b91011db5..6b4b18652 100644 --- a/patch_test.go +++ b/patch_test.go @@ -36,12 +36,12 @@ func TestPatch(t *testing.T) { t.Fatalf("patch was bad") } - numAdditions, numDeletions, err := patch.Stats() + numAdditions, numDeletions, err := patch.LineStats() checkFatal(t, err) if numAdditions != 1 { - t.Fatal("Incorrect number of additions in stats") + t.Fatal("Incorrect number of additions in line stats") } if numDeletions != 1 { - t.Fatal("Incorrect number of deletions in stats") + t.Fatal("Incorrect number of deletions in line stats") } }