diff --git a/lib/src/bindings/patch.dart b/lib/src/bindings/patch.dart index 5641c23..044e191 100644 --- a/lib/src/bindings/patch.dart +++ b/lib/src/bindings/patch.dart @@ -177,6 +177,31 @@ Map hunk({ return {'hunk': out.value, 'linesN': linesN}; } +/// Get line counts of each type in a patch. +Map lineStats(Pointer patch) { + final context = calloc(); + final insertions = calloc(); + final deletions = calloc(); + libgit2.git_patch_line_stats( + context, + insertions, + deletions, + patch, + ); + + final result = { + 'context': context.value, + 'insertions': insertions.value, + 'deletions': deletions.value, + }; + + calloc.free(context); + calloc.free(insertions); + calloc.free(deletions); + + return result; +} + /// Get data about a line in a hunk of a patch. Pointer lines({ required Pointer patchPointer, diff --git a/lib/src/patch.dart b/lib/src/patch.dart index a0f0030..d7e842b 100644 --- a/lib/src/patch.dart +++ b/lib/src/patch.dart @@ -42,8 +42,6 @@ class Patch { int contextLines = 3, int interhunkLines = 0, }) { - libgit2.git_libgit2_init(); - _patchPointer = bindings.fromBlobs( oldBlobPointer: oldBlob?.pointer, oldAsPath: oldBlobPath, @@ -127,6 +125,8 @@ class Patch { int contextLines = 3, int interhunkLines = 0, }) { + libgit2.git_libgit2_init(); + _patchPointer = bindings.fromBuffers( oldBuffer: oldBuffer, oldAsPath: oldBufferPath, @@ -148,8 +148,6 @@ class Patch { /// /// Throws a [LibGit2Error] if error occured. Patch.fromDiff({required Diff diff, required int index}) { - libgit2.git_libgit2_init(); - _patchPointer = bindings.fromDiff(diffPointer: diff.pointer, index: index); } @@ -158,6 +156,17 @@ class Patch { /// Pointer to memory address for allocated patch object. Pointer get pointer => _patchPointer; + /// Line counts of each type in a patch. + PatchStats get stats { + final result = bindings.lineStats(_patchPointer); + + return PatchStats( + context: result['context']!, + insertions: result['insertions']!, + deletions: result['deletions']!, + ); + } + /// Content of a patch as a single diff text. /// /// Throws a [LibGit2Error] if error occured. @@ -216,3 +225,27 @@ class Patch { @override String toString() => 'Patch{size: ${size()}, delta: $delta}'; } + +/// Line counts of each type in a patch. +class PatchStats { + const PatchStats({ + required this.context, + required this.insertions, + required this.deletions, + }); + + /// Count of context lines. + final int context; + + /// Count of insertion lines. + final int insertions; + + /// Count of deletion lines. + final int deletions; + + @override + String toString() { + return 'PatchStats{context: $context, insertions: $insertions, ' + 'deletions: $deletions}'; + } +} diff --git a/test/patch_test.dart b/test/patch_test.dart index c9161a6..f8ea295 100644 --- a/test/patch_test.dart +++ b/test/patch_test.dart @@ -163,6 +163,23 @@ index e69de29..0000000 expect(() => Patch(nullptr).text, throwsA(isA())); }); + test('returns line counts of each type in a patch', () { + final patch = Patch.fromBuffers( + oldBuffer: oldBuffer, + newBuffer: newBuffer, + oldBufferPath: path, + newBufferPath: path, + ); + + final stats = patch.stats; + expect(stats.context, equals(0)); + expect(stats.insertions, equals(1)); + expect(stats.deletions, equals(0)); + expect(stats.toString(), contains('PatchStats{')); + + patch.free(); + }); + test('returns string representation of Patch object', () { final patch = Patch.fromBuffers( oldBuffer: oldBuffer,