mirror of
https://github.com/SkinnyMind/libgit2dart.git
synced 2025-05-05 04:39:07 -04:00
feat(diff): add binding and API method for git_diff_tree_to_workdir_with_index
This commit is contained in:
parent
5740831bb9
commit
25203c7f9f
3 changed files with 107 additions and 0 deletions
|
@ -89,6 +89,44 @@ Pointer<git_diff> treeToWorkdir({
|
||||||
return out.value;
|
return out.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create a diff between a tree and the working directory using index data to
|
||||||
|
/// account for staged deletes, tracked files, etc.
|
||||||
|
///
|
||||||
|
/// This emulates `git diff <tree>` by diffing the tree to the index and the
|
||||||
|
/// index to the working directory and blending the results into a single diff
|
||||||
|
/// that includes staged deleted, etc.
|
||||||
|
///
|
||||||
|
/// Throws a [LibGit2Error] if error occured.
|
||||||
|
Pointer<git_diff> treeToWorkdirWithIndex({
|
||||||
|
required Pointer<git_repository> repoPointer,
|
||||||
|
required Pointer<git_tree>? treePointer,
|
||||||
|
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_tree_to_workdir_with_index(
|
||||||
|
out,
|
||||||
|
repoPointer,
|
||||||
|
treePointer ?? nullptr,
|
||||||
|
opts,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (error < 0) {
|
||||||
|
throw LibGit2Error(libgit2.git_error_last());
|
||||||
|
}
|
||||||
|
|
||||||
|
calloc.free(opts);
|
||||||
|
|
||||||
|
return out.value;
|
||||||
|
}
|
||||||
|
|
||||||
/// Create a diff with the difference between two tree objects.
|
/// Create a diff with the difference between two tree objects.
|
||||||
///
|
///
|
||||||
/// Throws a [LibGit2Error] if error occured.
|
/// Throws a [LibGit2Error] if error occured.
|
||||||
|
|
|
@ -14,6 +14,44 @@ class Diff {
|
||||||
/// **IMPORTANT**: Should be freed to release allocated memory.
|
/// **IMPORTANT**: Should be freed to release allocated memory.
|
||||||
Diff(this._diffPointer);
|
Diff(this._diffPointer);
|
||||||
|
|
||||||
|
/// Creates a diff between a [tree] and the working directory using index
|
||||||
|
/// data to account for staged deletes, tracked files, etc.
|
||||||
|
///
|
||||||
|
/// This emulates `git diff <tree>` by diffing the tree to the index and the
|
||||||
|
/// index to the working directory and blending the results into a single diff
|
||||||
|
/// that includes staged deleted, etc.
|
||||||
|
///
|
||||||
|
/// [repo] is the repository containing the tree.
|
||||||
|
///
|
||||||
|
/// [tree] is a [Tree] object to diff from, or null for empty tree.
|
||||||
|
///
|
||||||
|
/// [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.
|
||||||
|
///
|
||||||
|
/// **IMPORTANT**: Should be freed to release allocated memory.
|
||||||
|
///
|
||||||
|
/// Throws a [LibGit2Error] if error occured.
|
||||||
|
Diff.treeToWorkdirWithIndex({
|
||||||
|
required Repository repo,
|
||||||
|
required Tree? tree,
|
||||||
|
Set<GitDiff> flags = const {GitDiff.normal},
|
||||||
|
int contextLines = 3,
|
||||||
|
int interhunkLines = 0,
|
||||||
|
}) {
|
||||||
|
_diffPointer = bindings.treeToWorkdirWithIndex(
|
||||||
|
repoPointer: repo.pointer,
|
||||||
|
treePointer: tree?.pointer,
|
||||||
|
flags: flags.fold(0, (acc, e) => acc | e.value),
|
||||||
|
contextLines: contextLines,
|
||||||
|
interhunkLines: interhunkLines,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/// Reads the [content]s of a git patch file into a git diff object.
|
/// Reads the [content]s of a git patch file into a git diff object.
|
||||||
///
|
///
|
||||||
/// The diff object produced is similar to the one that would be produced if
|
/// The diff object produced is similar to the one that would be produced if
|
||||||
|
|
|
@ -56,6 +56,20 @@ void main() {
|
||||||
'subdir/modified_file',
|
'subdir/modified_file',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
const treeToWorkdirWithIndex = [
|
||||||
|
'file_deleted',
|
||||||
|
'modified_file',
|
||||||
|
'staged_changes',
|
||||||
|
'staged_changes_file_deleted',
|
||||||
|
'staged_changes_file_modified',
|
||||||
|
'staged_delete',
|
||||||
|
'staged_delete_file_modified',
|
||||||
|
'staged_new',
|
||||||
|
'staged_new_file_modified',
|
||||||
|
'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
|
||||||
|
@ -205,6 +219,23 @@ index e69de29..c217c63 100644
|
||||||
tree.free();
|
tree.free();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('returns diff between tree and workdir with index', () {
|
||||||
|
final head = repo.head;
|
||||||
|
final commit = repo.lookupCommit(head.target);
|
||||||
|
final tree = commit.tree;
|
||||||
|
|
||||||
|
final diff = Diff.treeToWorkdirWithIndex(repo: repo, tree: tree);
|
||||||
|
expect(diff.length, 11);
|
||||||
|
for (var i = 0; i < diff.deltas.length; i++) {
|
||||||
|
expect(diff.deltas[i].newFile.path, treeToWorkdirWithIndex[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
diff.free();
|
||||||
|
tree.free();
|
||||||
|
commit.free();
|
||||||
|
head.free();
|
||||||
|
});
|
||||||
|
|
||||||
test('successfully merges diffs', () {
|
test('successfully 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