feat(reset): add binding and api method for git_reset_default

This commit is contained in:
Aleksey Kulikov 2021-12-04 16:02:15 +03:00
parent 33d2750d38
commit bfe2d118b2
3 changed files with 89 additions and 0 deletions

View file

@ -1,6 +1,8 @@
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'package:libgit2dart/src/bindings/libgit2_bindings.dart';
import 'package:libgit2dart/src/error.dart';
import 'package:libgit2dart/src/util.dart';
/// Sets the current head to the specified commit oid and optionally resets the
@ -22,3 +24,43 @@ void reset({
}) {
libgit2.git_reset(repoPointer, targetPointer, resetType, checkoutOptsPointer);
}
/// Updates some entries in the index from the target commit tree.
///
/// The scope of the updated entries is determined by the paths being passed in
/// the pathspec parameters.
///
/// Throws a [LibGit2Error] if error occured.
void resetDefault({
required Pointer<git_repository> repoPointer,
required Pointer<git_object> targetPointer,
required List<String> pathspec,
}) {
final pathspecC = calloc<git_strarray>();
final pathPointers =
pathspec.map((e) => e.toNativeUtf8().cast<Int8>()).toList();
final strArray = calloc<Pointer<Int8>>(pathspec.length);
for (var i = 0; i < pathspec.length; i++) {
strArray[i] = pathPointers[i];
}
pathspecC.ref.strings = strArray;
pathspecC.ref.count = pathspec.length;
final error = libgit2.git_reset_default(
repoPointer,
targetPointer,
pathspecC,
);
calloc.free(pathspecC);
for (final p in pathPointers) {
calloc.free(p);
}
calloc.free(strArray);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
}

View file

@ -1227,6 +1227,26 @@ class Repository {
object_bindings.free(object);
}
/// Updates some entries in the index from the [oid] commit tree.
///
/// The scope of the updated entries is determined by the paths being passed
/// in the [pathspec].
///
/// Throws a [LibGit2Error] if error occured.
void resetDefault({required Oid oid, required List<String> pathspec}) {
final object = object_bindings.lookup(
repoPointer: _repoPointer,
oidPointer: oid.pointer,
type: GitObject.commit.value,
);
reset_bindings.resetDefault(
repoPointer: _repoPointer,
targetPointer: object,
pathspec: pathspec,
);
}
/// Returns a [Diff] with changes between the trees, tree and index, tree and
/// workdir or index and workdir.
///

View file

@ -61,5 +61,32 @@ void main() {
index.free();
});
group('resetDefault', () {
test('successfully updates entry in the index', () {
file.writeAsStringSync('new edit');
final index = repo.index;
index.add('feature_file');
expect(repo.status['feature_file'], {GitStatus.indexModified});
final head = repo.head;
repo.resetDefault(oid: head.target, pathspec: ['feature_file']);
expect(repo.status['feature_file'], {GitStatus.wtModified});
head.free();
index.free();
});
test('throws when pathspec list is empty', () {
final head = repo.head;
expect(
() => repo.resetDefault(oid: head.target, pathspec: []),
throwsA(isA<LibGit2Error>()),
);
head.free();
});
});
});
}