feat(patch): add bindings and api

This commit is contained in:
Aleksey Kulikov 2021-09-16 16:35:37 +03:00
parent f7f4a395c0
commit 344dba60e9
11 changed files with 1087 additions and 47 deletions

View file

@ -96,5 +96,82 @@ void main() {
newBlob.free();
});
group('diff', () {
const path = 'feature_file';
const oldBlobSha = 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391';
const newBlobSha = '9c78c21d6680a7ffebc76f7ac68cacc11d8f48bc';
const blobPatch = """
diff --git a/feature_file b/feature_file
index e69de29..9c78c21 100644
--- a/feature_file
+++ b/feature_file
@@ -0,0 +1 @@
+Feature edit
""";
const blobPatchDelete = """
diff --git a/feature_file b/feature_file
deleted file mode 100644
index e69de29..0000000
--- a/feature_file
+++ /dev/null
""";
test('successfully creates from blobs', () {
final a = repo[oldBlobSha] as Blob;
final b = repo[newBlobSha] as Blob;
final patch = repo.diffBlobs(
a: a,
b: b,
aPath: path,
bPath: path,
);
expect(patch.text, blobPatch);
patch.free();
});
test('successfully creates from one blob (delete)', () {
final a = repo[oldBlobSha] as Blob;
final patch = a.diff(
newBlob: null,
oldAsPath: path,
newAsPath: path,
);
expect(patch.text, blobPatchDelete);
patch.free();
});
test('successfully creates from blob and buffer', () {
final a = repo[oldBlobSha] as Blob;
final patch = Patch.createFrom(
a: a,
b: 'Feature edit\n',
aPath: path,
bPath: path,
);
expect(patch.text, blobPatch);
patch.free();
});
test('successfully creates from blob and buffer (delete)', () {
final a = repo[oldBlobSha] as Blob;
final patch = Patch.createFrom(
a: a,
b: null,
aPath: path,
bPath: path,
);
expect(patch.text, blobPatchDelete);
patch.free();
});
});
});
}

View file

