|
| 1 | +const path = require('path') |
| 2 | +const fs = require('fs-plus') |
| 3 | +const {CompositeDisposable, Emitter} = require('atom') |
| 4 | +const {repoForPath} = require('./helpers') |
| 5 | + |
| 6 | +module.exports = |
| 7 | +class File { |
| 8 | + constructor({name, fullPath, symlink, realpathCache, ignoredNames, useSyncFS, stats}) { |
| 9 | + this.name = name |
| 10 | + this.symlink = symlink |
| 11 | + this.ignoredNames = ignoredNames |
| 12 | + this.stats = stats |
| 13 | + this.destroyed = false |
| 14 | + this.emitter = new Emitter() |
| 15 | + this.subscriptions = new CompositeDisposable() |
| 16 | + |
| 17 | + this.path = fullPath |
| 18 | + this.realPath = this.path |
| 19 | + |
| 20 | + this.subscribeToRepo() |
| 21 | + this.updateStatus() |
| 22 | + |
| 23 | + if (useSyncFS) { |
| 24 | + this.realPath = fs.realpathSync(this.path) |
| 25 | + } else { |
| 26 | + fs.realpath(this.path, realpathCache, (error, realPath) => { |
| 27 | + if (this.destroyed) return |
| 28 | + if (realPath && realPath !== this.path) { |
| 29 | + this.realPath = realPath |
| 30 | + this.updateStatus() |
| 31 | + } |
| 32 | + }) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + destroy() { |
| 37 | + this.destroyed = true |
| 38 | + this.subscriptions.dispose() |
| 39 | + this.emitter.emit('did-destroy') |
| 40 | + } |
| 41 | + |
| 42 | + onDidDestroy(callback) { |
| 43 | + return this.emitter.on('did-destroy', callback) |
| 44 | + } |
| 45 | + |
| 46 | + onDidStatusChange(callback) { |
| 47 | + return this.emitter.on('did-status-change', callback) |
| 48 | + } |
| 49 | + |
| 50 | + // Subscribe to the project's repo for changes to the Git status of this file. |
| 51 | + subscribeToRepo() { |
| 52 | + const repo = repoForPath(this.path) |
| 53 | + if (repo == null) return |
| 54 | + |
| 55 | + this.subscriptions.add(repo.onDidChangeStatus(event => { |
| 56 | + if (this.isPathEqual(event.path)) { |
| 57 | + this.updateStatus(repo) |
| 58 | + } |
| 59 | + })) |
| 60 | + this.subscriptions.add(repo.onDidChangeStatuses(() => { |
| 61 | + this.updateStatus(repo) |
| 62 | + })) |
| 63 | + } |
| 64 | + |
| 65 | + // Update the status property of this directory using the repo. |
| 66 | + updateStatus() { |
| 67 | + const repo = repoForPath(this.path) |
| 68 | + if (repo == null) return |
| 69 | + |
| 70 | + let newStatus = null |
| 71 | + if (repo.isPathIgnored(this.path)) { |
| 72 | + newStatus = 'ignored' |
| 73 | + } else if (ignoredNames.matches(this.path)) { |
| 74 | + newStatus = 'ignored-name' |
| 75 | + } else { |
| 76 | + const status = repo.getCachedPathStatus(this.path) |
| 77 | + if (repo.isStatusModified(status)) { |
| 78 | + newStatus = 'modified' |
| 79 | + } else if (repo.isStatusNew(status)) { |
| 80 | + newStatus = 'added' |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if (newStatus !== this.status) { |
| 85 | + this.status = newStatus |
| 86 | + this.emitter.emit('did-status-change', newStatus) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + isPathEqual(pathToCompare) { |
| 91 | + return this.path === pathToCompare || this.realPath === pathToCompare |
| 92 | + } |
| 93 | +} |
0 commit comments