Skip to content

Commit

Permalink
refactor: resolveGitDir uses execGit
Browse files Browse the repository at this point in the history
  • Loading branch information
Iiro Jäppinen authored and okonet committed Jun 6, 2019
1 parent 6ac666d commit 9871389
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
23 changes: 23 additions & 0 deletions 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)
}
}
22 changes: 2 additions & 20 deletions 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)
}
Expand Down
11 changes: 9 additions & 2 deletions 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
}
}

0 comments on commit 9871389

Please sign in to comment.