I am trying to format the committed source code inside my pre-commit hook, but I only want to execute this hook when I am in a specific repo. My "clever" way of checking I am in this repo is by checking whether there is a substring in my repo's remotes. All good until now.
However, my special repo, that requires formatting, has submodules and therefore I also want to be able to check the super repo's remotes when I am committing something from the context of a submodule. My solution is something like the below:
test=$(cd ../../; git remote -vv | grep 'my_special_repo')
echo $test
if [[ $test == *"my_special_repo"* ]]; then
echo "This is a my_special_repo child submodule"
echo "Commit should be formatted"
else
exit 0
fi
This does not work. When I echo $test it is empty.
But this:
test=$(cd ../../; echo $PWD; git remote -vv | grep 'my_special_repo')
echo $test
if [[ $test == *"my_special_repo"* ]]; then
echo "This is a my_special_repo child submodule"
echo "Commit should be formatted"
else
exit 0
fi
works fine.
I have found this fact that cd any/path && git any-command does not work from the context of a hook in other questions or comments in relevant questions/answers but without explanation.
- Why does this not work?
- Why does my workaround work?
PS: My git version is 2.41.0.windows.1
git -C ../../yields the exact same results, i.e. it works when just executed in git bash, but doesn't when it is part of the hook.unset $(git rev-parse --local-env-vars)Found in stackoverflow.com/…