Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: toplenboren/simple-git-hooks
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 2.9.0
Choose a base ref
...
head repository: toplenboren/simple-git-hooks
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 2.10.0
Choose a head ref
Loading
Showing with 652 additions and 309 deletions.
  1. +4 −4 .github/workflows/codeql-analysis.yml
  2. +2 −2 .github/workflows/lint.yml
  3. +2 −2 .github/workflows/tests.yml
  4. +53 −6 README.md
  5. +1 −1 _tests/project_with_configuration_in_alternative_separate_cjs/package.json
  6. +1 −1 _tests/project_with_configuration_in_alternative_separate_js/package.json
  7. +1 −1 _tests/project_with_configuration_in_alternative_separate_json/package.json
  8. +1 −0 _tests/project_with_configuration_in_package_json/initrc_that_does_nothing.sh
  9. +1 −0 _tests/project_with_configuration_in_package_json/initrc_that_prevents_hook_fail.sh
  10. +1 −1 _tests/project_with_configuration_in_package_json/package.json
  11. +1 −1 _tests/project_with_configuration_in_separate_cjs/package.json
  12. +1 −1 _tests/project_with_configuration_in_separate_js/package.json
  13. +1 −1 _tests/project_with_configuration_in_separate_json/package.json
  14. +1 −1 _tests/project_with_custom_configuration/package.json
  15. +1 −1 _tests/project_with_incorrect_configuration_in_package_json/package.json
  16. +1 −1 _tests/{project_with_simple_pre_commit_in_deps → project_with_simple_git_hooks_in_deps}/package.json
  17. +1 −1 ...ject_with_simple_pre_commit_in_dev_deps → project_with_simple_git_hooks_in_dev_deps}/package.json
  18. +2 −2 _tests/project_with_unused_configuration_in_package_json/package.json
  19. +1 −1 _tests/project_without_configuration/package.json
  20. +1 −1 _tests/{project_without_simple_pre_commit → project_without_simple_git_hooks}/package.json
  21. +6 −4 package.json
  22. +19 −4 simple-git-hooks.js
  23. +549 −272 simple-git-hooks.test.js
8 changes: 4 additions & 4 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -39,11 +39,11 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
@@ -54,7 +54,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1
uses: github/codeql-action/autobuild@v3

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
@@ -68,4 +68,4 @@ jobs:
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
uses: github/codeql-action/analyze@v3
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -12,9 +12,9 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Lint
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: 14.x
- run: npm install
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -12,9 +12,9 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Lint
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: 14.x
- run: npm install
59 changes: 53 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -15,9 +15,9 @@ A tool that lets you easily manage git hooks
| Package | Unpacked size | With deps |
| ---------------------------- | ------------- | --------- |
| husky v4 `4.3.8` | `53.5 kB` | `~1 mB` |
| husky v6 `6.0.0` | `6.86 kB` | `6.86 kB` |
| husky v8 `8.0.3` | `6.44 kB` | `6.44 kB` |
| pre-commit `1.2.2` | `~80 kB` | `~850 kB` |
| **simple-git-hooks** `2.2.0` | `10.1 kB` | `10.1 kB` |
| **simple-git-hooks** `2.10.0` | `11.8 kB` | `11.8 kB` |

### Who uses simple-git-hooks?

@@ -54,7 +54,7 @@ Also, this package allows you to set only one command per git hook.
If you need multiple verbose commands per git hook, flexible configuration or automatic update of git hooks, please check out the other packages:

