Skip to content

Commit 06b350b

Browse files
author
Eric Amodio
committed
Fixes issues with ^ in a sha
Dynamically pads uri index based on need
1 parent 33fe3c5 commit 06b350b

File tree

4 files changed

+58
-51
lines changed

4 files changed

+58
-51
lines changed

src/codeLensProvider.ts

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export class GitBlameCodeLens extends CodeLens {
1414
return this.blame.then(allLines => allLines.slice(this.blameRange.start.line, this.blameRange.end.line + 1));
1515
}
1616

17-
static toUri(lens: GitBlameCodeLens, index: number, line: IGitBlameLine, lines: IGitBlameLine[]): Uri {
18-
return toGitBlameUri(Object.assign({ repoPath: lens.repoPath, index: index, range: lens.blameRange, lines: lines }, line));
17+
static toUri(lens: GitBlameCodeLens, index: number, line: IGitBlameLine, lines: IGitBlameLine[], commits: string[]): Uri {
18+
return toGitBlameUri(Object.assign({ repoPath: lens.repoPath, index: index, range: lens.blameRange, lines: lines, commits: commits }, line));
1919
}
2020
}
2121

@@ -78,46 +78,50 @@ export default class GitCodeLensProvider implements CodeLensProvider {
7878
}
7979

8080
_resolveGitBlameCodeLens(lens: GitBlameCodeLens, token: CancellationToken): Thenable<CodeLens> {
81-
return lens.getBlameLines().then(lines => {
82-
if (!lines.length) {
83-
console.error('No blame lines found', lens);
84-
throw new Error('No blame lines found');
85-
}
86-
87-
let recentLine = lines[0];
88-
89-
let locations: Location[] = [];
90-
if (lines.length > 1) {
91-
let sorted = lines.sort((a, b) => b.date.getTime() - a.date.getTime());
92-
recentLine = sorted[0];
93-
94-
// console.log(lens.fileName, 'Blame lines:', sorted);
95-
96-
let map: Map<string, IGitBlameLine[]> = new Map();
97-
sorted.forEach(l => {
98-
let item = map.get(l.sha);
99-
if (item) {
100-
item.push(l);
101-
} else {
102-
map.set(l.sha, [l]);
103-
}
104-
});
105-
106-
Array.from(map.values()).forEach((lines, i) => {
107-
const uri = GitBlameCodeLens.toUri(lens, i + 1, lines[0], lines);
108-
lines.forEach(l => locations.push(new Location(uri, new Position(l.originalLine, 0))));
109-
});
110-
} else {
111-
locations = [new Location(GitBlameCodeLens.toUri(lens, 1, recentLine, lines), lens.range.start)];
112-
}
113-
114-
lens.command = {
115-
title: `${recentLine.author}, ${moment(recentLine.date).fromNow()}`,
116-
command: Commands.ShowBlameHistory,
117-
arguments: [Uri.file(lens.fileName), lens.range.start, locations]
118-
};
119-
return lens;
120-
}).catch(ex => Promise.reject(ex)); // TODO: Figure out a better way to stop the codelens from appearing
81+
return new Promise<CodeLens>((resolve, reject) => {
82+
lens.getBlameLines().then(lines => {
83+
if (!lines.length) {
84+
console.error('No blame lines found', lens);
85+
reject(null);
86+
return;
87+
}
88+
89+
let recentLine = lines[0];
90+
91+
let locations: Location[] = [];
92+
if (lines.length > 1) {
93+
let sorted = lines.sort((a, b) => b.date.getTime() - a.date.getTime());
94+
recentLine = sorted[0];
95+
96+
// console.log(lens.fileName, 'Blame lines:', sorted);
97+
98+
let map: Map<string, IGitBlameLine[]> = new Map();
99+
sorted.forEach(l => {
100+
let item = map.get(l.sha);
101+
if (item) {
102+
item.push(l);
103+
} else {
104+
map.set(l.sha, [l]);
105+
}
106+
});
107+
108+
const commits = Array.from(map.keys());
109+
Array.from(map.values()).forEach((lines, i) => {
110+
const uri = GitBlameCodeLens.toUri(lens, i + 1, lines[0], lines, commits);
111+
lines.forEach(l => locations.push(new Location(uri, new Position(l.originalLine, 0))));
112+
});
113+
} else {
114+
locations = [new Location(GitBlameCodeLens.toUri(lens, 1, recentLine, lines, [recentLine.sha]), lens.range.start)];
115+
}
116+
117+
lens.command = {
118+
title: `${recentLine.author}, ${moment(recentLine.date).fromNow()}`,
119+
command: Commands.ShowBlameHistory,
120+
arguments: [Uri.file(lens.fileName), lens.range.start, locations]
121+
};
122+
resolve(lens);
123+
});
124+
});//.catch(ex => Promise.reject(ex)); // TODO: Figure out a better way to stop the codelens from appearing
121125
}
122126

