Reworks git abstraction and acccess

Adds commands module
Adds git commit message to blame hover decorator
This commit is contained in:
Eric Amodio
2016-08-31 17:20:53 -04:00
parent 9964ea691b
commit 0e064f15c7
7 changed files with 181 additions and 130 deletions

View File

@@ -4,59 +4,70 @@ import * as fs from 'fs';
import * as tmp from 'tmp';
import {spawnPromise} from 'spawn-rx';
export function gitNormalizePath(fileName: string, repoPath: string) {
fileName = fileName.replace(/\\/g, '/');
return isAbsolute(fileName) ? relative(repoPath, fileName) : fileName;
function gitCommand(cwd: string, ...args) {
return spawnPromise('git', args, { cwd: cwd });
}
export function gitRepoPath(cwd) {
return gitCommand(cwd, 'rev-parse', '--show-toplevel').then(data => data.replace(/\r?\n|\r/g, ''));
}
export default class Git {
static normalizePath(fileName: string, repoPath: string) {
fileName = fileName.replace(/\\/g, '/');
return isAbsolute(fileName) ? relative(repoPath, fileName) : fileName;
}
export function gitBlame(fileName: string, repoPath: string) {
fileName = gitNormalizePath(fileName, repoPath);
static repoPath(cwd: string) {
return gitCommand(cwd, 'rev-parse', '--show-toplevel').then(data => data.replace(/\r?\n|\r/g, ''));
}
console.log('git', 'blame', '-fnw', '--root', '--', fileName);
return gitCommand(repoPath, 'blame', '-fnw', '--root', '--', fileName);
// .then(s => { console.log(s); return s; })
// .catch(ex => console.error(ex));
}
static blame(fileName: string, repoPath: string) {
fileName = Git.normalizePath(fileName, repoPath);
export function gitGetVersionFile(fileName: string, repoPath: string, sha: string) {
return new Promise<string>((resolve, reject) => {
gitGetVersionText(fileName, repoPath, sha).then(data => {
let ext = extname(fileName);
tmp.file({ prefix: `${basename(fileName, ext)}-${sha}_`, postfix: ext }, (err, destination, fd, cleanupCallback) => {
if (err) {
reject(err);
return;
}
console.log('git', 'blame', '-fnw', '--root', '--', fileName);
return gitCommand(repoPath, 'blame', '-fnw', '--root', '--', fileName);
// .then(s => { console.log(s); return s; })
// .catch(ex => console.error(ex));
}
console.log("File: ", destination);
console.log("Filedescriptor: ", fd);
fs.appendFile(destination, data, err => {
static getVersionedFile(fileName: string, repoPath: string, sha: string) {
return new Promise<string>((resolve, reject) => {
Git.getVersionedFileText(fileName, repoPath, sha).then(data => {
let ext = extname(fileName);
tmp.file({ prefix: `${basename(fileName, ext)}-${sha}_`, postfix: ext }, (err, destination, fd, cleanupCallback) => {
if (err) {
reject(err);
return;
}
resolve(destination);
console.log("File: ", destination);
console.log("Filedescriptor: ", fd);
fs.appendFile(destination, data, err => {
if (err) {
reject(err);
return;
}
resolve(destination);
});
});
});
});
});
}
}
export function gitGetVersionText(fileName: string, repoPath: string, sha: string) {
fileName = gitNormalizePath(fileName, repoPath);
sha = sha.replace('^', '');
static getVersionedFileText(fileName: string, repoPath: string, sha: string) {
fileName = Git.normalizePath(fileName, repoPath);
sha = sha.replace('^', '');
console.log('git', 'show', `${sha}:${fileName}`);
return gitCommand(repoPath, 'show', `${sha}:${fileName}`);
// .then(s => { console.log(s); return s; })
// .catch(ex => console.error(ex));
}
console.log('git', 'show', `${sha}:${fileName}`);
return gitCommand(repoPath, 'show', `${sha}:${fileName}`);
// .then(s => { console.log(s); return s; })
// .catch(ex => console.error(ex));
}
function gitCommand(cwd: string, ...args) {
return spawnPromise('git', args, { cwd: cwd });
static getCommitMessage(sha: string, repoPath: string) {
sha = sha.replace('^', '');
console.log('git', 'show', '-s', '--format=%B', sha);
return gitCommand(repoPath, 'show', '-s', '--format=%B', sha);
// .then(s => { console.log(s); return s; })
// .catch(ex => console.error(ex));
}
}