From bfda1cb41564147ded4c94d3709c4b95e30fb158 Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Fri, 9 Aug 2019 11:34:26 +0200
Subject: [PATCH] [hook] Added file extension check, fixed lfs check.

---
 .pre-commit-config.yaml                     |  5 +++++
 scripts/hooks/pre-commit-file-extensions.sh | 24 +++++++++++++++++++++
 scripts/hooks/pre-commit-git-lfs.sh         | 23 ++++++++++++++------
 3 files changed, 46 insertions(+), 6 deletions(-)
 create mode 100755 scripts/hooks/pre-commit-file-extensions.sh

diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index cd2a9bf2f52..2af73f120b4 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -16,6 +16,11 @@ repos:
         entry: git diff --check --cached -- ':!*.md' ':!*.pandoc' ':!*.asc' ':!*.dat' ':!*.ts'
         language: system
         stages: [commit, manual]
+    -   id: file-extensions
+        name: Check file extensions
+        entry: scripts/hooks/pre-commit-file-extensions.sh
+        language: system
+        stages: [commit, manual]
     -   id: git-lfs
         name: Check git lfs files
         entry: scripts/hooks/pre-commit-git-lfs.sh
diff --git a/scripts/hooks/pre-commit-file-extensions.sh b/scripts/hooks/pre-commit-file-extensions.sh
new file mode 100755
index 00000000000..1b6c6d9933b
--- /dev/null
+++ b/scripts/hooks/pre-commit-file-extensions.sh
@@ -0,0 +1,24 @@
+#!/usr/bin/env bash
+
+# set -e
+
+# 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" )
+
+if [[ -n $files ]];
+then
+    echo "Attention!"
+    echo "----------"
+    echo "Found files that contain capital letters in the file extension."
+    echo "Please rename the following files and commit again:"
+
+    while read -r file; do
+        echo -e '\E[0;32m'"$file"'\033[0m'
+    done <<< "$files"
+    # Abort commit
+    exit 1
+fi
+
+exit 0
diff --git a/scripts/hooks/pre-commit-git-lfs.sh b/scripts/hooks/pre-commit-git-lfs.sh
index 4b71c7d77f8..88caf5f95bc 100755
--- a/scripts/hooks/pre-commit-git-lfs.sh
+++ b/scripts/hooks/pre-commit-git-lfs.sh
@@ -3,17 +3,28 @@
 set -e
 
 BINARY_FILES=""
-CHANGED_FILES=$(git diff --cached --name-only --diff-filter=ACM)
-LFS_FILES=$(echo $CHANGED_FILES | xargs -I{lin} git check-attr filter "{lin}" | grep 'filter: lfs$' | sed -e 's/: filter: lfs//')
+LFS_FILES=""
+CHANGED_FILES=$(git diff --cached --name-only --diff-filter=ACMRTXBU)
 
-for FILE in $LFS_FILES; do
-    SOFT_SHA=$(git hash-object -w $FILE)
-    RAW_SHA=$(git hash-object -w --no-filters $FILE)
+while read -r FILE; do
+    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"
+    fi
+done <<< "$CHANGED_FILES"
+
+if [ -z "$LFS_FILES" ]; then
+    exit 0
+fi
+
+while read -r FILE; 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
+done <<< "$LFS_FILES"
 
 if [[ -n "$BINARY_FILES" ]]; then
     echo "Attention!"
-- 
GitLab