From c17b3173091a11b0a96700eee05913f6ae29ab69 Mon Sep 17 00:00:00 2001 From: Lars Bilke <lars.bilke@ufz.de> Date: Wed, 7 Aug 2019 14:48:50 +0200 Subject: [PATCH] [hook] Check git lfs files. Implementation adapted from http://shuhrat.github.io/programming/git-lfs-tips-and-tricks.html --- .pre-commit-config.yaml | 5 +++++ scripts/hooks/pre-commit-git-lfs.sh | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100755 scripts/hooks/pre-commit-git-lfs.sh diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index fb364a3bd91..cd2a9bf2f52 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 00000000000..4b71c7d77f8 --- /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 -- GitLab