|
| 1 | +define(function (require, exports, module) { |
| 2 | + |
| 3 | + // Brackets modules |
| 4 | + const _ = brackets.getModule("thirdparty/lodash"), |
| 5 | + DocumentManager = brackets.getModule("document/DocumentManager"), |
| 6 | + FileSystem = brackets.getModule("filesystem/FileSystem"), |
| 7 | + ProjectManager = brackets.getModule("project/ProjectManager"), |
| 8 | + MainViewManager = brackets.getModule("view/MainViewManager"); |
| 9 | + |
| 10 | + // Local modules |
| 11 | + const Events = require("src/Events"), |
| 12 | + EventEmitter = require("src/EventEmitter"), |
| 13 | + HistoryViewer = require("src/HistoryViewer"), |
| 14 | + Preferences = require("src/Preferences"), |
| 15 | + Utils = require("src/Utils"); |
| 16 | + |
| 17 | + // White-list for .git file watching |
| 18 | + const watchedInsideGit = ["HEAD"]; |
| 19 | + const GIT_EVENTS = "gitEvents"; |
| 20 | + |
| 21 | + FileSystem.on(`change.${GIT_EVENTS}`, function (evt, file) { |
| 22 | + // we care only for files in current project |
| 23 | + var currentGitRoot = Preferences.get("currentGitRoot"); |
| 24 | + if (file && file.fullPath.indexOf(currentGitRoot) === 0) { |
| 25 | + |
| 26 | + if (file.fullPath.indexOf(currentGitRoot + ".git/") === 0) { |
| 27 | + |
| 28 | + var whitelisted = _.any(watchedInsideGit, function (entry) { |
| 29 | + return file.fullPath === currentGitRoot + ".git/" + entry; |
| 30 | + }); |
| 31 | + if (!whitelisted) { |
| 32 | + Utils.consoleDebug("Ignored FileSystem.change event: " + file.fullPath); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + EventEmitter.emit(Events.BRACKETS_FILE_CHANGED, file); |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + DocumentManager.on(`documentSaved.${GIT_EVENTS}`, function (evt, doc) { |
| 43 | + // we care only for files in current project |
| 44 | + if (doc.file.fullPath.indexOf(Preferences.get("currentGitRoot")) === 0) { |
| 45 | + EventEmitter.emit(Events.BRACKETS_DOCUMENT_SAVED, doc); |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + MainViewManager.on(`currentFileChange.${GIT_EVENTS}`, function (evt, currentDocument, previousDocument) { |
| 50 | + currentDocument = currentDocument || DocumentManager.getCurrentDocument(); |
| 51 | + if (!HistoryViewer.isVisible()) { |
| 52 | + EventEmitter.emit(Events.BRACKETS_CURRENT_DOCUMENT_CHANGE, currentDocument, previousDocument); |
| 53 | + } else { |
| 54 | + HistoryViewer.hide(); |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + ProjectManager.on(`projectOpen.${GIT_EVENTS}`, function () { |
| 59 | + EventEmitter.emit(Events.BRACKETS_PROJECT_CHANGE); |
| 60 | + }); |
| 61 | + |
| 62 | + ProjectManager.on(`projectRefresh.${GIT_EVENTS}`, function () { |
| 63 | + EventEmitter.emit(Events.BRACKETS_PROJECT_REFRESH); |
| 64 | + }); |
| 65 | + |
| 66 | + ProjectManager.on(`beforeProjectClose.${GIT_EVENTS}`, function () { |
| 67 | + // Disable Git when closing a project so listeners won't fire before new is opened |
| 68 | + EventEmitter.emit(Events.GIT_DISABLED); |
| 69 | + }); |
| 70 | + |
| 71 | + function disableAll() { |
| 72 | + FileSystem.off(`change.${GIT_EVENTS}`); |
| 73 | + DocumentManager.off(`documentSaved.${GIT_EVENTS}`); |
| 74 | + MainViewManager.off(`currentFileChange.${GIT_EVENTS}`); |
| 75 | + ProjectManager.off(`projectOpen.${GIT_EVENTS}`); |
| 76 | + ProjectManager.off(`projectRefresh.${GIT_EVENTS}`); |
| 77 | + ProjectManager.off(`beforeProjectClose.${GIT_EVENTS}`); |
| 78 | + } |
| 79 | + |
| 80 | + exports.disableAll = disableAll; |
| 81 | +}); |
0 commit comments