Skip to content

Commit 85d9b50

Browse files
committed
Fixes #381 - Unable to save stash with older Git
1 parent cbd5612 commit 85d9b50

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

src/commands/stashSave.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ export class StashSaveCommand extends Command {
6060
}
6161
catch (ex) {
6262
Logger.error(ex, 'StashSaveCommand');
63+
64+
const msg = ex && ex.message;
65+
if (msg.includes('newer version of Git')) {
66+
return window.showErrorMessage(`Unable to save stash. ${msg}`);
67+
}
6368
return window.showErrorMessage(`Unable to save stash. See output channel for more details`);
6469
}
6570
}

src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ async function migrateSettings(context: ExtensionContext, previousVersion: strin
299299
}
300300

301301
function notifyOnUnsupportedGitVersion(version: string) {
302-
if (GitService.validateGitVersion(2, 2)) return;
302+
if (GitService.compareGitVersion('2.2.0') !== -1) return;
303303

304304
// If git is less than v2.2.0
305305
Messages.showGitVersionUnsupportedErrorMessage(version);

src/gitService.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
import { Iterables, Objects, Strings, TernarySearchTree } from './system';
2+
import { Iterables, Objects, Strings, TernarySearchTree, Versions } from './system';
33
import { ConfigurationChangeEvent, Disposable, Event, EventEmitter, Range, TextEditor, Uri, window, WindowState, workspace, WorkspaceFolder, WorkspaceFoldersChangeEvent } from 'vscode';
44
import { configuration, IRemotesConfig } from './configuration';
55
import { CommandContext, DocumentSchemes, setCommandContext } from './constants';
@@ -1506,6 +1506,9 @@ export class GitService extends Disposable {
15061506
Logger.log(`stashSave('${repoPath}', '${message}', ${uris})`);
15071507

15081508
if (uris === undefined) return Git.stash_save(repoPath, message);
1509+
1510+
GitService.ensureGitVersion('2.13.2', 'Stashing individual files');
1511+
15091512
const pathspecs = uris.map(u => Git.splitPath(u.fsPath, repoPath)[0]);
15101513
return Git.stash_push(repoPath, pathspecs, message);
15111514
}
@@ -1560,8 +1563,14 @@ export class GitService extends Disposable {
15601563
: sha;
15611564
}
15621565

1563-
static validateGitVersion(major: number, minor: number): boolean {
1564-
const [gitMajor, gitMinor] = this.getGitVersion().split('.');
1565-
return (parseInt(gitMajor, 10) >= major && parseInt(gitMinor, 10) >= minor);
1566+
static compareGitVersion(version: string, throwIfLessThan?: Error) {
1567+
return Versions.compare(Versions.fromString(this.getGitVersion()), Versions.fromString(version));
1568+
}
1569+
1570+
static ensureGitVersion(version: string, feature: string): void {
1571+
const gitVersion = this.getGitVersion();
1572+
if (Versions.compare(Versions.fromString(gitVersion), Versions.fromString(version)) === -1) {
1573+
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.`);
1574+
}
15661575
}
1567-
}
1576+
}

src/system/version.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
'use strict';
22

33
export namespace Versions {
4+
declare type VersionComparisonResult = -1 | 0 | 1;
5+
46
export interface Version {
57
major: number;
68
minor: number;
79
patch: number;
810
pre?: string;
911
}
1012

11-
export function compare(v1: Version, v2: Version): number {
13+
export function compare(v1: Version, v2: Version): VersionComparisonResult {
1214
if (v1.major > v2.major) return 1;
1315
if (v1.major < v2.major) return -1;
1416

@@ -21,7 +23,7 @@ export namespace Versions {
2123
if (v1.pre === undefined && v2.pre !== undefined) return 1;
2224
if (v1.pre !== undefined && v2.pre === undefined) return -1;
2325

24-
if (v1.pre !== undefined && v2.pre !== undefined) return v1.pre.localeCompare(v2.pre);
26+
if (v1.pre !== undefined && v2.pre !== undefined) return v1.pre.localeCompare(v2.pre) as VersionComparisonResult;
2527

2628
return 0;
2729
}

0 commit comments

Comments
 (0)