@ -54,7 +54,7 @@ void main() {
'subdir/modified_file',
];
const patch = """
const patchText = """
diff --git a/subdir/modified_file b/subdir/modified_file
index e69de29..c217c63 100644
--- a/subdir/modified_file
@ -94,7 +94,7 @@ index e69de29..c217c63 100644
group('Diff', () {
test('successfully returns diff between index and workdir', () {
final index = repo.index;
final diff = index.diffToWorkdir();
final diff = repo.diff();
expect(diff.length, 8);
for (var i = 0; i < diff.deltas.length; i++) {
@ -122,7 +122,7 @@ index e69de29..c217c63 100644
test('successfully returns diff between tree and workdir', () {
final tree = (repo[repo.head.target.sha] as Commit).tree;
final diff = tree.diffToWorkdir();
final diff = repo.diff(a: tree);
expect(diff.length, 9);
for (var i = 0; i < diff.deltas.length; i++) {
@ -136,7 +136,7 @@ index e69de29..c217c63 100644
test('successfully returns diff between tree and index', () {
final index = repo.index;
final tree = (repo[repo.head.target.sha] as Commit).tree;
final diff = tree.diffToIndex(index: index);
final diff = repo.diff(a: tree, cached: true);
expect(diff.length, 8);
for (var i = 0; i < diff.deltas.length; i++) {
@ -151,7 +151,7 @@ index e69de29..c217c63 100644
test('successfully returns diff between tree and tree', () {
final tree1 = (repo[repo.head.target.sha] as Commit).tree;
final tree2 = repo['b85d53c9236e89aff2b62558adaa885fd1d6ff1c'] as Tree;
final diff = tree1.diffToTree(tree: tree2);
final diff = repo.diff(a: tree1, b: tree2);
expect(diff.length, 10);
for (var i = 0; i < diff.deltas.length; i++) {
@ -182,7 +182,7 @@ index e69de29..c217c63 100644
});
test('successfully parses provided diff', () {
final diff = Diff.parse(patch);
final diff = Diff.parse(patchText);
final stats = diff.stats;
expect(diff.length, 1);
@ -194,6 +194,17 @@ index e69de29..c217c63 100644
diff.free();
});
test('successfully creates patch from entry index in diff', () {
final diff = Diff.parse(patchText);
final patch = Patch.fromDiff(diff, 0);
expect(diff.length, 1);
expect(patch.text, patchText);
patch.free();
diff.free();
});
test('successfully finds similar entries', () {
final index = repo.index;
final oldTree = (repo[repo.head.target.sha] as Commit).tree;
@ -217,7 +228,7 @@ index e69de29..c217c63 100644
newTree.free();
});
test('returns deltas and patches', () {
test('returns deltas', () {
final index = repo.index;
final diff = index.diffToWorkdir();
@ -268,5 +279,48 @@ index e69de29..c217c63 100644
diff.free();
index.free();
});
test('returns patch diff string', () {
final diff = Diff.parse(patchText);
expect(diff.patch, patchText);
diff.free();
});
test('returns hunks in a patch', () {
final diff = Diff.parse(patchText);
final patch = Patch.fromDiff(diff, 0);
final hunk = patch.hunks[0];
expect(patch.hunks.length, 1);
expect(hunk.linesCount, 1);
expect(hunk.oldStart, 0);
expect(hunk.oldLines, 0);
expect(hunk.newStart, 1);
expect(hunk.newLines, 1);
expect(hunk.header, '\x00\x00\x00\x00@@ -0,0 +1');
patch.free();
diff.free();
});
test('returns lines in a hunk', () {
final diff = Diff.parse(patchText);
final patch = Patch.fromDiff(diff, 0);
final hunk = patch.hunks[0];
final line = hunk.lines[0];
expect(hunk.lines.length, 1);
expect(line.origin, GitDiffLine.addition);
expect(line.oldLineNumber, -1);
expect(line.newLineNumber, 1);
expect(line.numLines, 1);
expect(line.contentOffset, 155);
expect(line.content, 'Modified content\n');
patch.free();
diff.free();
});
});
}

180
test/patch_test.dart Normal file
View file

@ -0,0 +1,180 @@
import 'dart:io';
import 'package:test/test.dart';
import 'package:libgit2dart/libgit2dart.dart';
import 'helpers/util.dart';
void main() {
late Repository repo;
final tmpDir = '${Directory.systemTemp.path}/patch_testrepo/';
const oldBlob = '';
const newBlob = 'Feature edit\n';
const path = 'feature_file';
const oldBlobSha = 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391';
const newBlobSha = '9c78c21d6680a7ffebc76f7ac68cacc11d8f48bc';
const blobPatch = """
diff --git a/feature_file b/feature_file
index e69de29..9c78c21 100644
--- a/feature_file
+++ b/feature_file
@@ -0,0 +1 @@
+Feature edit
""";
const blobPatchAdd = """
diff --git a/feature_file b/feature_file
new file mode 100644
index 0000000..9c78c21
--- /dev/null
+++ b/feature_file
@@ -0,0 +1 @@
+Feature edit
""";
const blobPatchDelete = """
diff --git a/feature_file b/feature_file
deleted file mode 100644
index e69de29..0000000
--- a/feature_file
+++ /dev/null
""";
setUp(() async {
if (await Directory(tmpDir).exists()) {
await Directory(tmpDir).delete(recursive: true);
}
await copyRepo(
from: Directory('test/assets/testrepo/'),
to: await Directory(tmpDir).create(),
);
repo = Repository.open(tmpDir);
});
tearDown(() async {
repo.free();
await Directory(tmpDir).delete(recursive: true);
});
group('Patch', () {
test('successfully creates from buffers', () {
final patch = Patch.createFrom(
a: oldBlob,
b: newBlob,
aPath: path,
bPath: path,
);
expect(patch.size(), 14);
expect(patch.text, blobPatch);
patch.free();
});
test('successfully creates from one buffer (add)', () {
final patch = Patch.createFrom(
a: null,
b: newBlob,
aPath: path,
bPath: path,
);
expect(patch.text, blobPatchAdd);
patch.free();
});
test('successfully creates from one buffer (delete)', () {
final patch = Patch.createFrom(
a: oldBlob,
b: null,
aPath: path,
bPath: path,
);
expect(patch.text, blobPatchDelete);
patch.free();
});
test('successfully creates from blobs', () {
final a = repo[oldBlobSha] as Blob;
final b = repo[newBlobSha] as Blob;
final patch = Patch.createFrom(
a: a,
b: b,
aPath: path,
bPath: path,
);
expect(patch.text, blobPatch);
patch.free();
});
test('successfully creates from one blob (add)', () {
final b = repo[newBlobSha] as Blob;
final patch = Patch.createFrom(
a: null,
b: b,
aPath: path,
bPath: path,
);
expect(patch.text, blobPatchAdd);
patch.free();
});
test('successfully creates from one blob (delete)', () {
final a = repo[oldBlobSha] as Blob;
final patch = Patch.createFrom(
a: a,
b: null,
aPath: path,
bPath: path,
);
expect(patch.text, blobPatchDelete);
patch.free();
});
test('successfully creates from blob and buffer', () {
final a = repo[oldBlobSha] as Blob;
final patch = Patch.createFrom(
a: a,
b: newBlob,
aPath: path,
bPath: path,
);
expect(patch.text, blobPatch);
patch.free();
});
test('throws when argument is not Blob or String', () {
final commit = repo['fc38877b2552ab554752d9a77e1f48f738cca79b'] as Commit;
expect(
() => Patch.createFrom(
a: commit,
b: null,
aPath: 'file',
bPath: 'file',
),
throwsA(isA<ArgumentError>()),
);
expect(
() => Patch.createFrom(
a: null,
b: commit,
aPath: 'file',
bPath: 'file',
),
throwsA(isA<ArgumentError>()),
);
commit.free();
});
});
}