mirror of
https://github.com/ckaczor/vscode-gitlens.git
synced 2026-02-09 01:32:40 -05:00
Fixes issues with file renames
And other git related edge cases
This commit is contained in:
@@ -10,7 +10,7 @@ export class GitBlameCodeLens extends CodeLens {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getBlame(): Promise<IGitBlame> {
|
getBlame(): Promise<IGitBlame> {
|
||||||
return this.blameProvider.getBlameForRange(this.fileName, this.blameRange);
|
return this.blameProvider.getBlameForRange(this.fileName, this.blameProvider.repoPath, this.blameRange);
|
||||||
}
|
}
|
||||||
|
|
||||||
static toUri(lens: GitBlameCodeLens, repoPath: string, commit: IGitBlameCommit, index: number, commitCount: number): Uri {
|
static toUri(lens: GitBlameCodeLens, repoPath: string, commit: IGitBlameCommit, index: number, commitCount: number): Uri {
|
||||||
@@ -32,7 +32,7 @@ export default class GitCodeLensProvider implements CodeLensProvider {
|
|||||||
constructor(context: ExtensionContext, public blameProvider: GitBlameProvider) { }
|
constructor(context: ExtensionContext, public blameProvider: GitBlameProvider) { }
|
||||||
|
|
||||||
provideCodeLenses(document: TextDocument, token: CancellationToken): CodeLens[] | Thenable<CodeLens[]> {
|
provideCodeLenses(document: TextDocument, token: CancellationToken): CodeLens[] | Thenable<CodeLens[]> {
|
||||||
this.blameProvider.blameFile(document.fileName);
|
this.blameProvider.blameFile(document.fileName, this.blameProvider.repoPath);
|
||||||
|
|
||||||
return (commands.executeCommand(VsCodeCommands.ExecuteDocumentSymbolProvider, document.uri) as Promise<SymbolInformation[]>).then(symbols => {
|
return (commands.executeCommand(VsCodeCommands.ExecuteDocumentSymbolProvider, document.uri) as Promise<SymbolInformation[]>).then(symbols => {
|
||||||
let lenses: CodeLens[] = [];
|
let lenses: CodeLens[] = [];
|
||||||
@@ -67,11 +67,11 @@ export default class GitCodeLensProvider implements CodeLensProvider {
|
|||||||
|
|
||||||
const line = document.lineAt(symbol.location.range.start);
|
const line = document.lineAt(symbol.location.range.start);
|
||||||
|
|
||||||
let startChar = line.text.indexOf(symbol.name); //line.firstNonWhitespaceCharacterIndex;
|
let startChar = line.text.search(`\\b${symbol.name}\\b`); //line.firstNonWhitespaceCharacterIndex;
|
||||||
if (startChar === -1) {
|
if (startChar === -1) {
|
||||||
startChar = line.firstNonWhitespaceCharacterIndex;
|
startChar = line.firstNonWhitespaceCharacterIndex;
|
||||||
} else {
|
} else {
|
||||||
startChar += Math.floor(symbol.name.length / 2) - 1;
|
startChar += Math.floor(symbol.name.length / 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
lenses.push(new GitBlameCodeLens(this.blameProvider, document.fileName, symbol.location.range, line.range.with(new Position(line.range.start.line, startChar))));
|
lenses.push(new GitBlameCodeLens(this.blameProvider, document.fileName, symbol.location.range, line.range.with(new Position(line.range.start.line, startChar))));
|
||||||
@@ -96,7 +96,7 @@ export default class GitCodeLensProvider implements CodeLensProvider {
|
|||||||
lens.command = {
|
lens.command = {
|
||||||
title: `${recentCommit.author}, ${moment(recentCommit.date).fromNow()}`,
|
title: `${recentCommit.author}, ${moment(recentCommit.date).fromNow()}`,
|
||||||
command: Commands.ShowBlameHistory,
|
command: Commands.ShowBlameHistory,
|
||||||
arguments: [Uri.file(lens.fileName), lens.blameRange, lens.range.start] //, lens.locations]
|
arguments: [Uri.file(lens.fileName), lens.blameRange, lens.range.start]
|
||||||
};
|
};
|
||||||
resolve(lens);
|
resolve(lens);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ export default class GitBlameContentProvider implements TextDocumentContentProvi
|
|||||||
|
|
||||||
//const editor = this._findEditor(Uri.file(join(data.repoPath, data.file)));
|
//const editor = this._findEditor(Uri.file(join(data.repoPath, data.file)));
|
||||||
|
|
||||||
return gitGetVersionText(data.fileName, this.blameProvider.repoPath, data.sha).then(text => {
|
return gitGetVersionText(data.originalFileName || data.fileName, this.blameProvider.repoPath, data.sha).then(text => {
|
||||||
this.update(uri);
|
this.update(uri);
|
||||||
|
|
||||||
// TODO: This only works on the first load -- not after since it is cached
|
// TODO: This only works on the first load -- not after since it is cached
|
||||||
@@ -89,7 +89,7 @@ export default class GitBlameContentProvider implements TextDocumentContentProvi
|
|||||||
let editor = this._findEditor(uri);
|
let editor = this._findEditor(uri);
|
||||||
if (editor) {
|
if (editor) {
|
||||||
clearInterval(handle);
|
clearInterval(handle);
|
||||||
this.blameProvider.getBlameForShaRange(data.fileName, data.sha, data.range).then(blame => {
|
this.blameProvider.getBlameForShaRange(data.fileName, this.blameProvider.repoPath, data.sha, data.range).then(blame => {
|
||||||
if (blame.lines.length) {
|
if (blame.lines.length) {
|
||||||
editor.setDecorations(this._blameDecoration, blame.lines.map(l => {
|
editor.setDecorations(this._blameDecoration, blame.lines.map(l => {
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ export function activate(context: ExtensionContext) {
|
|||||||
if (!uri) return;
|
if (!uri) return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return blameProvider.getBlameLocations(uri.path, blameRange).then(locations => {
|
return blameProvider.getBlameLocations(uri.path, blameProvider.repoPath, blameRange).then(locations => {
|
||||||
return commands.executeCommand(VsCodeCommands.ShowReferences, uri, range, locations);
|
return commands.executeCommand(VsCodeCommands.ShowReferences, uri, range, locations);
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
|
|||||||
27
src/git.ts
27
src/git.ts
@@ -1,16 +1,25 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
import {basename, dirname, extname, relative} from 'path';
|
import {basename, dirname, extname, isAbsolute, relative} from 'path';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as tmp from 'tmp';
|
import * as tmp from 'tmp';
|
||||||
import {spawnPromise} from 'spawn-rx';
|
import {spawnPromise} from 'spawn-rx';
|
||||||
|
|
||||||
|
export function gitNormalizePath(fileName: string, repoPath: string) {
|
||||||
|
fileName = fileName.replace(/\\/g, '/');
|
||||||
|
return isAbsolute(fileName) ? relative(repoPath, fileName) : fileName;
|
||||||
|
}
|
||||||
|
|
||||||
export function gitRepoPath(cwd) {
|
export function gitRepoPath(cwd) {
|
||||||
return gitCommand(cwd, 'rev-parse', '--show-toplevel').then(data => data.replace(/\r?\n|\r/g, ''));
|
return gitCommand(cwd, 'rev-parse', '--show-toplevel').then(data => data.replace(/\r?\n|\r/g, ''));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function gitBlame(fileName: string) {
|
export function gitBlame(fileName: string, repoPath: string) {
|
||||||
|
fileName = gitNormalizePath(fileName, repoPath);
|
||||||
|
|
||||||
console.log('git', 'blame', '-fnw', '--root', '--', fileName);
|
console.log('git', 'blame', '-fnw', '--root', '--', fileName);
|
||||||
return gitCommand(dirname(fileName), 'blame', '-fnw', '--root', '--', fileName);
|
return gitCommand(repoPath, 'blame', '-fnw', '--root', '--', fileName);
|
||||||
|
// .then(s => { console.log(s); return s; })
|
||||||
|
// .catch(ex => console.error(ex));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function gitGetVersionFile(fileName: string, repoPath: string, sha: string) {
|
export function gitGetVersionFile(fileName: string, repoPath: string, sha: string) {
|
||||||
@@ -39,13 +48,13 @@ export function gitGetVersionFile(fileName: string, repoPath: string, sha: strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function gitGetVersionText(fileName: string, repoPath: string, sha: string) {
|
export function gitGetVersionText(fileName: string, repoPath: string, sha: string) {
|
||||||
const gitArg = normalizeArgument(fileName, repoPath, sha);
|
fileName = gitNormalizePath(fileName, repoPath);
|
||||||
console.log('git', 'show', gitArg);
|
sha = sha.replace('^', '');
|
||||||
return gitCommand(dirname(fileName), 'show', gitArg);
|
|
||||||
}
|
|
||||||
|
|
||||||
function normalizeArgument(fileName: string, repoPath: string, sha: string) {
|
console.log('git', 'show', `${sha}:${fileName}`);
|
||||||
return `${sha.replace('^', '')}:${relative(repoPath, fileName.replace(/\\/g, '/'))}`;
|
return gitCommand(repoPath, 'show', `${sha}:${fileName}`);
|
||||||
|
// .then(s => { console.log(s); return s; })
|
||||||
|
// .catch(ex => console.error(ex));
|
||||||
}
|
}
|
||||||
|
|
||||||
function gitCommand(cwd: string, ...args) {
|
function gitCommand(cwd: string, ...args) {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import {Disposable, ExtensionContext, Location, Position, Range, Uri, workspace} from 'vscode';
|
import {Disposable, ExtensionContext, Location, Position, Range, Uri, workspace} from 'vscode';
|
||||||
import {DocumentSchemes, WorkspaceState} from './constants';
|
import {DocumentSchemes, WorkspaceState} from './constants';
|
||||||
import {gitBlame} from './git';
|
import {gitBlame, gitNormalizePath} from './git';
|
||||||
import {basename, dirname, extname, join} from 'path';
|
import {basename, dirname, extname} from 'path';
|
||||||
import * as moment from 'moment';
|
import * as moment from 'moment';
|
||||||
import * as _ from 'lodash';
|
import * as _ from 'lodash';
|
||||||
|
|
||||||
@@ -29,46 +29,53 @@ export default class GitBlameProvider extends Disposable {
|
|||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
blameFile(fileName: string) {
|
blameFile(fileName: string, repoPath: string) {
|
||||||
|
fileName = gitNormalizePath(fileName, repoPath);
|
||||||
|
|
||||||
let blame = this._files.get(fileName);
|
let blame = this._files.get(fileName);
|
||||||
if (blame !== undefined) return blame;
|
if (blame !== undefined) return blame;
|
||||||
|
|
||||||
blame = gitBlame(fileName)
|
blame = gitBlame(fileName, repoPath)
|
||||||
.then(data => {
|
.then(data => {
|
||||||
const commits: Map<string, IGitBlameCommit> = new Map();
|
const commits: Map<string, IGitBlameCommit> = new Map();
|
||||||
const lines: Array<IGitBlameLine> = [];
|
const lines: Array<IGitBlameLine> = [];
|
||||||
let m: Array<string>;
|
let m: Array<string>;
|
||||||
while ((m = blameMatcher.exec(data)) != null) {
|
while ((m = blameMatcher.exec(data)) != null) {
|
||||||
let sha = m[1];
|
let sha = m[1];
|
||||||
|
|
||||||
if (!commits.has(sha)) {
|
if (!commits.has(sha)) {
|
||||||
commits.set(sha, {
|
commits.set(sha, {
|
||||||
sha,
|
sha,
|
||||||
fileName: m[2].trim(),
|
fileName: fileName,
|
||||||
author: m[4].trim(),
|
author: m[4].trim(),
|
||||||
date: new Date(m[5])
|
date: new Date(m[5])
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
lines.push({
|
const line: IGitBlameLine = {
|
||||||
sha,
|
sha,
|
||||||
|
line: parseInt(m[6], 10) - 1,
|
||||||
originalLine: parseInt(m[3], 10) - 1,
|
originalLine: parseInt(m[3], 10) - 1,
|
||||||
line: parseInt(m[6], 10) - 1
|
|
||||||
//code: m[7]
|
//code: m[7]
|
||||||
});
|
}
|
||||||
|
|
||||||
|
let file = m[2].trim();
|
||||||
|
if (!fileName.toLowerCase().endsWith(file.toLowerCase())) {
|
||||||
|
line.originalFileName = file;
|
||||||
|
}
|
||||||
|
|
||||||
|
lines.push(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
return { commits, lines };
|
return { commits, lines };
|
||||||
});
|
});
|
||||||
// .catch(ex => {
|
|
||||||
// console.error(ex);
|
|
||||||
// });
|
|
||||||
|
|
||||||
this._files.set(fileName, blame);
|
this._files.set(fileName, blame);
|
||||||
return blame;
|
return blame;
|
||||||
}
|
}
|
||||||
|
|
||||||
getBlameForRange(fileName: string, range: Range): Promise<IGitBlame> {
|
getBlameForRange(fileName: string, repoPath: string, range: Range): Promise<IGitBlame> {
|
||||||
return this.blameFile(fileName).then(blame => {
|
return this.blameFile(fileName, repoPath).then(blame => {
|
||||||
if (!blame.lines.length) return blame;
|
if (!blame.lines.length) return blame;
|
||||||
|
|
||||||
const lines = blame.lines.slice(range.start.line, range.end.line + 1);
|
const lines = blame.lines.slice(range.start.line, range.end.line + 1);
|
||||||
@@ -79,8 +86,8 @@ export default class GitBlameProvider extends Disposable {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
getBlameForShaRange(fileName: string, sha: string, range: Range): Promise<{commit: IGitBlameCommit, lines: IGitBlameLine[]}> {
|
getBlameForShaRange(fileName: string, repoPath: string, sha: string, range: Range): Promise<{commit: IGitBlameCommit, lines: IGitBlameLine[]}> {
|
||||||
return this.blameFile(fileName).then(blame => {
|
return this.blameFile(fileName, repoPath).then(blame => {
|
||||||
return {
|
return {
|
||||||
commit: blame.commits.get(sha),
|
commit: blame.commits.get(sha),
|
||||||
lines: blame.lines.slice(range.start.line, range.end.line + 1).filter(l => l.sha === sha)
|
lines: blame.lines.slice(range.start.line, range.end.line + 1).filter(l => l.sha === sha)
|
||||||
@@ -88,8 +95,8 @@ export default class GitBlameProvider extends Disposable {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
getBlameLocations(fileName: string, range: Range) {
|
getBlameLocations(fileName: string, repoPath: string, range: Range) {
|
||||||
return this.getBlameForRange(fileName, range).then(blame => {
|
return this.getBlameForRange(fileName, repoPath, range).then(blame => {
|
||||||
const commitCount = blame.commits.size;
|
const commitCount = blame.commits.size;
|
||||||
|
|
||||||
const locations: Array<Location> = [];
|
const locations: Array<Location> = [];
|
||||||
@@ -99,7 +106,10 @@ export default class GitBlameProvider extends Disposable {
|
|||||||
const uri = GitBlameProvider.toBlameUri(this.repoPath, c, range, i + 1, commitCount);
|
const uri = GitBlameProvider.toBlameUri(this.repoPath, c, range, i + 1, commitCount);
|
||||||
blame.lines
|
blame.lines
|
||||||
.filter(l => l.sha === c.sha)
|
.filter(l => l.sha === c.sha)
|
||||||
.forEach(l => locations.push(new Location(uri, new Position(l.originalLine, 0))));
|
.forEach(l => locations.push(new Location(l.originalFileName
|
||||||
|
? GitBlameProvider.toBlameUri(this.repoPath, c, range, i + 1, commitCount, l.originalFileName)
|
||||||
|
: uri,
|
||||||
|
new Position(l.originalLine, 0))));
|
||||||
});
|
});
|
||||||
|
|
||||||
return locations;
|
return locations;
|
||||||
@@ -110,12 +120,16 @@ export default class GitBlameProvider extends Disposable {
|
|||||||
this._files.delete(fileName);
|
this._files.delete(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
static toBlameUri(repoPath: string, commit: IGitBlameCommit, range: Range, index: number, commitCount: number) {
|
static toBlameUri(repoPath: string, commit: IGitBlameCommit, range: Range, index: number, commitCount: number, originalFileName?: string) {
|
||||||
const pad = n => ("0000000" + n).slice(-("" + commitCount).length);
|
const pad = n => ("0000000" + n).slice(-("" + commitCount).length);
|
||||||
|
|
||||||
const ext = extname(commit.fileName);
|
const fileName = originalFileName || commit.fileName;
|
||||||
const path = `${dirname(commit.fileName)}/${commit.sha}: ${basename(commit.fileName, ext)}${ext}`;
|
const ext = extname(fileName);
|
||||||
const data: IGitBlameUriData = { fileName: join(repoPath, commit.fileName), sha: commit.sha, range: range, index: index };
|
const path = `${dirname(fileName)}/${commit.sha}: ${basename(fileName, ext)}${ext}`;
|
||||||
|
const data: IGitBlameUriData = { fileName: commit.fileName, sha: commit.sha, range: range, index: index };
|
||||||
|
if (originalFileName) {
|
||||||
|
data.originalFileName = originalFileName;
|
||||||
|
}
|
||||||
// NOTE: Need to specify an index here, since I can't control the sort order -- just alphabetic or by file location
|
// NOTE: Need to specify an index here, since I can't control the sort order -- just alphabetic or by file location
|
||||||
return Uri.parse(`${DocumentSchemes.GitBlame}:${pad(index)}. ${commit.author}, ${moment(commit.date).format('MMM D, YYYY hh:MMa')} - ${path}?${JSON.stringify(data)}`);
|
return Uri.parse(`${DocumentSchemes.GitBlame}:${pad(index)}. ${commit.author}, ${moment(commit.date).format('MMM D, YYYY hh:MMa')} - ${path}?${JSON.stringify(data)}`);
|
||||||
}
|
}
|
||||||
@@ -140,12 +154,14 @@ export interface IGitBlameCommit {
|
|||||||
}
|
}
|
||||||
export interface IGitBlameLine {
|
export interface IGitBlameLine {
|
||||||
sha: string;
|
sha: string;
|
||||||
originalLine: number;
|
|
||||||
line: number;
|
line: number;
|
||||||
|
originalLine: number;
|
||||||
|
originalFileName?: string;
|
||||||
code?: string;
|
code?: string;
|
||||||
}
|
}
|
||||||
export interface IGitBlameUriData {
|
export interface IGitBlameUriData {
|
||||||
fileName: string,
|
fileName: string,
|
||||||
|
originalFileName?: string;
|
||||||
sha: string,
|
sha: string,
|
||||||
range: Range,
|
range: Range,
|
||||||
index: number
|
index: number
|
||||||
|
|||||||
Reference in New Issue
Block a user