From 41c62e8628bc616966fea5b71cdbf83f3a1d918e Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 13 Aug 2014 15:41:50 -0700 Subject: [PATCH 1/9] Add Git::checkoutHeadForEditor This moves the logic from Editor::checkoutHead --- src/editor-component.coffee | 2 +- src/editor-view.coffee | 7 +------ src/editor.coffee | 19 ------------------- src/git.coffee | 19 +++++++++++++++++++ 4 files changed, 21 insertions(+), 26 deletions(-) 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..580a312b7 100644 --- a/src/editor-view.coffee +++ b/src/editor-view.coffee @@ -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..b6894fc08 100644 --- a/src/git.coffee +++ b/src/git.coffee @@ -93,6 +93,25 @@ class Git @getPathStatus(path) @subscribe buffer, 'destroyed', => @unsubscribe(buffer) + # Subscribes to editor view event. + checkoutHeadForEditor: (editor) -> + filePath = editor.getPath() + return unless filePath + + revert = => + editor.buffer.reload() if editor.buffer.isModified() + @checkoutHead(filePath) + + 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: revert + Cancel: null + else + revert() + # Public: Destroy this `Git` object. This destroys any tasks and subscriptions # and releases the underlying libgit2 repository handle. destroy: -> From 84d0abc52c8a5ccc974d7846a57f82e2f1f3c514 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 13 Aug 2014 15:47:17 -0700 Subject: [PATCH 2/9] Use same dialog language as GitHub for Mac --- package.json | 2 ++ src/git.coffee | 10 ++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index bdc30a46a..4b7d69764 100644 --- a/package.json +++ b/package.json @@ -1,3 +1,5 @@ + + { "name": "atom", "productName": "Atom", diff --git a/src/git.coffee b/src/git.coffee index b6894fc08..f666633f0 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' @@ -98,16 +98,18 @@ class Git filePath = editor.getPath() return unless filePath + fileName = basename(filePath) + revert = => editor.buffer.reload() if editor.buffer.isModified() @checkoutHead(filePath) 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}" + message: 'Confirm Discard Changes' + detailedMessage: "Are you sure you want to discard all changes to \"#{fileName}\"?" buttons: - Revert: revert + OK: revert Cancel: null else revert() From 27ca9576297cc4930e8b39bddb956be4b92b2d9e Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 13 Aug 2014 15:47:53 -0700 Subject: [PATCH 3/9] revert -> checkoutHead --- src/git.coffee | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/git.coffee b/src/git.coffee index f666633f0..19e516ee2 100644 --- a/src/git.coffee +++ b/src/git.coffee @@ -100,7 +100,7 @@ class Git fileName = basename(filePath) - revert = => + checkoutHead = => editor.buffer.reload() if editor.buffer.isModified() @checkoutHead(filePath) @@ -109,10 +109,10 @@ class Git message: 'Confirm Discard Changes' detailedMessage: "Are you sure you want to discard all changes to \"#{fileName}\"?" buttons: - OK: revert + OK: checkoutHead Cancel: null else - revert() + checkoutHead() # Public: Destroy this `Git` object. This destroys any tasks and subscriptions # and releases the underlying libgit2 repository handle. From 3ce641f53bf992a3e0cde6b78bcefd075c42fc21 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 13 Aug 2014 15:55:38 -0700 Subject: [PATCH 4/9] Move checkout head editor specs to git spec --- spec/editor-spec.coffee | 21 --------------------- spec/git-spec.coffee | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 21 deletions(-) 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/git-spec.coffee b/spec/git-spec.coffee index 46263c8ff..8c89ab106 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.confirmCheckoutHead', 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.confirmCheckoutHead', 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')) From d7e5f05f836d093f90d9067edf17805fe528427b Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 13 Aug 2014 15:56:58 -0700 Subject: [PATCH 5/9] :lipstick: --- package.json | 2 -- 1 file changed, 2 deletions(-) diff --git a/package.json b/package.json index 4b7d69764..bdc30a46a 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,3 @@ - - { "name": "atom", "productName": "Atom", From 00170804e56b40c69585a2a47b5eec88eeb9abb7 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 13 Aug 2014 16:08:50 -0700 Subject: [PATCH 6/9] Remove spec now covered in git spec --- spec/editor-view-spec.coffee | 31 ------------------------------- 1 file changed, 31 deletions(-) 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", -> From 27f0c525acdc180a31490c185da4c5a6c2776211 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 13 Aug 2014 18:44:33 -0700 Subject: [PATCH 7/9] Add since the last Git commit to detailed message --- src/git.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/git.coffee b/src/git.coffee index 19e516ee2..b9d65e70c 100644 --- a/src/git.coffee +++ b/src/git.coffee @@ -107,7 +107,7 @@ class Git if atom.config.get('editor.confirmCheckoutHead') atom.confirm message: 'Confirm Discard Changes' - detailedMessage: "Are you sure you want to discard all changes to \"#{fileName}\"?" + detailedMessage: "Are you sure you want to discard all changes to \"#{fileName}\" since the last Git commit?" buttons: OK: checkoutHead Cancel: null From 5a966240b9c0333f2679e9d7c13fcb53629c66b0 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 13 Aug 2014 18:44:58 -0700 Subject: [PATCH 8/9] Make message map to command name --- src/git.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/git.coffee b/src/git.coffee index b9d65e70c..8a74f519f 100644 --- a/src/git.coffee +++ b/src/git.coffee @@ -106,7 +106,7 @@ class Git if atom.config.get('editor.confirmCheckoutHead') atom.confirm - message: 'Confirm Discard Changes' + 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 From 776e431cc50c9a03df48146e17c628ffdfef4048 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 13 Aug 2014 18:46:16 -0700 Subject: [PATCH 9/9] confirmCheckoutHead -> confirmCheckoutHeadRevision --- spec/git-spec.coffee | 4 ++-- src/editor-view.coffee | 2 +- src/git.coffee | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/git-spec.coffee b/spec/git-spec.coffee index 8c89ab106..b40f68182 100644 --- a/spec/git-spec.coffee +++ b/spec/git-spec.coffee @@ -136,7 +136,7 @@ describe "Git", -> it "displays a confirmation dialog by default", -> spyOn(atom, 'confirm').andCallFake ({buttons}) -> buttons.OK() - atom.config.set('editor.confirmCheckoutHead', true) + atom.config.set('editor.confirmCheckoutHeadRevision', true) repo.checkoutHeadForEditor(editor) @@ -144,7 +144,7 @@ describe "Git", -> it "does not display a dialog when confirmation is disabled", -> spyOn(atom, 'confirm') - atom.config.set('editor.confirmCheckoutHead', false) + atom.config.set('editor.confirmCheckoutHeadRevision', false) repo.checkoutHeadForEditor(editor) diff --git a/src/editor-view.coffee b/src/editor-view.coffee index 580a312b7..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' diff --git a/src/git.coffee b/src/git.coffee index 8a74f519f..b8e35911f 100644 --- a/src/git.coffee +++ b/src/git.coffee @@ -104,7 +104,7 @@ class Git editor.buffer.reload() if editor.buffer.isModified() @checkoutHead(filePath) - if atom.config.get('editor.confirmCheckoutHead') + 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?"