mirror of
https://github.com/SkinnyMind/libgit2dart.git
synced 2025-05-04 20:29:08 -04:00
feat(oid): add base bindings and api
This commit is contained in:
parent
21001d170c
commit
2c28fddcec
3 changed files with 149 additions and 0 deletions
44
lib/src/bindings/oid.dart
Normal file
44
lib/src/bindings/oid.dart
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
import 'dart:ffi';
|
||||||
|
import 'package:ffi/ffi.dart';
|
||||||
|
import '../error.dart';
|
||||||
|
import 'libgit2_bindings.dart';
|
||||||
|
import '../util.dart';
|
||||||
|
|
||||||
|
/// Parse a hex formatted object id into a git_oid.
|
||||||
|
///
|
||||||
|
/// Throws a [LibGit2Error] if error occured.
|
||||||
|
Pointer<git_oid> fromSHA(String hex) {
|
||||||
|
final out = calloc<git_oid>();
|
||||||
|
final str = hex.toNativeUtf8().cast<Int8>();
|
||||||
|
final error = libgit2.git_oid_fromstr(out, str);
|
||||||
|
calloc.free(str);
|
||||||
|
|
||||||
|
if (error < 0) {
|
||||||
|
throw LibGit2Error(libgit2.git_error_last());
|
||||||
|
} else {
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Format a git_oid into a hex string.
|
||||||
|
///
|
||||||
|
/// Throws a [LibGit2Error] if error occured.
|
||||||
|
String toSHA(Pointer<git_oid> id) {
|
||||||
|
final out = calloc.allocate<Int8>(40);
|
||||||
|
final error = libgit2.git_oid_fmt(out, id);
|
||||||
|
final result = out.cast<Utf8>().toDartString(length: 40);
|
||||||
|
malloc.free(out);
|
||||||
|
|
||||||
|
if (error < 0) {
|
||||||
|
throw LibGit2Error(libgit2.git_error_last());
|
||||||
|
} else {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Compare two oid structures.
|
||||||
|
///
|
||||||
|
/// Returns <0 if a < b, 0 if a == b, >0 if a > b.
|
||||||
|
int compare(Pointer<git_oid> a, Pointer<git_oid> b) {
|
||||||
|
return libgit2.git_oid_cmp(a, b);
|
||||||
|
}
|
58
lib/src/oid.dart
Normal file
58
lib/src/oid.dart
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
import 'dart:ffi';
|
||||||
|
import 'bindings/libgit2_bindings.dart';
|
||||||
|
import 'bindings/oid.dart' as bindings;
|
||||||
|
import 'util.dart';
|
||||||
|
|
||||||
|
class Oid {
|
||||||
|
/// Initializes a new instance of [Oid] class from provided
|
||||||
|
/// hexadecimal [sha] string.
|
||||||
|
///
|
||||||
|
/// Throws a [LibGit2Error] if error occured.
|
||||||
|
Oid.fromSHA(String sha) {
|
||||||
|
libgit2.git_libgit2_init();
|
||||||
|
|
||||||
|
try {
|
||||||
|
_oidPointer = bindings.fromSHA(sha);
|
||||||
|
} catch (e) {
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Pointer to memory address for allocated oid object.
|
||||||
|
late Pointer<git_oid> _oidPointer;
|
||||||
|
|
||||||
|
/// Returns hexadecimal SHA-1 string.
|
||||||
|
String get sha {
|
||||||
|
try {
|
||||||
|
return bindings.toSHA(_oidPointer);
|
||||||
|
} catch (e) {
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool operator ==(other) {
|
||||||
|
return (other is Oid) &&
|
||||||
|
(bindings.compare(_oidPointer, other._oidPointer) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator <(other) {
|
||||||
|
return (other is Oid) &&
|
||||||
|
(bindings.compare(_oidPointer, other._oidPointer) == -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator <=(other) {
|
||||||
|
return (other is Oid) &&
|
||||||
|
(bindings.compare(_oidPointer, other._oidPointer) == -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator >(other) {
|
||||||
|
return (other is Oid) &&
|
||||||
|
(bindings.compare(_oidPointer, other._oidPointer) == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator >=(other) {
|
||||||
|
return (other is Oid) &&
|
||||||
|
(bindings.compare(_oidPointer, other._oidPointer) == 1);
|
||||||
|
}
|
||||||
|
}
|
47
test/oid_test.dart
Normal file
47
test/oid_test.dart
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
import 'package:libgit2dart/src/oid.dart';
|
||||||
|
import 'package:libgit2dart/src/error.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
const sha = '9d81c715ff606057fa448e558c7458467a86c8c7';
|
||||||
|
|
||||||
|
group('Oid', () {
|
||||||
|
group('fromSHA()', () {
|
||||||
|
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>()));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('returns sha hex string', () {
|
||||||
|
final oid = Oid.fromSHA(sha);
|
||||||
|
final hex = oid.sha;
|
||||||
|
expect(hex, equals(sha));
|
||||||
|
});
|
||||||
|
|
||||||
|
group('compare', () {
|
||||||
|
test('< and <=', () {
|
||||||
|
final oid1 = Oid.fromSHA(sha);
|
||||||
|
final oid2 = Oid.fromSHA('9d81c715ff606057fa448e558c7458467a86c8c8');
|
||||||
|
expect(oid1 < oid2, true);
|
||||||
|
expect(oid1 <= oid2, true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('==', () {
|
||||||
|
final oid1 = Oid.fromSHA(sha);
|
||||||
|
final oid2 = Oid.fromSHA(sha);
|
||||||
|
expect(oid1 == oid2, true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('> and >=', () {
|
||||||
|
final oid1 = Oid.fromSHA(sha);
|
||||||
|
final oid2 = Oid.fromSHA('9d81c715ff606057fa448e558c7458467a86c8c6');
|
||||||
|
expect(oid1 > oid2, true);
|
||||||
|
expect(oid1 >= oid2, true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue