Consolidate Git status checking

Now the status bar and tree view both listen for
status change events and use the cached information
available from the git object to update their views.
This commit is contained in:
Kevin Sawicki
2013-02-27 18:37:39 -08:00
parent 2ec4d558ba
commit 4fe6db240b
8 changed files with 47 additions and 30 deletions

View File

@@ -137,16 +137,18 @@ describe "Editor", ->
describe ".remove()", ->
it "removes subscriptions from all edit session buffers", ->
previousEditSession = editor.activeEditSession
otherEditSession = project.buildEditSessionForPath(project.resolve('sample.txt'))
expect(previousEditSession.buffer.subscriptionCount()).toBeGreaterThan 1
editSession1 = editor.activeEditSession
subscriberCount1 = editSession1.buffer.subscriptionCount()
editSession2 = project.buildEditSessionForPath(project.resolve('sample.txt'))
expect(subscriberCount1).toBeGreaterThan 1
editor.edit(otherEditSession)
expect(otherEditSession.buffer.subscriptionCount()).toBeGreaterThan 1
editor.edit(editSession2)
subscriberCount2 = editSession2.buffer.subscriptionCount()
expect(subscriberCount2).toBeGreaterThan 1
editor.remove()
expect(previousEditSession.buffer.subscriptionCount()).toBe 0
expect(otherEditSession.buffer.subscriptionCount()).toBe 0
expect(editSession1.buffer.subscriptionCount()).toBeLessThan subscriberCount1
expect(editSession2.buffer.subscriptionCount()).toBeLessThan subscriberCount2
describe "when 'close' is triggered", ->
it "adds a closed session path to the array", ->

View File

@@ -38,6 +38,13 @@ class Git
@refreshIndex()
@refreshStatus()
project?.eachBuffer this, (buffer) =>
bufferStatusHandler = =>
path = buffer.getPath()
@getPathStatus(path) if path
@subscribe buffer, 'saved', bufferStatusHandler
@subscribe buffer, 'reloaded', bufferStatusHandler
getRepo: ->
unless @repo?
throw new Error("Repository has been destroyed")

View File

@@ -128,9 +128,15 @@ class Project
buffers.push editSession.buffer
buffers
eachBuffer: (callback) ->
eachBuffer: (args...) ->
subscriber = args.shift() if args.length > 1
callback = args.shift()
callback(buffer) for buffer in @getBuffers()
@on 'buffer-created', (buffer) -> callback(buffer)
if subscriber
subscriber.subscribe this, 'buffer-created', (buffer) -> callback(buffer)
else
@on 'buffer-created', (buffer) -> callback(buffer)
bufferForPath: (filePath) ->
if filePath?

View File

@@ -69,9 +69,11 @@ window.shutdown = ->
rootView: rootView.serialize()
rootView.deactivate()
project.destroy()
git?.destroy()
$(window).off('focus blur before')
window.rootView = null
window.project = null
window.git = null
window.installAtomCommand = (commandPath) ->
return if fs.exists(commandPath)

View File

@@ -32,9 +32,13 @@ class StatusBarView extends View
@updateCursorPositionText()
@subscribe @editor, 'cursor:moved', => @updateCursorPositionText()
@subscribe $(window), 'focus', => @updateStatusBar()
@subscribe @grammarName, 'click', => @editor.trigger 'editor:select-grammar'
@subscribe @editor, 'editor:grammar-changed', => @updateGrammarText()
if git?
@subscribe git, 'status-changed', (path, status) =>
@updateStatusBar() if path is @buffer?.getPath()
@subscribe git, 'statuses-changed', =>
@updateStatusBar()
@subscribeToBuffer()
@@ -42,7 +46,6 @@ class StatusBarView extends View
@buffer?.off '.status-bar'
@buffer = @editor.getBuffer()
@buffer.on 'contents-modified.status-bar', (e) => @updateBufferHasModifiedText(e.differsFromDisk)
@buffer.on 'saved.status-bar', => @updateStatusBar()
@buffer.on 'git-status-changed.status-bar', => @updateStatusBar()
@updateStatusBar()
@@ -80,7 +83,7 @@ class StatusBarView extends View
@gitStatusIcon.addClass('git-status octicons')
return unless git?
status = git.getPathStatus(path)
status = git.statuses[path]
if git.isStatusModified(status)
@gitStatusIcon.addClass('modified-status-icon')
stats = git.getDiffStats(path)

