Skip to content

Commit c5164be

Browse files
authored
Add git version check before using system git (#93)
1 parent f2fa8eb commit c5164be

File tree

2 files changed

+65
-6
lines changed

2 files changed

+65
-6
lines changed

src/utils/gitUtil.mts

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,31 @@ import { SettingsKey, HOME_VAR } from "../settings.mjs";
77
import { homedir } from "os";
88
import which from "which";
99
import { window } from "vscode";
10+
import { compareGe } from "./semverUtil.mjs";
1011

1112
export 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 = /git version (\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 {

src/utils/requirementsUtil.mts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { SettingsKey, HOME_VAR } from "../settings.mjs";
55
import { homedir } from "os";
66
import { downloadGit } from "./downloadGit.mjs";
77
import Logger, { LoggerSource } from "../logger.mjs";
8+
import { checkGitVersion, MIN_GIT_VERSION } from "./gitUtil.mjs";
89

910
/**
1011
* Shows an error message that the requirements for development are not met
@@ -37,6 +38,12 @@ export async function checkForGit(settings: Settings): Promise<boolean> {
3738
const git: string | null = await which(gitExe, { nothrow: true });
3839

3940
let isGitInstalled = git !== null;
41+
let gitVersion: string | undefined;
42+
if (git !== null) {
43+
const ret = await checkGitVersion(git);
44+
isGitInstalled = ret[0];
45+
gitVersion = ret[1];
46+
}
4047
if (!isGitInstalled) {
4148
// try to install
4249
const gitDownloaded: string | undefined = await downloadGit();
@@ -45,6 +52,17 @@ export async function checkForGit(settings: Settings): Promise<boolean> {
4552
// if git is downloaded set custom git path
4653
await settings.updateGlobal(SettingsKey.gitPath, gitDownloaded);
4754
isGitInstalled = true;
55+
} else if (gitVersion !== undefined) {
56+
Logger.error(
57+
LoggerSource.requirements,
58+
"Installed Git is too old and a new version could not be downloaded."
59+
);
60+
void window.showErrorMessage(
61+
"The installation of the Pico SDK requires Git " +
62+
`version ${MIN_GIT_VERSION} or higher ` +
63+
"to be installed and available in the PATH - " +
64+
`you have version ${gitVersion}.`
65+
);
4866
} else {
4967
Logger.error(
5068
LoggerSource.requirements,

0 commit comments

Comments
 (0)