|
| 1 | +define(function (require) { |
| 2 | + const EventEmitter = require("src/EventEmitter"); |
| 3 | + const Events = require("src/Events"); |
| 4 | + const Git = require("src/git/Git"); |
| 5 | + const Preferences = require("src/Preferences"); |
| 6 | + |
| 7 | + // the cache of file statuses by path |
| 8 | + let fileStatusCache = {}; |
| 9 | + |
| 10 | + /** |
| 11 | + * this function is responsible to get the Git status for a file path |
| 12 | + * |
| 13 | + * @param {string} fullPath - the file path |
| 14 | + * @returns {Array|null} - Array of status strings or null if no status |
| 15 | + */ |
| 16 | + function getFileStatus(fullPath) { |
| 17 | + return fileStatusCache[fullPath] || null; |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * whether the file is modified or not |
| 22 | + * |
| 23 | + * @param {string} fullPath - the file path |
| 24 | + * @returns {boolean} - True if the file is modified otherwise false |
| 25 | + */ |
| 26 | + function isModified(fullPath) { |
| 27 | + const status = getFileStatus(fullPath); |
| 28 | + if (!status) { |
| 29 | + return false; |
| 30 | + } |
| 31 | + return ( |
| 32 | + status.some( |
| 33 | + (statusType) => |
| 34 | + statusType === Git.FILE_STATUS.MODIFIED || |
| 35 | + statusType === Git.FILE_STATUS.RENAMED || |
| 36 | + statusType === Git.FILE_STATUS.COPIED |
| 37 | + ) && !status.includes(Git.FILE_STATUS.STAGED) |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * whether the file is untracked or not |
| 43 | + * |
| 44 | + * @param {string} fullPath - the file path |
| 45 | + * @returns {boolean} - True if the file is untracked otherwise false |
| 46 | + */ |
| 47 | + function isUntracked(fullPath) { |
| 48 | + const status = getFileStatus(fullPath); |
| 49 | + if (!status) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + return status.includes(Git.FILE_STATUS.UNTRACKED); |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + // Update file status cache when Git status results are received |
| 58 | + EventEmitter.on(Events.GIT_STATUS_RESULTS, function (files) { |
| 59 | + // reset the cache |
| 60 | + fileStatusCache = {}; |
| 61 | + |
| 62 | + const gitRoot = Preferences.get("currentGitRoot"); |
| 63 | + if (!gitRoot) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + // we need to update cache with new status results |
| 68 | + files.forEach(function (entry) { |
| 69 | + const fullPath = gitRoot + entry.file; |
| 70 | + fileStatusCache[fullPath] = entry.status; |
| 71 | + }); |
| 72 | + |
| 73 | + // notify that file statuses have been updated |
| 74 | + EventEmitter.emit("GIT_FILE_STATUS_CHANGED", fileStatusCache); |
| 75 | + }); |
| 76 | + |
| 77 | + // clear cache when Git is disabled |
| 78 | + EventEmitter.on(Events.GIT_DISABLED, function () { |
| 79 | + fileStatusCache = {}; |
| 80 | + EventEmitter.emit("GIT_FILE_STATUS_CHANGED", fileStatusCache); |
| 81 | + }); |
| 82 | + |
| 83 | + return { |
| 84 | + getFileStatus: getFileStatus, |
| 85 | + isModified: isModified, |
| 86 | + isUntracked: isUntracked |
| 87 | + }; |
| 88 | +}); |
0 commit comments