diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index fb364a3bd91117ac716e1d7b006d12bcd630f3fb..cd2a9bf2f52643ac2de4431a52022792bdaaac5d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -16,5 +16,10 @@ repos: entry: git diff --check --cached -- ':!*.md' ':!*.pandoc' ':!*.asc' ':!*.dat' ':!*.ts' language: system stages: [commit, manual] + - id: git-lfs + name: Check git lfs files + entry: scripts/hooks/pre-commit-git-lfs.sh + language: system + stages: [commit, manual] exclude: 'ThirdParty/*|Tests/Data/*' diff --git a/scripts/hooks/pre-commit-git-lfs.sh b/scripts/hooks/pre-commit-git-lfs.sh new file mode 100755 index 0000000000000000000000000000000000000000..4b71c7d77f8f7f495d802628774bb3e435625981 --- /dev/null +++ b/scripts/hooks/pre-commit-git-lfs.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +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//') + +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 + +if [[ -n "$BINARY_FILES" ]]; then + echo "Attention!" + echo "----------" + echo "You tried to commit binary files:" + echo -e "\x1B[31m$BINARY_FILES\x1B[0m" + echo "Make sure you have git-lfs installed properly!" + echo "Revert your changes and commit those files with git-lfs!" + echo "----------" + exit 1 +fi