From f95ce87a8a5894a87ca36cfd203faddc30762ea6 Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Mon, 12 Aug 2019 15:33:59 +0200
Subject: [PATCH] [hooks] Added file name check.

Fixed own hooks to use the correct files passes by pre-commit.
---
 .pre-commit-config.yaml                     |  6 ++++
 scripts/hooks/pre-commit-file-extensions.sh |  3 +-
 scripts/hooks/pre-commit-file-names.sh      | 39 +++++++++++++++++++++
 scripts/hooks/pre-commit-git-lfs.sh         | 15 ++++----
 4 files changed, 55 insertions(+), 8 deletions(-)
 create mode 100755 scripts/hooks/pre-commit-file-names.sh

diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index addcf5e1319..1437ab2a2f4 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -20,6 +20,12 @@ repos:
         name: Check file extensions
         entry: scripts/hooks/pre-commit-file-extensions.sh
         language: system
+        files: '.*\.cpp'
+        stages: [commit, manual]
+    -   id: file-names
+        name: Check file names
+        entry: scripts/hooks/pre-commit-file-names.sh
+        language: system
         stages: [commit, manual]
     -   id: git-lfs
         name: Check git lfs files
diff --git a/scripts/hooks/pre-commit-file-extensions.sh b/scripts/hooks/pre-commit-file-extensions.sh
index 1b6c6d9933b..4affa7f697b 100755
--- a/scripts/hooks/pre-commit-file-extensions.sh
+++ b/scripts/hooks/pre-commit-file-extensions.sh
@@ -4,8 +4,7 @@
 
 # Check for uppercase letters in filename extensions
 pattern=".*\.\w*([A-Z]+)\w*$"
-files=$(git diff --cached --find-copies --find-renames --name-only --diff-filter=ACMRTXBU |
-        grep -E  "$pattern" )
+files=$(echo $@ | grep -E "$pattern")
 
 if [[ -n $files ]];
 then
diff --git a/scripts/hooks/pre-commit-file-names.sh b/scripts/hooks/pre-commit-file-names.sh
new file mode 100755
index 00000000000..abdc6f10945
--- /dev/null
+++ b/scripts/hooks/pre-commit-file-names.sh
@@ -0,0 +1,39 @@
+#!/bin/bash
+
+if git rev-parse --verify HEAD >/dev/null 2>&1
+then
+	against=HEAD
+else
+	# Initial commit: diff against an empty tree object
+	against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
+fi
+
+# If you want to allow non-ASCII filenames set this variable to true.
+allownonascii=$(git config --bool hooks.allownonascii)
+
+# Redirect output to stderr.
+exec 1>&2
+
+# Cross platform projects tend to avoid non-ASCII filenames; prevent
+# them from being added to the repository. We exploit the fact that the
+# printable range starts at the space character and ends with tilde.
+if [ "$allownonascii" != "true" ] &&
+	# Note that the use of brackets around a tr range is ok here, (it's
+	# even required, for portability to Solaris 10's /usr/bin/tr), since
+	# the square bracket bytes happen to fall in the designated range.
+	test $($@ |
+	  LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
+then
+	cat <<\EOF
+Error: Attempt to add a non-ASCII file name.
+
+This can cause problems if you want to work with people on other platforms.
+
+To be portable it is advisable to rename the file.
+
+If you know what you are doing you can disable this check using:
+
+  git config hooks.allownonascii true
+EOF
+	exit 1
+fi
diff --git a/scripts/hooks/pre-commit-git-lfs.sh b/scripts/hooks/pre-commit-git-lfs.sh
index 88caf5f95bc..820f73e2250 100755
--- a/scripts/hooks/pre-commit-git-lfs.sh
+++ b/scripts/hooks/pre-commit-git-lfs.sh
@@ -4,27 +4,30 @@ set -e
 
 BINARY_FILES=""
 LFS_FILES=""
-CHANGED_FILES=$(git diff --cached --name-only --diff-filter=ACMRTXBU)
 
-while read -r FILE; do
+LFS_FILES=()
+for FILE in "$@"
+do
+    echo FILE: $FILE
     LFS_FILE=$(git check-attr filter "$FILE" | grep 'filter: lfs$' | sed -e 's/: filter: lfs//')
     if [ ! -z "$LFS_FILE" ]; then
-        LFS_FILES="$LFS_FILES $LFS_FILE"
+        LFS_FILES+=("$LFS_FILE")
     fi
-done <<< "$CHANGED_FILES"
+done
 
 if [ -z "$LFS_FILES" ]; then
     exit 0
 fi
 
-while read -r FILE; do
+for FILE in ${LFS_FILES[@]}
+do
     SOFT_SHA=$(git hash-object -w "$FILE")
     RAW_SHA=$(git hash-object -w --no-filters "$FILE")
 
     if [ $SOFT_SHA == $RAW_SHA ]; then
         BINARY_FILES="$FILE\n$BINARY_FILES"
     fi
-done <<< "$LFS_FILES"
+done
 
 if [[ -n "$BINARY_FILES" ]]; then
     echo "Attention!"
-- 
GitLab