1

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.

  1. Why does this not work?
  2. Why does my workaround work?

PS: My git version is 2.41.0.windows.1

12
  • 1
    Those two snippets are identical. Commented Nov 14, 2024 at 13:18
  • 3
    If what you mean by "does not work" is "does not use the cd to change which repo it's looking at", that's because there are environment variables set during hook execution that override the default of referring to the current working directory to find the target to work on. But you really need to be much more specific about what the words "does not work" actually mean in this context in particular. Commented Nov 14, 2024 at 13:25
  • 1
    Apologies for my silly copy-paste mistake. It's fixed now. Commented Nov 14, 2024 at 14:20
  • 1
    @phd I want the context of my git command to be a parent directory rather than a subdir. Using 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. Commented Nov 14, 2024 at 14:24
  • 2
    stackoverflow.com/a/6394777/7976758 Try unset $(git rev-parse --local-env-vars) Found in stackoverflow.com/… Commented Nov 14, 2024 at 14:29

1 Answer 1

1

As phd said in his comment what was missing was this:

unset $(git rev-parse --local-env-vars)

before

test=$(cd ../../; git remote -vv | grep 'my_special_repo')

so that environment variables like GIT_DIR are reset and therefore the hook's script works as expected. This is based on this answer.

PS: Now that it works I reworked my script to its more appropriate form (in my opinion):

test=$(git -C ../../ remote -vv | grep 'my_special_repo')
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.