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: use SIMPLE_GIT_HOOKS_RC to run optional init script #103

Merged
merged 1 commit into from
Mar 3, 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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,23 @@ validate the value is set:
should output: `.git/hooks/`

Then remove the `.husky` folder that are generated previously by `husky`.

### I am getting "npx: command not found" error in a GUI git client

This happens when using a node version manager such as `nodenv`, `nvm`, `mise` which require
init script to provide project-specific node binaries.

Create init script in `~/.simple-git-hooks.rc` that should be executed prior to git hooks.
Please refer to your node manager documentation for details. For example, for mise, that will
be:

```sh
export PATH="$HOME/.local/share/mise/shims:$PATH"
```

Add `SIMPLE_GIT_HOOKS_RC` global environment variable pointing to that new script. For
example, on macOS, add this to `~/.zshenv`:

```sh
export SIMPLE_GIT_HOOKS_RC="$HOME/.simple-git-hooks.rc"
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo SIMPLE_GIT_HOOKS_RC that does nothing.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alias exit=echo
17 changes: 12 additions & 5 deletions simple-git-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,18 @@ const VALID_GIT_HOOKS = [
const VALID_OPTIONS = ['preserveUnused']

const PREPEND_SCRIPT =
"#!/bin/sh\n\n" +
'if [ "$SKIP_SIMPLE_GIT_HOOKS" = "1" ]; then\n' +
' echo "[INFO] SKIP_SIMPLE_GIT_HOOKS is set to 1, skipping hook."\n' +
" exit 0\n" +
"fi\n\n";
`#!/bin/sh

if [ "$SKIP_SIMPLE_GIT_HOOKS" = "1" ]; then
echo "[INFO] SKIP_SIMPLE_GIT_HOOKS is set to 1, skipping hook."
exit 0
fi

if [ -f "$SIMPLE_GIT_HOOKS_RC" ]; then
. "$SIMPLE_GIT_HOOKS_RC"
fi

`

/**
* Recursively gets the .git folder path from provided directory
Expand Down
62 changes: 42 additions & 20 deletions simple-git-hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ describe("CLI Command-Based Hook Management", () => {
});
});

describe("Tests for skipping git hooks using SKIP_SIMPLE_GIT_HOOKS env var", () => {
describe("Test env vars", () => {
const GIT_USER_NAME = "github-actions";
const GIT_USER_EMAIL = "github-actions@github.com";

Expand Down Expand Up @@ -493,10 +493,6 @@ describe("Tests for skipping git hooks using SKIP_SIMPLE_GIT_HOOKS env var", ()
spc.setHooksFromConfig(projectWithConfigurationInPackageJsonPath);
});

afterEach(() => {
delete process.env.SKIP_SIMPLE_GIT_HOOKS;
});

const expectCommitToSucceed = (path) => {
const errorOccurred = performTestCommit(path);
expect(errorOccurred).toBe(false);
Expand All @@ -507,22 +503,48 @@ describe("Tests for skipping git hooks using SKIP_SIMPLE_GIT_HOOKS env var", ()
expect(errorOccurred).toBe(true);
};

it('commits successfully when SKIP_SIMPLE_GIT_HOOKS is set to "1"', () => {
process.env.SKIP_SIMPLE_GIT_HOOKS = "1";
expectCommitToSucceed(projectWithConfigurationInPackageJsonPath);
});
describe("SKIP_SIMPLE_GIT_HOOKS", () => {
afterEach(() => {
delete process.env.SKIP_SIMPLE_GIT_HOOKS;
});

it("fails to commit when SKIP_SIMPLE_GIT_HOOKS is not set", () => {
expectCommitToFail(projectWithConfigurationInPackageJsonPath);
});
it('commits successfully when SKIP_SIMPLE_GIT_HOOKS is set to "1"', () => {
process.env.SKIP_SIMPLE_GIT_HOOKS = "1";
expectCommitToSucceed(projectWithConfigurationInPackageJsonPath);
});

it('fails to commit when SKIP_SIMPLE_GIT_HOOKS is set to "0"', () => {
process.env.SKIP_SIMPLE_GIT_HOOKS = "0";
expectCommitToFail(projectWithConfigurationInPackageJsonPath);
});
it("fails to commit when SKIP_SIMPLE_GIT_HOOKS is not set", () => {
expectCommitToFail(projectWithConfigurationInPackageJsonPath);
});

it("fails to commit when SKIP_SIMPLE_GIT_HOOKS is set to a random string", () => {
process.env.SKIP_SIMPLE_GIT_HOOKS = "simple-git-hooks";
expectCommitToFail(projectWithConfigurationInPackageJsonPath);
});
it('fails to commit when SKIP_SIMPLE_GIT_HOOKS is set to "0"', () => {
process.env.SKIP_SIMPLE_GIT_HOOKS = "0";
expectCommitToFail(projectWithConfigurationInPackageJsonPath);
});

it("fails to commit when SKIP_SIMPLE_GIT_HOOKS is set to a random string", () => {
process.env.SKIP_SIMPLE_GIT_HOOKS = "simple-git-hooks";
expectCommitToFail(projectWithConfigurationInPackageJsonPath);
});
})

describe("SIMPLE_GIT_HOOKS_RC", () => {
afterEach(() => {
delete process.env.SIMPLE_GIT_HOOKS_RC;
});

it("fails to commit when SIMPLE_GIT_HOOKS_RC is not set", () => {
expectCommitToFail(projectWithConfigurationInPackageJsonPath);
});

it('commits successfully when SIMPLE_GIT_HOOKS_RC points to initrc_that_prevents_hook_fail.sh', () => {
process.env.SIMPLE_GIT_HOOKS_RC = path.join(projectWithConfigurationInPackageJsonPath, "initrc_that_prevents_hook_fail.sh");
expectCommitToSucceed(projectWithConfigurationInPackageJsonPath);
});

it('fails to commit when SIMPLE_GIT_HOOKS_RC points to initrc_that_does_nothing.sh', () => {
process.env.SIMPLE_GIT_HOOKS_RC = path.join(projectWithConfigurationInPackageJsonPath, "initrc_that_does_nothing.sh");
expectCommitToFail(projectWithConfigurationInPackageJsonPath);
});
})
});