feat(index): add ability to create diff between two indexes (#34)

This commit is contained in:
Aleksey Kulikov 2022-01-14 18:44:14 +03:00 committed by GitHub
parent cc78e7945f
commit 6fe24dcb65
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 139 additions and 15 deletions

View file

@ -5,6 +5,42 @@ import 'package:libgit2dart/src/bindings/libgit2_bindings.dart';
import 'package:libgit2dart/src/error.dart';
import 'package:libgit2dart/src/util.dart';
/// Create a diff with the difference between two index objects.
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_diff> indexToIndex({
required Pointer<git_repository> repoPointer,
required Pointer<git_index> oldIndexPointer,
required Pointer<git_index> newIndexPointer,
required int flags,
required int contextLines,
required int interhunkLines,
}) {
final out = calloc<Pointer<git_diff>>();
final opts = _diffOptionsInit(
flags: flags,
contextLines: contextLines,
interhunkLines: interhunkLines,
);
final error = libgit2.git_diff_index_to_index(
out,
repoPointer,
oldIndexPointer,
newIndexPointer,
opts,
);
calloc.free(opts);
if (error < 0) {
calloc.free(out);
throw LibGit2Error(libgit2.git_error_last());
} else {
return out.value;
}
}
/// Create a diff between the repository index and the workdir directory.
Pointer<git_diff> indexToWorkdir({
required Pointer<git_repository> repoPointer,
@ -80,13 +116,14 @@ Pointer<git_diff> treeToWorkdir({
opts,
);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
calloc.free(opts);
return out.value;
if (error < 0) {
calloc.free(out);
throw LibGit2Error(libgit2.git_error_last());
} else {
return out.value;
}
}
/// Create a diff between a tree and the working directory using index data to
@ -118,13 +155,14 @@ Pointer<git_diff> treeToWorkdirWithIndex({
opts,
);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
calloc.free(opts);
return out.value;
if (error < 0) {
calloc.free(out);
throw LibGit2Error(libgit2.git_error_last());
} else {
return out.value;
}
}
/// Create a diff with the difference between two tree objects.
@ -153,13 +191,14 @@ Pointer<git_diff> treeToTree({
opts,
);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
calloc.free(opts);
return out.value;
if (error < 0) {
calloc.free(out);
throw LibGit2Error(libgit2.git_error_last());
} else {
return out.value;
}
}
/// Query how many diff records are there in a diff.

View file

@ -300,6 +300,37 @@ class Index with IterableMixin<IndexEntry> {
void removeAll(List<String> path) =>
bindings.removeAll(indexPointer: _indexPointer, pathspec: path);
/// Creates a diff with the difference between two index objects.
///
/// [index] is the [Index] object to diff to.
///
/// [flags] is a combination of [GitDiff] flags. Defaults to [GitDiff.normal].
///
/// [contextLines] is the number of unchanged lines that define the boundary
/// of a hunk (and to display before and after). Defaults to 3.
///
/// [interhunkLines] is the maximum number of unchanged lines between hunk
/// boundaries before the hunks will be merged into one. Defaults to 0.
///
/// Throws a [LibGit2Error] if error occured.
Diff diffToIndex({
required Index index,
Set<GitDiff> flags = const {GitDiff.normal},
int contextLines = 3,
int interhunkLines = 0,
}) {
return Diff(
diff_bindings.indexToIndex(
repoPointer: bindings.owner(_indexPointer),
oldIndexPointer: _indexPointer,
newIndexPointer: index.pointer,
flags: flags.fold(0, (acc, e) => acc | e.value),
contextLines: contextLines,
interhunkLines: interhunkLines,
),
);
}
/// Creates a diff between the repository index and the workdir directory.
///
/// [flags] is a combination of [GitDiff] flags. Defaults to [GitDiff.normal].