Skip to content

Commit 9c78251

Browse files
committed
fix: don't show git file history of closed or non repo files
1 parent 6eb5b96 commit 9c78251

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

src/extensions/default/Git/src/History.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ define(function (require) {
77
LocalizationUtils = brackets.getModule("utils/LocalizationUtils"),
88
Strings = brackets.getModule("strings"),
99
Metrics = brackets.getModule("utils/Metrics"),
10+
NotificationUI = brackets.getModule("widgets/NotificationUI"),
1011
Mustache = brackets.getModule("thirdparty/mustache/mustache");
1112

1213
// Local modules
@@ -205,7 +206,18 @@ define(function (require) {
205206
if (doc) {
206207
lastDocumentSeen = doc;
207208
}
208-
return doc || lastDocumentSeen;
209+
// no fallback to lastDocumentSeen here: when no document is open, file
210+
// history must not stick to a file the user has already closed
211+
return doc;
212+
}
213+
214+
function _showFileHistoryToast(message) {
215+
NotificationUI.createToastFromTemplate(Strings.GIT_SHOW_FILE_HISTORY,
216+
"<div>" + _.escape(message) + "</div>", {
217+
toastStyle: NotificationUI.NOTIFICATION_STYLES_CSS_CLASS.INFO,
218+
autoCloseTimeS: 15,
219+
instantOpen: true
220+
});
209221
}
210222

211223
function handleFileChange() {
@@ -251,10 +263,15 @@ define(function (require) {
251263
}
252264

253265
if (historyEnabled && newHistoryMode === "FILE") {
254-
if (doc) {
266+
if (doc && doc.file) {
255267
file = {};
256268
file.absolute = doc.file.fullPath;
257269
file.relative = FileUtils.getRelativeFilename(Preferences.get("currentGitRoot"), file.absolute);
270+
if (!file.relative) {
271+
// the file is not inside the repository, so it has no history
272+
historyEnabled = false;
273+
file = null;
274+
}
258275
} else {
259276
// we want a file history but no file was found
260277
historyEnabled = false;
@@ -315,8 +332,17 @@ define(function (require) {
315332
$historyList = $();
316333
});
317334
EventEmitter.on(Events.HISTORY_SHOW_FILE, function () {
318-
handleToggleHistory("FILE");
319335
Metrics.countEvent(Metrics.EVENT_TYPE.GIT, 'panel', "fileHistory");
336+
const doc = getCurrentDocument();
337+
if (!doc || !doc.file) {
338+
_showFileHistoryToast(Strings.GIT_FILE_HISTORY_OPEN_A_FILE);
339+
return;
340+
}
341+
if (!FileUtils.getRelativeFilename(Preferences.get("currentGitRoot"), doc.file.fullPath)) {
342+
_showFileHistoryToast(Strings.GIT_FILE_HISTORY_NOT_IN_REPO);
343+
return;
344+
}
345+
handleToggleHistory("FILE");
320346
});
321347
EventEmitter.on(Events.HISTORY_SHOW_GLOBAL, function () {
322348
handleToggleHistory("GLOBAL");

src/nls/root/strings.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2227,6 +2227,8 @@ define({
22272227
"TOOLTIP_SHOW_HISTORY": "Show history",
22282228
"GIT_SHOW_FILE_HISTORY": "File Commit history",
22292229
"GIT_SHOW_HISTORY": "Commit history",
2230+
"GIT_FILE_HISTORY_OPEN_A_FILE": "Open a file in the editor to see its commit history.",
2231+
"GIT_FILE_HISTORY_NOT_IN_REPO": "The file is not part of this Git repository.",
22302232
"UNDO_CHANGES": "Discard changes",
22312233
"UNDO_CHANGES_BTN": "Discard changes\u2026",
22322234
"UNDO_LAST_LOCAL_COMMIT": "Undo last local (not pushed) commit\u2026",

0 commit comments

Comments
 (0)