Skip to content

Commit 92beca2

Browse files
author
Eric Amodio
committed
Fixes issues with file renames
And other git related edge cases
1 parent 0ccac8d commit 92beca2

File tree

5 files changed

+65
-40
lines changed

5 files changed

+65
-40
lines changed

src/codeLensProvider.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class GitBlameCodeLens extends CodeLens {
1010
}
1111

1212
getBlame(): Promise<IGitBlame> {
13-
return this.blameProvider.getBlameForRange(this.fileName, this.blameRange);
13+
return this.blameProvider.getBlameForRange(this.fileName, this.blameProvider.repoPath, this.blameRange);
1414
}
1515

1616
static toUri(lens: GitBlameCodeLens, repoPath: string, commit: IGitBlameCommit, index: number, commitCount: number): Uri {
@@ -32,7 +32,7 @@ export default class GitCodeLensProvider implements CodeLensProvider {
3232
constructor(context: ExtensionContext, public blameProvider: GitBlameProvider) { }
3333

3434
provideCodeLenses(document: TextDocument, token: CancellationToken): CodeLens[] | Thenable<CodeLens[]> {
35-
this.blameProvider.blameFile(document.fileName);
35+
this.blameProvider.blameFile(document.fileName, this.blameProvider.repoPath);
3636

3737
return (commands.executeCommand(VsCodeCommands.ExecuteDocumentSymbolProvider, document.uri) as Promise<SymbolInformation[]>).then(symbols => {
3838
let lenses: CodeLens[] = [];
@@ -67,11 +67,11 @@ export default class GitCodeLensProvider implements CodeLensProvider {
6767

6868
const line = document.lineAt(symbol.location.range.start);
6969

70-
let startChar = line.text.indexOf(symbol.name); //line.firstNonWhitespaceCharacterIndex;
70+
let startChar = line.text.search(`\\b${symbol.name}\\b`); //line.firstNonWhitespaceCharacterIndex;
7171
if (startChar === -1) {
7272
startChar = line.firstNonWhitespaceCharacterIndex;
7373
} else {
74-
startChar += Math.floor(symbol.name.length / 2) - 1;
74+
startChar += Math.floor(symbol.name.length / 2);
7575
}
7676

7777
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 {
9696
lens.command = {
9797
title: `${recentCommit.author}, ${moment(recentCommit.date).fromNow()}`,
9898
command: Commands.ShowBlameHistory,
99-
arguments: [Uri.file(lens.fileName), lens.blameRange, lens.range.start] //, lens.locations]
99+
arguments: [Uri.file(lens.fileName), lens.blameRange, lens.range.start]
100100
};
101101
resolve(lens);
102102
});

src/contentProvider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export default class GitBlameContentProvider implements TextDocumentContentProvi
5252

5353
//const editor = this._findEditor(Uri.file(join(data.repoPath, data.file)));
5454

55-
return gitGetVersionText(data.fileName, this.blameProvider.repoPath, data.sha).then(text => {
55+
return gitGetVersionText(data.originalFileName || data.fileName, this.blameProvider.repoPath, data.sha).then(text => {
5656
this.update(uri);
5757

5858
// TODO: This only works on the first load -- not after since it is cached
@@ -89,7 +89,7 @@ export default class GitBlameContentProvider implements TextDocumentContentProvi
8989
let editor = this._findEditor(uri);
9090
if (editor) {
9191
clearInterval(handle);
92-
this.blameProvider.getBlameForShaRange(data.fileName, data.sha, data.range).then(blame => {
92+
this.blameProvider.getBlameForShaRange(data.fileName, this.blameProvider.repoPath, data.sha, data.range).then(blame => {
9393
if (blame.lines.length) {
9494
editor.setDecorations(this._blameDecoration, blame.lines.map(l => {
9595
return {

src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function activate(context: ExtensionContext) {
3636
if (!uri) return;
3737
}
3838

39-
return blameProvider.getBlameLocations(uri.path, blameRange).then(locations => {
39+
return blameProvider.getBlameLocations(uri.path, blameProvider.repoPath, blameRange).then(locations => {
4040
return commands.executeCommand(VsCodeCommands.ShowReferences, uri, range, locations);
4141
});
4242
}));

src/git.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
'use strict';
2-
import {basename, dirname, extname, relative} from 'path';
2+
import {basename, dirname, extname, isAbsolute, relative} from 'path';
33
import * as fs from 'fs';
44
import * as tmp from 'tmp';
55
import {spawnPromise} from 'spawn-rx';
66

7+
export function gitNormalizePath(fileName: string, repoPath: string) {
8+
fileName = fileName.replace(/\\/g, '/');
9+
return isAbsolute(fileName) ? relative(repoPath, fileName) : fileName;
10+
}
11+
712
export function gitRepoPath(cwd) {
813
return gitCommand(cwd, 'rev-parse', '--show-toplevel').then(data => data.replace(/\r?\n|\r/g, ''));
914
}
1015

11-
export function gitBlame(fileName: string) {
16+
export function gitBlame(fileName: string, repoPath: string) {
17+
fileName = gitNormalizePath(fileName, repoPath);
18+
1219
console.log('git', 'blame', '-fnw', '--root', '--', fileName);
13-
return gitCommand(dirname(fileName), 'blame', '-fnw', '--root', '--', fileName);
20+
return gitCommand(repoPath, 'blame', '-fnw', '--root', '--', fileName);
21+
// .then(s => { console.log(s); return s; })
22+
// .catch(ex => console.error(ex));
1423
}
1524

1625
export function gitGetVersionFile(fileName: string, repoPath: string, sha: string) {
@@ -39,13 +48,13 @@ export function gitGetVersionFile(fileName: string, repoPath: string, sha: strin
3948
}
4049

4150
export function gitGetVersionText(fileName: string, repoPath: string, sha: string) {
42-
const gitArg = normalizeArgument(fileName, repoPath, sha);
43-
console.log('git', 'show', gitArg);
44-
return gitCommand(dirname(fileName), 'show', gitArg);
45-
}
51+
fileName = gitNormalizePath(fileName, repoPath);
52+
sha = sha.replace('^', '');
4653

47-
function normalizeArgument(fileName: string, repoPath: string, sha: string) {
48-
return `${sha.replace('^', '')}:${relative(repoPath, fileName.replace(/\\/g, '/'))}`;
54+
console.log('git', 'show', `${sha}:${fileName}`);
55+
return gitCommand(repoPath, 'show', `${sha}:${fileName}`);
56+
// .then(s => { console.log(s); return s; })
57+
// .catch(ex => console.error(ex));
4958
}
5059

5160
function gitCommand(cwd: string, ...args) {

src/gitBlameProvider.ts

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Disposable, ExtensionContext, Location, Position, Range, Uri, workspace} from 'vscode';
22
import {DocumentSchemes, WorkspaceState} from './constants';
3-
import {gitBlame} from './git';
4-
import {basename, dirname, extname, join} from 'path';
3+
import {gitBlame, gitNormalizePath} from './git';
4+
import {basename, dirname, extname} from 'path';
55
import * as moment from 'moment';
66
import * as _ from 'lodash';
77

@@ -29,46 +29,53 @@ export default class GitBlameProvider extends Disposable {
2929
super.dispose();
3030
}
3131

32-
blameFile(fileName: string) {
32+
blameFile(fileName: string, repoPath: string) {
33+
fileName = gitNormalizePath(fileName, repoPath);
34+
3335
let blame = this._files.get(fileName);
3436
if (blame !== undefined) return blame;
3537

36-
blame = gitBlame(fileName)
38+
blame = gitBlame(fileName, repoPath)
3739
.then(data => {
3840
const commits: Map<string, IGitBlameCommit> = new Map();
3941
const lines: Array<IGitBlameLine> = [];
4042
let m: Array<string>;
4143
while ((m = blameMatcher.exec(data)) != null) {
4244
let sha = m[1];
45+
4346
if (!commits.has(sha)) {
4447
commits.set(sha, {
4548
sha,
46-
fileName: m[2].trim(),
49+
fileName: fileName,
4750
author: m[4].trim(),
4851
date: new Date(m[5])
4952
});
5053
}
5154

52-
lines.push({
55+
const line: IGitBlameLine = {
5356
sha,
57+
line: parseInt(m[6], 10) - 1,
5458
originalLine: parseInt(m[3], 10) - 1,
55-
line: parseInt(m[6], 10) - 1
5659
//code: m[7]
57-
});
60+
}
61+
62+
let file = m[2].trim();
63+
if (!fileName.toLowerCase().endsWith(file.toLowerCase())) {
64+
line.originalFileName = file;
65+
}
66+
67+
lines.push(line);
5868
}
5969

6070
return { commits, lines };
6171
});
62-
// .catch(ex => {
63-
// console.error(ex);
64-
// });
6572

6673
this._files.set(fileName, blame);
6774
return blame;
6875
}
6976

70-
getBlameForRange(fileName: string, range: Range): Promise<IGitBlame> {
71-
return this.blameFile(fileName).then(blame => {
77+
getBlameForRange(fileName: string, repoPath: string, range: Range): Promise<IGitBlame> {
78+
return this.blameFile(fileName, repoPath).then(blame => {
7279
if (!blame.lines.length) return blame;
7380

7481
const lines = blame.lines.slice(range.start.line, range.end.line + 1);
@@ -79,17 +86,17 @@ export default class GitBlameProvider extends Disposable {
7986
});
8087
}
8188

82-
getBlameForShaRange(fileName: string, sha: string, range: Range): Promise<{commit: IGitBlameCommit, lines: IGitBlameLine[]}> {
83-
return this.blameFile(fileName).then(blame => {
89+
getBlameForShaRange(fileName: string, repoPath: string, sha: string, range: Range): Promise<{commit: IGitBlameCommit, lines: IGitBlameLine[]}> {
90+
return this.blameFile(fileName, repoPath).then(blame => {
8491
return {
8592
commit: blame.commits.get(sha),
8693
lines: blame.lines.slice(range.start.line, range.end.line + 1).filter(l => l.sha === sha)
8794
};
8895
});
8996
}
9097

91-
getBlameLocations(fileName: string, range: Range) {
92-
return this.getBlameForRange(fileName, range).then(blame => {
98+
getBlameLocations(fileName: string, repoPath: string, range: Range) {
99+
return this.getBlameForRange(fileName, repoPath, range).then(blame => {
93100
const commitCount = blame.commits.size;
94101

95102
const locations: Array<Location> = [];
@@ -99,7 +106,10 @@ export default class GitBlameProvider extends Disposable {
99106
const uri = GitBlameProvider.toBlameUri(this.repoPath, c, range, i + 1, commitCount);
100107
blame.lines
101108
.filter(l => l.sha === c.sha)
102-
.forEach(l => locations.push(new Location(uri, new Position(l.originalLine, 0))));
109+
.forEach(l => locations.push(new Location(l.originalFileName
110+
? GitBlameProvider.toBlameUri(this.repoPath, c, range, i + 1, commitCount, l.originalFileName)
111+
: uri,
112+
new Position(l.originalLine, 0))));
103113
});
104114

105115
return locations;
@@ -110,12 +120,16 @@ export default class GitBlameProvider extends Disposable {
110120
this._files.delete(fileName);
111121
}
112122

113-
static toBlameUri(repoPath: string, commit: IGitBlameCommit, range: Range, index: number, commitCount: number) {
123+
static toBlameUri(repoPath: string, commit: IGitBlameCommit, range: Range, index: number, commitCount: number, originalFileName?: string) {
114124
const pad = n => ("0000000" + n).slice(-("" + commitCount).length);
115125

116-
const ext = extname(commit.fileName);
117-
const path = `${dirname(commit.fileName)}/${commit.sha}: ${basename(commit.fileName, ext)}${ext}`;
118-
const data: IGitBlameUriData = { fileName: join(repoPath, commit.fileName), sha: commit.sha, range: range, index: index };
126+
const fileName = originalFileName || commit.fileName;
127+
const ext = extname(fileName);
128+
const path = `${dirname(fileName)}/${commit.sha}: ${basename(fileName, ext)}${ext}`;
129+
const data: IGitBlameUriData = { fileName: commit.fileName, sha: commit.sha, range: range, index: index };
130+
if (originalFileName) {
131+
data.originalFileName = originalFileName;
132+
}
119133
// NOTE: Need to specify an index here, since I can't control the sort order -- just alphabetic or by file location
120134
return Uri.parse(`${DocumentSchemes.GitBlame}:${pad(index)}. ${commit.author}, ${moment(commit.date).format('MMM D, YYYY hh:MMa')} - ${path}?${JSON.stringify(data)}`);
121135
}
@@ -140,12 +154,14 @@ export interface IGitBlameCommit {
140154
}
141155
export interface IGitBlameLine {
142156
sha: string;
143-
originalLine: number;
144157
line: number;
158+
originalLine: number;
159+
originalFileName?: string;
145160
code?: string;
146161
}
147162
export interface IGitBlameUriData {
148163
fileName: string,
164+
originalFileName?: string;
149165
sha: string,
150166
range: Range,
151167
index: number

0 commit comments

Comments
 (0)