Skip to content

Commit

Permalink
Add HUSKY_SKIP_HOOKS env var to skip running hooks (#492)
Browse files Browse the repository at this point in the history
* Add HUSKY_SKIP_HOOKS env var to skip running hooks

* Add test for HUSKY_SKIP_HOOKS

* Add documentation for HUSKY_SKIP_HOOKS
  • Loading branch information
ozzywalsh authored and typicode committed Jun 5, 2019
1 parent 9309361 commit f198d18
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 30 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Expand Up @@ -17,6 +17,10 @@ script:
- cat .git/hooks/post-checkout
- time git checkout
- node scripts/ci-post-checkout-check
# Test HUSKY_SKIP_HOOKS
- cat .git/hooks/post-checkout
- HUSKY_SKIP_HOOKS=1 time git checkout
- node scripts/ci-husky-skip-hooks
# Should not fail due to missing script
- node scripts/ci-break-path.js
- cat .git/hooks/post-checkout
Expand Down
8 changes: 8 additions & 0 deletions DOCS.md
Expand Up @@ -28,6 +28,14 @@ If you don't want `husky` to automatically install Git hooks, simply set `HUSKY_
HUSKY_SKIP_INSTALL=1 npm install
```

## Skip running hooks

If you don't want `husky` to run your hooks, simply set `HUSKY_SKIP_HOOKS` environment variable to `1`.

```sh
HUSKY_SKIP_HOOKS=1 git checkout
```

## Multi-package repository (monorepo)

If you have a multi-package repository, it's __recommended__ to use tools like [lerna](https://github.com/lerna/lerna) and have `husky` installed ONLY in the root `package.json` to act as the source of truth.
Expand Down
41 changes: 11 additions & 30 deletions package-lock.json

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

25 changes: 25 additions & 0 deletions scripts/ci-husky-skip-hooks.js
@@ -0,0 +1,25 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs')
const path = require('path')

const filename = path.join(__dirname, '../ci-post-checkout')

if (fs.existsSync(filename)) {
const data = fs.readFileSync(filename, 'utf-8')

if (data.split(' ').length !== 3) {
console.log('Not all params were set in HUSKY_GIT_PARAMS')
console.log('Got', data)
process.exit(1)
}

fs.unlinkSync(filename)
console.log(
'.git/hooks/post-checkout script has run, hooks were not skipped.'
)
process.exit(1)
} else {
console.log(
'.git/hooks/post-checkout script has not run, hooks were successfully skipped.'
)
}
12 changes: 12 additions & 0 deletions src/runner/index.ts
Expand Up @@ -7,6 +7,7 @@ import getConf from '../getConf'
export interface Env extends NodeJS.ProcessEnv {
HUSKY_GIT_STDIN?: string
HUSKY_GIT_PARAMS?: string
HUSKY_SKIP_HOOKS?: string
}

/**
Expand All @@ -17,6 +18,17 @@ export default async function run(
[, scriptPath, hookName = '', HUSKY_GIT_PARAMS]: string[],
getStdinFn: () => Promise<string> = getStdin
): Promise<number> {
if (
process.env.HUSKY_SKIP_HOOKS === 'true' ||
process.env.HUSKY_SKIP_HOOKS === '1'
) {
console.log(
"HUSKY_SKIP_HOOKS environment variable is set to 'true',",
'skipping running Git hooks.'
)
return 0
}

const cwd = path.resolve(scriptPath.split('node_modules')[0])
// In some cases, package.json may not exist
// For example, when switching to gh-page branch
Expand Down

0 comments on commit f198d18

Please sign in to comment.