88import * as nls from 'vscode-nls' ;
99const 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' ;
1212import { findGit , Git , IGit } from './git' ;
1313import { Model } from './model' ;
1414import { CommandCenter } from './commands' ;
@@ -20,6 +20,8 @@ import TelemetryReporter from 'vscode-extension-telemetry';
2020import { GitExtension } from './api/git' ;
2121import { GitProtocolHandler } from './protocolHandler' ;
2222import { createGitExtension } from './api/extension' ;
23+ import * as path from 'path' ;
24+ import * as fs from 'fs' ;
2325
2426const 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+
74124export 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