Skip to content

Instantly share code, notes, and snippets.

@skfarhat
Last active August 1, 2016 18:46
Show Gist options
  • Save skfarhat/5091bd2b3755c2dabce6e35d1eed3a51 to your computer and use it in GitHub Desktop.
Save skfarhat/5091bd2b3755c2dabce6e35d1eed3a51 to your computer and use it in GitHub Desktop.
#!/bin/bash
# pre-commit.sh
#
# NOTE:
# Typically placed in scripts/ directory in the root of a CMake project
# the expected directory structure
#
# Expects lint.sh in the same directory
#
# ├── project_root
# |.. ├── .git
# │   ├── CMakeLists.txt
# │   ├── bin
# │   ├── build
# │   ├── scripts
# │   │   ├── pre-commit
# │   │   ├── lint.sh
# │   ├── src
# │   ├── tests
# TODO:
# * This doesn't work for the first commit which atm must be done with git commit -n
# Fix this later
#
function safe_exit {
git stash pop -q
exit $1
}
exec < /dev/tty
ROOT=$(git rev-parse --show-toplevel)
git stash -q --keep-index
# ----------------------------------------------------
# Test
mkdir -p $ROOT/build
cd $ROOT/build
echo "Running CMake"
cmake ..
if [ $? -ne 0 ]; then
echo "Commit aborted because CMake failed"
safe_exit 1
fi
echo "Running Make"
make -j 8
if [ $? -ne 0 ]; then
echo "Commit aborted because Make failed"
safe_exit 1
fi
echo "Done with Make"
# do some linting
cd $ROOT
FILES=$(git diff --cached --name-only)
for file in $FILES; do
scripts/lint.sh $file
if [ $? -ne 0 ]; then
safe_exit 1
fi
done
# ----------------------------------------------------
cd $ROOT
safe_exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment