Adds support for custom git installation locations

Also gracefully deals with the times when git isn't in the PATH
This commit is contained in:
Eric Amodio
2016-11-10 18:33:28 -05:00
parent f4410be30a
commit 7ace9cb152
8 changed files with 142 additions and 47 deletions

View File

@@ -1,28 +1,30 @@
'use strict';
import { findGitPath, IGit } from './gitLocator';
import { Logger } from '../logger';
import { spawnPromise } from 'spawn-rx';
import * as fs from 'fs';
import * as path from 'path';
import * as tmp from 'tmp';
import { spawnPromise } from 'spawn-rx';
import { Logger } from '../logger';
export * from './gitEnrichment';
export * from './enrichers/blameParserEnricher';
export * from './enrichers/logParserEnricher';
let git: IGit;
const UncommittedRegex = /^[0]+$/;
async function gitCommand(cwd: string, ...args: any[]) {
try {
const s = await spawnPromise('git', args, { cwd: cwd });
Logger.log('git', ...args, cwd);
const s = await spawnPromise(git.path, args, { cwd: cwd });
Logger.log('git', ...args, ` cwd='${cwd}'`);
return s;
}
catch (ex) {
const msg = ex && ex.toString();
if (msg && (msg.includes('is outside repository') || msg.includes('no such path'))) {
Logger.warn('git', ...args, cwd, msg && msg.replace(/\r?\n|\r/g, ' '));
if (msg && (msg.includes('Not a git repository') || msg.includes('is outside repository') || msg.includes('no such path'))) {
Logger.warn('git', ...args, ` cwd='${cwd}'`, msg && `\n ${msg.replace(/\r?\n|\r/g, ' ')}`);
} else {
Logger.error('git', ...args, cwd, msg && msg.replace(/\r?\n|\r/g, ' '));
Logger.error('git', ...args, ` cwd='${cwd}'`, msg && `\n ${msg.replace(/\r?\n|\r/g, ' ')}`);
}
throw ex;
}
@@ -52,8 +54,12 @@ export default class Git {
}
}
static repoPath(cwd: string) {
return gitCommand(cwd, 'rev-parse', '--show-toplevel').then(data => data.replace(/\r?\n|\r/g, '').replace(/\\/g, '/'));
static async repoPath(cwd: string, gitPath?: string) {
git = await findGitPath(gitPath);
let data = await gitCommand(cwd, 'rev-parse', '--show-toplevel');
data = data.replace(/\r?\n|\r/g, '').replace(/\\/g, '/');
return data;
}
static blame(format: GitBlameFormat, fileName: string, sha?: string, repoPath?: string) {

74
src/git/gitLocator.ts Normal file
View File

@@ -0,0 +1,74 @@
import { spawnPromise } from 'spawn-rx';
import * as path from 'path';
export interface IGit {
path: string;
version: string;
}
function parseVersion(raw: string): string {
return raw.replace(/^git version /, '');
}
async function findSpecificGit(path: string): Promise<IGit> {
const version = await spawnPromise(path, ['--version']);
return {
path,
version: parseVersion(version.trim())
};
}
async function findGitDarwin(): Promise<IGit> {
try {
let path = await spawnPromise('which', ['git']);
path = path.replace(/^\s+|\s+$/g, '');
if (path !== '/usr/bin/git') {
return findSpecificGit(path);
}
try {
await spawnPromise('xcode-select', ['-p']);
return findSpecificGit(path);
}
catch (ex) {
if (ex.code === 2) {
return Promise.reject('Unable to find git');
}
return findSpecificGit(path);
}
}
catch (ex) {
return Promise.reject('Unable to find git');
}
}
function findSystemGitWin32(basePath: string): Promise<IGit> {
if (!basePath) return Promise.reject('Unable to find git');
return findSpecificGit(path.join(basePath, 'Git', 'cmd', 'git.exe'));
}
function findGitWin32(): Promise<IGit> {
return findSystemGitWin32(process.env['ProgramW6432'])
.then(null, () => findSystemGitWin32(process.env['ProgramFiles(x86)']))
.then(null, () => findSystemGitWin32(process.env['ProgramFiles']))
.then(null, () => findSpecificGit('git'));
}
export async function findGitPath(path?: string): Promise<IGit> {
try {
return await findSpecificGit(path || 'git');
}
catch (ex) {
try {
switch (process.platform) {
case 'darwin': return await findGitDarwin();
case 'win32': return await findGitWin32();
default: return Promise.reject('Unable to find git');
}
}
catch (ex) {
return Promise.reject('Unable to find git');
}
}
}