From f613009ecd1d79252eca29f252e72e49e31c74a2 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 18 Oct 2012 17:28:31 -0400 Subject: rewrite jh-checksource --- jh-checksource.sh | 98 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 63 insertions(+), 35 deletions(-) diff --git a/jh-checksource.sh b/jh-checksource.sh index ef3cf47..89046e4 100644 --- a/jh-checksource.sh +++ b/jh-checksource.sh @@ -3,43 +3,71 @@ sep='' resep='@' -safe_types=( - '.*text.*' - '(GIF|JPEG|PNG) image data.*' -) -safe_files=("$@") - -unsafe_files="$(mktemp)" - -safevar="$(mktemp)" -find . -type f -exec file -F"$sep" {} + | while read line; do - file="$(echo "$line"|sed "s${resep}${sep}.*${resep}${resep}")" - type="$(echo "$line"|sed "s${resep}.*${sep}\s*${resep}${resep}")" - echo false > "$safevar" - if [[ "$file" =~ ".*/\.git/.*" ]]; then - echo true > "$safevar" - fi - for filename in "${safe_files[@]}"; do - if [[ "$file" = "$filename" ]]; then - echo true > "$safevar" +safe_types_regexp=('text' '(GIF|JPEG|PNG) image data') +safe_types_string=() +safe_files_rexexp=('.*/\.git/.*') +safe_files_string=() + +normalize_filename() { + local cwd="`pwd`" + readlink -m -- "$1"|sed "s|^$cwd/|./|" +} + +matches_string() { + local needle=$1 + shift + for straw in "$@"; do + if [[ "$needle" = "$straw" ]]; then + return 0 fi done - for filetype in "${safe_types[@]}"; do - if [[ "$type" =~ "$filetype" ]]; then - echo true > "$safevar" + return 1 +} + +matches_regexp() { + local needle=$1 + shift + for straw in "$@"; do + if [[ "$needle" =~ $straw ]]; then + return 0 fi done - if [[ "$(cat "$safevar")" = 'true' ]]; then - printf "%s\n" "$file" + return 1 +} + +main() { + # Parse arguments + for file in "$@"; do safe_files_string+=("$(normalize_filename "$file")"); done + + # Init + unsafe_files="$(mktemp)" + + # Heavy lifting + find . -type f -exec file -F"$sep" {} + | while read line; do + file="$(echo "$line"|sed "s${resep}${sep}.*${resep}${resep}")" + type="$(echo "$line"|sed "s${resep}.*${sep}\s*${resep}${resep}")" + + file="$(normalize_filename "$file")" + + if \ + matches_string "$file" "${safe_files_string[@]}" || \ + matches_string "$type" "${safe_types_string[@]}" || \ + matches_regexp "$file" "${safe_files_regexp[@]}" || \ + matches_regexp "$type" "${safe_types_regexp[@]}" ; then + : # do nothing + else + printf "%s\n" "$file" + fi + done > "$unsafe_files" + + if [[ -n "$(cat "$unsafe_files")" ]]; then + echo "==> WARNING: The source directory `pwd` contains binary files:" + sed 's/./ -> &/' "$unsafe_files" + rm -f "$unsafe_files" + exit 1 + else + rm -f "$unsafe_files" fi -done > "$unsafe_files" -rm -f "$safevar" - -if [[ -n "$(cat "$unsafe_files")" ]]; then - echo " ==> The source directory `pwd` contains binary files:" - sed 's/./ ->/' "$unsafe_files" - rm -f "$unsafe_files" - exit 1 -else - rm -f "$unsafe_files" -fi +} + +main "$@" -- cgit v1.2.3-54-g00ecf