2

We use a git module with several submodules. In order to make it more convenient for daily use, I wanted to have a post-commit hook which automatically adds the submodule to the git index in the main/super/root git repo/clone/module such as git add mychangedsubmodulename so as to prepare a commit ready to update the submodule's commit reference in the main repo and probably more ideas to come.

The .git folder is located inside the main module root folder and seems to contain as well the submodule related stuff as a result of having cloned the main repo with --recursive option. My version of git is git version 2.6.4 (Apple Git-63).

I could easily create a post-commit hook in the main repo but it does not fire from commits within a submodule, which I consider correct and appropriate so far but also might be a clue for a possible workaround.

I have tried to define several post-commit hook files in different locations also following the question 10848191 without any success.

How can I achieve to automatically modify the main module on commits in a submodule?

Update: After the submodule post-commit hook now works sometimes, I managed to cd into the main repository and successfully invoke git by cleaning the environment variables.

# clear git environment or git would confuse main and sub repos when crossing boundary by only changing pwd
while read variable; do
    unset $variable
done < <(env | grep "^GIT_" | sed 's/=.*//g')

However, the original question was as how to fire the submodule hook. This seems to be working only sometimes, still.

Community
  • 1
  • 1

1 Answers1

0

You can use something like below inside your post-commit hook:

git --work-tree=$PWD/.. --git-dir=$PWD/../.git branch

The above command is taken from .git/modules/my-sub-module/hooks/pre-commit file on my git version 2.3.2 (Apple Git-55). I am using the above command to list all the local branches of the main repo.

NOTE: Unfortunately changing directory and running git-commands inside the post-commit hooks may not work, because of the git relying on some of the preconfigured default settings and env-vars. So in my case cd path-to-main-repo-directory && git some-command inside my .git/modules/my-sub-module/hooks/pre-commit did not help me.

You can also check the manual here for more details.

dopeddude
  • 4,943
  • 3
  • 33
  • 41
  • event though it looks very similar if not even identical to (http://stackoverflow.com/questions/10848191/git-submodule-commit-hooks) I have tried it and it works. Cool, thanks! – Philipp Kunz Mar 29 '16 at 15:06
  • @PhilippKunz Great you already solved it, I just added the commands working for me, which may probably save some hours for someone as I had wasted some time when the `cd` and `git -C` did not help me out. :-) – dopeddude Mar 30 '16 at 06:58
  • 1
    if figure it works only sometimes. I've experienced it again just not to fire the hook inside the submodule but don't have any clue why. Thank you for encouraging me to try again which then happened to be the first successful attempt. – Philipp Kunz Mar 31 '16 at 09:05