diff --git a/scripts/trailing-whitespace.sh b/scripts/trailing-whitespace.sh index 6daf7d7..5a9e7d0 100755 --- a/scripts/trailing-whitespace.sh +++ b/scripts/trailing-whitespace.sh @@ -4,13 +4,17 @@ function remove-whitespace { # exclude file types # (ignore generated files/directories) exclude_paths=( - '*/.git/*' + ".git" ) exclude_files=( "*.lst" "*.map" "*.svd" "*.sym" + "*.doc" + "*.docx" + "*.ppt" + "*.pptx" ) unset help @@ -23,7 +27,7 @@ function remove-whitespace { unset args unset arguments - for arg in $@; do + for arg in "$@"; do if [[ "${arg}" =~ ^-[a-zA-Z]{2,}$ ]]; then temp=($(echo ${arg} | grep --color=never -o ".")) args+=(${temp[@]/#/-}) @@ -32,14 +36,14 @@ function remove-whitespace { fi done - for arg in ${args[@]}; do + for arg in "${args[@]}"; do [[ "${arg}" != "--" ]] && arguments+=("${arg}") done unset args - for arg in ${arguments[@]}; do + for arg in "${arguments[@]}"; do unset arg_found - case ${arg} in + case "${arg}" in "-u" | "--unix") arg_found="true" ffunix="true" @@ -82,26 +86,42 @@ function remove-whitespace { grep_regex=$'[[:blank:]]+\r?$' fi - for arg in ${path_args[@]}; do - files+=($( + # compile exclude arguments + unset excludes + excludes=("(") + + for n in ${!exclude_paths[@]}; do + excludes+=("-name" "${exclude_paths[$n]}") + if ((${n} < ${#exclude_paths[@]} - 1)); then + excludes+=("-o") + fi + done + + excludes+=(")" "-type d" "-prune" "-o") + + for arg in "${exclude_files[@]}"; do + excludes+=("-not" "-name" "${arg}") + done + + for arg in "${path_args[@]}"; do + files=("$( find "${arg}" \ - ${exclude_paths[@]/#/-not -path } \ - ${exclude_files[@]/#/-not -name } \ + ${excludes[@]} \ -type f \ -exec grep -EIq "${grep_regex}" {} \; \ -print - )) - done - for arg in ${file_args[@]}; do - grep -EIq "${grep_regex}" ${arg} && files+=("${arg}") + )") done - if [[ -z "${files[@]}" ]] && [[ -n "${path_args}" ]]; then - echo "no files with trailing whitespace or dos style line endings found" - return 0 - fi + IFS=$'\n' + files=(${files}) + unset IFS - if [[ -n "${help}" ]] || [[ -z "${files[@]}" ]]; then + for arg in "${file_args[@]}"; do + grep -EIq "${grep_regex}" "${arg}" && files+=("${arg}") + done + + if [[ -n "${help}" ]] || [[ -z "${file_args[@]} ${path_args[@]}" ]]; then cat <