0

I am working on a project containing a submodule and a submodule at the same time. As they are continually build locally, I need to update the submodule at least on every commit I make inside of it.

Here is a post-commit hook inspired by this post on hooks and this post on submodules and also by this for cleaning the env and for the debugging

this is already a working solution, but if there is a better way I will accept an answer.

#!/bin/sh

### functions

exec_push()
{
ping -c 1 heise.de 2>&1 >> /dev/null # check connectivity
PING_RESULT=$?
if [ $PING_RESULT -eq "0" ]
then
    git push --recurse-submodules=on-demand --quiet
else
    echo "sorry no internet connectivity"
    exit 1
fi
}

### end functions

echo "post-commit started"
branch="$(git rev-parse --abbrev-ref HEAD)"
echo "running on branch $branch"
superproject="$(git rev-parse --show-superproject-working-tree)"
echo "running for superproject $superproject"
current_submodule="$(pwd)"
echo "adding submodule $current_submodule"

if [ "$branch" = "master" ]
then
    exec_push
    env -i git -C "$superproject" add .
fi

echo "post-commit finished"

instead of commiting the change, it only stages them, so they are commited with the next full commit.

927589452
  • 13
  • 1
  • 5

1 Answers1

0

A basic post-commit-hook:

#!/bin/sh

echo "post-commit started"
branch="$(git rev-parse --abbrev-ref HEAD)"
echo "running on branch $branch"
superproject="$(git rev-parse --show-superproject-working-tree)"
echo "running for superproject $superproject"
current_submodule="$(pwd)"
echo "adding submodule $current_submodule"

if [ "$branch" = "master" ]
then
    env -i git -C "$superproject" add .
fi

echo "post-commit finished"
927589452
  • 13
  • 1
  • 5
  • My complains about the code are: `env -i` clears too much, it could, for example, clear `$PATH` if my `git` is in a non-standard path; use [`unset $(git rev-parse --local-env-vars)`](https://stackoverflow.com/a/6394777/7976758). `git add .` adds too much; the command could add changes in the superproject not related to the submodule; there have to be a way to name the submodule in `git add `. – phd Sep 09 '19 at 12:29
  • I will add the `unset rev-parse` directly and I will have a look for adding, but as the cwd isn't changed it should add the submodule – 927589452 Sep 10 '19 at 07:44