Skip to content

Instantly share code, notes, and snippets.

@drAlberT
Last active February 11, 2019 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drAlberT/606e0698cb00805efa3a6f1c3ee97a3e to your computer and use it in GitHub Desktop.
Save drAlberT/606e0698cb00805efa3a6f1c3ee97a3e to your computer and use it in GitHub Desktop.
Automatically update git sub-modules
#!/usr/bin/env bash
#
# Quick script to automatically update git submodules on checkout.
# Save it in the repo `.git/hooks` dir and make it executable
#echo "post-checkout hook: '$1' '$2' '$3'"
oldRef=$1
newRef=$2
# Exit early if the local branch is behind the remote
LOCAL=$(git rev-parse @)
REMOTE=$(git rev-parse @{u} 2> /dev/null)
BASE=$(git merge-base @ @{u} 2> /dev/null)
if [[ "$LOCAL" != "$REMOTE" && "$LOCAL" = "$BASE" ]]; then
exit 0
fi
function isChanged {
git diff --name-only $oldRef $newRef | grep "^$1\$" >/dev/null 2>&1
}
if isChanged '.gitmodules'; then
echo "Change detected to '.gitmodules', updating git submodules."
git submodule update --init --force # --recursive
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment