Skip to content

Commit

Permalink
Merge pull request #96 from bhanuprasadcherukuvada/bypass-hooks
Browse files Browse the repository at this point in the history
feat: add env var to bypass hooks execution
  • Loading branch information
toplenboren authored Jan 4, 2024
2 parents d53a27c + 47dec20 commit 20121df
Show file tree
Hide file tree
Showing 4 changed files with 508 additions and 227 deletions.
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down Expand Up @@ -158,7 +158,32 @@ npm uninstall simple-git-hooks

You should use `--no-verify` option

https://bobbyhadz.com/blog/git-commit-skip-hooks#skip-git-commit-hooks
```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

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

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"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"
}
}
10 changes: 9 additions & 1 deletion simple-git-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ 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";

/**
* Recursively gets the .git folder path from provided directory
* @param {string} directory
Expand Down Expand Up @@ -178,7 +185,7 @@ function _setHook(hook, command, projectRoot=process.cwd()) {
return
}

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

Expand Down Expand Up @@ -361,4 +368,5 @@ module.exports = {
getProjectRootDirectoryFromNodeModules,
getGitProjectRoot,
removeHooks,
PREPEND_SCRIPT
}
Loading

0 comments on commit 20121df

Please sign in to comment.