#!/bin/sh NULL_HASH='0000000000000000000000000000000000000000' commit_range() { local old_rev=$1 local new_rev=$2 case $NULL_HASH in $new_rev) return ;; $old_rev) git rev-list --reverse $new_rev --not --all ;; *) git rev-list --reverse $old_rev..$new_rev ;; esac } is_binary() { # perl -e '$/ = undef; exit 1 if (<> =~ /\x00/)' # hexdump -b | grep -q -w 000 hexdump -e '1/1 "%02x""\n"' | grep -qF 00 } changed_files() { commit=$1 root="" if ! git rev-parse --quiet --verify $commit~ >/dev/null then root="--root" fi git diff-tree --name-only --format= --diff-filter=bdx $root $commit } check_for_binary_files() { local commit=$1 local ref_name=$2 local files=$(changed_files $commit) for file in $files do if git show $commit:"$file" | head -c1024 | is_binary then echo "File '$file' contains binary content, rejected" echo "Offending commit: $commit in ref '$ref_name'" fi done } while read old_rev new_rev ref_name do commits=$(commit_range $old_rev $new_rev) for commit in $commits do check_for_binary_files $commit $ref_name || exit 1 done done exit 1