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.
///

View file

@ -0,0 +1 @@
add .gitattributes

Binary file not shown.

View file

@ -0,0 +1,12 @@
0000000000000000000000000000000000000000 f17d0d48eae3aa08cecf29128a35e310c97b3521 Aleksey Kulikov <skinny.mind@gmail.com> 1626090830 +0300 commit (initial): init
f17d0d48eae3aa08cecf29128a35e310c97b3521 f17d0d48eae3aa08cecf29128a35e310c97b3521 Aleksey Kulikov <skinny.mind@gmail.com> 1626090891 +0300 checkout: moving from master to feature
f17d0d48eae3aa08cecf29128a35e310c97b3521 6cbc22e509d72758ab4c8d9f287ea846b90c448b Aleksey Kulikov <skinny.mind@gmail.com> 1626091020 +0300 commit: add feature file
6cbc22e509d72758ab4c8d9f287ea846b90c448b fc38877b2552ab554752d9a77e1f48f738cca79b Aleksey Kulikov <skinny.mind@gmail.com> 1626091054 +0300 commit: edit feature file
fc38877b2552ab554752d9a77e1f48f738cca79b f17d0d48eae3aa08cecf29128a35e310c97b3521 Aleksey Kulikov <skinny.mind@gmail.com> 1626091067 +0300 checkout: moving from feature to master
f17d0d48eae3aa08cecf29128a35e310c97b3521 c68ff54aabf660fcdd9a2838d401583fe31249e3 Aleksey Kulikov <skinny.mind@gmail.com> 1626091171 +0300 commit: add .gitignore
c68ff54aabf660fcdd9a2838d401583fe31249e3 78b8bf123e3952c970ae5c1ce0a3ea1d1336f6e8 Aleksey Kulikov <skinny.mind@gmail.com> 1626091184 +0300 merge feature: Merge made by the 'recursive' strategy.
78b8bf123e3952c970ae5c1ce0a3ea1d1336f6e8 fc38877b2552ab554752d9a77e1f48f738cca79b Aleksey Kulikov <skinny.mind@gmail.com> 1626091245 +0300 checkout: moving from master to feature
fc38877b2552ab554752d9a77e1f48f738cca79b 5aecfa0fb97eadaac050ccb99f03c3fb65460ad4 Aleksey Kulikov <skinny.mind@gmail.com> 1626091274 +0300 commit: add another feature file
5aecfa0fb97eadaac050ccb99f03c3fb65460ad4 78b8bf123e3952c970ae5c1ce0a3ea1d1336f6e8 Aleksey Kulikov <skinny.mind@gmail.com> 1626091285 +0300 checkout: moving from feature to master
78b8bf123e3952c970ae5c1ce0a3ea1d1336f6e8 821ed6e80627b8769d170a293862f9fc60825226 Aleksey Kulikov <skinny.mind@gmail.com> 1630568461 +0300 commit: add subdirectory file
821ed6e80627b8769d170a293862f9fc60825226 d2f3abc9324a22a9f80fec2c131ec43c93430618 Aleksey Kulikov <skinny.mind@gmail.com> 1640009945 +0300 commit: add .gitattributes

View file

@ -0,0 +1,5 @@
0000000000000000000000000000000000000000 f17d0d48eae3aa08cecf29128a35e310c97b3521 Aleksey Kulikov <skinny.mind@gmail.com> 1626090830 +0300 commit (initial): init
f17d0d48eae3aa08cecf29128a35e310c97b3521 c68ff54aabf660fcdd9a2838d401583fe31249e3 Aleksey Kulikov <skinny.mind@gmail.com> 1626091171 +0300 commit: add .gitignore
c68ff54aabf660fcdd9a2838d401583fe31249e3 78b8bf123e3952c970ae5c1ce0a3ea1d1336f6e8 Aleksey Kulikov <skinny.mind@gmail.com> 1626091184 +0300 merge feature: Merge made by the 'recursive' strategy.
78b8bf123e3952c970ae5c1ce0a3ea1d1336f6e8 821ed6e80627b8769d170a293862f9fc60825226 Aleksey Kulikov <skinny.mind@gmail.com> 1630568461 +0300 commit: add subdirectory file
821ed6e80627b8769d170a293862f9fc60825226 d2f3abc9324a22a9f80fec2c131ec43c93430618 Aleksey Kulikov <skinny.mind@gmail.com> 1640009945 +0300 commit: add .gitattributes

View file

@ -0,0 +1,2 @@
<EFBFBD>
<EFBFBD>

View file

@ -0,0 +1 @@
d2f3abc9324a22a9f80fec2c131ec43c93430618

View file

@ -0,0 +1 @@
d2f3abc9324a22a9f80fec2c131ec43c93430618

View file

@ -0,0 +1,3 @@
*.txt text
*.crlf text eol=crlf

Some files were not shown because too many files have changed in this diff Show more