feat(blob): add more bindings and API methods (#21)

This commit is contained in:
Aleksey Kulikov 2021-12-20 18:02:53 +03:00 committed by GitHub
parent 39a71811cb
commit e6bfdc5a85
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
423 changed files with 1076 additions and 39 deletions

View file

@ -123,5 +123,51 @@ Pointer<git_oid> createFromDisk({
}
}
/// Create an in-memory copy of a blob. The copy must be explicitly free'd or
/// it will leak.
Pointer<git_blob> duplicate(Pointer<git_blob> source) {
final out = calloc<Pointer<git_blob>>();
libgit2.git_blob_dup(out, source);
return out.value;
}
/// Get a buffer with the filtered content of a blob.
///
/// This applies filters as if the blob was being checked out to the working
/// directory under the specified filename. This may apply CRLF filtering or
/// other types of changes depending on the file attributes set for the blob
/// and the content detected in it.
///
/// Throws a [LibGit2Error] if error occured.
String filterContent({
required Pointer<git_blob> blobPointer,
required String asPath,
required int flags,
git_oid? attributesCommit,
}) {
final out = calloc<git_buf>();
final asPathC = asPath.toNativeUtf8().cast<Int8>();
final opts = calloc<git_blob_filter_options>();
libgit2.git_blob_filter_options_init(opts, GIT_BLOB_FILTER_OPTIONS_VERSION);
opts.ref.flags = flags;
if (attributesCommit != null) {
opts.ref.attr_commit_id = attributesCommit;
}
final error = libgit2.git_blob_filter(out, blobPointer, asPathC, opts);
calloc.free(asPathC);
calloc.free(opts);
if (error < 0) {
calloc.free(out);
throw LibGit2Error(libgit2.git_error_last());
} else {
final result = out.ref.ptr.cast<Utf8>().toDartString(length: out.ref.size);
calloc.free(out);
return result;
}
}
/// Close an open blob to release memory.
void free(Pointer<git_blob> blob) => libgit2.git_blob_free(blob);

View file

@ -80,6 +80,11 @@ class Blob {
/// Size in bytes of the contents of a blob.
int get size => bindings.size(_blobPointer);
/// Creates an in-memory copy of a blob.
///
/// **IMPORTANT**: Should be freed to release allocated memory.
Blob duplicate() => Blob(bindings.duplicate(_blobPointer));
/// Directly generates a [Patch] from the difference between two blobs.
///
/// [newBlob] is the blob for new side of diff, or null for empty blob.
@ -161,6 +166,35 @@ class Blob {
);
}
/// Returns filtered content of a blob.
///
/// This applies filters as if the blob was being checked out to the working
/// directory under the specified filename. This may apply CRLF filtering or
/// other types of changes depending on the file attributes set for the blob
/// and the content detected in it.
///
/// [asPath] is path used for file attribute lookups, etc.
///
/// [flags] is a combination of [GitBlobFilter] flags to use for filtering
/// the blob. Defaults to none.
///
/// [attributesCommit] is the commit to load attributes from, when
/// [GitBlobFilter.attributesFromCommit] is provided in [flags].
///
/// Throws a [LibGit2Error] if error occured.
String filterContent({
required String asPath,
Set<GitBlobFilter> flags = const {},
Commit? attributesCommit,
}) {
return bindings.filterContent(
blobPointer: _blobPointer,
asPath: asPath,
flags: flags.fold(0, (acc, e) => acc | e.value),
attributesCommit: attributesCommit?.oid.pointer.ref,
);
}
/// Releases memory allocated for blob object.
void free() => bindings.free(_blobPointer);

View file

@ -1728,3 +1728,40 @@ class GitIndexCapability {
@override
String toString() => 'GitIndexCapability.$_name';
}
/// Flags to control the functionality of blob content filtering.
class GitBlobFilter {
const GitBlobFilter._(this._value, this._name);
final int _value;
final String _name;
/// When set, filters will not be applied to binary files.
static const checkForBinary = GitBlobFilter._(1, 'checkForBinary');
/// When set, filters will not load configuration from the
/// system-wide `gitattributes` in `/etc` (or system equivalent).
static const noSystemAttributes = GitBlobFilter._(2, 'noSystemAttributes');
/// When set, filters will be loaded from a `.gitattributes` file
/// in the HEAD commit.
static const attributesFromHead = GitBlobFilter._(4, 'attributesFromHead');
/// When set, filters will be loaded from a `.gitattributes` file
/// in the specified commit.
static const attributesFromCommit = GitBlobFilter._(
8,
'attributesFromCommit',
);
static const List<GitBlobFilter> values = [
checkForBinary,
noSystemAttributes,
attributesFromHead,
attributesFromCommit,
];
int get value => _value;
@override
String toString() => 'GitBlobFilter.$_name';
}

View file

@ -1142,7 +1142,7 @@ class Repository {
}
/// Checkouts the provided reference [refName] using the given strategy, and
/// update the HEAD.
/// updates the HEAD.
///
/// If no reference [refName] is given, checkouts from the index.
///