- [Lefthook](https://github.com/Arkweid/lefthook)
- [husky](https://github.com/typicode/husky)
- [husky](https://typicode.github.io/husky/)
- [pre-commit](https://github.com/pre-commit/pre-commit)

## Usage
@@ -99,7 +99,7 @@ If you need multiple verbose commands per git hook, flexible configuration or au
# [Optional] These 2 steps can be skipped for non-husky users
git config core.hooksPath .git/hooks/
rm -rf .git/hooks

# Update ./git/hooks
npx simple-git-hooks
```
@@ -114,6 +114,8 @@ Now all the git hooks are created.

Note for **yarn2** users: Please run `yarn dlx simple-git-hooks` instead of the command above. More info on [dlx](https://yarnpkg.com/cli/dlx)

Note for **yarn1** users: Please run `ynpx simple-git-hooks` instead of the command above. More info on [ynpx](https://npm.io/package/ynpx)

Note that you should manually run `npx simple-git-hooks` **every time you change a command**.

### Additional configuration options
@@ -154,9 +156,34 @@ npm uninstall simple-git-hooks

### I want to skip git hooks!

You should use -no-verify option
You should use `--no-verify` option

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

you can read more about it here https://bobbyhadz.com/blog/git-commit-skip-hooks#skip-git-commit-hooks


If you need to bypass hooks for multiple Git operations, setting the SKIP_SIMPLE_GIT_HOOKS environment variable can be more convenient. Once set, all subsequent Git operations in the same terminal session will bypass the associated hooks.

```sh
# Set the environment variable
export SKIP_SIMPLE_GIT_HOOKS=1

# Subsequent Git commands will skip the hooks
git add .
git commit -m "commit message" # pre-commit hooks are bypassed
git push origin main # pre-push hooks are bypassed
```

### Skipping Hooks in 3rd party git clients

https://bobbyhadz.com/blog/git-commit-skip-hooks#skip-git-commit-hooks
If your client provides a toggle to skip Git hooks, you can utilize it to bypass the hooks. For instance, in VSCode, you can toggle git.allowNoVerifyCommit in the settings.

If you have the option to set arguments or environment variables, you can use the --no-verify option or the SKIP_SIMPLE_GIT_HOOKS environment variable.

If these options are not available, you may need to resort to using the terminal for skipping hooks.

### When migrating from `husky` git hooks are not running

@@ -185,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
@@ -1,5 +1,5 @@
{
"name": "simple-pre-commit-test-package",
"name": "simple-git-hooks-test-package",
"version": "1.0.0",
"devDependencies": {
"simple-git-hooks": "1.0.0"
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "simple-pre-commit-test-package",
"name": "simple-git-hooks-test-package",
"version": "1.0.0",
"devDependencies": {
"simple-git-hooks": "1.0.0"
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "simple-pre-commit-test-package",
"name": "simple-git-hooks-test-package",
"version": "1.0.0",
"devDependencies": {
"simple-git-hooks": "1.0.0"
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "simple-pre-commit-test-package",
"name": "simple-git-hooks-test-package",
"version": "1.0.0",
"simple-git-hooks": {
"pre-commit": "exit 1"
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "simple-pre-commit-test-package",
"name": "simple-git-hooks-test-package",
"version": "1.0.0",
"devDependencies": {
"simple-git-hooks": "1.0.0"
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "simple-pre-commit-test-package",
"name": "simple-git-hooks-test-package",
"version": "1.0.0",
"devDependencies": {
"simple-git-hooks": "1.0.0"
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "simple-pre-commit-test-package",
"name": "simple-git-hooks-test-package",
"version": "1.0.0",
"devDependencies": {
"simple-git-hooks": "1.0.0"
2 changes: 1 addition & 1 deletion _tests/project_with_custom_configuration/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "simple-pre-commit-test-package",
"name": "simple-git-hooks-test-package",
"version": "1.0.0",
"devDependencies": {
"simple-git-hooks": "1.0.0"
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "simple-pre-commit-test-package",
"name": "simple-git-hooks-test-package",
"version": "1.0.0",
"devDependencies": {
"simple-git-hooks": "1.0.0"
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "simple-pre-commit-test-package",
"name": "simple-git-hooks-test-package",
"version": "1.0.0",
"dependencies": {
"simple-git-hooks": "1.0.0"
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "simple-pre-commit-test-package",
"name": "simple-git-hooks-test-package",
"version": "1.0.0",
"devDependencies": {
"simple-git-hooks": "1.0.0"
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "simple-pre-commit-test-package",
"name": "simple-git-hooks-test-package",
"version": "1.0.0",
"simple-git-hooks": {
"pre-commit": "exit 1",
"preserveUnused": ["commit-msg"]
},
"devDependencies": {
"simple-pre-commit": "1.0.0"
"simple-git-hooks": "1.0.0"
}
}
2 changes: 1 addition & 1 deletion _tests/project_without_configuration/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "simple-pre-commit-test-package",
"name": "simple-git-hooks-test-package",
"version": "1.0.0",
"devDependencies": {
"simple-git-hooks": "1.0.0"
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "simple-pre-commit-test-package",
"name": "simple-git-hooks-test-package",
"version": "1.0.0",
"devDependencies": {
},
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-git-hooks",
"version": "2.8.1",
"version": "2.9.0",
"description": "A simple, zero dependency tool for setting up git hooks for small projects",
"author": "Mikhail Gorbunov <toplenboren@gmail.com> (toplenboren.github.io)",
"main": "simple-git-hooks.js",
@@ -9,7 +9,8 @@
"postinstall": "node ./postinstall.js",
"lint": "eslint *.js",
"test": "jest",
"publish": "clean-publish",
"publish": "publish",
"create-publish-dist": "clean-publish --no-publish",
"uninstall": "node ./uninstall.js"
},
"keywords": [
@@ -40,9 +41,10 @@
"packageManager": "npm"
},
"devDependencies": {
"clean-publish": "^1.1.8",
"clean-publish": "^4.2.0",
"eslint": "^7.19.0",
"jest": "^26.6.3",
"lint-staged": "^10.5.4"
"lint-staged": "^10.5.4",
"lodash.isequal": "^4.5.0"
}
}
23 changes: 19 additions & 4 deletions simple-git-hooks.js
Original file line number Diff line number Diff line change
@@ -34,6 +34,20 @@ const VALID_GIT_HOOKS = [

const VALID_OPTIONS = ['preserveUnused']

const PREPEND_SCRIPT =
`#!/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
* @param {string} directory
@@ -130,7 +144,7 @@ function checkSimpleGitHooksInDependencies(projectRootPath) {

// if simple-git-hooks in dependencies -> note user that he should remove move it to devDeps!
if ('dependencies' in packageJsonContent && 'simple-git-hooks' in packageJsonContent.dependencies) {
console.log('[WARN] You should move simple-git-hooks to the devDependencies!')
console.warn('[WARN] You should move simple-git-hooks to the devDependencies!')
return true // We only check that we are in the correct package, e.g not in a dependency of a dependency
}
if (!('devDependencies' in packageJsonContent)) {
@@ -174,11 +188,11 @@ function _setHook(hook, command, projectRoot=process.cwd()) {
const gitRoot = getGitProjectRoot(projectRoot)

if (!gitRoot) {
console.log('[INFO] No `.git` root folder found, skipping')
console.info('[INFO] No `.git` root folder found, skipping')
return
}

const hookCommand = "#!/bin/sh\n" + command
const hookCommand = PREPEND_SCRIPT + command
const hookDirectory = gitRoot + '/hooks/'
const hookPath = path.normalize(hookDirectory + hook)

@@ -190,7 +204,7 @@ function _setHook(hook, command, projectRoot=process.cwd()) {
fs.writeFileSync(hookPath, hookCommand)
fs.chmodSync(hookPath, 0o0755)

console.log(`[INFO] Successfully set the ${hook} with command: ${command}`)
console.info(`[INFO] Successfully set the ${hook} with command: ${command}`)
}

/**
@@ -361,4 +375,5 @@ module.exports = {
getProjectRootDirectoryFromNodeModules,
getGitProjectRoot,
removeHooks,
PREPEND_SCRIPT
}
Loading