summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorJoseph Herlant <aerostitch@users.noreply.github.com>2018-09-02 00:30:29 -0700
committerjkoan <jkoan@users.noreply.github.com>2018-09-02 09:30:29 +0200
commit9ad40a0b74bf66cdfd0e94bb58f3372379b163ed (patch)
treefb255fe563abe10aed2a2ee6076143b0e39a7d4f /scripts
parent13c9d8ad417340481450dc949c580de5e246f2d4 (diff)
downloadnavit-9ad40a0b74bf66cdfd0e94bb58f3372379b163ed.tar.gz
add:helper:Add a pre-commit hook script for users to use locally (#603)
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/pre-commit44
1 files changed, 44 insertions, 0 deletions
diff --git a/scripts/pre-commit b/scripts/pre-commit
new file mode 100755
index 000000000..4a07a26a8
--- /dev/null
+++ b/scripts/pre-commit
@@ -0,0 +1,44 @@
+#!/usr/bin/env bash
+set -eu
+
+# #############################################################################
+# This is a pre-commit hook that allows you to respect the code formatting
+# chosen by the Navit team.
+#
+# To install it, create a folder named "hooks" in the .git directory of this
+# project and just symlink this script inside it. Or run the following script
+# from the root directory of the project:
+# mkdir .git/hooks
+# ln -s ${PWD}/scripts/pre-commit ${PWD}/.git/hooks/
+#
+# This will not work on Windows platform unless you are using cygwin.
+# #############################################################################
+
+files=$(git diff --cached --name-only --diff-filter=ACM)
+git_dir=$(git rev-parse --show-toplevel)
+
+# check for common misspells if misspell is installed
+[[ -n "$(which misspell)" ]] && misspell
+if [[ -z "$(which astyle)" ]]; then
+ echo "Unable to find the astyle executable. Please install it to have automatic formatting of your files."
+fi
+
+# Only work on the files that are part of the commit
+for f in $files; do
+ if [[ -e "${git_dir}/${f}" ]]; then
+ if [[ "${f: -4}" != ".bat" ]]; then
+ # Makes sure to not commit ^M
+ [[ -n "$(which dos2unix)" ]] && dos2unix -s -S -q "${git_dir}/${f}"
+ # Removes trailing spaces
+ [[ "$(file -bi """${git_dir}/${f}""")" =~ ^text ]] && sed 's/\s*$//' -i "${git_dir}/${f}"
+ git add "${git_dir}/${f}"
+ fi
+ # Formats any *.c and *.cpp files
+ if [[ "${f: -2}" == ".c" ]] || [[ "${f: -4}" == ".cpp" ]]; then
+ if [[ -n "$(which astyle)" ]]; then
+ astyle --indent=spaces=4 --style=attach -n --max-code-length=120 -xf -xh "${git_dir}/${f}"
+ git add "${git_dir}/${f}"
+ fi
+ fi
+ fi
+done