diff --git a/spec/git-repository-async-spec.coffee b/spec/git-repository-async-spec.coffee index 1506d3453..81dd27cd4 100644 --- a/spec/git-repository-async-spec.coffee +++ b/spec/git-repository-async-spec.coffee @@ -215,12 +215,12 @@ describe "GitRepositoryAsync", -> runs -> expect(statusHandler.callCount).toBe 1 - xdescribe ".checkoutHeadForEditor(editor)", -> + fdescribe ".checkoutHeadForEditor(editor)", -> [filePath, editor] = [] beforeEach -> workingDirPath = copyRepository() - repo = new GitRepository(workingDirPath) + repo = repo = GitRepositoryAsync.open(workingDirPath) filePath = path.join(workingDirPath, 'a.txt') fs.writeFileSync(filePath, 'ch ch changes') @@ -242,10 +242,11 @@ describe "GitRepositoryAsync", -> spyOn(atom, 'confirm') atom.config.set('editor.confirmCheckoutHeadRevision', false) - repo.checkoutHeadForEditor(editor) - - expect(fs.readFileSync(filePath, 'utf8')).toBe '' - expect(atom.confirm).not.toHaveBeenCalled() + waitsForPromise -> + repo.checkoutHeadForEditor(editor) + runs -> + expect(fs.readFileSync(filePath, 'utf8')).toBe '' + expect(atom.confirm).not.toHaveBeenCalled() xdescribe ".destroy()", -> it "throws an exception when any method is called after it is called", -> diff --git a/src/git-repository-async.js b/src/git-repository-async.js index 47f1c0a9b..53e592998 100644 --- a/src/git-repository-async.js +++ b/src/git-repository-async.js @@ -1,4 +1,5 @@ 'use babel' +const atom = window.atom const Git = require('nodegit') const path = require('path') @@ -89,6 +90,39 @@ module.exports = class GitRepositoryAsync { }) } + checkoutHeadForEditor (editor) { + var filePath = editor.getPath() + if (!filePath) { + return Promise.reject('editor.filePath() is empty') + } + + var fileName = path.basename(filePath) + var checkoutHead = () => { + if (editor.buffer.isModified()) { + editor.buffer.reload() + } + return this.checkoutHead(filePath) + } + + var confirmCheckout = function () { + // This is bad tho + return Promise.resolve(atom.confirm({ + message: 'Confirm Checkout HEAD Revision', + detailedMessage: `Are you sure you want to discard all changes to "${fileName}" since the last Git commit?`, + buttons: { + OK: checkoutHead, + Cancel: null + } + })) + } + + if (atom.config.get('editor.confirmCheckoutHeadRevision')) { + return confirmCheckout() + } else { + return checkoutHead() + } + } + // Returns a Promise that resolves to the status bit of a given path if it has // one, otherwise 'current'. getPathStatus (_path) {