Skip to content

Instantly share code, notes, and snippets.

@silvein
Created March 26, 2011 07:52
Show Gist options
  • Save silvein/888112 to your computer and use it in GitHub Desktop.
Save silvein/888112 to your computer and use it in GitHub Desktop.
This is a Git pre-commit hook I'm developing to make sure local submodule changes are pushed before committing. This would probably be better as a pre-push hook, but there does not appear to be a facility for that.
#!/bin/bash
function git_submodule_unchanged () {
SUB_MODULE=$1
GSC_RC=0
pushd $SUB_MODULE > /dev/null
SUB_BRANCH=$(git branch | grep '*' | cut -d' ' -f 2)
if [[ "$(git log --pretty=oneline origin/${SUB_BRANCH}..${SUB_BRANCH})" != "" ]]; then
GSC_RC=1
fi
popd > /dev/null
return $GSC_RC
}
RETURN_CODE=0
SUPERPROJECT_PWD="$(pwd)/"
for MODULE in $(git submodule foreach --recursive --quiet pwd)
do
for FILE_INDEX in $(git diff --cached --name-only)
do
if [[ "${MODULE##$SUPERPROJECT_PWD}" == "$FILE_INDEX" ]]; then
if ! git_submodule_unchanged $MODULE; then
echo "There are local only changes in submodule '$FILE_INDEX'."
echo "Please push these changes before committing the superproject."
RETURN_CODE=1
fi
fi
done
done
exit $RETURN_CODE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment