Skip to content

Commit 10e0a22

Browse files
committed
1 parent 1eb0b5b commit 10e0a22

1 file changed

Lines changed: 55 additions & 23 deletions

File tree

extensions/git/src/main.ts

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import * as nls from 'vscode-nls';
99
const localize = nls.loadMessageBundle();
1010

11-
import { ExtensionContext, workspace, window, Disposable, commands, Uri, OutputChannel } from 'vscode';
11+
import { ExtensionContext, workspace, window, Disposable, commands, Uri, OutputChannel, WorkspaceFolder } from 'vscode';
1212
import { findGit, Git, IGit } from './git';
1313
import { Model } from './model';
1414
import { CommandCenter } from './commands';
@@ -20,6 +20,8 @@ import TelemetryReporter from 'vscode-extension-telemetry';
2020
import { GitExtension } from './api/git';
2121
import { GitProtocolHandler } from './protocolHandler';
2222
import { createGitExtension } from './api/extension';
23+
import * as path from 'path';
24+
import * as fs from 'fs';
2325

2426
const deactivateTasks: { (): Promise<any>; }[] = [];
2527

@@ -71,6 +73,54 @@ async function createModel(context: ExtensionContext, outputChannel: OutputChann
7173
return model;
7274
}
7375

76+
async function isGitRepository(folder: WorkspaceFolder): Promise<boolean> {
77+
if (folder.uri.scheme !== 'file') {
78+
return false;
79+
}
80+
81+
const dotGit = path.join(folder.uri.fsPath, '.git');
82+
83+
try {
84+
const dotGitStat = await new Promise<fs.Stats>((c, e) => fs.stat(dotGit, (err, stat) => err ? e(err) : c(stat)));
85+
return dotGitStat.isDirectory();
86+
} catch (err) {
87+
return false;
88+
}
89+
}
90+
91+
async function warnAboutMissingGit(): Promise<void> {
92+
const config = workspace.getConfiguration('git');
93+
const shouldIgnore = config.get<boolean>('ignoreMissingGitWarning') === true;
94+
95+
if (shouldIgnore) {
96+
return;
97+
}
98+
99+
if (!workspace.workspaceFolders) {
100+
return;
101+
}
102+
103+
const areGitRepositories = await Promise.all(workspace.workspaceFolders.map(isGitRepository));
104+
105+
if (areGitRepositories.every(isGitRepository => !isGitRepository)) {
106+
return;
107+
}
108+
109+
const download = localize('downloadgit', "Download Git");
110+
const neverShowAgain = localize('neverShowAgain', "Don't Show Again");
111+
const choice = await window.showWarningMessage(
112+
localize('notfound', "Git not found. Install it or configure it using the 'git.path' setting."),
113+
download,
114+
neverShowAgain
115+
);
116+
117+
if (choice === download) {
118+
commands.executeCommand('vscode.open', Uri.parse('https://git-scm.com/'));
119+
} else if (choice === neverShowAgain) {
120+
await config.update('ignoreMissingGitWarning', true, true);
121+
}
122+
}
123+
74124
export async function activate(context: ExtensionContext): Promise<GitExtension> {
75125
const disposables: Disposable[] = [];
76126
context.subscriptions.push(new Disposable(() => Disposable.from(...disposables).dispose()));
@@ -100,28 +150,10 @@ export async function activate(context: ExtensionContext): Promise<GitExtension>
100150
throw err;
101151
}
102152

103-
const config = workspace.getConfiguration('git');
104-
const shouldIgnore = config.get<boolean>('ignoreMissingGitWarning') === true;
105-
106-
if (!shouldIgnore) {
107-
console.warn(err.message);
108-
outputChannel.appendLine(err.message);
109-
outputChannel.show();
110-
111-
const download = localize('downloadgit', "Download Git");
112-
const neverShowAgain = localize('neverShowAgain', "Don't Show Again");
113-
const choice = await window.showWarningMessage(
114-
localize('notfound', "Git not found. Install it or configure it using the 'git.path' setting."),
115-
download,
116-
neverShowAgain
117-
);
118-
119-
if (choice === download) {
120-
commands.executeCommand('vscode.open', Uri.parse('https://git-scm.com/'));
121-
} else if (choice === neverShowAgain) {
122-
await config.update('ignoreMissingGitWarning', true, true);
123-
}
124-
}
153+
console.warn(err.message);
154+
outputChannel.appendLine(err.message);
155+
156+
await warnAboutMissingGit();
125157

126158
return createGitExtension();
127159
}

0 commit comments

Comments
 (0)