From e761601a5f68b49099d119a38eb23d584482d996 Mon Sep 17 00:00:00 2001 From: William Vinnicombe Date: Mon, 23 Sep 2024 16:13:58 +0100 Subject: [PATCH 1/2] Add git version check before using system git Fixes #87 --- src/utils/gitUtil.mts | 51 ++++++++++++++++++++++++++++++---- src/utils/requirementsUtil.mts | 18 ++++++++++++ 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/src/utils/gitUtil.mts b/src/utils/gitUtil.mts index ed5f2ba..037a4d6 100644 --- a/src/utils/gitUtil.mts +++ b/src/utils/gitUtil.mts @@ -7,9 +7,29 @@ import { SettingsKey, HOME_VAR } from "../settings.mjs"; import { homedir } from "os"; import which from "which"; import { window } from "vscode"; +import { compareGe } from "./semverUtil.mjs"; export const execAsync = promisify(exec); +export const MIN_GIT_VERSION="2.28.0"; + +export async function checkGitVersion(gitExecutable: string): + Promise<[boolean, string]> +{ + const versionCommand = + `${ + process.env.ComSpec === "powershell.exe" ? "&" : "" + }"${gitExecutable}" version`; + const ret = await execAsync(versionCommand) + const stdout = ret.stdout.split(" "); + let gitVersion = stdout[stdout.length-1]; + if (gitVersion.includes("windows")) { + gitVersion = gitVersion.split(".windows")[0]; + } + + return [compareGe(gitVersion, MIN_GIT_VERSION), gitVersion]; +} + /** * Get installed version of git, and install it if it isn't already */ @@ -19,6 +39,14 @@ export async function getGit(settings: Settings): Promise { .getString(SettingsKey.gitPath) ?.replace(HOME_VAR, homedir().replaceAll("\\", "/")) || "git"; let gitPath = await which(gitExecutable, { nothrow: true }); + let gitVersion: string | undefined; + if (gitPath !== null) { + const versionRet = await checkGitVersion(gitPath); + if (!versionRet[0]) { + gitPath = null; + } + gitVersion = versionRet[1]; + } if (gitPath === null) { // if git is not in path then checkForInstallationRequirements // maye downloaded it, so reload @@ -27,12 +55,23 @@ export async function getGit(settings: Settings): Promise { .getString(SettingsKey.gitPath) ?.replace(HOME_VAR, homedir().replaceAll("\\", "/")); if (gitExecutable === null || gitExecutable === undefined) { - Logger.log("Error: Git not found."); - - await window.showErrorMessage( - "Git not found. Please install and add to PATH or " + - "set the path to the git executable in global settings." - ); + if (gitVersion !== undefined) { + Logger.log(`Error: Found Git version ${gitVersion} - ` + + `requires ${MIN_GIT_VERSION}.`); + + await window.showErrorMessage( + `Found Git version ${gitVersion}, but requires ${MIN_GIT_VERSION}. ` + + "Please install and add to PATH or " + + "set the path to the git executable in global settings." + ); + } else { + Logger.log("Error: Git not found."); + + await window.showErrorMessage( + "Git not found. Please install and add to PATH or " + + "set the path to the git executable in global settings." + ); + } return undefined; } else { diff --git a/src/utils/requirementsUtil.mts b/src/utils/requirementsUtil.mts index 02f873d..80820b0 100644 --- a/src/utils/requirementsUtil.mts +++ b/src/utils/requirementsUtil.mts @@ -5,6 +5,7 @@ import { SettingsKey, HOME_VAR } from "../settings.mjs"; import { homedir } from "os"; import { downloadGit } from "./downloadGit.mjs"; import Logger, { LoggerSource } from "../logger.mjs"; +import { checkGitVersion, MIN_GIT_VERSION } from "./gitUtil.mjs"; /** * Shows an error message that the requirements for development are not met @@ -37,6 +38,12 @@ export async function checkForGit(settings: Settings): Promise { const git: string | null = await which(gitExe, { nothrow: true }); let isGitInstalled = git !== null; + let gitVersion: string | undefined; + if (git !== null) { + const ret = await checkGitVersion(git); + isGitInstalled = ret[0]; + gitVersion = ret[1]; + } if (!isGitInstalled) { // try to install const gitDownloaded: string | undefined = await downloadGit(); @@ -45,6 +52,17 @@ export async function checkForGit(settings: Settings): Promise { // if git is downloaded set custom git path await settings.updateGlobal(SettingsKey.gitPath, gitDownloaded); isGitInstalled = true; + } else if (gitVersion !== undefined) { + Logger.error( + LoggerSource.requirements, + "Installed Git is too old and a new version could not be downloaded." + ); + void window.showErrorMessage( + "The installation of the Pico SDK requires Git " + + `version ${MIN_GIT_VERSION} or higher ` + + "to be installed and available in the PATH - " + + `you have version ${gitVersion}.` + ); } else { Logger.error( LoggerSource.requirements, From 04dd1aa55e0fe0e8097343d1820bc05819293066 Mon Sep 17 00:00:00 2001 From: William Vinnicombe Date: Tue, 24 Sep 2024 14:01:10 +0100 Subject: [PATCH 2/2] Use regex for parsing git version --- src/utils/gitUtil.mts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/utils/gitUtil.mts b/src/utils/gitUtil.mts index 037a4d6..b073471 100644 --- a/src/utils/gitUtil.mts +++ b/src/utils/gitUtil.mts @@ -21,13 +21,15 @@ export async function checkGitVersion(gitExecutable: string): process.env.ComSpec === "powershell.exe" ? "&" : "" }"${gitExecutable}" version`; const ret = await execAsync(versionCommand) - const stdout = ret.stdout.split(" "); - let gitVersion = stdout[stdout.length-1]; - if (gitVersion.includes("windows")) { - gitVersion = gitVersion.split(".windows")[0]; + const regex = /git version (\d+\.\d+(\.\d+)*)/; + const match = regex.exec(ret.stdout); + if (match && match[1]) { + const gitVersion = match[1]; + + return [compareGe(gitVersion, MIN_GIT_VERSION), gitVersion]; + } else { + return [false, "unknown"]; } - - return [compareGe(gitVersion, MIN_GIT_VERSION), gitVersion]; } /**