Skip to content

Commit f8b9b94

Browse files
authored
feat: support SKIP_INSTALL_SIMPLE_GIT_HOOKS env (#107)
ta da! Thanks @JounQin Will release a new version today || tomorrow
1 parent 1bb083d commit f8b9b94

File tree

4 files changed

+169
-515
lines changed

4 files changed

+169
-515
lines changed

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,15 @@ npm uninstall simple-git-hooks
162162

163163
### I want to skip git hooks!
164164

165-
You should use `--no-verify` option
165+
If you need to bypass install hooks at all, for example on CI, you can use `SKIP_INSTALL_SIMPLE_GIT_HOOKS` environment variable at the first place.
166+
167+
```sh
168+
export SKIP_INSTALL_SIMPLE_GIT_HOOKS=1
169+
170+
npm install simple-git-hooks --save-dev
171+
```
172+
173+
Or if you only need to bypass hooks for a single git operation, you should use `--no-verify` option
166174

167175
```sh
168176
git commit -m "commit message" --no-verify # -n for shorthand

cli.js

100644100755
+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
*/
77
const {setHooksFromConfig} = require('./simple-git-hooks')
88

9+
const {SKIP_INSTALL_SIMPLE_GIT_HOOKS} = process.env
10+
11+
if (['1', 'true'].includes(SKIP_INSTALL_SIMPLE_GIT_HOOKS)) {
12+
console.log(`[INFO] SKIP_INSTALL_SIMPLE_GIT_HOOKS is set to "${SKIP_INSTALL_SIMPLE_GIT_HOOKS}", skipping installing hook.`)
13+
return
14+
}
15+
916
try {
1017
setHooksFromConfig(process.cwd(), process.argv)
1118
console.log('[INFO] Successfully set all git hooks')

simple-git-hooks.test.js

+53
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,59 @@ describe("Simple Git Hooks tests", () => {
449449
expect(isEqual(installedHooks, COMMON_GIT_HOOKS)).toBe(true);
450450
});
451451
});
452+
453+
describe("SKIP_INSTALL_SIMPLE_GIT_HOOKS", () => {
454+
afterEach(() => {
455+
removeGitHooksFolder(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
456+
});
457+
458+
it("does not create git hooks when SKIP_INSTALL_SIMPLE_GIT_HOOKS is set to 1", () => {
459+
createGitHooksFolder(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
460+
execSync(`node ${require.resolve("./cli")}`, {
461+
cwd: PROJECT_WITH_CONF_IN_PACKAGE_JSON,
462+
env: {
463+
...process.env,
464+
SKIP_INSTALL_SIMPLE_GIT_HOOKS: "1",
465+
},
466+
});
467+
const installedHooks = getInstalledGitHooks(
468+
path.normalize(
469+
path.join(PROJECT_WITH_CONF_IN_PACKAGE_JSON, ".git", "hooks")
470+
)
471+
);
472+
expect(installedHooks).toEqual({});
473+
});
474+
475+
it("creates git hooks when SKIP_INSTALL_SIMPLE_GIT_HOOKS is set to 0", () => {
476+
createGitHooksFolder(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
477+
execSync(`node ${require.resolve("./cli")}`, {
478+
cwd: PROJECT_WITH_CONF_IN_PACKAGE_JSON,
479+
env: {
480+
...process.env,
481+
SKIP_INSTALL_SIMPLE_GIT_HOOKS: "0",
482+
},
483+
});
484+
const installedHooks = getInstalledGitHooks(
485+
path.normalize(
486+
path.join(PROJECT_WITH_CONF_IN_PACKAGE_JSON, ".git", "hooks")
487+
)
488+
);
489+
expect(installedHooks).toEqual({ "pre-commit": TEST_SCRIPT });
490+
});
491+
492+
it("creates git hooks when SKIP_INSTALL_SIMPLE_GIT_HOOKS is not set", () => {
493+
createGitHooksFolder(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
494+
execSync(`node ${require.resolve("./cli")}`, {
495+
cwd: PROJECT_WITH_CONF_IN_PACKAGE_JSON,
496+
});
497+
const installedHooks = getInstalledGitHooks(
498+
path.normalize(
499+
path.join(PROJECT_WITH_CONF_IN_PACKAGE_JSON, ".git", "hooks")
500+
)
501+
);
502+
expect(installedHooks).toEqual({ "pre-commit": TEST_SCRIPT });
503+
});
504+
});
452505
});
453506

454507
describe("ENV vars features tests", () => {

0 commit comments

Comments
 (0)