mirror of
https://github.com/SkinnyMind/libgit2dart.git
synced 2025-05-04 20:29:08 -04:00
feat(packbuilder): add bindings and api
This commit is contained in:
parent
d40c65fa80
commit
90e99faf81
4 changed files with 320 additions and 0 deletions
107
lib/src/bindings/packbuilder.dart
Normal file
107
lib/src/bindings/packbuilder.dart
Normal file
|
@ -0,0 +1,107 @@
|
|||
import 'dart:ffi';
|
||||
import 'package:ffi/ffi.dart';
|
||||
import 'libgit2_bindings.dart';
|
||||
import '../error.dart';
|
||||
import '../util.dart';
|
||||
|
||||
/// Initialize a new packbuilder.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
Pointer<git_packbuilder> init(Pointer<git_repository> repo) {
|
||||
final out = calloc<Pointer<git_packbuilder>>();
|
||||
final error = libgit2.git_packbuilder_new(out, repo);
|
||||
|
||||
if (error < 0) {
|
||||
throw LibGit2Error(libgit2.git_error_last());
|
||||
} else {
|
||||
return out.value;
|
||||
}
|
||||
}
|
||||
|
||||
/// Insert a single object.
|
||||
///
|
||||
/// For an optimal pack it's mandatory to insert objects in recency order,
|
||||
/// commits followed by trees and blobs.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
void add({
|
||||
required Pointer<git_packbuilder> packbuilderPointer,
|
||||
required Pointer<git_oid> oidPointer,
|
||||
}) {
|
||||
final error = libgit2.git_packbuilder_insert(
|
||||
packbuilderPointer,
|
||||
oidPointer,
|
||||
nullptr,
|
||||
);
|
||||
|
||||
if (error < 0) {
|
||||
throw LibGit2Error(libgit2.git_error_last());
|
||||
}
|
||||
}
|
||||
|
||||
/// Recursively insert an object and its referenced objects.
|
||||
///
|
||||
/// Insert the object as well as any object it references.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
void addRecursively({
|
||||
required Pointer<git_packbuilder> packbuilderPointer,
|
||||
required Pointer<git_oid> oidPointer,
|
||||
}) {
|
||||
final error = libgit2.git_packbuilder_insert_recur(
|
||||
packbuilderPointer,
|
||||
oidPointer,
|
||||
nullptr,
|
||||
);
|
||||
|
||||
if (error < 0) {
|
||||
throw LibGit2Error(libgit2.git_error_last());
|
||||
}
|
||||
}
|
||||
|
||||
/// Write the new pack and corresponding index file to path.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
void write({
|
||||
required Pointer<git_packbuilder> packbuilderPointer,
|
||||
String? path,
|
||||
}) {
|
||||
final pathC = path?.toNativeUtf8().cast<Int8>() ?? nullptr;
|
||||
final error = libgit2.git_packbuilder_write(
|
||||
packbuilderPointer,
|
||||
pathC,
|
||||
0,
|
||||
nullptr,
|
||||
nullptr,
|
||||
);
|
||||
|
||||
calloc.free(pathC);
|
||||
|
||||
if (error < 0) {
|
||||
throw LibGit2Error(libgit2.git_error_last());
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the total number of objects the packbuilder will write out.
|
||||
int length(Pointer<git_packbuilder> pb) {
|
||||
return libgit2.git_packbuilder_object_count(pb);
|
||||
}
|
||||
|
||||
/// Get the number of objects the packbuilder has already written out.
|
||||
int writtenCount(Pointer<git_packbuilder> pb) {
|
||||
return libgit2.git_packbuilder_written(pb);
|
||||
}
|
||||
|
||||
/// Set number of threads to spawn.
|
||||
///
|
||||
/// By default, libgit2 won't spawn any threads at all; when set to 0,
|
||||
/// libgit2 will autodetect the number of CPUs.
|
||||
int setThreads({
|
||||
required Pointer<git_packbuilder> packbuilderPointer,
|
||||
required int number,
|
||||
}) {
|
||||
return libgit2.git_packbuilder_set_threads(packbuilderPointer, number);
|
||||
}
|
||||
|
||||
/// Free the packbuilder and all associated data.
|
||||
void free(Pointer<git_packbuilder> pb) => libgit2.git_packbuilder_free(pb);
|
73
lib/src/packbuilder.dart
Normal file
73
lib/src/packbuilder.dart
Normal file
|
@ -0,0 +1,73 @@
|
|||
import 'dart:ffi';
|
||||
import 'package:libgit2dart/libgit2dart.dart';
|
||||
import 'bindings/libgit2_bindings.dart';
|
||||
import 'bindings/packbuilder.dart' as bindings;
|
||||
|
||||
class PackBuilder {
|
||||
/// Initializes a new instance of [PackBuilder] class.
|
||||
///
|
||||
/// Should be freed with `free()`.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
PackBuilder(Repository repo) {
|
||||
_packbuilderPointer = bindings.init(repo.pointer);
|
||||
}
|
||||
|
||||
late final Pointer<git_packbuilder> _packbuilderPointer;
|
||||
|
||||
/// Pointer to memory address for allocated packbuilder object.
|
||||
Pointer<git_packbuilder> get pointer => _packbuilderPointer;
|
||||
|
||||
/// Adds a single object.
|
||||
///
|
||||
/// For an optimal pack it's mandatory to add objects in recency order,
|
||||
/// commits followed by trees and blobs.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
void add(Oid oid) {
|
||||
bindings.add(
|
||||
packbuilderPointer: _packbuilderPointer,
|
||||
oidPointer: oid.pointer,
|
||||
);
|
||||
}
|
||||
|
||||
/// Recursively adds an object and its referenced objects.
|
||||
///
|
||||
/// Adds the object as well as any object it references.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
void addRecursively(Oid oid) {
|
||||
bindings.addRecursively(
|
||||
packbuilderPointer: _packbuilderPointer,
|
||||
oidPointer: oid.pointer,
|
||||
);
|
||||
}
|
||||
|
||||
/// Writes the new pack and corresponding index file to [path] if provided
|
||||
/// or default location.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
void write(String? path) {
|
||||
bindings.write(packbuilderPointer: _packbuilderPointer, path: path);
|
||||
}
|
||||
|
||||
/// Returns the total number of objects the packbuilder will write out.
|
||||
int get length => bindings.length(_packbuilderPointer);
|
||||
|
||||
/// Returns the number of objects the packbuilder has already written out.
|
||||
int get writtenLength => bindings.writtenCount(_packbuilderPointer);
|
||||
|
||||
/// Sets and returns the number of threads to spawn.
|
||||
///
|
||||
/// By default, libgit2 won't spawn any threads at all; when set to 0,
|
||||
/// libgit2 will autodetect the number of CPUs.
|
||||
int setThreads(int number) {
|
||||
return bindings.setThreads(
|
||||
packbuilderPointer: _packbuilderPointer,
|
||||
number: number,
|
||||
);
|
||||
}
|
||||
|
||||
/// Releases memory allocated for packbuilder object.
|
||||
void free() => bindings.free(_packbuilderPointer);
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
import 'dart:ffi';
|
||||
import 'package:ffi/ffi.dart';
|
||||
import 'package:libgit2dart/libgit2dart.dart';
|
||||
import 'package:libgit2dart/src/packbuilder.dart';
|
||||
import 'bindings/libgit2_bindings.dart';
|
||||
import 'bindings/repository.dart' as bindings;
|
||||
import 'bindings/merge.dart' as merge_bindings;
|
||||
|
@ -1297,4 +1298,39 @@ class Repository {
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Packs the objects in the odb chosen by the [packDelegate] function and
|
||||
/// writes .pack and .idx files for them into provided [path] or default location.
|
||||
///
|
||||
/// Returns the number of objects written to the pack.
|
||||
///
|
||||
/// [packDelegate] is a function that will provide what objects should be added to the
|
||||
/// pack builder. Default is add all objects.
|
||||
///
|
||||
/// [threads] is number of threads the PackBuilder will spawn. Default is none. 0 will
|
||||
/// let libgit2 to autodetect number of CPUs.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
int pack(
|
||||
{String? path, void Function(PackBuilder)? packDelegate, int? threads}) {
|
||||
void packAll(PackBuilder packbuilder) {
|
||||
for (var object in odb.objects) {
|
||||
packbuilder.add(object);
|
||||
}
|
||||
}
|
||||
|
||||
final _packDelegate = packDelegate ?? packAll;
|
||||
|
||||
final packbuilder = PackBuilder(this);
|
||||
if (threads != null) {
|
||||
packbuilder.setThreads(threads);
|
||||
}
|
||||
_packDelegate(packbuilder);
|
||||
packbuilder.write(path);
|
||||
final result = packbuilder.writtenLength;
|
||||
|
||||
packbuilder.free();
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue