(scripts) fix for spaces in paths/arguments of whitespace remover
This commit is contained in:
@@ -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 <<EOF
|
||||
Usage: remove-whitespace [-h] [-n] path [path]
|
||||
|
||||
@@ -123,12 +143,17 @@ EOF
|
||||
return 0
|
||||
fi
|
||||
|
||||
if [[ -z "${files[@]}" ]]; then
|
||||
echo "no files with trailing whitespace or dos style line endings found"
|
||||
return 0
|
||||
fi
|
||||
|
||||
if [[ -z "${no_action}" ]]; then
|
||||
for file in ${files[@]}; do
|
||||
for file in "${files[@]}"; do
|
||||
if [[ -n "${ffunix}" ]]; then
|
||||
sed -i "${sed_cmd}" ${file}
|
||||
sed -i "${sed_cmd}" "${file}"
|
||||
else
|
||||
sed -i "${sed_cmd}" ${file}
|
||||
sed -i "${sed_cmd}" "${file}"
|
||||
fi
|
||||
done
|
||||
fi
|
||||
@@ -139,7 +164,7 @@ EOF
|
||||
tput smso
|
||||
echo "The following files contain whitespace:"
|
||||
tput sgr0
|
||||
for file in ${files[@]}; do
|
||||
for file in "${files[@]}"; do
|
||||
echo "${file}"
|
||||
done
|
||||
elif [[ -z "${silent}" ]] &&
|
||||
@@ -148,7 +173,7 @@ EOF
|
||||
tput smso
|
||||
echo "The following files contained whitespace:"
|
||||
tput sgr0
|
||||
for file in ${files[@]}; do
|
||||
for file in "${files[@]}"; do
|
||||
echo "${file}"
|
||||
done
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user