From 5c5b469b9c682dc35bdafbd41c2b274cf55b7fd4 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Thu, 9 Dec 2021 01:43:44 -0500 Subject: [PATCH] Consolidates Git version check methods --- src/git/git.ts | 14 +++++++------- src/git/providers/localGitProvider.ts | 26 +++++++++++--------------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/git/git.ts b/src/git/git.ts index 32687c0..625b559 100644 --- a/src/git/git.ts +++ b/src/git/git.ts @@ -6,7 +6,7 @@ import { hrtime } from '@env/hrtime'; import { GlyphChars } from '../constants'; import { Container } from '../container'; import { Logger } from '../logger'; -import { Paths, Strings } from '../system'; +import { Paths, Strings, Versions } from '../system'; import { GitLocation } from './locator'; import { GitRevision } from './models/models'; import { GitBranchParser, GitLogParser, GitReflogParser, GitStashParser, GitTagParser } from './parsers/parsers'; @@ -220,9 +220,9 @@ export namespace Git { return (await gitLocator()).version; } - export async function validateVersion(major: number, minor: number): Promise { - const [gitMajor, gitMinor] = (await version()).split('.'); - return parseInt(gitMajor, 10) >= major && parseInt(gitMinor, 10) >= minor; + export async function isAtLeastVersion(minimum: string): Promise { + const result = Versions.compare(Versions.fromString(await Git.version()), Versions.fromString(minimum)); + return result !== -1; } // Git commands @@ -263,7 +263,7 @@ export namespace Git { const index = params.indexOf('--ignore-revs-file'); if (index !== -1) { // Ensure the version of Git supports the --ignore-revs-file flag, otherwise the blame will fail - let supported = await Git.validateVersion(2, 23); + let supported = await Git.isAtLeastVersion('2.23'); if (supported) { let ignoreRevsFile = params[index + 1]; if (!paths.isAbsolute(ignoreRevsFile)) { @@ -1481,7 +1481,7 @@ export namespace Git { '--branch', '-u', ]; - if (await Git.validateVersion(2, 18)) { + if (await Git.isAtLeastVersion('2.18')) { params.push(`--find-renames${similarityThreshold == null ? '' : `=${similarityThreshold}%`}`); } @@ -1501,7 +1501,7 @@ export namespace Git { const [file, root] = Paths.splitPath(fileName, repoPath); const params = ['status', porcelainVersion >= 2 ? `--porcelain=v${porcelainVersion}` : '--porcelain']; - if (await Git.validateVersion(2, 18)) { + if (await Git.isAtLeastVersion('2.18')) { params.push(`--find-renames${similarityThreshold == null ? '' : `=${similarityThreshold}%`}`); } diff --git a/src/git/providers/localGitProvider.ts b/src/git/providers/localGitProvider.ts index 87b361a..30b5fe9 100644 --- a/src/git/providers/localGitProvider.ts +++ b/src/git/providers/localGitProvider.ts @@ -3149,7 +3149,7 @@ export class LocalGitProvider implements GitProvider, Disposable { @log() async getStatusForFile(repoPath: string, fileName: string): Promise { - const porcelainVersion = (await Git.validateVersion(2, 11)) ? 2 : 1; + const porcelainVersion = (await Git.isAtLeastVersion('2.11')) ? 2 : 1; const data = await Git.status__file(repoPath, fileName, porcelainVersion, { similarityThreshold: this.container.config.advanced.similarityThreshold, @@ -3162,7 +3162,7 @@ export class LocalGitProvider implements GitProvider, Disposable { @log() async getStatusForFiles(repoPath: string, path: string): Promise { - const porcelainVersion = (await Git.validateVersion(2, 11)) ? 2 : 1; + const porcelainVersion = (await Git.isAtLeastVersion('2.11')) ? 2 : 1; const data = await Git.status__file(repoPath, path, porcelainVersion, { similarityThreshold: this.container.config.advanced.similarityThreshold, @@ -3177,7 +3177,7 @@ export class LocalGitProvider implements GitProvider, Disposable { async getStatusForRepo(repoPath: string | undefined): Promise { if (repoPath == null) return undefined; - const porcelainVersion = (await Git.validateVersion(2, 11)) ? 2 : 1; + const porcelainVersion = (await Git.isAtLeastVersion('2.11')) ? 2 : 1; const data = await Git.status(repoPath, porcelainVersion, { similarityThreshold: this.container.config.advanced.similarityThreshold, @@ -3686,19 +3686,19 @@ export class LocalGitProvider implements GitProvider, Disposable { await this.ensureGitVersion( '2.13.2', 'Stashing individual files', - ' Please retry by stashing everything or install a more recent version of Git.', + ' Please retry by stashing everything or install a more recent version of Git and try again.', ); const pathspecs = uris.map(u => `./${Paths.splitPath(u.fsPath, repoPath)[0]}`); const stdinVersion = '2.30.0'; - const stdin = (await this.compareGitVersion(stdinVersion)) !== -1; + const stdin = await Git.isAtLeastVersion(stdinVersion); // If we don't support stdin, then error out if we are over the maximum allowed git cli length if (!stdin && Arrays.countStringLength(pathspecs) > maxGitCliLength) { await this.ensureGitVersion( stdinVersion, `Stashing so many files (${pathspecs.length}) at once`, - ' Please retry by stashing fewer files or install a more recent version of Git.', + ' Please retry by stashing fewer files or install a more recent version of Git and try again.', ); } @@ -3767,15 +3767,11 @@ export class LocalGitProvider implements GitProvider, Disposable { } } - private async compareGitVersion(version: string) { - return Versions.compare(Versions.fromString(await Git.version()), Versions.fromString(version)); - } - private async ensureGitVersion(version: string, prefix: string, suffix: string): Promise { - if ((await this.compareGitVersion(version)) === -1) { - throw new Error( - `${prefix} requires a newer version of Git (>= ${version}) than is currently installed (${await Git.version()}).${suffix}`, - ); - } + if (await Git.isAtLeastVersion(version)) return; + + throw new Error( + `${prefix} requires a newer version of Git (>= ${version}) than is currently installed (${await Git.version()}).${suffix}`, + ); } }