Reworked git access

Cleaned up the blame hightlights (wip)
This commit is contained in:
Eric Amodio
2016-08-26 18:27:38 -04:00
parent 03d4cc8597
commit 1576c08fa8
8 changed files with 125 additions and 143 deletions

View File

@@ -1,8 +1,8 @@
'use strict';
import {spawn} from 'child_process';
import {basename, dirname, extname} from 'path';
import * as fs from 'fs';
import * as tmp from 'tmp';
import {spawnPromise} from 'spawn-rx';
export declare interface IGitBlameLine {
sha: string;
@@ -14,28 +14,14 @@ export declare interface IGitBlameLine {
code: string;
}
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 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));
// });
export function gitRepoPath(cwd) {
return gitCommand(cwd, 'rev-parse', '--show-toplevel').then(data => data.replace(/\r?\n|\r/g, ''));
}
//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;
export function gitBlame(fileName: string): Promise<IGitBlameLine[]> {
let data: string = '';
const capture = input => data += input.toString();
const output = () => {
export function gitBlame(fileName: string) {
return gitCommand(dirname(fileName), 'blame', '-fnw', '--', fileName).then(data => {
let lines: Array<IGitBlameLine> = [];
let m: Array<string>;
while ((m = blameMatcher.exec(data)) != null) {
@@ -50,18 +36,12 @@ export function gitBlame(fileName: string): Promise<IGitBlameLine[]> {
});
}
return lines;
};
return gitCommand(dirname(fileName), capture, output, 'blame', '-fnw', '--', fileName);
});
}
export function gitGetVersionFile(repoPath: string, sha: string, source: string): Promise<any> {
let data: Array<any> = [];
const capture = input => data.push(input);
const output = () => data;
export function gitGetVersionFile(repoPath: string, sha: string, source: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
(gitCommand(repoPath, capture, output, 'show', `${sha}:${source.replace(/\\/g, '/')}`) as Promise<Array<Buffer>>).then(o => {
gitCommand(repoPath, 'show', `${sha}:${source.replace(/\\/g, '/')}`).then(data => {
let ext = extname(source);
tmp.file({ prefix: `${basename(source, ext)}-${sha}_`, postfix: ext }, (err, destination, fd, cleanupCallback) => {
if (err) {
@@ -72,7 +52,7 @@ export function gitGetVersionFile(repoPath: string, sha: string, source: string)
console.log("File: ", destination);
console.log("Filedescriptor: ", fd);
fs.appendFile(destination, o.join(), err => {
fs.appendFile(destination, data, err => {
if (err) {
reject(err);
return;
@@ -84,39 +64,10 @@ export function gitGetVersionFile(repoPath: string, sha: string, source: string)
});
}
export function gitGetVersionText(repoPath: string, sha: string, source: string): Promise<string> {
let data: Array<string> = [];
const capture = input => data.push(input.toString());
const output = () => data;
return new Promise<string>((resolve, reject) => (gitCommand(repoPath, capture, output, 'show', `${sha}:${source.replace(/\\/g, '/')}`) as Promise<Array<string>>).then(o => resolve(o.join())));
export function gitGetVersionText(repoPath: string, sha: string, source: string) {
return gitCommand(repoPath, 'show', `${sha}:${source.replace(/\\/g, '/')}`);
}
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 });
process.stdout.on('data', data => {
capture(data);
});
let errors: Array<string> = [];
process.stderr.on('data', err => {
errors.push(err.toString());
});
process.on('close', (exitCode, exitSignal) => {
if (exitCode && errors.length) {
reject(errors.toString());
return;
}
try {
resolve(output());
} catch (ex) {
reject(ex);
}
});
});
function gitCommand(cwd: string, ...args) {
return spawnPromise('git', args, { cwd: cwd });
}