mirror of
https://github.com/SkinnyMind/libgit2dart.git
synced 2025-05-05 04:39:07 -04:00
feat(reference): add ability to create direct reference
This commit is contained in:
parent
6643527f2d
commit
9190ed2e0f
16 changed files with 669 additions and 60 deletions
46
test/odb_test.dart
Normal file
46
test/odb_test.dart
Normal file
|
@ -0,0 +1,46 @@
|
|||
import 'dart:io';
|
||||
|
||||
import 'package:test/test.dart';
|
||||
import 'package:libgit2dart/src/repository.dart';
|
||||
import 'package:libgit2dart/src/odb.dart';
|
||||
import 'package:libgit2dart/src/oid.dart';
|
||||
|
||||
import 'helpers/util.dart';
|
||||
|
||||
void main() {
|
||||
const lastCommit = '78b8bf123e3952c970ae5c1ce0a3ea1d1336f6e8';
|
||||
|
||||
group('Odb', () {
|
||||
late Repository repo;
|
||||
final tmpDir = '${Directory.systemTemp.path}/odb_testrepo/';
|
||||
|
||||
setUpAll(() 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);
|
||||
});
|
||||
|
||||
tearDownAll(() async {
|
||||
repo.close();
|
||||
await Directory(tmpDir).delete(recursive: true);
|
||||
});
|
||||
|
||||
test('successfully initializes', () {
|
||||
expect(repo.odb, isA<Odb>());
|
||||
repo.odb.free();
|
||||
});
|
||||
|
||||
test('finds object by short oid', () {
|
||||
final shortSha = '78b8bf';
|
||||
final shortOid = Oid.fromSHAn(shortSha);
|
||||
final oid = repo.odb.existsPrefix(shortOid.pointer, shortSha.length);
|
||||
expect(Oid(oid).sha, lastCommit);
|
||||
repo.odb.free();
|
||||
});
|
||||
});
|
||||
}
|
|
@ -1,6 +1,5 @@
|
|||
import 'package:test/test.dart';
|
||||
import 'package:libgit2dart/src/oid.dart';
|
||||
import 'package:libgit2dart/src/error.dart';
|
||||
|
||||
void main() {
|
||||
const sha = '9d81c715ff606057fa448e558c7458467a86c8c7';
|
||||
|
@ -10,9 +9,12 @@ void main() {
|
|||
test('initializes successfully', () {
|
||||
expect(Oid.fromSHA(sha), isA<Oid>());
|
||||
});
|
||||
});
|
||||
|
||||
test('throws when hex string is lesser than 40 characters', () {
|
||||
expect(() => Oid.fromSHA('9d8'), throwsA(isA<LibGit2Error>()));
|
||||
group('fromSHAn()', () {
|
||||
test('initializes successfully from short hex string', () {
|
||||
final oid = Oid.fromSHAn('9d81');
|
||||
expect(oid, isA<Oid>());
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import 'dart:io';
|
||||
|
||||
import 'package:libgit2dart/src/reflog.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'package:libgit2dart/src/repository.dart';
|
||||
import 'package:libgit2dart/src/reference.dart';
|
||||
|
@ -11,8 +12,8 @@ void main() {
|
|||
const lastCommit = '78b8bf123e3952c970ae5c1ce0a3ea1d1336f6e8';
|
||||
|
||||
group('Reference', () {
|
||||
late Repository repo;
|
||||
final tmpDir = '${Directory.systemTemp.path}/testrepo/';
|
||||
late final Repository repo;
|
||||
final tmpDir = '${Directory.systemTemp.path}/ref_testrepo/';
|
||||
|
||||
setUpAll(() async {
|
||||
if (await Directory(tmpDir).exists()) {
|
||||
|
@ -30,23 +31,150 @@ void main() {
|
|||
await Directory(tmpDir).delete(recursive: true);
|
||||
});
|
||||
|
||||
group('.createDirect()', () {
|
||||
test('successfully creates with Oid as target', () {
|
||||
final ref = repo.getReference('refs/heads/master');
|
||||
final refFromOid = repo.createReference(
|
||||
name: 'refs/tags/from.oid',
|
||||
target: ref.target,
|
||||
);
|
||||
|
||||
expect(repo.references, contains('refs/tags/from.oid'));
|
||||
|
||||
refFromOid.delete();
|
||||
refFromOid.free();
|
||||
ref.free();
|
||||
});
|
||||
|
||||
test('successfully creates with SHA hash as target', () {
|
||||
final refFromHash = repo.createReference(
|
||||
name: 'refs/tags/from.hash',
|
||||
target: lastCommit,
|
||||
);
|
||||
|
||||
expect(repo.references, contains('refs/tags/from.hash'));
|
||||
|
||||
refFromHash.delete();
|
||||
refFromHash.free();
|
||||
});
|
||||
|
||||
test('successfully creates with short SHA hash as target', () {
|
||||
final refFromHash = repo.createReference(
|
||||
name: 'refs/tags/from.short.hash',
|
||||
target: '78b8bf',
|
||||
);
|
||||
|
||||
expect(repo.references, contains('refs/tags/from.short.hash'));
|
||||
|
||||
refFromHash.delete();
|
||||
refFromHash.free();
|
||||
});
|
||||
|
||||
test('successfully creates with log message', () {
|
||||
repo.setIdentity(name: 'name', email: 'email');
|
||||
final ref = repo.createReference(
|
||||
name: 'refs/heads/log.message',
|
||||
target: lastCommit,
|
||||
logMessage: 'log message',
|
||||
);
|
||||
|
||||
final reflog = RefLog(ref);
|
||||
final reflogEntry = reflog.entryAt(0);
|
||||
|
||||
expect(reflogEntry.message, 'log message');
|
||||
expect(reflogEntry.committer, {'name': 'name', 'email': 'email'});
|
||||
|
||||
reflog.free();
|
||||
ref.delete();
|
||||
ref.free();
|
||||
});
|
||||
|
||||
test('throws if target is not valid', () {
|
||||
expect(
|
||||
() => repo.createReference(
|
||||
name: 'refs/tags/invalid',
|
||||
target: '78b',
|
||||
),
|
||||
throwsA(isA<LibGit2Error>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('throws if name is not valid', () {
|
||||
expect(
|
||||
() => repo.createReference(
|
||||
name: 'refs/tags/invalid~',
|
||||
target: lastCommit,
|
||||
),
|
||||
throwsA(isA<LibGit2Error>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('successfully creates with force flag if name already exists', () {
|
||||
final ref = repo.createReference(
|
||||
name: 'refs/tags/test',
|
||||
target: lastCommit,
|
||||
);
|
||||
|
||||
final forceRef = repo.createReference(
|
||||
name: 'refs/tags/test',
|
||||
target: lastCommit,
|
||||
force: true,
|
||||
);
|
||||
|
||||
expect(forceRef.target.sha, lastCommit);
|
||||
|
||||
forceRef.delete();
|
||||
ref.free();
|
||||
forceRef.free();
|
||||
});
|
||||
|
||||
test('throws if name already exists', () {
|
||||
final ref = repo.createReference(
|
||||
name: 'refs/tags/test',
|
||||
target: lastCommit,
|
||||
);
|
||||
|
||||
expect(
|
||||
() => repo.createReference(
|
||||
name: 'refs/tags/test',
|
||||
target: lastCommit,
|
||||
),
|
||||
throwsA(isA<LibGit2Error>()),
|
||||
);
|
||||
|
||||
ref.delete();
|
||||
ref.free();
|
||||
});
|
||||
});
|
||||
|
||||
test('successfully deletes reference', () {
|
||||
final ref = repo.createReference(
|
||||
name: 'refs/tags/test',
|
||||
target: lastCommit,
|
||||
);
|
||||
expect(repo.references, contains('refs/tags/test'));
|
||||
|
||||
ref.delete();
|
||||
expect(repo.references, isNot(contains('refs/tags/test')));
|
||||
});
|
||||
|
||||
test('returns correct type of reference', () {
|
||||
expect(repo.head.type, ReferenceType.direct);
|
||||
repo.head.free();
|
||||
|
||||
final ref = Reference.lookup(repo, 'HEAD');
|
||||
final ref = repo.getReference('HEAD');
|
||||
expect(ref.type, ReferenceType.symbolic);
|
||||
ref.free();
|
||||
});
|
||||
|
||||
test('returns SHA hex of direct reference', () {
|
||||
expect(repo.head.target, lastCommit);
|
||||
expect(repo.head.target.sha, lastCommit);
|
||||
repo.head.free();
|
||||
});
|
||||
|
||||
test('returns SHA hex of symbolic reference', () {
|
||||
final ref = Reference.lookup(repo, 'HEAD');
|
||||
expect(ref.target, lastCommit);
|
||||
final ref = repo.getReference('HEAD');
|
||||
expect(ref.target.sha, lastCommit);
|
||||
ref.free();
|
||||
});
|
||||
|
||||
|
@ -67,44 +195,50 @@ void main() {
|
|||
});
|
||||
|
||||
test('checks if reflog exists for the reference', () {
|
||||
expect(Reference.hasLog(repo, 'refs/heads/master'), true);
|
||||
expect(Reference.hasLog(repo, 'refs/heads/not/there'), false);
|
||||
expect(repo.referenceHasLog('refs/heads/master'), true);
|
||||
expect(repo.referenceHasLog('refs/tags/v0.1'), false);
|
||||
});
|
||||
|
||||
test('checks if reference is a local branch', () {
|
||||
final ref = Reference.lookup(repo, 'refs/heads/feature');
|
||||
final ref = repo.getReference('refs/heads/feature');
|
||||
expect(ref.isBranch, true);
|
||||
ref.free();
|
||||
});
|
||||
|
||||
test('checks if reference is a note', () {
|
||||
final ref = Reference.lookup(repo, 'refs/heads/master');
|
||||
final ref = repo.getReference('refs/heads/master');
|
||||
expect(ref.isNote, false);
|
||||
ref.free();
|
||||
});
|
||||
|
||||
test('checks if reference is a remote branch', () {
|
||||
final ref = Reference.lookup(repo, 'refs/heads/master');
|
||||
expect(ref.isRemote, false);
|
||||
final ref = repo.createReference(
|
||||
name: 'refs/remotes/origin/master',
|
||||
target: lastCommit,
|
||||
);
|
||||
|
||||
expect(ref.isRemote, true);
|
||||
|
||||
ref.delete();
|
||||
ref.free();
|
||||
});
|
||||
|
||||
test('checks if reference is a tag', () {
|
||||
final ref = Reference.lookup(repo, 'refs/tags/v0.1');
|
||||
final ref = repo.getReference('refs/tags/v0.1');
|
||||
expect(ref.isTag, true);
|
||||
ref.free();
|
||||
});
|
||||
|
||||
group('.lookup()', () {
|
||||
test('finds a reference with provided name', () {
|
||||
final ref = Reference.lookup(repo, 'refs/heads/master');
|
||||
expect(ref.target, lastCommit);
|
||||
final ref = repo.getReference('refs/heads/master');
|
||||
expect(ref.target.sha, lastCommit);
|
||||
ref.free();
|
||||
});
|
||||
|
||||
test('throws when error occured', () {
|
||||
expect(
|
||||
() => Reference.lookup(repo, 'refs/heads/not/there'),
|
||||
() => repo.getReference('refs/heads/not/there'),
|
||||
throwsA(isA<LibGit2Error>()),
|
||||
);
|
||||
});
|
||||
|
|
56
test/reflog_test.dart
Normal file
56
test/reflog_test.dart
Normal file
|
@ -0,0 +1,56 @@
|
|||
import 'dart:io';
|
||||
|
||||
import 'package:test/test.dart';
|
||||
import 'package:libgit2dart/src/repository.dart';
|
||||
import 'package:libgit2dart/src/reflog.dart';
|
||||
|
||||
import 'helpers/util.dart';
|
||||
|
||||
void main() {
|
||||
group('RefLog', () {
|
||||
late final Repository repo;
|
||||
late final RefLog reflog;
|
||||
final tmpDir = '${Directory.systemTemp.path}/reflog_testrepo/';
|
||||
|
||||
setUpAll(() 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);
|
||||
reflog = RefLog(repo.head);
|
||||
});
|
||||
|
||||
tearDownAll(() async {
|
||||
repo.head.free();
|
||||
reflog.free();
|
||||
repo.close();
|
||||
await Directory(tmpDir).delete(recursive: true);
|
||||
});
|
||||
|
||||
test('initializes successfully', () {
|
||||
expect(reflog, isA<RefLog>());
|
||||
});
|
||||
|
||||
test('returns correct number of log entries', () {
|
||||
expect(reflog.count, 3);
|
||||
});
|
||||
|
||||
test('returns the log message', () {
|
||||
final entry = reflog.entryAt(0);
|
||||
expect(
|
||||
entry.message,
|
||||
"merge feature: Merge made by the 'recursive' strategy.",
|
||||
);
|
||||
});
|
||||
|
||||
test('returns the committer of the entry', () {
|
||||
final entry = reflog.entryAt(0);
|
||||
expect(entry.committer['name'], 'Aleksey Kulikov');
|
||||
expect(entry.committer['email'], 'skinny.mind@gmail.com');
|
||||
});
|
||||
});
|
||||
}
|
|
@ -6,9 +6,8 @@ import 'package:libgit2dart/src/error.dart';
|
|||
import 'helpers/util.dart';
|
||||
|
||||
void main() {
|
||||
late Repository repo;
|
||||
|
||||
group('Repository', () {
|
||||
late Repository repo;
|
||||
test('throws when repository isn\'t found at provided path', () {
|
||||
expect(
|
||||
() => Repository.open(''),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue