Skip to content

Commit 1c3a8a3

Browse files
committed
fix: optimize the migration experience from husky.
1 parent 8486a22 commit 1c3a8a3

File tree

2 files changed

+34
-37
lines changed

2 files changed

+34
-37
lines changed

README.md

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,6 @@ If you need multiple verbose commands per git hook, flexible configuration or au
9696
3. Run the CLI script to update the git hooks with the commands from the config:
9797

9898
```sh
99-
# [Optional] These 2 steps can be skipped for non-husky users
100-
git config core.hooksPath .git/hooks/
101-
rm -rf .git/hooks
102-
103-
# Update ./git/hooks
10499
npx simple-git-hooks
105100
```
106101

@@ -211,34 +206,6 @@ If you have the option to set arguments or environment variables, you can use th
211206

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

214-
### When migrating from `husky` git hooks are not running
215-
216-
**Why is this happening?**
217-
218-
Husky might change the `core.gitHooks` value to `.husky`, this way, git hooks would search `.husky` directory instead of `.git/hooks/`.
219-
220-
Read more on git configuration in [Git book](https://git-scm.com/docs/githooks)
221-
222-
You can check it by running this command inside of your repo:
223-
224-
`git config core.hooksPath`
225-
226-
If it outputs `.husky` then this is your case
227-
228-
**How to fix?**
229-
230-
you need to point `core.gitHooks` value to `your-awesome-project/.git/hooks`. You can use this command:
231-
232-
`git config core.hooksPath .git/hooks/`
233-
234-
validate the value is set:
235-
236-
`git config core.hooksPath`
237-
238-
should output: `.git/hooks/`
239-
240-
Then remove the `.husky` folder that are generated previously by `husky`.
241-
242209
### I am getting "npx: command not found" error in a GUI git client
243210

244211
This happens when using a node version manager such as `nodenv`, `nvm`, `mise` which require

simple-git-hooks.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const fs = require('fs')
22
const path = require('path')
33
const url = require('url')
4+
const { execSync } = require('child_process');
45

56
const VALID_GIT_HOOKS = [
67
'applypatch-msg',
@@ -183,7 +184,36 @@ async function setHooksFromConfig(projectRootPath=process.cwd(), argv=process.ar
183184
}
184185

185186
/**
186-
* Creates or replaces an existing executable script in .git/hooks/<hook> with provided command
187+
* Returns the absolute path to the Git hooks directory.
188+
* Respects user-defined core.hooksPath from Git config if present;
189+
* otherwise defaults to <gitRoot>/.git/hooks.
190+
*
191+
* @param {string} gitRoot - The absolute path to the Git project root
192+
* @returns {string} - The resolved absolute path to the hooks directory
193+
* @private
194+
*/
195+
function _getHooksPath(gitRoot) {
196+
const defaultHooksPath = path.join(gitRoot, '.git', 'hooks')
197+
try {
198+
const customHooksPath = execSync('git config core.hooksPath', {
199+
cwd: gitRoot,
200+
encoding: 'utf8'
201+
}).trim()
202+
203+
if (!customHooksPath) {
204+
return defaultHooksPath
205+
}
206+
207+
return path.isAbsolute(customHooksPath)
208+
? customHooksPath
209+
: path.resolve(gitRoot, customHooksPath)
210+
} catch {
211+
return defaultHooksPath
212+
}
213+
}
214+
215+
/**
216+
* Creates or replaces an existing executable script in the git hooks directory with provided command
187217
* @param {string} hook
188218
* @param {string} command
189219
* @param {string} projectRoot
@@ -198,12 +228,12 @@ function _setHook(hook, command, projectRoot=process.cwd()) {
198228
}
199229

200230
const hookCommand = PREPEND_SCRIPT + command
201-
const hookDirectory = gitRoot + '/hooks/'
202-
const hookPath = path.normalize(hookDirectory + hook)
231+
const hookDirectory = _getHooksPath(gitRoot)
232+
const hookPath = path.join(hookDirectory, hook)
203233

204234
const normalizedHookDirectory = path.normalize(hookDirectory)
205235
if (!fs.existsSync(normalizedHookDirectory)) {
206-
fs.mkdirSync(normalizedHookDirectory)
236+
fs.mkdirSync(normalizedHookDirectory, { recursive: true })
207237
}
208238

209239
fs.writeFileSync(hookPath, hookCommand)

0 commit comments

Comments
 (0)