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

Support hooks on git events triggered by submodules #1450

Open
1 of 2 tasks
jclaveau opened this issue May 24, 2024 · 2 comments
Open
1 of 2 tasks

Support hooks on git events triggered by submodules #1450

jclaveau opened this issue May 24, 2024 · 2 comments

Comments

@jclaveau
Copy link

jclaveau commented May 24, 2024

Summary
I see two use cases for submodules with Husky

  • I want my root repo to configure hooks of its submodules
  • A submodule repo uses husky

Here I focus only on the first case

Context

  • Terminal or GUI client (PowerShell, Git Bash, GitHub Desktop, ...)
  • git / zsh

Steps

mkdir husky_submodule_hooks_from_root; cd husky_submodule_hooks_from_root; git init; pnpm init
pnpm install husky; pnpm husky
git config --get core.hooksPath
=> .husky/_

git submodule add [email protected]:typicode/husky.git husky_submodule
cd husky_submodule
git rev-parse --git-path hooks
=> .../husky_submodule_hooks_from_root/.git/modules/husky_submodule/hooks
git config --get core.hooksPath
=> nothing
  • As you can see the hooks path of the submodule is outside the hooksPath of the root repo so Husky can't handle submodules hooks.

As a workaround I created the script .husky/link-submodules-hooks.sh

#!/bin/bash
# Create the symlinks .git/modules/<submodule_path>/hooks to .husky/<submodule_paths>
git submodule foreach --quiet '\
    cd $(git rev-parse --git-path hooks)/.. ;\
    mv hooks hooks.bak || true ;\
    mkdir -p ../../../../.husky/$path ;\
    ln -s ../../../../.husky/$path hooks ;\
' || true # No blocking if the .git folder is missing (like during deploy)

And run it from my package.json:

{
    "prepare": "husky; ./.husky/link-submodules-hooks.sh"
}

Glad to it, I have folders in my .husky directory where i can store my hooks like

.husky
-- submodules-path/
---- submodule1
------ pre-commit
------ pre-push
------ ...
---- submodule2
------ ...

This feature should belong, IMHO, to Husky. Meanwhile, this script may help other Husky users to achieve it!

This use case also answers your message, here #1308 (comment)

Thanks a lot for sharing your work!

@jclaveau jclaveau changed the title Support hooks on submodules git events Support hooks on git events triggered by submodules May 24, 2024
@Zackin-He
Copy link

I am using version v9, how do I write the./.husky/link-submodules-hooks.sh file

@jclaveau
Copy link
Author

Just create it and copy the following content as written above

#!/bin/bash
# Create the symlinks .git/modules/<submodule_path>/hooks to .husky/<submodule_paths>
git submodule foreach --quiet '\
    cd $(git rev-parse --git-path hooks)/.. ;\
    mv hooks hooks.bak || true ;\
    mkdir -p ../../../../.husky/$path ;\
    ln -s ../../../../.husky/$path hooks ;\
' || true # No blocking if the .git folder is missing (like during deploy)

NB: Windows doesn't support symlinks for what I know, I personnaly use Linux

Then add the prepare line to your package
Then run npm / yarn or pnpm install

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants