Add task to refresh status of repository

By default this will occur when the window gains
focus and the Git class can now be subscribed to
so listeners can become notified when the status
of a repository changes.
This commit is contained in:
Kevin Sawicki 2013-02-27 14:28:30 -08:00
parent 4add7b6213
commit 50bc1aac74
5 changed files with 60 additions and 12 deletions

View File

@ -148,7 +148,7 @@ describe "Git", ->
fs.write(path, "#{originalPathText} edited line")
expect(repo.getDiffStats(path)).toEqual {added: 1, deleted: 1}
describe ".getStatuses()", ->
describe ".refreshStatuses()", ->
[newPath, modifiedPath, cleanPath, originalModifiedPathText] = []
beforeEach ->
@ -165,7 +165,15 @@ describe "Git", ->
it "returns status information for all new and modified files", ->
fs.write(modifiedPath, 'making this path modified')
statuses = repo.getAllStatuses()
expect(statuses[cleanPath]).toBeUndefined()
expect(repo.isStatusNew(statuses[repo.relativize(newPath)])).toBeTruthy()
expect(repo.isStatusModified(statuses[repo.relativize(modifiedPath)])).toBeTruthy()
statusHandler = jasmine.createSpy('statusHandler')
repo.on 'statuses-changed', statusHandler
repo.refreshStatuses()
waitsFor ->
statusHandler.callCount > 0
runs ->
statuses = repo.statuses
expect(statuses[cleanPath]).toBeUndefined()
expect(repo.isStatusNew(statuses[newPath])).toBeTruthy()
expect(repo.isStatusModified(statuses[modifiedPath])).toBeTruthy()

View File

@ -1,7 +1,9 @@
_ = require 'underscore'
fs = require 'fs'
Subscriber = require 'subscriber'
EventEmitter = require 'event-emitter'
GitRepository = require 'git-repository'
RepositoryStatusTask = require 'repository-status-task'
module.exports =
class Git
@ -23,12 +25,16 @@ class Git
working_dir_typechange: 1 << 10
ignore: 1 << 14
statuses: {}
constructor: (path, options={}) ->
@repo = GitRepository.open(path)
refreshIndexOnFocus = options.refreshIndexOnFocus ? true
if refreshIndexOnFocus
refreshOnWindowFocus = options.refreshOnWindowFocus ? true
if refreshOnWindowFocus
$ = require 'jquery'
@subscribe $(window), 'focus', => @refreshIndex()
@subscribe $(window), 'focus', =>
@refreshIndex()
@refreshStatuses()
getRepo: ->
unless @repo?
@ -54,9 +60,6 @@ class Git
getPathStatus: (path) ->
pathStatus = @getRepo().getStatus(@relativize(path))
getAllStatuses: (path) ->
@getRepo().getStatuses()
isPathIgnored: (path) ->
@getRepo().isIgnored(@relativize(path))
@ -104,4 +107,8 @@ class Git
isSubmodule: (path) ->
@getRepo().isSubmodule(@relativize(path))
refreshStatuses: ->
new RepositoryStatusTask(this).start()
_.extend Git.prototype, Subscriber
_.extend Git.prototype, EventEmitter

View File

@ -0,0 +1,16 @@
Git = require 'git'
fs = require 'fs'
module.exports =
loadStatuses: (path) ->
repo = Git.open(path)
if repo?
workingDirectoryPath = repo.getWorkingDirectory()
statuses = {}
for path, status of repo.getRepo().getStatuses()
statuses[fs.join(workingDirectoryPath, path)] = status
repo.destroy()
else
statuses = {}
callTaskMethod('statusesLoaded', statuses)

View File

@ -0,0 +1,17 @@
Task = require 'task'
_ = require 'underscore'
module.exports =
class RepositoryStatusTask extends Task
constructor: (@repo) ->
super('repository-status-handler')
started: ->
@callWorkerMethod('loadStatuses', @repo.getPath())
statusesLoaded: (statuses) ->
@done()
unless _.isEqual(statuses, @repo.statuses)
@repo.statuses = statuses
@repo.trigger 'statuses-changed'

View File

@ -5,7 +5,7 @@ module.exports =
loadPaths: (rootPath, ignoredNames, excludeGitIgnoredPaths) ->
if excludeGitIgnoredPaths
Git = require 'git'
repo = Git.open(rootPath, refreshIndexOnFocus: false)
repo = Git.open(rootPath, refreshOnWindowFocus: false)
paths = []
isIgnored = (path) ->