diff --git a/spec/editor-spec.coffee b/spec/editor-spec.coffee index 77e2096c5..e6df88a0e 100644 --- a/spec/editor-spec.coffee +++ b/spec/editor-spec.coffee @@ -3438,24 +3438,3 @@ describe "Editor", -> editor.selectPageUp() expect(editor.getScrollTop()).toBe 0 expect(editor.getSelectedBufferRanges()).toEqual [[[0,0], [12,2]]] - - describe ".checkoutHead()", -> - [repo] = [] - - beforeEach -> - repo = jasmine.createSpyObj('repo', ['checkoutHead']) - spyOn(atom.project, "getRepo").andReturn(repo) - - it "displays a confirmation dialog by default", -> - spyOn(atom, "confirm") - editor.checkoutHead() - - args = atom.confirm.mostRecentCall.args[0] - expect(Object.keys(args.buttons)).toEqual ["Revert", "Cancel"] - - it "does not display a dialog when confirmation is disabled", -> - spyOn(atom, "confirm") - atom.config.set("editor.confirmCheckoutHead", false) - editor.checkoutHead() - - expect(atom.confirm).not.toHaveBeenCalled() diff --git a/spec/editor-view-spec.coffee b/spec/editor-view-spec.coffee index 22104da0f..467266023 100644 --- a/spec/editor-view-spec.coffee +++ b/spec/editor-view-spec.coffee @@ -2414,37 +2414,6 @@ describe "EditorView", -> expect(editor.getCursor().getScreenPosition().row).toBe(0) expect(editorView.getFirstVisibleScreenRow()).toBe(0) - describe ".checkoutHead()", -> - [filePath] = [] - - beforeEach -> - workingDirPath = temp.mkdirSync('atom-working-dir') - fs.copySync(path.join(__dirname, 'fixtures', 'git', 'working-dir'), workingDirPath) - fs.renameSync(path.join(workingDirPath, 'git.git'), path.join(workingDirPath, '.git')) - atom.project.setPath(workingDirPath) - filePath = atom.project.resolve('file.txt') - - waitsForPromise -> - atom.workspace.open(filePath).then (o) -> editor = o - - runs -> - editorView.edit(editor) - - it "restores the contents of the editor view to the HEAD revision", -> - editor.setText('') - editor.save() - - fileChangeHandler = jasmine.createSpy('fileChange') - editor.getBuffer().file.on 'contents-changed', fileChangeHandler - - editorView.checkoutHead() - - waitsFor "file to trigger contents-changed event", -> - fileChangeHandler.callCount > 0 - - runs -> - expect(editor.getText()).toBe('undefined') - describe ".pixelPositionForBufferPosition(position)", -> describe "when the editor view is detached", -> it "returns top and left values of 0", -> diff --git a/spec/git-spec.coffee b/spec/git-spec.coffee index 46263c8ff..b40f68182 100644 --- a/spec/git-spec.coffee +++ b/spec/git-spec.coffee @@ -119,6 +119,38 @@ describe "Git", -> repo.checkoutHead(filePath) expect(statusHandler.callCount).toBe 1 + describe ".checkoutHeadForEditor(editor)", -> + [filePath, editor] = [] + + beforeEach -> + workingDirPath = copyRepository() + repo = new Git(workingDirPath) + filePath = path.join(workingDirPath, 'a.txt') + fs.writeFileSync(filePath, 'ch ch changes') + + waitsForPromise -> + atom.workspace.open(filePath) + + runs -> + editor = atom.workspace.getActiveEditor() + + it "displays a confirmation dialog by default", -> + spyOn(atom, 'confirm').andCallFake ({buttons}) -> buttons.OK() + atom.config.set('editor.confirmCheckoutHeadRevision', true) + + repo.checkoutHeadForEditor(editor) + + expect(fs.readFileSync(filePath, 'utf8')).toBe '' + + it "does not display a dialog when confirmation is disabled", -> + spyOn(atom, 'confirm') + atom.config.set('editor.confirmCheckoutHeadRevision', false) + + repo.checkoutHeadForEditor(editor) + + expect(fs.readFileSync(filePath, 'utf8')).toBe '' + expect(atom.confirm).not.toHaveBeenCalled() + describe ".destroy()", -> it "throws an exception when any method is called after it is called", -> repo = new Git(require.resolve('./fixtures/git/master.git/HEAD')) diff --git a/src/editor-component.coffee b/src/editor-component.coffee index f11a3a9b2..ebe970a52 100644 --- a/src/editor-component.coffee +++ b/src/editor-component.coffee @@ -500,7 +500,7 @@ EditorComponent = React.createClass 'editor:fold-at-indent-level-9': => editor.foldAllAtIndentLevel(8) 'editor:toggle-line-comments': => editor.toggleLineCommentsInSelection() 'editor:log-cursor-scope': => editor.logCursorScope() - 'editor:checkout-head-revision': => editor.checkoutHead() + 'editor:checkout-head-revision': => atom.project.getRepo()?.checkoutHeadForEditor(editor) 'editor:copy-path': => editor.copyPathToClipboard() 'editor:move-line-up': => editor.moveLineUp() 'editor:move-line-down': => editor.moveLineDown() diff --git a/src/editor-view.coffee b/src/editor-view.coffee index 2b29cac9a..41fa8b3e3 100644 --- a/src/editor-view.coffee +++ b/src/editor-view.coffee @@ -58,7 +58,7 @@ class EditorView extends View softWrapAtPreferredLineLength: false scrollSensitivity: 40 useHardwareAcceleration: true - confirmCheckoutHead: true + confirmCheckoutHeadRevision: true invisibles: eol: '\u00ac' space: '\u00b7' @@ -237,7 +237,7 @@ class EditorView extends View 'editor:fold-at-indent-level-9': => @editor.foldAllAtIndentLevel(8) 'editor:toggle-line-comments': => @toggleLineCommentsInSelection() 'editor:log-cursor-scope': => @logCursorScope() - 'editor:checkout-head-revision': => @checkoutHead() + 'editor:checkout-head-revision': => atom.project.getRepo()?.checkoutHeadForEditor(@editor) 'editor:copy-path': => @copyPathToClipboard() 'editor:move-line-up': => @editor.moveLineUp() 'editor:move-line-down': => @editor.moveLineDown() @@ -344,11 +344,6 @@ class EditorView extends View getPlaceholderText: -> @placeholderText - # Checkout the HEAD revision of this editor's file. - checkoutHead: -> - if path = @editor.getPath() - atom.project.getRepo()?.checkoutHead(path) - configure: -> @subscribe atom.config.observe 'editor.showLineNumbers', (showLineNumbers) => @gutter.setShowLineNumbers(showLineNumbers) @subscribe atom.config.observe 'editor.showInvisibles', (showInvisibles) => @setShowInvisibles(showInvisibles) diff --git a/src/editor.coffee b/src/editor.coffee index 24157498e..be99f71a3 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -446,25 +446,6 @@ class Editor extends Model # filePath - A {String} path. saveAs: (filePath) -> @buffer.saveAs(filePath) - checkoutHead: -> - if (filePath = @getPath()) and (repo = atom.project.getRepo()) - confirmed = false - - if atom.config.get("editor.confirmCheckoutHead") - atom.confirm - message: "Are you sure you want to revert this file to the last Git commit?" - detailedMessage: "You are reverting: #{filePath}" - buttons: - "Revert": -> - confirmed = true - "Cancel": null - else - confirmed = true - - if confirmed - @buffer.reload() if @buffer.isModified() - repo.checkoutHead(filePath) - # Copies the current file path to the native clipboard. copyPathToClipboard: -> if filePath = @getPath() diff --git a/src/git.coffee b/src/git.coffee index 25c3db9fe..b8e35911f 100644 --- a/src/git.coffee +++ b/src/git.coffee @@ -1,4 +1,4 @@ -{join} = require 'path' +{basename, join} = require 'path' _ = require 'underscore-plus' {Emitter, Subscriber} = require 'emissary' @@ -93,6 +93,27 @@ class Git @getPathStatus(path) @subscribe buffer, 'destroyed', => @unsubscribe(buffer) + # Subscribes to editor view event. + checkoutHeadForEditor: (editor) -> + filePath = editor.getPath() + return unless filePath + + fileName = basename(filePath) + + checkoutHead = => + editor.buffer.reload() if editor.buffer.isModified() + @checkoutHead(filePath) + + if atom.config.get('editor.confirmCheckoutHeadRevision') + 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 + else + checkoutHead() + # Public: Destroy this `Git` object. This destroys any tasks and subscriptions # and releases the underlying libgit2 repository handle. destroy: ->