Skip to content

Commit 20121df

Browse files
authored
Merge pull request #96 from bhanuprasadcherukuvada/bypass-hooks
feat: add env var to bypass hooks execution
2 parents d53a27c + 47dec20 commit 20121df

File tree

4 files changed

+508
-227
lines changed

4 files changed

+508
-227
lines changed

README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ If you need multiple verbose commands per git hook, flexible configuration or au
9999
# [Optional] These 2 steps can be skipped for non-husky users
100100
git config core.hooksPath .git/hooks/
101101
rm -rf .git/hooks
102-
102+
103103
# Update ./git/hooks
104104
npx simple-git-hooks
105105
```
@@ -158,7 +158,32 @@ npm uninstall simple-git-hooks
158158

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

161-
https://bobbyhadz.com/blog/git-commit-skip-hooks#skip-git-commit-hooks
161+
```sh
162+
git commit -m "commit message" --no-verify # -n for shorthand
163+
```
164+
165+
you can read more about it here https://bobbyhadz.com/blog/git-commit-skip-hooks#skip-git-commit-hooks
166+
167+
168+
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.
169+
170+
```sh
171+
# Set the environment variable
172+
export SKIP_SIMPLE_GIT_HOOKS=1
173+
174+
# Subsequent Git commands will skip the hooks
175+
git add .
176+
git commit -m "commit message" # pre-commit hooks are bypassed
177+
git push origin main # pre-push hooks are bypassed
178+
```
179+
180+
### Skipping Hooks in 3rd party git clients
181+
182+
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.
183+
184+
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.
185+
186+
If these options are not available, you may need to resort to using the terminal for skipping hooks.
162187

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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"clean-publish": "^4.2.0",
4545
"eslint": "^7.19.0",
4646
"jest": "^26.6.3",
47-
"lint-staged": "^10.5.4"
47+
"lint-staged": "^10.5.4",
48+
"lodash.isequal": "^4.5.0"
4849
}
4950
}

simple-git-hooks.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ const VALID_GIT_HOOKS = [
3434

3535
const VALID_OPTIONS = ['preserveUnused']
3636

37+
const PREPEND_SCRIPT =
38+
"#!/bin/sh\n\n" +
39+
'if [ "$SKIP_SIMPLE_GIT_HOOKS" = "1" ]; then\n' +
40+
' echo "[INFO] SKIP_SIMPLE_GIT_HOOKS is set to 1, skipping hook."\n' +
41+
" exit 0\n" +
42+
"fi\n\n";
43+
3744
/**
3845
* Recursively gets the .git folder path from provided directory
3946
* @param {string} directory
@@ -178,7 +185,7 @@ function _setHook(hook, command, projectRoot=process.cwd()) {
178185
return
179186
}
180187

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

@@ -361,4 +368,5 @@ module.exports = {
361368
getProjectRootDirectoryFromNodeModules,
362369
getGitProjectRoot,
363370
removeHooks,
371+
PREPEND_SCRIPT
364372
}

0 commit comments

Comments
 (0)