feat(index): add ability to read tree and write tree

This commit is contained in:
Aleksey Kulikov 2021-08-12 17:14:17 +03:00
parent f5e43f3d90
commit 0194d7c361
5 changed files with 179 additions and 0 deletions

View file

@ -25,6 +25,41 @@ void read(Pointer<git_index> index, bool force) {
}
}
/// Read a tree into the index file with stats.
///
/// The current index contents will be replaced by the specified tree.
///
/// Throws a [LibGit2Error] if error occured.
void readTree(Pointer<git_index> index, Pointer<git_tree> tree) {
final error = libgit2.git_index_read_tree(index, tree);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
}
/// Write the index as a tree.
///
/// This method will scan the index and write a representation of its current state back to disk;
/// it recursively creates tree objects for each of the subtrees stored in the index, but only
/// returns the OID of the root tree. This is the OID that can be used e.g. to create a commit.
///
/// The index instance cannot be bare, and needs to be associated to an existing repository.
///
/// The index must not contain any file in conflict.
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_oid> writeTree(Pointer<git_index> index) {
final out = calloc<git_oid>();
final error = libgit2.git_index_write_tree(out, index);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return out;
}
}
/// Find the first position of any entries which point to given path in the Git index.
bool find(Pointer<git_index> index, String path) {
final pathC = path.toNativeUtf8().cast<Int8>();
@ -223,5 +258,9 @@ void removeAll(Pointer<git_index> index, List<String> pathspec) {
}
}
/// Get the repository this index relates to.
Pointer<git_repository> owner(Pointer<git_index> index) =>
libgit2.git_index_owner(index);
/// Free an existing index object.
void free(Pointer<git_index> index) => libgit2.git_index_free(index);

View file

@ -0,0 +1,48 @@
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import '../error.dart';
import 'libgit2_bindings.dart';
import '../util.dart';
/// Get the id of a tree.
Pointer<git_oid> id(Pointer<git_tree> tree) => libgit2.git_tree_id(tree);
/// Lookup a tree object from the repository.
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_tree> lookup(Pointer<git_repository> repo, Pointer<git_oid> id) {
final out = calloc<Pointer<git_tree>>();
final error = libgit2.git_tree_lookup(out, repo, id);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return out.value;
}
}
/// Lookup a tree object from the repository, given a prefix of its identifier (short id).
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_tree> lookupPrefix(
Pointer<git_repository> repo,
Pointer<git_oid> id,
int len,
) {
final out = calloc<Pointer<git_tree>>();
final error = libgit2.git_tree_lookup_prefix(out, repo, id, len);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return out.value;
}
}
/// Close an open tree.
///
/// You can no longer use the git_tree pointer after this call.
///
/// IMPORTANT: You MUST call this method when you stop using a tree to release memory.
/// Failure to do so will cause a memory leak.
void free(Pointer<git_tree> tree) => libgit2.git_tree_free(tree);