1

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.

  • 1. https://stackoverflow.com/a/67848922/7976758 2. Submodules have `.git` directory; but you have to understand submodules are separate repositories and have separate hooks. – phd Oct 20 '21 at 19:20
  • Not for the repo I'm in: from partent repo cd submodule ls -lah .rw-r--r-- 28 uname 7 Oct DATE .git if i clone the submodule separately I can edit it (as I mentioned in the question) – Thomas Hall Oct 20 '21 at 19:54
  • I'd be totally down to add the hooks to the submodule but I can't do it when it's attached to the parent module and created by git submodule update --init as the submodule dosen't have a .git directory, just a file. When submod is independent: `drwxrwxr-x - user 20 Oct DATE .git` When submod is attached to parent and created by git submodule update: `.rw-r--r-- 28 uname 7 Oct DATE .git` – Thomas Hall Oct 20 '21 at 20:07
  • Read the `.git` file: it contains the path to the submodule repository. Look there: it's a repository, with a `hooks` directory. You can put Git hooks here. It's all kind of painful though. Using `git rev-parse --git-directory` will help you automate this if you want to go that route. – torek Oct 20 '21 at 21:54

1 Answers1

2

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.

bk2204
  • 64,793
  • 6
  • 84
  • 100
  • This is the answer that worked for me. I had to: `vim .git/modules/submodule/hooks/prepare-commit-msg`, `vim .git/modules/submodule/hooks/pre-commit`. Thanks. – Thomas Hall Oct 21 '21 at 12:39