Thomas Hall
Thomas Hall

Reputation: 21

Add pre commit hook to submodule from parent module's .git

So I have a project with a submodule that gets updated every once in awhile. I have some rules for commits that I try to follow and one of them is to include the branchname in the commit. I accomplish this with githooks as follows:

From top level directory

.git/hooks/prepare-commit-msg

#!/bin/sh

BRANCH=`git branch | grep '^\*' | cut -b3-`
FILE=`cat "$1"`
echo "$BRANCH $FILE" > "$1"

.git/hooks/pre-commit

#!/bin/bash

find vendor -name ".git*" -type d | while read i
do
        if [ -d "$i" ]; then
                DIR=`dirname $i`
                rm -fR $i
                git rm -r --cached $DIR > /dev/null 2>&1
                git add $DIR > /dev/null 2>&1
        fi
done

Then I just set permissions sudo chmod 755 .git/hooks/prepare-commit-msg sudo chmod 755 .git/hooks/pre-commit

However, this does not work for the submodule as it does not have the .git directory. Is there a way to force the submodule to use the hook from the parent directory? If I open the submodule independently I can make it work however, it's alot more practical to keep it under the parent as it keeps stuff organized.

Upvotes: 2

Views: 2369

Answers (1)

bk2204
bk2204

Reputation: 76489

With a submodule, you can either have a separate Git directory, or you can have an absorbed submodule. The latter is preferred these days by the Git code, and when one is in use, .git in the submodule is a special file called a gitlink, which points to a directory in the main repository's .git directory.

If you want to find the hooks directory for any Git repository, whether a submodule or not, you can run this command:

$ git rev-parse --git-path hooks

That will print the hooks directory. If you want to use the same hooks in the submodule as for the parent, you can make the submodule's hooks directory a symlink to the parent's.

Upvotes: 3

Related Questions