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

feat: support SKIP_INSTALL_SIMPLE_GIT_HOOKS env #107

Merged
merged 3 commits into from
Mar 13, 2024
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
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
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.
JounQin marked this conversation as resolved.
Show resolved Hide resolved

```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
Original file line number Diff line number Diff line change
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.`)
toplenboren marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
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", () => {
JounQin marked this conversation as resolved.
Show resolved Hide resolved
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