Skip to content

Instantly share code, notes, and snippets.

@wesbland
Last active February 9, 2018 21:41
Show Gist options
  • Save wesbland/3546c4483ceffe129a6e7fc4105fbf75 to your computer and use it in GitHub Desktop.
Save wesbland/3546c4483ceffe129a6e7fc4105fbf75 to your computer and use it in GitHub Desktop.
MPICH Style Checking Pre-Commit Hook
#!/bin/bash
cat > pre-commit.sh << "EOF"
#!/bin/sh
MIRROR=/tmp/${USER}/mpich-tmp-mirror
TMP_FILENAME=/tmp/${USER}/mpich-tmp-file
# Checkout a copy of the current index into MIRROR
git checkout-index --prefix=$MIRROR/ -af
# Remove files from MIRROR which are no longer present in the index
git diff-index --cached --name-only --diff-filter=D -z HEAD~1 | \
(cd $MIRROR && xargs -0 rm -f --)
filestring=`git diff --name-only --diff-filter=ACM HEAD~1`
IFS=', ' read -r -a file_array <<< "$filestring"
# Everything else happens in the temporary build tree
pushd $MIRROR > /dev/null
ret=0
for file in "${file_array[@]}"
do
if [[ ($file == *.c || $file == *.h || $file == *.c.in || $file == *.h.in) &&
!($file == *mpi.h.in || $file == *mpio.h.in) ]]; then
cp ${file} ${TMP_FILENAME}
maint/code-cleanup.sh ${file}
git --no-pager diff ${file} ${TMP_FILENAME}
if [ $? != 0 ] ; then
ret=1
fi
fi
done
rm -rf ${MIRROR} ${TMP_FILENAME}
if [ $ret != 0 ] ; then
exit $ret
fi
popd > /dev/null
EOF
chmod +x pre-commit.sh
set -xe
git config core.whitespace \
blank-at-eol,blank-at-eof,space-before-tab,tab-in-indent
git rebase --exec ./pre-commit.sh origin-pull/$GITHUB_PR_TARGET_BRANCH
#!/bin/bash
MIRROR=/tmp/${USER}/mpich-tmp-mirror
TMP_FILENAME=/tmp/${USER}/mpich-tmp-file
# Checkout a copy of the current index into MIRROR
git checkout-index --prefix=$MIRROR/ -af
# Remove files from MIRROR which are no longer present in the index
git diff-index --cached --name-only --diff-filter=D -z HEAD | \
(cd $MIRROR && xargs -0 rm -f --)
filestring=`git diff --cached --name-only --diff-filter=ACM`
IFS=', ' read -r -a file_array <<< "$filestring"
# Everything else happens in the temporary build tree
pushd $MIRROR > /dev/null
ret=0
for file in "${file_array[@]}"
do
if [[ ($file == *.c || $file == *.h || $file == *.c.in || $file == *.h.in) &&
!($file == *mpi.h.in || $file == *mpio.h.in) ]]; then
cp ${file} ${TMP_FILENAME}
maint/code-cleanup.sh ${file}
git --no-pager diff ${file} ${TMP_FILENAME}
if [ $? != 0 ] ; then
ret=1
fi
fi
done
rm -rf ${MIRROR} ${TMP_FILENAME}
if [ $ret != 0 ] ; then
RED='\033[0;31m'
NC='\033[0m' # No Color
echo -e "${RED}== CODE CLEANUP SCRIPT FAILED ==${NC}"
exit $ret
fi
popd > /dev/null
@wesbland
Copy link
Author

I've updated this script to deal with non-C/H files so it shouldn't trigger when working on Makefiles and other unrelated things.

@wesbland
Copy link
Author

wesbland commented Nov 20, 2017

I've also added a Jenkins hook for GitHub PR testing. It relies on the GitHub Integration Plugin for Jenkins, but could be adapted for others.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment