From 85d9b500a63bbc385a1d2cd0276a1d3f3f7ef859 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Fri, 25 May 2018 20:17:15 -0400 Subject: [PATCH] Fixes #381 - Unable to save stash with older Git --- src/commands/stashSave.ts | 5 +++++ src/extension.ts | 2 +- src/gitService.ts | 19 ++++++++++++++----- src/system/version.ts | 6 ++++-- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/commands/stashSave.ts b/src/commands/stashSave.ts index b0d4f65..62e83aa 100644 --- a/src/commands/stashSave.ts +++ b/src/commands/stashSave.ts @@ -60,6 +60,11 @@ export class StashSaveCommand extends Command { } catch (ex) { Logger.error(ex, 'StashSaveCommand'); + + const msg = ex && ex.message; + if (msg.includes('newer version of Git')) { + return window.showErrorMessage(`Unable to save stash. ${msg}`); + } return window.showErrorMessage(`Unable to save stash. See output channel for more details`); } } diff --git a/src/extension.ts b/src/extension.ts index 23148a4..323697b 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -299,7 +299,7 @@ async function migrateSettings(context: ExtensionContext, previousVersion: strin } function notifyOnUnsupportedGitVersion(version: string) { - if (GitService.validateGitVersion(2, 2)) return; + if (GitService.compareGitVersion('2.2.0') !== -1) return; // If git is less than v2.2.0 Messages.showGitVersionUnsupportedErrorMessage(version); diff --git a/src/gitService.ts b/src/gitService.ts index 6453b21..601e210 100644 --- a/src/gitService.ts +++ b/src/gitService.ts @@ -1,5 +1,5 @@ 'use strict'; -import { Iterables, Objects, Strings, TernarySearchTree } from './system'; +import { Iterables, Objects, Strings, TernarySearchTree, Versions } from './system'; import { ConfigurationChangeEvent, Disposable, Event, EventEmitter, Range, TextEditor, Uri, window, WindowState, workspace, WorkspaceFolder, WorkspaceFoldersChangeEvent } from 'vscode'; import { configuration, IRemotesConfig } from './configuration'; import { CommandContext, DocumentSchemes, setCommandContext } from './constants'; @@ -1506,6 +1506,9 @@ export class GitService extends Disposable { Logger.log(`stashSave('${repoPath}', '${message}', ${uris})`); if (uris === undefined) return Git.stash_save(repoPath, message); + + GitService.ensureGitVersion('2.13.2', 'Stashing individual files'); + const pathspecs = uris.map(u => Git.splitPath(u.fsPath, repoPath)[0]); return Git.stash_push(repoPath, pathspecs, message); } @@ -1560,8 +1563,14 @@ export class GitService extends Disposable { : sha; } - static validateGitVersion(major: number, minor: number): boolean { - const [gitMajor, gitMinor] = this.getGitVersion().split('.'); - return (parseInt(gitMajor, 10) >= major && parseInt(gitMinor, 10) >= minor); + static compareGitVersion(version: string, throwIfLessThan?: Error) { + return Versions.compare(Versions.fromString(this.getGitVersion()), Versions.fromString(version)); + } + + static ensureGitVersion(version: string, feature: string): void { + const gitVersion = this.getGitVersion(); + if (Versions.compare(Versions.fromString(gitVersion), Versions.fromString(version)) === -1) { + throw new Error(`${feature} requires a newer version of Git (>= ${version}) than is currently installed (${gitVersion}). Please install a more recent version of Git to use this GitLens feature.`); + } } -} \ No newline at end of file +} diff --git a/src/system/version.ts b/src/system/version.ts index 47fab99..f9bb4fa 100644 --- a/src/system/version.ts +++ b/src/system/version.ts @@ -1,6 +1,8 @@ 'use strict'; export namespace Versions { + declare type VersionComparisonResult = -1 | 0 | 1; + export interface Version { major: number; minor: number; @@ -8,7 +10,7 @@ export namespace Versions { pre?: string; } - export function compare(v1: Version, v2: Version): number { + export function compare(v1: Version, v2: Version): VersionComparisonResult { if (v1.major > v2.major) return 1; if (v1.major < v2.major) return -1; @@ -21,7 +23,7 @@ export namespace Versions { if (v1.pre === undefined && v2.pre !== undefined) return 1; if (v1.pre !== undefined && v2.pre === undefined) return -1; - if (v1.pre !== undefined && v2.pre !== undefined) return v1.pre.localeCompare(v2.pre); + if (v1.pre !== undefined && v2.pre !== undefined) return v1.pre.localeCompare(v2.pre) as VersionComparisonResult; return 0; }