mirror of
https://github.com/SkinnyMind/libgit2dart.git
synced 2025-05-05 04:39:07 -04:00
feat(index): add ability to create diff between two indexes (#34)
This commit is contained in:
parent
cc78e7945f
commit
6fe24dcb65
3 changed files with 139 additions and 15 deletions
|
@ -5,6 +5,42 @@ import 'package:libgit2dart/src/bindings/libgit2_bindings.dart';
|
||||||
import 'package:libgit2dart/src/error.dart';
|
import 'package:libgit2dart/src/error.dart';
|
||||||
import 'package:libgit2dart/src/util.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.
|
/// Create a diff between the repository index and the workdir directory.
|
||||||
Pointer<git_diff> indexToWorkdir({
|
Pointer<git_diff> indexToWorkdir({
|
||||||
required Pointer<git_repository> repoPointer,
|
required Pointer<git_repository> repoPointer,
|
||||||
|
@ -80,14 +116,15 @@ Pointer<git_diff> treeToWorkdir({
|
||||||
opts,
|
opts,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (error < 0) {
|
|
||||||
throw LibGit2Error(libgit2.git_error_last());
|
|
||||||
}
|
|
||||||
|
|
||||||
calloc.free(opts);
|
calloc.free(opts);
|
||||||
|
|
||||||
|
if (error < 0) {
|
||||||
|
calloc.free(out);
|
||||||
|
throw LibGit2Error(libgit2.git_error_last());
|
||||||
|
} else {
|
||||||
return out.value;
|
return out.value;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Create a diff between a tree and the working directory using index data to
|
/// Create a diff between a tree and the working directory using index data to
|
||||||
/// account for staged deletes, tracked files, etc.
|
/// account for staged deletes, tracked files, etc.
|
||||||
|
@ -118,14 +155,15 @@ Pointer<git_diff> treeToWorkdirWithIndex({
|
||||||
opts,
|
opts,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (error < 0) {
|
|
||||||
throw LibGit2Error(libgit2.git_error_last());
|
|
||||||
}
|
|
||||||
|
|
||||||
calloc.free(opts);
|
calloc.free(opts);
|
||||||
|
|
||||||
|
if (error < 0) {
|
||||||
|
calloc.free(out);
|
||||||
|
throw LibGit2Error(libgit2.git_error_last());
|
||||||
|
} else {
|
||||||
return out.value;
|
return out.value;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Create a diff with the difference between two tree objects.
|
/// Create a diff with the difference between two tree objects.
|
||||||
///
|
///
|
||||||
|
@ -153,14 +191,15 @@ Pointer<git_diff> treeToTree({
|
||||||
opts,
|
opts,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (error < 0) {
|
|
||||||
throw LibGit2Error(libgit2.git_error_last());
|
|
||||||
}
|
|
||||||
|
|
||||||
calloc.free(opts);
|
calloc.free(opts);
|
||||||
|
|
||||||
|
if (error < 0) {
|
||||||
|
calloc.free(out);
|
||||||
|
throw LibGit2Error(libgit2.git_error_last());
|
||||||
|
} else {
|
||||||
return out.value;
|
return out.value;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Query how many diff records are there in a diff.
|
/// Query how many diff records are there in a diff.
|
||||||
int length(Pointer<git_diff> diff) => libgit2.git_diff_num_deltas(diff);
|
int length(Pointer<git_diff> diff) => libgit2.git_diff_num_deltas(diff);
|
||||||
|
|
|
@ -300,6 +300,37 @@ class Index with IterableMixin<IndexEntry> {
|
||||||
void removeAll(List<String> path) =>
|
void removeAll(List<String> path) =>
|
||||||
bindings.removeAll(indexPointer: _indexPointer, pathspec: 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.
|
/// Creates a diff between the repository index and the workdir directory.
|
||||||
///
|
///
|
||||||
/// [flags] is a combination of [GitDiff] flags. Defaults to [GitDiff.normal].
|
/// [flags] is a combination of [GitDiff] flags. Defaults to [GitDiff.normal].
|
||||||
|
|
|
@ -71,6 +71,21 @@ void main() {
|
||||||
'subdir/modified_file',
|
'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 = """
|
const patchText = """
|
||||||
diff --git a/subdir/modified_file b/subdir/modified_file
|
diff --git a/subdir/modified_file b/subdir/modified_file
|
||||||
index e69de29..c217c63 100644
|
index e69de29..c217c63 100644
|
||||||
|
@ -237,6 +252,45 @@ index e69de29..c217c63 100644
|
||||||
head.free();
|
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', () {
|
test('merges diffs', () {
|
||||||
final head = repo.head;
|
final head = repo.head;
|
||||||
final commit = repo.lookupCommit(head.target);
|
final commit = repo.lookupCommit(head.target);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue