feat(packbuilder): add more bindings and API methods (#25)

This commit is contained in:
Aleksey Kulikov 2021-12-21 18:28:44 +03:00 committed by GitHub
parent 9791b6324c
commit 50a6087a5b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 173 additions and 7 deletions

View file

@ -61,6 +61,55 @@ void addRecursively({
} }
} }
/// Insert a commit object.
///
/// This will add a commit as well as the completed referenced tree.
///
/// Throws a [LibGit2Error] if error occured.
void addCommit({
required Pointer<git_packbuilder> packbuilderPointer,
required Pointer<git_oid> oidPointer,
}) {
final error = libgit2.git_packbuilder_insert_commit(
packbuilderPointer,
oidPointer,
);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
}
/// Insert a root tree object.
///
/// This will add the tree as well as all referenced trees and blobs.
///
/// Throws a [LibGit2Error] if error occured.
void addTree({
required Pointer<git_packbuilder> packbuilderPointer,
required Pointer<git_oid> oidPointer,
}) {
final error = libgit2.git_packbuilder_insert_tree(
packbuilderPointer,
oidPointer,
);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
}
/// Insert objects as given by the walk.
///
/// Those commits and all objects they reference will be inserted into the
/// packbuilder.
void addWalk({
required Pointer<git_packbuilder> packbuilderPointer,
required Pointer<git_revwalk> walkerPointer,
}) {
libgit2.git_packbuilder_insert_walk(packbuilderPointer, walkerPointer);
}
/// Write the new pack and corresponding index file to path. /// Write the new pack and corresponding index file to path.
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
@ -94,6 +143,14 @@ int writtenCount(Pointer<git_packbuilder> pb) {
return libgit2.git_packbuilder_written(pb); return libgit2.git_packbuilder_written(pb);
} }
/// Get the packfile's hash.
///
/// A packfile's name is derived from the sorted hashing of all object names.
/// This is only correct after the packfile has been written.
Pointer<git_oid> hash(Pointer<git_packbuilder> pb) {
return libgit2.git_packbuilder_hash(pb);
}
/// Set number of threads to spawn. /// Set number of threads to spawn.
/// ///
/// By default, libgit2 won't spawn any threads at all; when set to 0, /// By default, libgit2 won't spawn any threads at all; when set to 0,

View file

@ -41,6 +41,41 @@ class PackBuilder {
); );
} }
/// Adds a commit object.
///
/// This will add a commit as well as the completed referenced tree.
///
/// Throws a [LibGit2Error] if error occured.
void addCommit(Oid oid) {
bindings.addCommit(
packbuilderPointer: _packbuilderPointer,
oidPointer: oid.pointer,
);
}
/// Adds a root tree object.
///
/// This will add the tree as well as all referenced trees and blobs.
///
/// Throws a [LibGit2Error] if error occured.
void addTree(Oid oid) {
bindings.addTree(
packbuilderPointer: _packbuilderPointer,
oidPointer: oid.pointer,
);
}
/// Adds objects as given by the walker.
///
/// Those commits and all objects they reference will be inserted into the
/// packbuilder.
void addWalk(RevWalk walker) {
bindings.addWalk(
packbuilderPointer: _packbuilderPointer,
walkerPointer: walker.pointer,
);
}
/// Writes the new pack and corresponding index file to [path] if provided /// Writes the new pack and corresponding index file to [path] if provided
/// or default location. /// or default location.
/// ///
@ -55,6 +90,12 @@ class PackBuilder {
/// Number of objects the packbuilder has already written out. /// Number of objects the packbuilder has already written out.
int get writtenLength => bindings.writtenCount(_packbuilderPointer); int get writtenLength => bindings.writtenCount(_packbuilderPointer);
/// Packfile's hash.
///
/// A packfile's name is derived from the sorted hashing of all object names.
/// This is only correct after the packfile has been written.
Oid get hash => Oid(bindings.hash(_packbuilderPointer));
/// Sets and returns the number of threads to spawn. /// Sets and returns the number of threads to spawn.
/// ///
/// By default, libgit2 won't spawn any threads at all. When set to 0, /// By default, libgit2 won't spawn any threads at all. When set to 0,

View file

@ -11,9 +11,11 @@ class RevWalk {
_revWalkPointer = bindings.create(repo.pointer); _revWalkPointer = bindings.create(repo.pointer);
} }
/// Pointer to memory address for allocated [RevWalk] object.
late final Pointer<git_revwalk> _revWalkPointer; late final Pointer<git_revwalk> _revWalkPointer;
/// Pointer to memory address for allocated [RevWalk] object.
Pointer<git_revwalk> get pointer => _revWalkPointer;
/// Returns the list of commits from the revision walk. /// Returns the list of commits from the revision walk.
/// ///
/// Default sorting is reverse chronological order (default in git). /// Default sorting is reverse chronological order (default in git).

