feat(checkout)!: add ability to checkout commit (#35)

This commit is contained in:
Aleksey Kulikov 2022-01-21 17:10:41 +03:00 committed by GitHub
parent 6fe24dcb65
commit f7dde69de3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 115 additions and 38 deletions

View file

@ -128,7 +128,7 @@ void main() {
});
test('filters content of a blob with provided commit for attributes', () {
repo.checkout(refName: 'refs/tags/v0.2');
repo.checkout(target: 'refs/tags/v0.2');
final blobOid = repo.createBlob('clrf\nclrf\n');
final blob = repo.lookupBlob(blobOid);

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>()));
});
});
}

View file

@ -337,7 +337,7 @@ index e69de29..c217c63 100644
);
final diff2 = Diff.parse(patchText);
repo.checkout(refName: 'HEAD', strategy: {GitCheckout.force});
repo.checkout(target: 'HEAD', strategy: {GitCheckout.force});
expect(
repo.applies(diff: diff2, location: GitApplyLocation.both),
true,
@ -357,7 +357,7 @@ index e69de29..c217c63 100644
final diff2 = Diff.parse(patchText);
final hunk = diff2.patches.first.hunks.first;
repo.checkout(refName: 'HEAD', strategy: {GitCheckout.force});
repo.checkout(target: 'HEAD', strategy: {GitCheckout.force});
expect(
repo.applies(
diff: diff2,
@ -375,7 +375,7 @@ index e69de29..c217c63 100644
final diff = Diff.parse(patchText);
final file = File(p.join(tmpDir.path, 'subdir', 'modified_file'));
repo.checkout(refName: 'HEAD', strategy: {GitCheckout.force});
repo.checkout(target: 'HEAD', strategy: {GitCheckout.force});
expect(file.readAsStringSync(), '');
repo.apply(diff: diff);
@ -405,7 +405,7 @@ index e69de29..c217c63 100644
final hunk = diff.patches.first.hunks.first;
final file = File(p.join(tmpDir.path, 'subdir', 'modified_file'));
repo.checkout(refName: 'HEAD', strategy: {GitCheckout.force});
repo.checkout(target: 'HEAD', strategy: {GitCheckout.force});
expect(file.readAsStringSync(), '');
repo.apply(diff: diff, hunkIndex: hunk.index);
@ -417,7 +417,7 @@ index e69de29..c217c63 100644
test('applies diff to tree', () {
final diff = Diff.parse(patchText);
repo.checkout(refName: 'HEAD', strategy: {GitCheckout.force});
repo.checkout(target: 'HEAD', strategy: {GitCheckout.force});
final head = repo.head;
final commit = repo.lookupCommit(head.target);
final tree = commit.tree;
@ -444,7 +444,7 @@ index e69de29..c217c63 100644
final diff = Diff.parse(patchText);
final hunk = diff.patches.first.hunks.first;
repo.checkout(refName: 'HEAD', strategy: {GitCheckout.force});
repo.checkout(target: 'HEAD', strategy: {GitCheckout.force});
final head = repo.head;
final commit = repo.lookupCommit(head.target);
final tree = commit.tree;

View file

@ -365,7 +365,7 @@ void main() {
oid: conflictBranch.target,
);
conflictRepo.checkout(refName: 'refs/heads/feature');
conflictRepo.checkout(target: 'refs/heads/feature');
conflictRepo.merge(commit: commit);
@ -421,7 +421,7 @@ void main() {
oid: conflictBranch.target,
);
conflictRepo.checkout(refName: 'refs/heads/our-conflict');
conflictRepo.checkout(target: 'refs/heads/our-conflict');
conflictRepo.merge(commit: commit);
@ -449,7 +449,7 @@ void main() {
oid: conflictBranch.target,
);
conflictRepo.checkout(refName: 'refs/heads/feature');
conflictRepo.checkout(target: 'refs/heads/feature');
conflictRepo.merge(commit: commit);

View file

@ -159,7 +159,7 @@ Another feature edit
repo: repo,
oid: conflictBranch.target,
);
repo.checkout(refName: 'refs/heads/feature');
repo.checkout(target: 'refs/heads/feature');
final index = repo.index;
repo.merge(commit: commit);

View file

@ -43,7 +43,7 @@ void main() {
reference: feature,
);
repo.checkout(refName: feature.name);
repo.checkout(target: feature.name);
expect(() => repo.index['.gitignore'], throwsA(isA<ArgumentError>()));
final rebase = Rebase.init(
@ -141,7 +141,7 @@ void main() {
final feature = repo.lookupReference('refs/heads/feature');
final upstream = AnnotatedCommit.lookup(repo: repo, oid: repo[shas[1]]);
repo.checkout(refName: feature.name);
repo.checkout(target: feature.name);
expect(() => repo.index['conflict_file'], throwsA(isA<ArgumentError>()));
final rebase = Rebase.init(
@ -194,7 +194,7 @@ void main() {
final conflict = repo.lookupReference('refs/heads/conflict-branch');
final ontoHead = AnnotatedCommit.lookup(repo: repo, oid: conflict.target);
repo.checkout(refName: conflict.name);
repo.checkout(target: conflict.name);
final rebase = Rebase.init(
repo: repo,
@ -231,7 +231,7 @@ void main() {
final conflict = repo.lookupReference('refs/heads/conflict-branch');
final ontoHead = AnnotatedCommit.lookup(repo: repo, oid: conflict.target);
repo.checkout(refName: conflict.name);
repo.checkout(target: conflict.name);
final rebase = Rebase.init(
repo: repo,
@ -257,7 +257,7 @@ void main() {
final conflict = repo.lookupReference('refs/heads/conflict-branch');
final ontoHead = AnnotatedCommit.lookup(repo: repo, oid: conflict.target);
repo.checkout(refName: conflict.name);
repo.checkout(target: conflict.name);
final rebase = Rebase.init(
repo: repo,