Forked from brickgao/cpplint_pre_commit_hook.sh
Last active
November 8, 2018 15:05
-
-
Save shawndooley/1aee8217e75a323299cf6bd86ee5ac90 to your computer and use it in GitHub Desktop.
cpplint pre-commit hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# Modified from http://qiita.com/janus_wel/items/cfc6914d6b7b8bf185b6 | |
# | |
# An example hook script to verify what is about to be committed. | |
# Called by "git commit" with no arguments. The hook should | |
# exit with non-zero status after issuing an appropriate message if | |
# it wants to stop the commit. | |
# | |
# To enable this hook, rename this file to "pre-commit". | |
# Added a prompt to allow commiting anyways even if there are style isseus. | |
# Put filters in an array. I thought it was easier to add or remove them when each one is on its own line. | |
# If we have a STDIN, use it, otherwise get one | |
if tty >/dev/null 2>&1; then | |
TTY=$(tty) | |
else | |
TTY=/dev/tty | |
fi | |
# http://djm.me/ask | |
ask() { | |
while true; do | |
if [ "${2:-}" = "Y" ]; then | |
prompt="Y/n" | |
default=Y | |
elif [ "${2:-}" = "N" ]; then | |
prompt="y/N" | |
default=N | |
else | |
prompt="y/n" | |
default= | |
fi | |
# Ask the question (not using "read -p" as it uses stderr not stdout) | |
echo -n "$1 [$prompt] " | |
# Read the answer | |
read REPLY < "$TTY" | |
# Default? | |
if [ -z "$REPLY" ]; then | |
REPLY=$default | |
fi | |
# Check if the reply is valid | |
case "$REPLY" in | |
Y*|y*) return 0 ;; | |
N*|n*) return 1 ;; | |
esac | |
done | |
} | |
if git rev-parse --verify HEAD >/dev/null 2>&1 | |
then | |
against=HEAD | |
else | |
# Hash for empty tree | |
against=$(git hash-object -t tree /dev/null) | |
fi | |
# Redirect output to stderr. | |
exec 1>&2 | |
cpplint=cpplint | |
sum=0 | |
declare -a filters=( | |
"-build/include" | |
"-build/include_order" | |
"-build/namespaces" | |
"-legal/copyright" | |
"-runtime/references" | |
"-whitespace/indent" | |
"-build/header_guard" | |
"-whitespace/line_length" | |
"-readability/namespace" | |
) | |
filters_joined=$(IFS=",$IFS"; printf '%s\n' "${filters[*]}") | |
# for cpp | |
for file in $(git diff-index --name-status $against -- | grep -E '\.[ch](pp)?$' | awk '{print $2}'); do | |
$cpplint --filter=$filters_joined $file | |
sum=$(expr ${sum} + $?) | |
done | |
if [ ${sum} -ne 0 ]; then | |
if ask "Files do not match style guidle. Commit anyway?"; then | |
exit 0 | |
else | |
exit 1 | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment