From aa634570e6371ee529dfb4ecc48a3e0795e03999 Mon Sep 17 00:00:00 2001 From: joshaber Date: Wed, 2 Dec 2015 16:16:30 -0500 Subject: [PATCH] Added getShortHead --- spec/git-repository-async-spec.js | 12 ++++++++++++ src/git-repository-async.js | 21 ++++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/spec/git-repository-async-spec.js b/spec/git-repository-async-spec.js index dc8ae0107..99162920f 100644 --- a/spec/git-repository-async-spec.js +++ b/spec/git-repository-async-spec.js @@ -397,4 +397,16 @@ describe('GitRepositoryAsync', () => { expect(relativizedPath).toBe('a/b.txt') }) }) + + describe('.getShortHead(path)', () => { + beforeEach(() => { + const workingDirectory = copyRepository() + repo = GitRepositoryAsync.open(workingDirectory) + }) + + it('returns the human-readable branch name', async () => { + const head = await repo.getShortHead() + expect(head).toBe('master') + }) + }) }) diff --git a/src/git-repository-async.js b/src/git-repository-async.js index 77c07013a..283b0a77d 100644 --- a/src/git-repository-async.js +++ b/src/git-repository-async.js @@ -166,9 +166,11 @@ export default class GitRepositoryAsync { // * `path` An optional {String} path in the repository to get this information // for, only needed if the repository contains submodules. // - // Returns a {String}. - getShortHead (path) { - throw new Error('Unimplemented') + // Returns a {Promise} which resolves to a {String}. + getShortHead (_path) { + return this._getRepo(_path) + .then(repo => repo.getCurrentBranch()) + .then(branch => branch.shorthand()) } // Public: Is the given path a submodule in the repository? @@ -468,6 +470,19 @@ export default class GitRepositoryAsync { return this._refreshingCount === 0 } + _getRepo (_path) { + if (!_path) return this.repoPromise + + return this.isSubmodule(_path) + .then(isSubmodule => { + if (isSubmodule) { + return Git.Repository.open(_path) + } else { + return this.repoPromise + } + }) + } + subscribeToBuffer (buffer) { const bufferSubscriptions = new CompositeDisposable()