diff --git a/lib/src/bindings/reference.dart b/lib/src/bindings/reference.dart index af1d3cb..9729b60 100644 --- a/lib/src/bindings/reference.dart +++ b/lib/src/bindings/reference.dart @@ -73,6 +73,15 @@ String name(Pointer ref) { return result.cast().toDartString(); } +/// Get the reference's short name. +/// +/// This will transform the reference name into a name "human-readable" version. +/// If no shortname is appropriate, it will return the full name. +String shorthand(Pointer ref) { + final result = libgit2.git_reference_shorthand(ref); + return result.cast().toDartString(); +} + /// Fill a list with all the references that can be found in a repository. /// /// The string array will be filled with the names of all references; diff --git a/lib/src/reference.dart b/lib/src/reference.dart index b521d13..efbd718 100644 --- a/lib/src/reference.dart +++ b/lib/src/reference.dart @@ -165,6 +165,12 @@ class Reference { /// Returns the full name of a reference. String get name => bindings.name(_refPointer); + /// Returns the reference's short name. + /// + /// This will transform the reference name into a name "human-readable" version. + /// If no shortname is appropriate, it will return the full name. + String get shorthand => bindings.shorthand(_refPointer); + /// Returns a list with all the references that can be found in a repository. /// /// Throws a [LibGit2Error] if error occured. diff --git a/test/reference_test.dart b/test/reference_test.dart index 7e1c794..2338efb 100644 --- a/test/reference_test.dart +++ b/test/reference_test.dart @@ -272,11 +272,25 @@ void main() { ref.free(); }); - test('returns the full name of a reference', () { + test('returns the full name', () { expect(repo.head.name, 'refs/heads/master'); repo.head.free(); }); + test('returns the short name', () { + final ref = repo.createReference( + name: 'refs/remotes/origin/master', + target: lastCommit, + ); + + expect(repo.head.shorthand, 'master'); + expect(ref.shorthand, 'origin/master'); + + repo.head.free(); + ref.delete(); + ref.free(); + }); + test('returns a map with all the references of repository', () { expect( repo.references,