.getPathStatus()

This commit is contained in:
Daniel Hengeveld
2015-10-19 15:15:42 +02:00
parent 58c989455a
commit ab4ba2ca4c
2 changed files with 70 additions and 15 deletions

View File

@@ -2,6 +2,7 @@
const Git = require('nodegit')
const path = require('path')
const {Emitter, Disposable, CompositeDisposable} = require('event-kit')
// GitUtils is temporarily used for ::relativize only, because I don't want
// to port it just yet. TODO: remove
@@ -15,10 +16,17 @@ module.exports = class GitRepositoryAsync {
constructor (path) {
this.repo = null
this.emitter = new Emitter()
this.subscriptions = new CompositeDisposable()
this.pathStatusCache = {}
this._gitUtilsRepo = GitUtils.open(path) // TODO remove after porting ::relativize
this.repoPromise = Git.Repository.open(path)
}
destroy () {
this.subscriptions.dispose()
}
getPath () {
return this.repoPromise.then((repo) => {
return Promise.resolve(repo.path().replace(/\/$/, ''))
@@ -38,6 +46,7 @@ module.exports = class GitRepositoryAsync {
basePath = repo.workdir()
return repo.getStatus()
}).then((statuses) => {
console.log('statuses', statuses)
return statuses.filter(function (status) {
return _path === path.join(basePath, status.path())
})
@@ -70,4 +79,38 @@ module.exports = class GitRepositoryAsync {
Git.Checkout.head(repo, checkoutOptions)
})
}
// Returns a Promise that resolves to the status bit of a given path if it has
// one, otherwise 'current'.
getPathStatus (_path) {
var relativePath = this._gitUtilsRepo.relativize(_path)
return this.repoPromise.then((repo) => {
return this._filterStatusesByPath(_path)
}).then((statuses) => {
var cachedStatus = this.pathStatusCache[relativePath] || 0
var status = statuses[0] ? statuses[0].statusBit() : Git.Status.STATUS.CURRENT
console.log('cachedStatus', cachedStatus, 'status', status)
if (status != cachedStatus) {
this.emitter.emit('did-change-status', {path: _path, pathStatus: status})
}
this.pathStatusCache[relativePath] = status
return Promise.resolve(status)
})
}
// Event subscription
// ==================
onDidChangeStatus (callback) {
return this.emitter.on('did-change-status', callback)
}
onDidChangeStatuses (callback) {
return this.emitter.on('did-change-statuses', callback)
}
onDidDestroy (callback) {
return this.emitter.on('did-destroy', callback)
}
}