Selaa lähdekoodia

Consolidates Git version check methods

main
Eric Amodio 3 vuotta sitten
vanhempi
commit
5c5b469b9c
2 muutettua tiedostoa jossa 18 lisäystä ja 22 poistoa
  1. +7
    -7
      src/git/git.ts
  2. +11
    -15
      src/git/providers/localGitProvider.ts

+ 7
- 7
src/git/git.ts Näytä tiedosto

@ -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<boolean> {
const [gitMajor, gitMinor] = (await version()).split('.');
return parseInt(gitMajor, 10) >= major && parseInt(gitMinor, 10) >= minor;
export async function isAtLeastVersion(minimum: string): Promise<boolean> {
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}%`}`);
}

+ 11
- 15
src/git/providers/localGitProvider.ts Näytä tiedosto

@ -3149,7 +3149,7 @@ export class LocalGitProvider implements GitProvider, Disposable {
@log()
async getStatusForFile(repoPath: string, fileName: string): Promise<GitStatusFile | undefined> {
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<GitStatusFile[] | undefined> {
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<GitStatus | undefined> {
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<void> {
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}`,
);
}
}

Ladataan…
Peruuta
Tallenna