refactor(remote)!: add RemoteReference class

BREAKING CHANGE: Return value of ls(...) changed from List<Map<String, Object?>> to List<RemoteReference>
This commit is contained in:
Aleksey Kulikov 2022-04-29 15:15:26 +03:00
parent 6d1ccd5c12
commit df9029485e
2 changed files with 54 additions and 18 deletions

View file

@ -194,16 +194,8 @@ class Remote {
/// [callbacks] is the combination of callback functions from [Callbacks] /// [callbacks] is the combination of callback functions from [Callbacks]
/// object. /// object.
/// ///
/// Returned map keys:
/// - `local` is true if remote head is available locally, false otherwise.
/// - `loid` is the oid of the object the local copy of the remote head is
/// currently pointing to. null if there is no local copy of the remote head.
/// - `name` is the name of the reference.
/// - `oid` is the oid of the object the remote head is currently pointing to.
/// - `symref` is the target of the symbolic reference or empty string.
///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
List<Map<String, Object?>> ls({ List<RemoteReference> ls({
String? proxy, String? proxy,
Callbacks callbacks = const Callbacks(), Callbacks callbacks = const Callbacks(),
}) { }) {
@ -213,8 +205,22 @@ class Remote {
callbacks: callbacks, callbacks: callbacks,
proxyOption: proxy, proxyOption: proxy,
); );
final result = bindings.lsRemotes(_remotePointer); final refs = bindings.lsRemotes(_remotePointer);
bindings.disconnect(_remotePointer); bindings.disconnect(_remotePointer);
final result = <RemoteReference>[];
for (final ref in refs) {
result.add(
RemoteReference._(
isLocal: ref['local']! as bool,
localId: ref['loid'] as Oid?,
name: ref['name']! as String,
oid: ref['oid']! as Oid,
symRef: ref['symref']! as String,
),
);
}
return result; return result;
} }
@ -371,3 +377,35 @@ class RemoteCallback {
/// Remote's fetch refspec. /// Remote's fetch refspec.
final String? fetch; final String? fetch;
} }
class RemoteReference {
const RemoteReference._({
required this.isLocal,
required this.localId,
required this.name,
required this.oid,
required this.symRef,
});
/// Whether remote head is available locally.
final bool isLocal;
/// Oid of the object the local copy of the remote head is currently pointing
/// to. Null if there is no local copy of the remote head.
final Oid? localId;
/// Name of the reference.
final String name;
/// Oid of the object the remote head is currently pointing to.
final Oid oid;
/// Target of the symbolic reference or empty string if reference is direct.
final String symRef;
@override
String toString() {
return 'RemoteReference{isLocal: $isLocal, localId: $localId, '
'name: $name, oid: $oid, symRef: $symRef}';
}
}

View file

@ -265,14 +265,12 @@ void main() {
final remote = Remote.lookup(repo: repo, name: 'libgit2'); final remote = Remote.lookup(repo: repo, name: 'libgit2');
final refs = remote.ls(); final refs = remote.ls();
expect(refs.first['local'], false); expect(refs.first.isLocal, false);
expect(refs.first['loid'], null); expect(refs.first.localId, null);
expect(refs.first['name'], 'HEAD'); expect(refs.first.name, 'HEAD');
expect(refs.first['symref'], 'refs/heads/master'); expect(refs.first.symRef, 'refs/heads/master');
expect( expect((refs.first.oid).sha, '49322bb17d3acc9146f98c97d078513228bbf3c0');
(refs.first['oid']! as Oid).sha, expect(refs.first.toString(), contains('RemoteReference{'));
'49322bb17d3acc9146f98c97d078513228bbf3c0',
);
}); });
test( test(