View File

@@ -130,6 +130,8 @@ describe "StatusBar", ->
path = require.resolve('fixtures/git/working-dir/file.txt')
newPath = fs.join(require.resolve('fixtures/git/working-dir'), 'new.txt')
fs.write(newPath, "I'm new here")
git.getPathStatus(path)
git.getPathStatus(newPath)
originalPathText = fs.read(path)
rootView.attachToDom()
@@ -139,6 +141,7 @@ describe "StatusBar", ->
it "displays the modified icon for a changed file", ->
fs.write(path, "i've changed for the worse")
git.getPathStatus(path)
rootView.open(path)
expect(statusBar.gitStatusIcon).toHaveClass('modified-status-icon')
@@ -152,22 +155,17 @@ describe "StatusBar", ->
it "updates when a git-status-changed event occurs", ->
fs.write(path, "i've changed for the worse")
git.getPathStatus(path)
rootView.open(path)
expect(statusBar.gitStatusIcon).toHaveClass('modified-status-icon')
fs.write(path, originalPathText)
git.getPathStatus(path)
rootView.getActiveEditor().getBuffer().trigger 'git-status-changed'
expect(statusBar.gitStatusIcon).not.toHaveClass('modified-status-icon')
it "updates when the window receives focus", ->
fs.write(path, "i've changed for the worse")
rootView.open(path)
expect(statusBar.gitStatusIcon).toHaveClass('modified-status-icon')
fs.write(path, originalPathText)
$(window).trigger 'focus'
expect(statusBar.gitStatusIcon).not.toHaveClass('modified-status-icon')
it "displays the diff stat for modified files", ->
fs.write(path, "i've changed for the worse")
git.getPathStatus(path)
rootView.open(path)
expect(statusBar.gitStatusIcon).toHaveText('+1,-1')

View File

@@ -14,8 +14,6 @@ class FileView extends View
file: null
initialize: ({@file, @project} = {}) ->
@subscribe $(window), 'focus', => @updateStatus()
extension = fs.extension(@getPath())
if fs.isReadmePath(@getPath())
@fileName.addClass('readme-icon')
@@ -30,6 +28,12 @@ class FileView extends View
else
@fileName.addClass('text-icon')
if git?
git.on 'status-changed', (path, status) =>
@updateStatus() if path is @getPath()
git.on 'statuses-changed', =>
@updateStatus()
@updateStatus()
updateStatus: ->
@@ -40,7 +44,7 @@ class FileView extends View
if git.isPathIgnored(path)
@addClass('ignored')
else
status = git.getPathStatus(path)
status = git.statuses[path]
if git.isStatusModified(status)
@addClass('modified')
else if git.isStatusNew(status)

View File

@@ -933,9 +933,11 @@ describe "TreeView", ->
config.set "core.hideGitIgnoredFiles", false
ignoreFile = fs.join(require.resolve('fixtures/tree-view'), '.gitignore')
fs.write(ignoreFile, 'tree-view.js')
git.getPathStatus(ignoreFile)
modifiedFile = fs.join(require.resolve('fixtures/tree-view'), 'tree-view.txt')
originalFileContent = fs.read(modifiedFile)
fs.write modifiedFile, 'ch ch changes'
git.getPathStatus(modifiedFile)
treeView.updateRoot()
afterEach ->
@@ -946,13 +948,6 @@ describe "TreeView", ->
it "adds a custom style", ->
expect(treeView.find('.file:contains(tree-view.txt)')).toHaveClass 'modified'
describe "when the window gains focus after the contents are restored to a clean state", ->
it "removes the custom style", ->
expect(treeView.find('.file:contains(tree-view.txt)')).toHaveClass 'modified'
fs.write modifiedFile, originalFileContent
$(window).trigger 'focus'
expect(treeView.find('.file:contains(tree-view.txt)')).not.toHaveClass 'modified'
describe "when a file is new", ->
it "adds a custom style", ->
expect(treeView.find('.file:contains(.gitignore)')).toHaveClass 'new'