@@ -7,9 +7,31 @@ import { SettingsKey, HOME_VAR } from "../settings.mjs";
77import { homedir } from "os" ;
88import which from "which" ;
99import { window } from "vscode" ;
10+ import { compareGe } from "./semverUtil.mjs" ;
1011
1112export const execAsync = promisify ( exec ) ;
1213
14+ export const MIN_GIT_VERSION = "2.28.0" ;
15+
16+ export async function checkGitVersion ( gitExecutable : string ) :
17+ Promise < [ boolean , string ] >
18+ {
19+ const versionCommand =
20+ `${
21+ process . env . ComSpec === "powershell.exe" ? "&" : ""
22+ } "${ gitExecutable } " version`;
23+ const ret = await execAsync ( versionCommand )
24+ const regex = / g i t v e r s i o n ( \d + \. \d + ( \. \d + ) * ) / ;
25+ const match = regex . exec ( ret . stdout ) ;
26+ if ( match && match [ 1 ] ) {
27+ const gitVersion = match [ 1 ] ;
28+
29+ return [ compareGe ( gitVersion , MIN_GIT_VERSION ) , gitVersion ] ;
30+ } else {
31+ return [ false , "unknown" ] ;
32+ }
33+ }
34+
1335/**
1436 * Get installed version of git, and install it if it isn't already
1537 */
@@ -19,6 +41,14 @@ export async function getGit(settings: Settings): Promise<string | undefined> {
1941 . getString ( SettingsKey . gitPath )
2042 ?. replace ( HOME_VAR , homedir ( ) . replaceAll ( "\\" , "/" ) ) || "git" ;
2143 let gitPath = await which ( gitExecutable , { nothrow : true } ) ;
44+ let gitVersion : string | undefined ;
45+ if ( gitPath !== null ) {
46+ const versionRet = await checkGitVersion ( gitPath ) ;
47+ if ( ! versionRet [ 0 ] ) {
48+ gitPath = null ;
49+ }
50+ gitVersion = versionRet [ 1 ] ;
51+ }
2252 if ( gitPath === null ) {
2353 // if git is not in path then checkForInstallationRequirements
2454 // maye downloaded it, so reload
@@ -27,12 +57,23 @@ export async function getGit(settings: Settings): Promise<string | undefined> {
2757 . getString ( SettingsKey . gitPath )
2858 ?. replace ( HOME_VAR , homedir ( ) . replaceAll ( "\\" , "/" ) ) ;
2959 if ( gitExecutable === null || gitExecutable === undefined ) {
30- Logger . log ( "Error: Git not found." ) ;
31-
32- await window . showErrorMessage (
33- "Git not found. Please install and add to PATH or " +
34- "set the path to the git executable in global settings."
35- ) ;
60+ if ( gitVersion !== undefined ) {
61+ Logger . log ( `Error: Found Git version ${ gitVersion } - ` +
62+ `requires ${ MIN_GIT_VERSION } .` ) ;
63+
64+ await window . showErrorMessage (
65+ `Found Git version ${ gitVersion } , but requires ${ MIN_GIT_VERSION } . ` +
66+ "Please install and add to PATH or " +
67+ "set the path to the git executable in global settings."
68+ ) ;
69+ } else {
70+ Logger . log ( "Error: Git not found." ) ;
71+
72+ await window . showErrorMessage (
73+ "Git not found. Please install and add to PATH or " +
74+ "set the path to the git executable in global settings."
75+ ) ;
76+ }
3677
3778 return undefined ;
3879 } else {
0 commit comments