diff --git a/src/execGit.js b/src/execGit.js new file mode 100644 index 000000000..b07049b40 --- /dev/null +++ b/src/execGit.js @@ -0,0 +1,23 @@ +'use strict' + +const debug = require('debug')('lint-staged:git') +const execa = require('execa') +const path = require('path') + +function getAbsolutePath(dir) { + return path.isAbsolute(dir) ? dir : path.resolve(dir) +} + +module.exports = async function execGit(cmd, options = {}) { + const cwd = options.cwd || process.cwd() + debug('Running git command', cmd) + try { + const { stdout } = await execa('git', [].concat(cmd), { + ...options, + cwd: getAbsolutePath(cwd) + }) + return stdout + } catch (err) { + throw new Error(err) + } +} diff --git a/src/gitWorkflow.js b/src/gitWorkflow.js index 3907e65dc..14f24998e 100644 --- a/src/gitWorkflow.js +++ b/src/gitWorkflow.js @@ -1,33 +1,15 @@ 'use strict' -const path = require('path') -const execa = require('execa') const gStatus = require('g-status') const del = require('del') const debug = require('debug')('lint-staged:git') +const execGit = require('./execGit') + let workingCopyTree = null let indexTree = null let formattedIndexTree = null -function getAbsolutePath(dir) { - return path.isAbsolute(dir) ? dir : path.resolve(dir) -} - -async function execGit(cmd, options) { - const { cwd } = options - debug('Running git command', cmd) - try { - const { stdout } = await execa('git', [].concat(cmd), { - ...options, - cwd: getAbsolutePath(cwd) - }) - return stdout - } catch (err) { - throw new Error(err) - } -} - async function writeTree(options) { return execGit(['write-tree'], options) } diff --git a/src/resolveGitDir.js b/src/resolveGitDir.js index cbd6316b3..09170c019 100644 --- a/src/resolveGitDir.js +++ b/src/resolveGitDir.js @@ -1,7 +1,14 @@ 'use strict' -const execa = require('execa') +const execGit = require('./execGit') +const printErrors = require('./printErrors') module.exports = async function resolveGitDir() { - return (await execa('git', ['rev-parse', '--show-toplevel'])).stdout + try { + const gitDir = await execGit(['rev-parse', '--show-toplevel']) + return gitDir + } catch (error) { + printErrors(error) + return null + } }