Incomplete implementation of checkoutHeadForEditor

This commit is contained in:
Daniel Hengeveld
2015-10-20 18:02:47 +02:00
parent 4c0c732766
commit bea002bddb
2 changed files with 41 additions and 6 deletions

View File

@@ -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", ->

View File

@@ -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) {