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].

View file

@ -71,6 +71,21 @@ void main() {
'subdir/modified_file',
];
const indexToIndex = [
'current_file',
'file_deleted',
'modified_file',
'staged_changes',
'staged_changes_file_deleted',
'staged_changes_file_modified',
'staged_new',
'staged_new_file_deleted',
'staged_new_file_modified',
'subdir/current_file',
'subdir/deleted_file',
'subdir/modified_file',
];
const patchText = """
diff --git a/subdir/modified_file b/subdir/modified_file
index e69de29..c217c63 100644
@ -237,6 +252,45 @@ index e69de29..c217c63 100644
head.free();
});
test(
'throws when trying to diff between tree and workdir with index and '
'error occurs', () {
expect(
() => Diff.treeToWorkdirWithIndex(
repo: Repository(nullptr),
tree: null,
),
throwsA(isA<LibGit2Error>()),
);
});
test('returns diff between index and index', () {
final index = repo.index;
final emptyIndex = Index.newInMemory();
final diff = index.diffToIndex(index: emptyIndex);
expect(diff.length, 12);
for (var i = 0; i < diff.deltas.length; i++) {
expect(diff.deltas[i].newFile.path, indexToIndex[i]);
}
index.free();
emptyIndex.free();
});
test('throws when trying to diff between index and index and error occurs',
() {
final index = repo.index;
expect(
() => index.diffToIndex(index: Index(nullptr)),
throwsA(isA<LibGit2Error>()),
);
index.free();
});
test('merges diffs', () {
final head = repo.head;
final commit = repo.lookupCommit(head.target);