Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check Git version #656

Merged
merged 1 commit into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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}.`)
}
}