Add pre-commit hook (#13)

Co-authored-by: Antonio Andelic <antonio.andelic@memgraph.io>
This commit is contained in:
antonio2368 2020-10-06 13:57:33 +02:00 committed by GitHub
parent 9d6b578237
commit 1b50dc60f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 0 deletions

43
.githooks/pre-commit Executable file
View File

@ -0,0 +1,43 @@
#!/bin/sh
project_folder=$(git rev-parse --show-toplevel)
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=$(git hash-object -t tree /dev/null)
fi
# Redirect output to stderr.
exec 1>&2
tmpdir=$(mktemp -d repo-XXXXXXXX)
trap "rm -rf $tmpdir" EXIT INT
modified_files=$(git diff --cached --name-only --diff-filter=AM $against | sed -nE "/.*\.(cpp|cc|cxx|c|h|hpp|lcp)/p")
for file in $modified_files; do
echo "Checking $file..."
cp $project_folder/.clang-format $project_folder/.clang-tidy $tmpdir
git checkout-index --prefix="$tmpdir/" -- $file
echo "Running clang-format..."
$project_folder/tools/git-clang-format $tmpdir/$file
code=$?
if [ $code -ne 0 ]; then
break
fi
echo "Running clang-tidy..."
$project_folder/tools/git-clang-tidy $tmpdir/$file
code=$?
if [ $code -ne 0 ]; then
break
fi
done;
return $code

6
init
View File

@ -195,3 +195,9 @@ setup_virtualenv tests/stress
setup_virtualenv tests/integration/ldap setup_virtualenv tests/integration/ldap
echo "Done installing dependencies for Memgraph" echo "Done installing dependencies for Memgraph"
echo "Linking git hooks"
for hook in $(find $DIR/.githooks -type f -printf "%f\n"); do
ln -s -f "$DIR/.githooks/$hook" "$DIR/.git/hooks/$hook"
echo "Added $hook hook"
done;

View File

@ -11,9 +11,13 @@ data = subprocess.run(
["clang-format", "--output-replacements-xml", sys.argv[1]], ["clang-format", "--output-replacements-xml", sys.argv[1]],
stdout=subprocess.PIPE, check=True).stdout.decode( stdout=subprocess.PIPE, check=True).stdout.decode(
"utf-8").strip().split("\n") "utf-8").strip().split("\n")
has_error = False
for row in data: for row in data:
match = re.match( match = re.match(
r"^<replacement offset='([0-9]+)' length='([0-9]+)'>", row) r"^<replacement offset='([0-9]+)' length='([0-9]+)'>", row)
if match: if match:
has_error = True
offset = int(match.group(1)) + int(match.group(2)) offset = int(match.group(1)) + int(match.group(2))
print("warning:{}:{}".format(offset, MESSAGE)) print("warning:{}:{}".format(offset, MESSAGE))
sys.exit(1 if has_error else 0)