Skip to content

Commit

Permalink
Check Git version during install (#656)
Browse files Browse the repository at this point in the history
  • Loading branch information
typicode committed Jan 21, 2020
1 parent ef5b965 commit 3d3c9cc
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -51,7 +51,9 @@
"dependencies": {
"chalk": "^3.0.0",
"ci-info": "^2.0.0",
"compare-versions": "^3.5.1",
"cosmiconfig": "^6.0.0",
"find-versions": "^3.2.0",
"opencollective-postinstall": "^2.0.2",
"pkg-dir": "^4.2.0",
"please-upgrade-node": "^3.2.0",
Expand Down
26 changes: 26 additions & 0 deletions src/installer/__tests__/checkGitVersion.ts
@@ -0,0 +1,26 @@
jest.mock('child_process')

import cp from 'child_process'
import { checkGitVersion } from '../checkGitVersion'

describe('checkGitVersion', (): void => {
it('should throw an error if version <2.13.0', (): void => {
// eslint-disable-next-line
// @ts-ignore
cp.spawnSync.mockReturnValue({
status: 0,
stdout: Buffer.from('git version 2.12.0')
})
expect(() => checkGitVersion()).toThrowError()
})

it('should not throw an error if version >=2.13.0', (): void => {
// eslint-disable-next-line
// @ts-ignore
cp.spawnSync.mockReturnValue({
status: 0,
stdout: Buffer.from('git version 2.14.0')
})
expect(() => checkGitVersion()).not.toThrowError()
})
})
7 changes: 6 additions & 1 deletion src/installer/bin.ts
Expand Up @@ -7,6 +7,7 @@ import { checkGitDirEnv } from '../checkGitDirEnv'
import { debug } from '../debug'
import { install, uninstall } from './'
import { gitRevParse } from './gitRevParse'
import { checkGitVersion } from './checkGitVersion'

// Skip install if HUSKY_SKIP_INSTALL is true
function checkSkipInstallEnv(): void {
Expand Down Expand Up @@ -78,7 +79,11 @@ function run(): void {

debug(`Current working directory is ${process.cwd()}`)

if (action === 'install') checkSkipInstallEnv()
if (action === 'install') {
checkSkipInstallEnv()
checkGitVersion()
}

const INIT_CWD = getInitCwdEnv()
const userPkgDir = getUserPkgDir(INIT_CWD)
checkGitDirEnv()
Expand Down
17 changes: 17 additions & 0 deletions src/installer/checkGitVersion.ts
@@ -0,0 +1,17 @@
import cp = require('child_process')
import findVersions from 'find-versions'
import compareVersions from 'compare-versions'

export function checkGitVersion(): void {
const { status, stderr, stdout } = cp.spawnSync('git', ['--version'])

if (status !== 0) {
throw new Error(stderr.toString())
}

const [version] = findVersions(stdout.toString())

if (compareVersions(version, '2.13.0') === -1) {
throw new Error(`Husky requires Git >=2.13.0. Got v${version}.`)
}
}

0 comments on commit 3d3c9cc

Please sign in to comment.