From f4e9ae4167a671df2a7e43909a47c193411ccc98 Mon Sep 17 00:00:00 2001 From: Aleksey Kulikov Date: Thu, 24 Feb 2022 10:48:08 +0300 Subject: [PATCH] refactor: use git_diff_to_buf to produce patch diff text --- lib/src/bindings/diff.dart | 17 +++++++++-------- lib/src/diff.dart | 22 ++-------------------- lib/src/patch.dart | 3 --- 3 files changed, 11 insertions(+), 31 deletions(-) diff --git a/lib/src/bindings/diff.dart b/lib/src/bindings/diff.dart index 70a4a75..0a46fd3 100644 --- a/lib/src/bindings/diff.dart +++ b/lib/src/bindings/diff.dart @@ -365,14 +365,15 @@ String statsPrint({ } } -/// Add patch to buffer. -Pointer addToBuf({ - required Pointer patchPointer, - required Pointer bufferPointer, -}) { - libgit2.git_patch_to_buf(bufferPointer, patchPointer); - - return bufferPointer; +/// Produce the complete formatted text output from a diff into a buffer. +String addToBuf(Pointer diff) { + final out = calloc(); + libgit2.git_diff_to_buf(out, diff, git_diff_format_t.GIT_DIFF_FORMAT_PATCH); + final result = out.ref.ptr == nullptr + ? '' + : out.ref.ptr.cast().toDartString(length: out.ref.size); + calloc.free(out); + return result; } /// Counter for hunk number being applied. diff --git a/lib/src/diff.dart b/lib/src/diff.dart index da1c773..848e65a 100644 --- a/lib/src/diff.dart +++ b/lib/src/diff.dart @@ -293,26 +293,8 @@ class Diff { return patches; } - /// The patch diff string. - String get patch { - final length = bindings.length(_diffPointer); - var buffer = calloc(sizeOf()); - - for (var i = 0; i < length; i++) { - final patch = Patch.fromDiff(diff: this, index: i); - buffer = bindings.addToBuf( - patchPointer: patch.pointer, - bufferPointer: buffer, - ); - patch.free(); - } - - final result = buffer.ref.ptr == nullptr - ? '' - : buffer.ref.ptr.cast().toDartString(); - calloc.free(buffer); - return result; - } + /// The patch diff text. + String get patch => bindings.addToBuf(_diffPointer); /// Accumulates diff statistics for all patches. /// diff --git a/lib/src/patch.dart b/lib/src/patch.dart index d7e842b..f5f6005 100644 --- a/lib/src/patch.dart +++ b/lib/src/patch.dart @@ -153,9 +153,6 @@ class Patch { late final Pointer _patchPointer; - /// 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);