feat(patch): add binding and API method for git_patch_line_stats (#39)

This commit is contained in:
Aleksey Kulikov 2022-01-25 16:34:27 +03:00 committed by GitHub
parent 06cc0230c7
commit 432abffa89
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 4 deletions

View file

@ -177,6 +177,31 @@ Map<String, Object> hunk({
return {'hunk': out.value, 'linesN': linesN}; return {'hunk': out.value, 'linesN': linesN};
} }
/// Get line counts of each type in a patch.
Map<String, int> lineStats(Pointer<git_patch> patch) {
final context = calloc<Uint64>();
final insertions = calloc<Uint64>();
final deletions = calloc<Uint64>();
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. /// Get data about a line in a hunk of a patch.
Pointer<git_diff_line> lines({ Pointer<git_diff_line> lines({
required Pointer<git_patch> patchPointer, required Pointer<git_patch> patchPointer,

View file

@ -42,8 +42,6 @@ class Patch {
int contextLines = 3, int contextLines = 3,
int interhunkLines = 0, int interhunkLines = 0,
}) { }) {
libgit2.git_libgit2_init();
_patchPointer = bindings.fromBlobs( _patchPointer = bindings.fromBlobs(
oldBlobPointer: oldBlob?.pointer, oldBlobPointer: oldBlob?.pointer,
oldAsPath: oldBlobPath, oldAsPath: oldBlobPath,
@ -127,6 +125,8 @@ class Patch {
int contextLines = 3, int contextLines = 3,
int interhunkLines = 0, int interhunkLines = 0,
}) { }) {
libgit2.git_libgit2_init();
_patchPointer = bindings.fromBuffers( _patchPointer = bindings.fromBuffers(
oldBuffer: oldBuffer, oldBuffer: oldBuffer,
oldAsPath: oldBufferPath, oldAsPath: oldBufferPath,
@ -148,8 +148,6 @@ class Patch {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Patch.fromDiff({required Diff diff, required int index}) { Patch.fromDiff({required Diff diff, required int index}) {
libgit2.git_libgit2_init();
_patchPointer = bindings.fromDiff(diffPointer: diff.pointer, index: index); _patchPointer = bindings.fromDiff(diffPointer: diff.pointer, index: index);
} }
@ -158,6 +156,17 @@ class Patch {
/// Pointer to memory address for allocated patch object. /// Pointer to memory address for allocated patch object.
Pointer<git_patch> get pointer => _patchPointer; Pointer<git_patch> 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. /// Content of a patch as a single diff text.
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
@ -216,3 +225,27 @@ class Patch {
@override @override
String toString() => 'Patch{size: ${size()}, delta: $delta}'; 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}';
}
}

View file

@ -163,6 +163,23 @@ index e69de29..0000000
expect(() => Patch(nullptr).text, throwsA(isA<LibGit2Error>())); expect(() => Patch(nullptr).text, throwsA(isA<LibGit2Error>()));
}); });
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', () { test('returns string representation of Patch object', () {
final patch = Patch.fromBuffers( final patch = Patch.fromBuffers(
oldBuffer: oldBuffer, oldBuffer: oldBuffer,