mirror of
https://github.com/ckaczor/vscode-gitlens.git
synced 2026-02-16 18:48:45 -05:00
Fixes #55 - adds fallback for previous git versions
Reverts git version requirement to >= 2.2.0
This commit is contained in:
@@ -146,10 +146,9 @@ async function notifyOnNewGitLensVersion(context: ExtensionContext, version: str
|
|||||||
async function notifyOnUnsupportedGitVersion(context: ExtensionContext, version: string) {
|
async function notifyOnUnsupportedGitVersion(context: ExtensionContext, version: string) {
|
||||||
if (context.globalState.get(WorkspaceState.SuppressGitVersionWarning, false)) return;
|
if (context.globalState.get(WorkspaceState.SuppressGitVersionWarning, false)) return;
|
||||||
|
|
||||||
const [major, minor] = version.split('.');
|
// If git is less than v2.2.0
|
||||||
// If git is less than v2.11.0
|
if (!Git.validateVersion(2, 2)) {
|
||||||
if (parseInt(major, 10) < 2 || parseInt(minor, 10) < 11) {
|
const result = await window.showErrorMessage(`GitLens requires a newer version of Git (>= 2.2.0) than is currently installed (${version}). Please install a more recent version of Git.`, `Don't Show Again`);
|
||||||
const result = await window.showErrorMessage(`GitLens requires a newer version of Git (>= 2.11.0) than is currently installed (${version}). Please install a more recent version of Git.`, `Don't Show Again`);
|
|
||||||
if (result === `Don't Show Again`) {
|
if (result === `Don't Show Again`) {
|
||||||
context.globalState.update(WorkspaceState.SuppressGitVersionWarning, true);
|
context.globalState.update(WorkspaceState.SuppressGitVersionWarning, true);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -109,6 +109,11 @@ export class Git {
|
|||||||
return [ fileName, repoPath ];
|
return [ fileName, repoPath ];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static validateVersion(major: number, minor: number): boolean {
|
||||||
|
const [gitMajor, gitMinor] = git.version.split('.');
|
||||||
|
return (parseInt(gitMajor, 10) >= major && parseInt(gitMinor, 10) >= minor);
|
||||||
|
}
|
||||||
|
|
||||||
// Git commands
|
// Git commands
|
||||||
|
|
||||||
static blame(repoPath: string, fileName: string, sha?: string, startLine?: number, endLine?: number) {
|
static blame(repoPath: string, fileName: string, sha?: string, startLine?: number, endLine?: number) {
|
||||||
@@ -230,15 +235,17 @@ export class Git {
|
|||||||
return gitCommand(repoPath, ...params);
|
return gitCommand(repoPath, ...params);
|
||||||
}
|
}
|
||||||
|
|
||||||
static status(repoPath: string): Promise<string> {
|
static status(repoPath: string, porcelainVersion: number = 1): Promise<string> {
|
||||||
const params = ['status', '--porcelain=v2', '--branch'];
|
const porcelain = porcelainVersion >= 2 ? `--porcelain=v${porcelainVersion}` : '--porcelain';
|
||||||
|
const params = ['status', porcelain, '--branch'];
|
||||||
return gitCommand(repoPath, ...params);
|
return gitCommand(repoPath, ...params);
|
||||||
}
|
}
|
||||||
|
|
||||||
static status_file(repoPath: string, fileName: string): Promise<string> {
|
static status_file(repoPath: string, fileName: string, porcelainVersion: number = 1): Promise<string> {
|
||||||
const [file, root] = Git.splitPath(fileName, repoPath);
|
const [file, root] = Git.splitPath(fileName, repoPath);
|
||||||
|
|
||||||
const params = ['status', '--porcelain=v2', file];
|
const porcelain = porcelainVersion >= 2 ? `--porcelain=v${porcelainVersion}` : '--porcelain';
|
||||||
|
const params = ['status', porcelain, file];
|
||||||
return gitCommand(root, ...params);
|
return gitCommand(root, ...params);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8,9 +8,12 @@ interface IFileStatusEntry {
|
|||||||
originalFileName: string;
|
originalFileName: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const aheadStatusV1Regex = /(?:ahead ([0-9]+))/;
|
||||||
|
const behindStatusV1Regex = /(?:behind ([0-9]+))/;
|
||||||
|
|
||||||
export class GitStatusParser {
|
export class GitStatusParser {
|
||||||
|
|
||||||
static parse(data: string, repoPath: string): IGitStatus {
|
static parse(data: string, repoPath: string, porcelainVersion: number): IGitStatus {
|
||||||
if (!data) return undefined;
|
if (!data) return undefined;
|
||||||
|
|
||||||
const lines = data.split('\n').filter(_ => !!_);
|
const lines = data.split('\n').filter(_ => !!_);
|
||||||
@@ -25,6 +28,42 @@ export class GitStatusParser {
|
|||||||
files: []
|
files: []
|
||||||
} as IGitStatus;
|
} as IGitStatus;
|
||||||
|
|
||||||
|
if (porcelainVersion >= 2) {
|
||||||
|
this._parseV2(lines, repoPath, status);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this._parseV1(lines, repoPath, status);
|
||||||
|
}
|
||||||
|
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static _parseV1(lines: string[], repoPath: string, status: IGitStatus) {
|
||||||
|
let position = -1;
|
||||||
|
while (++position < lines.length) {
|
||||||
|
const line = lines[position];
|
||||||
|
// Header
|
||||||
|
if (line.startsWith('##')) {
|
||||||
|
const lineParts = line.split(' ');
|
||||||
|
[status.branch, status.upstream] = lineParts[1].split('...');
|
||||||
|
if (lineParts.length > 2) {
|
||||||
|
const upstreamStatus = lineParts.slice(2).join(' ');
|
||||||
|
|
||||||
|
const aheadStatus = aheadStatusV1Regex.exec(upstreamStatus);
|
||||||
|
status.state.ahead = +aheadStatus[1] || 0;
|
||||||
|
|
||||||
|
const behindStatus = behindStatusV1Regex.exec(upstreamStatus);
|
||||||
|
status.state.behind = +behindStatus[1] || 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
const entry = this._parseFileEntry(line.substring(0, 2), line.substring(3));
|
||||||
|
status.files.push(new GitStatusFile(repoPath, entry.status, entry.staged, entry.fileName, entry.originalFileName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static _parseV2(lines: string[], repoPath: string, status: IGitStatus) {
|
||||||
let position = -1;
|
let position = -1;
|
||||||
while (++position < lines.length) {
|
while (++position < lines.length) {
|
||||||
const line = lines[position];
|
const line = lines[position];
|
||||||
@@ -71,8 +110,6 @@ export class GitStatusParser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return status;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static _parseFileEntry(rawStatus: string, fileName: string, originalFileName?: string): IFileStatusEntry {
|
private static _parseFileEntry(rawStatus: string, fileName: string, originalFileName?: string): IFileStatusEntry {
|
||||||
|
|||||||
@@ -673,16 +673,20 @@ export class GitService extends Disposable {
|
|||||||
async getStatusForFile(repoPath: string, fileName: string): Promise<GitStatusFile> {
|
async getStatusForFile(repoPath: string, fileName: string): Promise<GitStatusFile> {
|
||||||
Logger.log(`getStatusForFile('${repoPath}', '${fileName}')`);
|
Logger.log(`getStatusForFile('${repoPath}', '${fileName}')`);
|
||||||
|
|
||||||
const data = await Git.status_file(repoPath, fileName);
|
const porcelainVersion = Git.validateVersion(2, 11) ? 2 : 1;
|
||||||
const status = GitStatusParser.parse(data, repoPath);
|
|
||||||
|
const data = await Git.status_file(repoPath, fileName, porcelainVersion);
|
||||||
|
const status = GitStatusParser.parse(data, repoPath, porcelainVersion);
|
||||||
return status && status.files.length && status.files[0];
|
return status && status.files.length && status.files[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
async getStatusForRepo(repoPath: string): Promise<IGitStatus> {
|
async getStatusForRepo(repoPath: string): Promise<IGitStatus> {
|
||||||
Logger.log(`getStatusForRepo('${repoPath}')`);
|
Logger.log(`getStatusForRepo('${repoPath}')`);
|
||||||
|
|
||||||
const data = await Git.status(repoPath);
|
const porcelainVersion = Git.validateVersion(2, 11) ? 2 : 1;
|
||||||
return GitStatusParser.parse(data, repoPath);
|
|
||||||
|
const data = await Git.status(repoPath, porcelainVersion);
|
||||||
|
return GitStatusParser.parse(data, repoPath, porcelainVersion);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getVersionedFile(repoPath: string, fileName: string, sha: string) {
|
async getVersionedFile(repoPath: string, fileName: string, sha: string) {
|
||||||
|
|||||||
@@ -132,9 +132,9 @@ export class RepoStatusQuickPick {
|
|||||||
if (status.upstream && status.state.behind) {
|
if (status.upstream && status.state.behind) {
|
||||||
items.splice(0, 0, new CommandQuickPickItem({
|
items.splice(0, 0, new CommandQuickPickItem({
|
||||||
label: `$(cloud-download)\u00a0 ${status.state.behind} Commit${status.state.behind > 1 ? 's' : ''} behind \u00a0$(git-branch) ${status.upstream}`,
|
label: `$(cloud-download)\u00a0 ${status.state.behind} Commit${status.state.behind > 1 ? 's' : ''} behind \u00a0$(git-branch) ${status.upstream}`,
|
||||||
description: `\u00a0 \u2014 \u00a0\u00a0 shows commits in \u00a0$(git-branch) ${status.upstream} but not \u00a0$(git-branch) ${status.branch} (since \u00a0$(git-commit) ${status.sha.substring(0, 8)})`
|
description: `\u00a0 \u2014 \u00a0\u00a0 shows commits in \u00a0$(git-branch) ${status.upstream} but not \u00a0$(git-branch) ${status.branch}${status.sha ? ` (since \u00a0$(git-commit) ${status.sha.substring(0, 8)})` : ''}`
|
||||||
}, Commands.ShowQuickBranchHistory, [
|
}, Commands.ShowQuickBranchHistory, [
|
||||||
new GitUri(Uri.file(status.repoPath), { fileName: '', repoPath: status.repoPath, sha: `${status.sha}..${status.upstream}` }),
|
new GitUri(Uri.file(status.repoPath), { fileName: '', repoPath: status.repoPath, sha: `${status.sha ? status.sha : 'HEAD'}..${status.upstream}` }),
|
||||||
status.upstream,
|
status.upstream,
|
||||||
0,
|
0,
|
||||||
new CommandQuickPickItem({
|
new CommandQuickPickItem({
|
||||||
|
|||||||
Reference in New Issue
Block a user