diff --git a/spec/app/editor-spec.coffee b/spec/app/editor-spec.coffee index 48da5749a..415940ba6 100644 --- a/spec/app/editor-spec.coffee +++ b/spec/app/editor-spec.coffee @@ -1889,3 +1889,32 @@ describe "Editor", -> editor.pageUp() expect(editor.getCursor().getScreenPosition().row).toBe(0) expect(editor.getFirstVisibleScreenRow()).toBe(0) + + describe ".checkoutHead()", -> + [repo, path, originalPathText] = [] + + beforeEach -> + path = require.resolve('fixtures/git/working-dir/file.txt') + originalPathText = fs.read(path) + rootView.open(path) + editor = rootView.getActiveEditor() + editor.attachToDom() + + afterEach -> + fs.write(path, originalPathText) + + it "restores the contents of the editor to the HEAD revision", -> + editor.setText('') + editor.save() + + fileChangeHandler = jasmine.createSpy('fileChange') + editor.getBuffer().file.on 'contents-change', fileChangeHandler + + editor.checkoutHead() + + waitsFor "file to trigger contents-change event", -> + fileChangeHandler.callCount > 0 + + runs -> + editor.checkoutHead() + expect(editor.getText()).toBe(originalPathText) diff --git a/spec/app/status-bar-spec.coffee b/spec/app/status-bar-spec.coffee index 8b7bfa19d..ca30e8157 100644 --- a/spec/app/status-bar-spec.coffee +++ b/spec/app/status-bar-spec.coffee @@ -146,3 +146,11 @@ describe "StatusBar", -> it "displays the new icon for a new file", -> rootView.open(newPath) expect(statusBar.gitStatusIcon).toHaveText('\uf26b') + + it "updates when an editor-git-status-change event occurs", -> + fs.write(path, "i've changed for the worse") + rootView.open(path) + expect(statusBar.gitStatusIcon).toHaveText('\uf26d') + fs.write(path, originalPathText) + rootView.getActiveEditor().trigger 'editor-git-status-change' + expect(statusBar.gitStatusIcon).toHaveText('') diff --git a/src/app/editor.coffee b/src/app/editor.coffee index 2b029d041..ba014958e 100644 --- a/src/app/editor.coffee +++ b/src/app/editor.coffee @@ -8,7 +8,7 @@ CursorView = require 'cursor-view' SelectionView = require 'selection-view' Native = require 'native' fs = require 'fs' - +Git = require 'git' $ = require 'jquery' _ = require 'underscore' @@ -154,6 +154,7 @@ class Editor extends View 'editor:show-previous-buffer': @loadPreviousEditSession 'editor:toggle-line-comments': @toggleLineCommentsInSelection 'editor:log-cursor-scope': @logCursorScope + 'editor:checkout-head-revision': @checkoutHead documentation = {} for name, method of editorBindings @@ -274,6 +275,11 @@ class Editor extends View setInvisibles: (@invisibles={}) -> @renderLines() + checkoutHead: -> + if path = @getPath() + if new Git(path).checkoutHead(path) + @trigger 'editor-git-status-change' + setText: (text) -> @getBuffer().setText(text) getText: -> @getBuffer().getText() getPath: -> @getBuffer().getPath() diff --git a/src/app/keymaps/git.coffee b/src/app/keymaps/git.coffee new file mode 100644 index 000000000..59ba77cc3 --- /dev/null +++ b/src/app/keymaps/git.coffee @@ -0,0 +1,2 @@ +window.keymap.bindKeys '.editor', + 'ctrl-Z': 'editor:checkout-head-revision' diff --git a/src/app/status-bar.coffee b/src/app/status-bar.coffee index 73d7cf8a4..dd9145129 100644 --- a/src/app/status-bar.coffee +++ b/src/app/status-bar.coffee @@ -40,6 +40,8 @@ class StatusBar extends View @updateCursorPositionText() @editor.on 'cursor-move', => _.delay (=> @updateCursorPositionText()), 50 + @editor.on 'editor-git-status-change', => @updateStatusBar() + @subscribeToBuffer() subscribeToBuffer: ->