From aef440e345ab741b7083fd77b82ed9b0b1f5fbc5 Mon Sep 17 00:00:00 2001 From: Aleksey Kulikov Date: Fri, 29 Apr 2022 15:24:05 +0300 Subject: [PATCH] refactor(remote)!: add RemoteReference class (#50) BREAKING CHANGE: Return value of ls(...) changed from List> to List --- lib/src/remote.dart | 58 +++++++++++++++++++++++++++++++++++-------- test/remote_test.dart | 14 +++++------ 2 files changed, 54 insertions(+), 18 deletions(-) diff --git a/lib/src/remote.dart b/lib/src/remote.dart index 87802b9..7ba5120 100644 --- a/lib/src/remote.dart +++ b/lib/src/remote.dart @@ -194,16 +194,8 @@ class Remote { /// [callbacks] is the combination of callback functions from [Callbacks] /// 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. - List> ls({ + List ls({ String? proxy, Callbacks callbacks = const Callbacks(), }) { @@ -213,8 +205,22 @@ class Remote { callbacks: callbacks, proxyOption: proxy, ); - final result = bindings.lsRemotes(_remotePointer); + final refs = bindings.lsRemotes(_remotePointer); bindings.disconnect(_remotePointer); + + final result = []; + 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; } @@ -371,3 +377,35 @@ class RemoteCallback { /// Remote's fetch refspec. 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}'; + } +} diff --git a/test/remote_test.dart b/test/remote_test.dart index 90d8956..4d61bbb 100644 --- a/test/remote_test.dart +++ b/test/remote_test.dart @@ -265,14 +265,12 @@ void main() { final remote = Remote.lookup(repo: repo, name: 'libgit2'); final refs = remote.ls(); - expect(refs.first['local'], false); - expect(refs.first['loid'], null); - expect(refs.first['name'], 'HEAD'); - expect(refs.first['symref'], 'refs/heads/master'); - expect( - (refs.first['oid']! as Oid).sha, - '49322bb17d3acc9146f98c97d078513228bbf3c0', - ); + expect(refs.first.isLocal, false); + expect(refs.first.localId, null); + expect(refs.first.name, 'HEAD'); + expect(refs.first.symRef, 'refs/heads/master'); + expect((refs.first.oid).sha, '49322bb17d3acc9146f98c97d078513228bbf3c0'); + expect(refs.first.toString(), contains('RemoteReference{')); }); test(