如何自动从子模块暂存提交

时间:2019-09-09 10:08:55

标签: git git-submodules githooks

我正在同时包含一个子模块和一个子模块的项目中工作。 由于它们不断在本地构建,因此我至少需要在子模块内部进行的每次提交时都更新子模块。

这是一个受this post on hooksthis post on submodules以及this for cleaning the envfor the debugging启发的post-commit钩子

这已经是一个可行的解决方案,但是如果有更好的方法,我将接受答案。

#!/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"

而不是提交更改,它只会暂存它们,因此会在下一次完全提交时提交它们。

1 个答案:

答案 0 :(得分:0)

基本的post-commit钩子:

#!/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"
相关问题