From 53be0cf9655d5e948941a3f2df4079092d0b7f76 Mon Sep 17 00:00:00 2001 From: joshaber Date: Thu, 3 Dec 2015 14:07:12 -0500 Subject: [PATCH] Added .checkoutReference --- spec/git-repository-async-spec.js | 26 +++++++++++++++++++++++--- src/git-repository-async.js | 26 +++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/spec/git-repository-async-spec.js b/spec/git-repository-async-spec.js index f226743b2..23acedff0 100644 --- a/spec/git-repository-async-spec.js +++ b/spec/git-repository-async-spec.js @@ -462,12 +462,12 @@ describe('GitRepositoryAsync', () => { it('resolves true when the branch exists', async () => { const hasBranch = await repo.hasBranch('master') - expect(hasBranch).toBe(true) + expect(hasBranch).toBeTruthy() }) it("resolves false when the branch doesn't exist", async () => { const hasBranch = await repo.hasBranch('trolleybus') - expect(hasBranch).toBe(false) + expect(hasBranch).toBeFalsy() }) }) @@ -511,7 +511,27 @@ describe('GitRepositoryAsync', () => { it("resolves to null if there's no value", async () => { const value = await repo.getConfigValue('my.special.key') - expect(value).toBe(null) + expect(value).toBeNull() + }) + }) + + describe('.checkoutReference(reference, create)', () => { + beforeEach(() => { + const workingDirectory = copyRepository() + console.log(workingDirectory) + repo = GitRepositoryAsync.open(workingDirectory) + }) + + it('can create new branches', () => { + let success = false + let threw = false + waitsForPromise(() => repo.checkoutReference('my-b', true) + .then(_ => success = true) + .catch(_ => threw = true)) + runs(() => { + expect(success).toBeTruthy() + expect(threw).toBeFalsy() + }) }) }) }) diff --git a/src/git-repository-async.js b/src/git-repository-async.js index d1cd90cc4..275feb4c8 100644 --- a/src/git-repository-async.js +++ b/src/git-repository-async.js @@ -483,6 +483,11 @@ export default class GitRepositoryAsync { // * `oldLines` The {Number} of lines in the old hunk. // * `newLines` The {Number} of lines in the new hunk getLineDiffs (_path, text) { + // # Ignore eol of line differences on windows so that files checked in as + // # LF don't report every line modified when the text contains CRLF endings. + // options = ignoreEolWhitespace: process.platform is 'win32' + // repo = @getRepo(path) + // repo.getLineDiffs(repo.relativize(path), text, options) throw new Error('Unimplemented') } @@ -514,16 +519,31 @@ export default class GitRepositoryAsync { .then(() => this.refreshStatusForPath(_path)) } + _createBranch (name) { + return this.repoPromise + .then(repo => Promise.all([repo, repo.getHeadCommit()])) + .then(([repo, commit]) => repo.createBranch(name, commit)) + } + // Public: Checks out a branch in your repository. // // * `reference` The {String} reference to checkout. // * `create` A {Boolean} value which, if true creates the new reference if // it doesn't exist. // - // Returns a Boolean that's true if the method was successful. + // Returns a {Promise} that resolves if the method was successful. checkoutReference (reference, create) { - throw new Error('Unimplemented') - // @getRepo().checkoutReference(reference, create) + return this.repoPromise + .then(repo => repo.checkoutBranch(reference)) + .catch(error => { + if (create) { + return this._createBranch(reference) + .then(_ => this.checkoutReference(reference, false)) + } else { + throw error + } + }) + .then(_ => null) } // Private