refactor: use git_diff_to_buf to produce patch diff text

This commit is contained in:
Aleksey Kulikov 2022-02-24 10:48:08 +03:00
parent 2eb50dec69
commit f4e9ae4167
3 changed files with 11 additions and 31 deletions

View file

@ -365,14 +365,15 @@ String statsPrint({
} }
} }
/// Add patch to buffer. /// Produce the complete formatted text output from a diff into a buffer.
Pointer<git_buf> addToBuf({ String addToBuf(Pointer<git_diff> diff) {
required Pointer<git_patch> patchPointer, final out = calloc<git_buf>();
required Pointer<git_buf> bufferPointer, libgit2.git_diff_to_buf(out, diff, git_diff_format_t.GIT_DIFF_FORMAT_PATCH);
}) { final result = out.ref.ptr == nullptr
libgit2.git_patch_to_buf(bufferPointer, patchPointer); ? ''
: out.ref.ptr.cast<Utf8>().toDartString(length: out.ref.size);
return bufferPointer; calloc.free(out);
return result;
} }
/// Counter for hunk number being applied. /// Counter for hunk number being applied.

View file

@ -293,26 +293,8 @@ class Diff {
return patches; return patches;
} }
/// The patch diff string. /// The patch diff text.
String get patch { String get patch => bindings.addToBuf(_diffPointer);
final length = bindings.length(_diffPointer);
var buffer = calloc<git_buf>(sizeOf<git_buf>());
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<Utf8>().toDartString();
calloc.free(buffer);
return result;
}
/// Accumulates diff statistics for all patches. /// Accumulates diff statistics for all patches.
/// ///

View file

@ -153,9 +153,6 @@ class Patch {
late final Pointer<git_patch> _patchPointer; late final Pointer<git_patch> _patchPointer;
/// Pointer to memory address for allocated patch object.
Pointer<git_patch> get pointer => _patchPointer;
/// Line counts of each type in a patch. /// Line counts of each type in a patch.
PatchStats get stats { PatchStats get stats {
final result = bindings.lineStats(_patchPointer); final result = bindings.lineStats(_patchPointer);