Fixes issues with ^ in a sha

Dynamically pads uri index based on need
This commit is contained in:
Eric Amodio
2016-08-26 22:43:55 -04:00
parent 33fe3c55f7
commit 06b350bc82
4 changed files with 52 additions and 45 deletions

View File

@@ -14,8 +14,8 @@ export class GitBlameCodeLens extends CodeLens {
return this.blame.then(allLines => allLines.slice(this.blameRange.start.line, this.blameRange.end.line + 1)); return this.blame.then(allLines => allLines.slice(this.blameRange.start.line, this.blameRange.end.line + 1));
} }
static toUri(lens: GitBlameCodeLens, index: number, line: IGitBlameLine, lines: IGitBlameLine[]): Uri { static toUri(lens: GitBlameCodeLens, index: number, line: IGitBlameLine, lines: IGitBlameLine[], commits: string[]): Uri {
return toGitBlameUri(Object.assign({ repoPath: lens.repoPath, index: index, range: lens.blameRange, lines: lines }, line)); return toGitBlameUri(Object.assign({ repoPath: lens.repoPath, index: index, range: lens.blameRange, lines: lines, commits: commits }, line));
} }
} }
@@ -78,10 +78,12 @@ export default class GitCodeLensProvider implements CodeLensProvider {
} }
_resolveGitBlameCodeLens(lens: GitBlameCodeLens, token: CancellationToken): Thenable<CodeLens> { _resolveGitBlameCodeLens(lens: GitBlameCodeLens, token: CancellationToken): Thenable<CodeLens> {
return lens.getBlameLines().then(lines => { return new Promise<CodeLens>((resolve, reject) => {
lens.getBlameLines().then(lines => {
if (!lines.length) { if (!lines.length) {
console.error('No blame lines found', lens); console.error('No blame lines found', lens);
throw new Error('No blame lines found'); reject(null);
return;
} }
let recentLine = lines[0]; let recentLine = lines[0];
@@ -103,12 +105,13 @@ export default class GitCodeLensProvider implements CodeLensProvider {
} }
}); });
const commits = Array.from(map.keys());
Array.from(map.values()).forEach((lines, i) => { Array.from(map.values()).forEach((lines, i) => {
const uri = GitBlameCodeLens.toUri(lens, i + 1, lines[0], lines); const uri = GitBlameCodeLens.toUri(lens, i + 1, lines[0], lines, commits);
lines.forEach(l => locations.push(new Location(uri, new Position(l.originalLine, 0)))); lines.forEach(l => locations.push(new Location(uri, new Position(l.originalLine, 0))));
}); });
} else { } else {
locations = [new Location(GitBlameCodeLens.toUri(lens, 1, recentLine, lines), lens.range.start)]; locations = [new Location(GitBlameCodeLens.toUri(lens, 1, recentLine, lines, [recentLine.sha]), lens.range.start)];
} }
lens.command = { lens.command = {
@@ -116,8 +119,9 @@ export default class GitCodeLensProvider implements CodeLensProvider {
command: Commands.ShowBlameHistory, command: Commands.ShowBlameHistory,
arguments: [Uri.file(lens.fileName), lens.range.start, locations] arguments: [Uri.file(lens.fileName), lens.range.start, locations]
}; };
return lens; resolve(lens);
}).catch(ex => Promise.reject(ex)); // TODO: Figure out a better way to stop the codelens from appearing });
});//.catch(ex => Promise.reject(ex)); // TODO: Figure out a better way to stop the codelens from appearing
} }
_resolveGitHistoryCodeLens(lens: GitHistoryCodeLens, token: CancellationToken): Thenable<CodeLens> { _resolveGitHistoryCodeLens(lens: GitHistoryCodeLens, token: CancellationToken): Thenable<CodeLens> {

View File

@@ -89,7 +89,7 @@ export default class GitBlameContentProvider implements TextDocumentContentProvi
private _findEditor(uri: Uri): TextEditor { private _findEditor(uri: Uri): TextEditor {
let uriString = uri.toString(); let uriString = uri.toString();
// TODO: This is a big hack :) // TODO: This is a big hack :)
const matcher = (e: any) => (e._documentData && e._documentData._uri && e._documentData._uri.toString()) === uriString; const matcher = (e: any) => (e && e._documentData && e._documentData._uri && e._documentData._uri.toString()) === uriString;
if (matcher(window.activeTextEditor)) { if (matcher(window.activeTextEditor)) {
return window.activeTextEditor; return window.activeTextEditor;
} }

View File

@@ -11,17 +11,18 @@ export declare interface IGitBlameLine {
author: string; author: string;
date: Date; date: Date;
line: number; line: number;
code: string; //code: string;
} }
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, ''));
} }
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; 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;
export function gitBlame(fileName: string) { export function gitBlame(fileName: string) {
return gitCommand(dirname(fileName), 'blame', '-fnw', '--', fileName).then(data => { console.log('git', 'blame', '-fnw', '--root', '--', fileName);
return gitCommand(dirname(fileName), 'blame', '-fnw', '--root', '--', fileName).then(data => {
let lines: Array<IGitBlameLine> = []; let lines: Array<IGitBlameLine> = [];
let m: Array<string>; let m: Array<string>;
while ((m = blameMatcher.exec(data)) != null) { while ((m = blameMatcher.exec(data)) != null) {
@@ -31,8 +32,8 @@ export function gitBlame(fileName: string) {
originalLine: parseInt(m[3], 10) - 1, originalLine: parseInt(m[3], 10) - 1,
author: m[4].trim(), author: m[4].trim(),
date: new Date(m[5]), date: new Date(m[5]),
line: parseInt(m[6], 10) - 1, line: parseInt(m[6], 10) - 1
code: m[7] //code: m[7]
}); });
} }
return lines; return lines;
@@ -41,7 +42,7 @@ export function gitBlame(fileName: string) {
export function gitGetVersionFile(repoPath: string, sha: string, source: string): Promise<string> { export function gitGetVersionFile(repoPath: string, sha: string, source: string): Promise<string> {
return new Promise<string>((resolve, reject) => { return new Promise<string>((resolve, reject) => {
gitCommand(repoPath, 'show', `${sha}:${source.replace(/\\/g, '/')}`).then(data => { gitCommand(repoPath, 'show', `${sha.replace('^', '')}:${source.replace(/\\/g, '/')}`).then(data => {
let ext = extname(source); let ext = extname(source);
tmp.file({ prefix: `${basename(source, ext)}-${sha}_`, postfix: ext }, (err, destination, fd, cleanupCallback) => { tmp.file({ prefix: `${basename(source, ext)}-${sha}_`, postfix: ext }, (err, destination, fd, cleanupCallback) => {
if (err) { if (err) {
@@ -65,6 +66,7 @@ export function gitGetVersionFile(repoPath: string, sha: string, source: string)
} }
export function gitGetVersionText(repoPath: string, sha: string, source: string) { export function gitGetVersionText(repoPath: string, sha: string, source: string) {
console.log('git', 'show', `${sha}:${source.replace(/\\/g, '/')}`);
return gitCommand(repoPath, 'show', `${sha}:${source.replace(/\\/g, '/')}`); return gitCommand(repoPath, 'show', `${sha}:${source.replace(/\\/g, '/')}`);
} }

View File

@@ -8,11 +8,12 @@ export interface IGitBlameUriData extends IGitBlameLine {
repoPath: string, repoPath: string,
range: Range, range: Range,
index: number, index: number,
lines: IGitBlameLine[] lines: IGitBlameLine[],
commits: string[]
} }
export function toGitBlameUri(data: IGitBlameUriData) { export function toGitBlameUri(data: IGitBlameUriData) {
const pad = n => ("000" + n).slice(-3); const pad = n => ("0000000" + n).slice(-("" + data.commits.length).length);
let ext = extname(data.file); let ext = extname(data.file);
let path = `${dirname(data.file)}/${data.sha}: ${basename(data.file, ext)}${ext}`; let path = `${dirname(data.file)}/${data.sha}: ${basename(data.file, ext)}${ext}`;