View file

@ -37,7 +37,7 @@ void main() {
); );
}); });
test('successfully adds objects', () { test('adds objects', () {
final packbuilder = PackBuilder(repo); final packbuilder = PackBuilder(repo);
final odb = repo.odb; final odb = repo.odb;
@ -62,7 +62,7 @@ void main() {
packbuilder.free(); packbuilder.free();
}); });
test('successfully adds object recursively', () { test('adds object recursively', () {
final packbuilder = PackBuilder(repo); final packbuilder = PackBuilder(repo);
final oid = Oid.fromSHA(repo: repo, sha: 'f17d0d48'); final oid = Oid.fromSHA(repo: repo, sha: 'f17d0d48');
@ -83,7 +83,59 @@ void main() {
packbuilder.free(); packbuilder.free();
}); });
test('successfully sets number of threads', () { test('adds commit', () {
final packbuilder = PackBuilder(repo);
final oid = repo['f17d0d4'];
packbuilder.addCommit(oid);
expect(packbuilder.length, 3);
packbuilder.free();
});
test('throws when trying to add commit with invalid oid', () {
final packbuilder = PackBuilder(repo);
final oid = Oid.fromSHA(repo: repo, sha: '0' * 40);
expect(() => packbuilder.addCommit(oid), throwsA(isA<LibGit2Error>()));
packbuilder.free();
});
test('adds tree', () {
final packbuilder = PackBuilder(repo);
final oid = repo['df2b8fc'];
packbuilder.addTree(oid);
expect(packbuilder.length, 2);
packbuilder.free();
});
test('throws when trying to add tree with invalid oid', () {
final packbuilder = PackBuilder(repo);
final oid = Oid.fromSHA(repo: repo, sha: '0' * 40);
expect(() => packbuilder.addTree(oid), throwsA(isA<LibGit2Error>()));
packbuilder.free();
});
test('adds objects with walker', () {
final oid = repo['f17d0d4'];
final packbuilder = PackBuilder(repo);
final walker = RevWalk(repo);
walker.sorting({GitSort.none});
walker.push(oid);
packbuilder.addWalk(walker);
expect(packbuilder.length, 3);
walker.free();
packbuilder.free();
});
test('sets number of threads', () {
final packbuilder = PackBuilder(repo); final packbuilder = PackBuilder(repo);
expect(packbuilder.setThreads(1), 1); expect(packbuilder.setThreads(1), 1);
@ -91,7 +143,21 @@ void main() {
packbuilder.free(); packbuilder.free();
}); });
test('successfully packs with default arguments', () { test('returns hash of packfile', () {
final packbuilder = PackBuilder(repo);
final odb = repo.odb;
packbuilder.add(odb.objects[0]);
Directory('${repo.workdir}.git/objects/pack/').createSync();
expect(packbuilder.hash.sha, '0' * 40);
packbuilder.write(null);
expect(packbuilder.hash.sha, isNot('0' * 40));
packbuilder.free();
});
test('packs with default arguments', () {
final odb = repo.odb; final odb = repo.odb;
final objectsCount = odb.objects.length; final objectsCount = odb.objects.length;
Directory('${repo.workdir}.git/objects/pack/').createSync(); Directory('${repo.workdir}.git/objects/pack/').createSync();
@ -102,7 +168,7 @@ void main() {
odb.free(); odb.free();
}); });
test('successfully packs into provided path with threads set', () { test('packs into provided path with threads set', () {
final odb = repo.odb; final odb = repo.odb;
final objectsCount = odb.objects.length; final objectsCount = odb.objects.length;
Directory('${repo.workdir}test-pack').createSync(); Directory('${repo.workdir}test-pack').createSync();
@ -120,7 +186,7 @@ void main() {
odb.free(); odb.free();
}); });
test('successfully packs with provided packDelegate', () { test('packs with provided packDelegate', () {
Directory('${repo.workdir}.git/objects/pack/').createSync(); Directory('${repo.workdir}.git/objects/pack/').createSync();
void packDelegate(PackBuilder packBuilder) { void packDelegate(PackBuilder packBuilder) {
final branches = repo.branchesLocal; final branches = repo.branchesLocal;