Skip to content

Commit

Permalink
feat: support SKIP_INSTALL_SIMPLE_GIT_HOOKS env (#107)
Browse files Browse the repository at this point in the history
ta da! Thanks @JounQin 

Will release a new version today || tomorrow
  • Loading branch information
JounQin committed Mar 13, 2024
1 parent 1bb083d commit f8b9b94
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 515 deletions.
10 changes: 9 additions & 1 deletion README.md
Expand Up @@ -162,7 +162,15 @@ npm uninstall simple-git-hooks

### I want to skip git hooks!

You should use `--no-verify` option
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.

```sh
export SKIP_INSTALL_SIMPLE_GIT_HOOKS=1

npm install simple-git-hooks --save-dev
```

Or if you only need to bypass hooks for a single git operation, you should use `--no-verify` option

```sh
git commit -m "commit message" --no-verify # -n for shorthand
Expand Down
7 changes: 7 additions & 0 deletions cli.js 100644 → 100755
Expand Up @@ -6,6 +6,13 @@
*/
const {setHooksFromConfig} = require('./simple-git-hooks')

const {SKIP_INSTALL_SIMPLE_GIT_HOOKS} = process.env

if (['1', 'true'].includes(SKIP_INSTALL_SIMPLE_GIT_HOOKS)) {
console.log(`[INFO] SKIP_INSTALL_SIMPLE_GIT_HOOKS is set to "${SKIP_INSTALL_SIMPLE_GIT_HOOKS}", skipping installing hook.`)
return
}

try {
setHooksFromConfig(process.cwd(), process.argv)
console.log('[INFO] Successfully set all git hooks')
Expand Down
53 changes: 53 additions & 0 deletions simple-git-hooks.test.js
Expand Up @@ -449,6 +449,59 @@ describe("Simple Git Hooks tests", () => {
expect(isEqual(installedHooks, COMMON_GIT_HOOKS)).toBe(true);
});
});

describe("SKIP_INSTALL_SIMPLE_GIT_HOOKS", () => {
afterEach(() => {
removeGitHooksFolder(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
});

it("does not create git hooks when SKIP_INSTALL_SIMPLE_GIT_HOOKS is set to 1", () => {
createGitHooksFolder(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
execSync(`node ${require.resolve("./cli")}`, {
cwd: PROJECT_WITH_CONF_IN_PACKAGE_JSON,
env: {
...process.env,
SKIP_INSTALL_SIMPLE_GIT_HOOKS: "1",
},
});
const installedHooks = getInstalledGitHooks(
path.normalize(
path.join(PROJECT_WITH_CONF_IN_PACKAGE_JSON, ".git", "hooks")
)
);
expect(installedHooks).toEqual({});
});

it("creates git hooks when SKIP_INSTALL_SIMPLE_GIT_HOOKS is set to 0", () => {
createGitHooksFolder(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
execSync(`node ${require.resolve("./cli")}`, {
cwd: PROJECT_WITH_CONF_IN_PACKAGE_JSON,
env: {
...process.env,
SKIP_INSTALL_SIMPLE_GIT_HOOKS: "0",
},
});
const installedHooks = getInstalledGitHooks(
path.normalize(
path.join(PROJECT_WITH_CONF_IN_PACKAGE_JSON, ".git", "hooks")
)
);
expect(installedHooks).toEqual({ "pre-commit": TEST_SCRIPT });
});

it("creates git hooks when SKIP_INSTALL_SIMPLE_GIT_HOOKS is not set", () => {
createGitHooksFolder(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
execSync(`node ${require.resolve("./cli")}`, {
cwd: PROJECT_WITH_CONF_IN_PACKAGE_JSON,
});
const installedHooks = getInstalledGitHooks(
path.normalize(
path.join(PROJECT_WITH_CONF_IN_PACKAGE_JSON, ".git", "hooks")
)
);
expect(installedHooks).toEqual({ "pre-commit": TEST_SCRIPT });
});
});
});

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

0 comments on commit f8b9b94

Please sign in to comment.