feat(checkout)!: add ability to checkout commit

This commit is contained in:
Aleksey Kulikov 2022-01-21 17:00:46 +03:00
parent 6fe24dcb65
commit eeffa6a008
7 changed files with 115 additions and 38 deletions

View file

@ -28,7 +28,7 @@ void main() {
expect(repo.status, contains('feature_file'));
repo.checkout(
refName: 'HEAD',
target: 'HEAD',
strategy: {GitCheckout.force},
paths: ['feature_file'],
);
@ -40,7 +40,7 @@ void main() {
'directory', () {
expect(
() => repo.checkout(
refName: 'HEAD',
target: 'HEAD',
directory: 'not/there',
),
throwsA(isA<LibGit2Error>()),
@ -70,7 +70,7 @@ void main() {
);
});
test('checkouts tree', () {
test('checkouts reference', () {
final masterHead = repo.lookupCommit(
repo['821ed6e80627b8769d170a293862f9fc60825226'],
);
@ -80,7 +80,7 @@ void main() {
false,
);
repo.checkout(refName: 'refs/heads/feature');
repo.checkout(target: 'refs/heads/feature');
final featureHead = repo.lookupCommit(
repo['5aecfa0fb97eadaac050ccb99f03c3fb65460ad4'],
);
@ -101,17 +101,56 @@ void main() {
});
test(
'throws when trying to checkout tree with invalid alternative '
'throws when trying to checkout reference with invalid alternative '
'directory', () {
expect(
() => repo.checkout(
refName: 'refs/heads/feature',
target: 'refs/heads/feature',
directory: 'not/there',
),
throwsA(isA<LibGit2Error>()),
);
});
test('checkouts commit', () {
final index = repo.index;
expect(index.find('another_feature_file'), equals(false));
final featureHead = repo.lookupCommit(
repo['5aecfa0fb97eadaac050ccb99f03c3fb65460ad4'],
);
repo.checkout(target: featureHead.oid);
final repoHead = repo.head;
expect(repoHead.target, featureHead.oid);
expect(repo.status, isEmpty);
expect(index.find('another_feature_file'), equals(true));
repoHead.free();
featureHead.free();
index.free();
});
test('checkouts commit with provided path', () {
final featureHead = repo.lookupCommit(
repo['5aecfa0fb97eadaac050ccb99f03c3fb65460ad4'],
);
repo.checkout(target: featureHead.oid, paths: ['another_feature_file']);
final repoHead = repo.head;
// When path is provided HEAD will not be set to target;
expect(repoHead.target, isNot(featureHead.oid));
expect(
repo.status,
{
'another_feature_file': {GitStatus.indexNew}
},
);
repoHead.free();
featureHead.free();
});
test('checkouts with alrenative directory', () {
final altDir = Directory(p.join(Directory.systemTemp.path, 'alt_dir'));
// making sure there is no directory
@ -121,16 +160,18 @@ void main() {
altDir.createSync();
expect(altDir.listSync().length, 0);
repo.checkout(refName: 'refs/heads/feature', directory: altDir.path);
repo.checkout(target: 'refs/heads/feature', directory: altDir.path);
expect(altDir.listSync().length, isNot(0));
altDir.deleteSync(recursive: true);
});
test('checkouts file with provided path', () {
final featureTip = repo.lookupReference('refs/heads/feature').target;
expect(repo.status, isEmpty);
repo.checkout(
refName: 'refs/heads/feature',
target: 'refs/heads/feature',
paths: ['another_feature_file'],
);
expect(
@ -139,6 +180,8 @@ void main() {
'another_feature_file': {GitStatus.indexNew}
},
);
// When path is provided HEAD will not be set to target;
expect(repo.head.target, isNot(featureTip));
});
test('performs dry run checkout', () {
@ -148,7 +191,7 @@ void main() {
expect(file.existsSync(), false);
repo.checkout(
refName: 'refs/heads/feature',
target: 'refs/heads/feature',
strategy: {GitCheckout.dryRun},
);
expect(index.length, 4);
@ -156,5 +199,9 @@ void main() {
index.free();
});
test('throws when provided target is not String or Oid', () {
expect(() => repo.checkout(target: 1), throwsA(isA<ArgumentError>()));
});
});
}