From 4e3e1c31d7f051885449dafea160f6569d565012 Mon Sep 17 00:00:00 2001 From: Andrey Okonetchnikov Date: Mon, 29 Oct 2018 21:01:18 +0100 Subject: [PATCH] fix(git): Use resolveGitDir to resolve to .git for git commands When options aren't specified, instead of using `process.cwd()` use `resolveGitDir()`. Fixes #514 --- src/gitWorkflow.js | 3 ++- test/gitWorkflow.spec.js | 10 +++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/gitWorkflow.js b/src/gitWorkflow.js index 02a677fbd..c63de1ab0 100644 --- a/src/gitWorkflow.js +++ b/src/gitWorkflow.js @@ -5,6 +5,7 @@ const execa = require('execa') const gStatus = require('g-status') const del = require('del') const debug = require('debug')('lint-staged:git') +const resolveGitDir = require('./resolveGitDir') let workingCopyTree = null let indexTree = null @@ -15,7 +16,7 @@ function getAbsolutePath(dir) { } async function execGit(cmd, options) { - const cwd = options && options.cwd ? options.cwd : process.cwd() + const cwd = options && options.cwd ? options.cwd : resolveGitDir() debug('Running git command', cmd) try { const { stdout } = await execa('git', [].concat(cmd), { diff --git a/test/gitWorkflow.spec.js b/test/gitWorkflow.spec.js index a03a4d2ed..155780932 100644 --- a/test/gitWorkflow.spec.js +++ b/test/gitWorkflow.spec.js @@ -3,28 +3,28 @@ import path from 'path' import tmp from 'tmp' import execa from 'execa' -import gitflow from '../src/gitWorkflow' +import { execGit } from '../src/gitWorkflow' tmp.setGracefulCleanup() describe('gitWorkflow', () => { describe('execGit', () => { - it('should execute git in cwd if working copy is not specified', async () => { - await gitflow.execGit(['init', 'param']) + it('should execute git in process.cwd if working copy is not specified', async () => { + await execGit(['init', 'param']) expect(execa).toHaveBeenCalledWith('git', ['init', 'param'], { cwd: path.resolve(process.cwd()) }) }) it('should execute git in a given working copy', async () => { - await gitflow.execGit(['init', 'param'], { cwd: 'test/__fixtures__' }) + await execGit(['init', 'param'], { cwd: 'test/__fixtures__' }) expect(execa).toHaveBeenCalledWith('git', ['init', 'param'], { cwd: path.resolve(process.cwd(), 'test', '__fixtures__') }) }) it('should work with relative paths', async () => { - await gitflow.execGit(['init', 'param'], { + await execGit(['init', 'param'], { cwd: 'test/__fixtures__' }) expect(execa).toHaveBeenCalledWith('git', ['init', 'param'], {