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

Configure husky (git hooks) in submodules #192

Open
tarsinzer opened this issue May 4, 2020 · 7 comments
Open

Configure husky (git hooks) in submodules #192

tarsinzer opened this issue May 4, 2020 · 7 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@tarsinzer
Copy link
Owner

Since it is a well-known issue, we need to come up with idea, how to make it work
typicode/husky#404

By the way, I do agree with this comment. This is not okay.
typicode/husky#404 (comment)

@tarsinzer tarsinzer added bug Something isn't working help wanted Extra attention is needed labels May 4, 2020
@witthong
Copy link

witthong commented Sep 4, 2020

find-path-to-git-hooks-directory-on-the-shell
https://stackoverflow.com/questions/14073053/find-path-to-git-hooks-directory-on-the-shell

@abrioy
Copy link

abrioy commented Sep 4, 2020

Here's the workaround we use to trick Husky into working with our submodule (on linux) :

  • The submodule is located in src/app/submodule
  • Create an empty file in src/app/submodule/.huskyrc or else the husky.sh script won't work at all
  • run GIT_DIR=$(cd src/app/submodule && git rev-parse --git-dir) node node_modules/husky/husky.js install
  • The Husky files should be properly created in .git/modules/submodule/hooks and the hooks should work nicely from the submodule and use the configuration from the parent repository.

The problem is that every developer needs to run the node command. To work around that, we set a yarn postinstall script in our package.json : "postinstall": "(export GIT_DIR=$(cd src/app/commons && git rev-parse --git-dir) ; test -f $GIT_DIR/hooks/pre-commit || node node_modules/husky/husky.js install)"

@bertho-zero
Copy link

bertho-zero commented Dec 2, 2020

@abrioy It almost works, there is a ; after GIT_DIR=... in step 3 which makes GIT_DIR not available for the next command. You must either remove the ; or add an export in front of it.

Thanks for the solution, it works wonderfully !

@bertho-zero
Copy link

I just switched to husky@5 and it's different to enable hooks in the submodule, In the new version husky modifies the path of the hooks directory, we can do it directly with the git command:

cd public && git config core.hooksPath ../.husky

This script helps you add the hooks in the main repository and a submodule public:

"postinstall": "husky install && cd public && git config core.hooksPath ../.husky"

@DiamondeX
Copy link

Here's the workaround we use to trick Husky into working with our submodule (on linux) :

  • The submodule is located in src/app/submodule
  • Create an empty file in src/app/submodule/.huskyrc or else the husky.sh script won't work at all
  • run GIT_DIR=$(cd src/app/submodule && git rev-parse --git-dir) node node_modules/husky/husky.js install
  • The Husky files should be properly created in .git/modules/submodule/hooks and the hooks should work nicely from the submodule and use the configuration from the parent repository.

The problem is that every developer needs to run the node command. To work around that, we set a yarn postinstall script in our package.json : "postinstall": "(export GIT_DIR=$(cd src/app/commons && git rev-parse --git-dir) ; test -f $GIT_DIR/hooks/pre-commit || node node_modules/husky/husky.js install)"

It was a little bit different for me.

  1. By me it works without src/app/submodule/.huskyrc, but it didn't without one of these: .huskyrc.js, huskyrc.cjs, husky.config.js, husky.config.cjs. They are mentioned in the config checking block of husky.sh. I've choosed .huskyrc.js and then I had to put there something like:
module.exports = {
  hooks: {
    'pre-commit': '<your-precommit-command>',
  },
}
  1. I'd better check $GIT_DIR/hooks/husky.sh in "postinstall", because $GIT_DIR/hooks/pre-commit may be created in some other way, not by husky.
    And I think that path in "postinstall" should match submodule path.
    So, with all corrections it should be like this in your case:
    "postinstall": "(export GIT_DIR=$(cd src/app/submodule && git rev-parse --git-dir) ; test -f $GIT_DIR/hooks/husky.sh || node node_modules/husky/husky.js install)"

@GuilhermeBorges3Ddev
Copy link

Here's the workaround we use to trick Husky into working with our submodule (on linux) :

  • The submodule is located in src/app/submodule
  • Create an empty file in src/app/submodule/.huskyrc or else the husky.sh script won't work at all
  • run GIT_DIR=$(cd src/app/submodule && git rev-parse --git-dir) node node_modules/husky/husky.js install
  • The Husky files should be properly created in .git/modules/submodule/hooks and the hooks should work nicely from the submodule and use the configuration from the parent repository.

The problem is that every developer needs to run the node command. To work around that, we set a yarn postinstall script in our package.json : "postinstall": "(export GIT_DIR=$(cd src/app/commons && git rev-parse --git-dir) ; test -f $GIT_DIR/hooks/pre-commit || node node_modules/husky/husky.js install)"

How can i redo those commands using Windows?

@cengit
Copy link

cengit commented Jun 14, 2022

I just switched to husky@5 and it's different to enable hooks in the submodule, In the new version husky modifies the path of the hooks directory, we can do it directly with the git command:

cd public && git config core.hooksPath ../.husky

This script helps you add the hooks in the main repository and a submodule public:

"postinstall": "husky install && cd public && git config core.hooksPath ../.husky"

this works for me very well, thank you very much!!!

if you have multiple submodlues you can add a shell script to do this. something like this:

package.json

"scripts": {
    "postinstall": "sh script/submodule-husky-hook-path.sh"
  }

submodule-husky-hook-path.sh

#!/usr/bin/env bash
cd module_a && git config core.hooksPath ../.husky
cd ..
cd module_b && git config core.hooksPath ../.husky
cd ..
cd module_c && git config core.hooksPath ../.husky

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

7 participants