123127
_resolveGitHistoryCodeLens(lens: GitHistoryCodeLens, token: CancellationToken): Thenable<CodeLens> {

src/contentProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export default class GitBlameContentProvider implements TextDocumentContentProvi
8989
private _findEditor(uri: Uri): TextEditor {
9090
let uriString = uri.toString();
9191
// TODO: This is a big hack :)
92-
const matcher = (e: any) => (e._documentData && e._documentData._uri && e._documentData._uri.toString()) === uriString;
92+
const matcher = (e: any) => (e && e._documentData && e._documentData._uri && e._documentData._uri.toString()) === uriString;
9393
if (matcher(window.activeTextEditor)) {
9494
return window.activeTextEditor;
9595
}

src/git.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@ export declare interface IGitBlameLine {
1111
author: string;
1212
date: Date;
1313
line: number;
14-
code: string;
14+
//code: string;
1515
}
1616

1717
export function gitRepoPath(cwd) {
1818
return gitCommand(cwd, 'rev-parse', '--show-toplevel').then(data => data.replace(/\r?\n|\r/g, ''));
1919
}
2020

21-
const blameMatcher = /^([0-9a-fA-F]{8})\s([\S]*)\s+([0-9\S]+)\s\((.*)\s([0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2}\s[-|+][0-9]{4})\s+([0-9]+)\)(.*)$/gm;
21+
const blameMatcher = /^([\^0-9a-fA-F]{8})\s([\S]*)\s+([0-9\S]+)\s\((.*)\s([0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2}\s[-|+][0-9]{4})\s+([0-9]+)\)(.*)$/gm;
2222

2323
export function gitBlame(fileName: string) {
24-
return gitCommand(dirname(fileName), 'blame', '-fnw', '--', fileName).then(data => {
24+
console.log('git', 'blame', '-fnw', '--root', '--', fileName);
25+
return gitCommand(dirname(fileName), 'blame', '-fnw', '--root', '--', fileName).then(data => {
2526
let lines: Array<IGitBlameLine> = [];
2627
let m: Array<string>;
2728
while ((m = blameMatcher.exec(data)) != null) {
@@ -31,8 +32,8 @@ export function gitBlame(fileName: string) {
3132
originalLine: parseInt(m[3], 10) - 1,
3233
author: m[4].trim(),
3334
date: new Date(m[5]),
34-
line: parseInt(m[6], 10) - 1,
35-
code: m[7]
35+
line: parseInt(m[6], 10) - 1
36+
//code: m[7]
3637
});
3738
}
3839
return lines;
@@ -41,7 +42,7 @@ export function gitBlame(fileName: string) {
4142

4243
export function gitGetVersionFile(repoPath: string, sha: string, source: string): Promise<string> {
4344
return new Promise<string>((resolve, reject) => {
44-
gitCommand(repoPath, 'show', `${sha}:${source.replace(/\\/g, '/')}`).then(data => {
45+
gitCommand(repoPath, 'show', `${sha.replace('^', '')}:${source.replace(/\\/g, '/')}`).then(data => {
4546
let ext = extname(source);
4647
tmp.file({ prefix: `${basename(source, ext)}-${sha}_`, postfix: ext }, (err, destination, fd, cleanupCallback) => {
4748
if (err) {
@@ -65,6 +66,7 @@ export function gitGetVersionFile(repoPath: string, sha: string, source: string)
6566
}
6667

6768
export function gitGetVersionText(repoPath: string, sha: string, source: string) {
69+
console.log('git', 'show', `${sha}:${source.replace(/\\/g, '/')}`);
6870
return gitCommand(repoPath, 'show', `${sha}:${source.replace(/\\/g, '/')}`);
6971
}
7072

src/gitBlameUri.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ export interface IGitBlameUriData extends IGitBlameLine {
88
repoPath: string,
99
range: Range,
1010
index: number,
11-
lines: IGitBlameLine[]
11+
lines: IGitBlameLine[],
12+
commits: string[]
1213
}
1314

1415
export function toGitBlameUri(data: IGitBlameUriData) {
15-
const pad = n => ("000" + n).slice(-3);
16+
const pad = n => ("0000000" + n).slice(-("" + data.commits.length).length);
1617

1718
let ext = extname(data.file);
1819
let path = `${dirname(data.file)}/${data.sha}: ${basename(data.file, ext)}${ext}`;

0 commit comments

Comments
 (0)