Richer blame info & highlight (wip)

This commit is contained in:
Eric Amodio
2016-08-14 02:03:36 -04:00
parent b08044fc18
commit 03d4cc8597
5 changed files with 157 additions and 61 deletions

View File

@@ -14,48 +14,54 @@ export declare interface IGitBlameLine {
code: string;
}
export function gitRepoPath(cwd) {
const mapper = (input, output) => {
output.push(input.toString().replace(/\r?\n|\r/g, ''))
};
export function gitRepoPath(cwd): Promise<string> {
let data: Array<string> = [];
const capture = input => data.push(input.toString().replace(/\r?\n|\r/g, ''));
const output = () => data[0];
return new Promise<string>((resolve, reject) => {
gitCommand(cwd, mapper, 'rev-parse', '--show-toplevel')
.then(result => resolve(result[0]))
.catch(reason => reject(reason));
});
return gitCommand(cwd, capture, output, 'rev-parse', '--show-toplevel');
// return new Promise<string>((resolve, reject) => {
// gitCommand(cwd, capture, output, 'rev-parse', '--show-toplevel')
// .then(result => resolve(result[0]))
// .catch(reason => reject(reason));
// });
}
//const blameMatcher = /^(.*)\t\((.*)\t(.*)\t(.*?)\)(.*)$/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;
//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): Promise<IGitBlameLine[]> {
const mapper = (input, output) => {
let blame = input.toString();
console.log(fileName, 'Blame:', blame);
let data: string = '';
const capture = input => data += input.toString();
const output = () => {
let lines: Array<IGitBlameLine> = [];
let m: Array<string>;
while ((m = blameMatcher.exec(blame)) != null) {
output.push({
while ((m = blameMatcher.exec(data)) != null) {
lines.push({
sha: m[1],
file: m[2].trim(),
originalLine: parseInt(m[3], 10),
originalLine: parseInt(m[3], 10) - 1,
author: m[4].trim(),
date: new Date(m[5]),
line: parseInt(m[6], 10),
line: parseInt(m[6], 10) - 1,
code: m[7]
});
}
return lines;
};
return gitCommand(dirname(fileName), mapper, 'blame', '-fnw', '--', fileName);
return gitCommand(dirname(fileName), capture, output, 'blame', '-fnw', '--', fileName);
}
export function gitGetVersionFile(repoPath: string, sha: string, source: string): Promise<any> {
const mapper = (input, output) => output.push(input);
let data: Array<any> = [];
const capture = input => data.push(input);
const output = () => data;
return new Promise<string>((resolve, reject) => {
(gitCommand(repoPath, mapper, 'show', `${sha}:${source.replace(/\\/g, '/')}`) as Promise<Array<Buffer>>).then(o => {
(gitCommand(repoPath, capture, output, 'show', `${sha}:${source.replace(/\\/g, '/')}`) as Promise<Array<Buffer>>).then(o => {
let ext = extname(source);
tmp.file({ prefix: `${basename(source, ext)}-${sha}_`, postfix: ext }, (err, destination, fd, cleanupCallback) => {
if (err) {
@@ -79,19 +85,20 @@ export function gitGetVersionFile(repoPath: string, sha: string, source: string)
}
export function gitGetVersionText(repoPath: string, sha: string, source: string): Promise<string> {
const mapper = (input, output) => output.push(input.toString());
let data: Array<string> = [];
const capture = input => data.push(input.toString());
const output = () => data;
return new Promise<string>((resolve, reject) => (gitCommand(repoPath, mapper, 'show', `${sha}:${source.replace(/\\/g, '/')}`) as Promise<Array<string>>).then(o => resolve(o.join())));
return new Promise<string>((resolve, reject) => (gitCommand(repoPath, capture, output, 'show', `${sha}:${source.replace(/\\/g, '/')}`) as Promise<Array<string>>).then(o => resolve(o.join())));
}
function gitCommand(cwd: string, mapper: (input: Buffer, output: Array<any>) => void, ...args): Promise<any> {
function gitCommand(cwd: string, capture: (input: Buffer) => void, output: () => any, ...args): Promise<any> {
return new Promise<any>((resolve, reject) => {
let spawn = require('child_process').spawn;
let process = spawn('git', args, { cwd: cwd });
let output: Array<any> = [];
process.stdout.on('data', data => {
mapper(data, output);
capture(data);
});
let errors: Array<string> = [];
@@ -105,7 +112,11 @@ function gitCommand(cwd: string, mapper: (input: Buffer, output: Array<any>) =>
return;
}
resolve(output);
try {
resolve(output());
} catch (ex) {
reject(ex);
}
});
});
}