(scripts) fix for spaces in paths/arguments of whitespace remover

This commit is contained in:
zegonix
2026-05-27 18:22:18 +02:00
committed by scbj
parent c1e058c61e
commit e4469cb67d
+48 -23
View File
@@ -4,13 +4,17 @@ function remove-whitespace {
# exclude file types # exclude file types
# (ignore generated files/directories) # (ignore generated files/directories)
exclude_paths=( exclude_paths=(
'*/.git/*' ".git"
) )
exclude_files=( exclude_files=(
"*.lst" "*.lst"
"*.map" "*.map"
"*.svd" "*.svd"
"*.sym" "*.sym"
"*.doc"
"*.docx"
"*.ppt"
"*.pptx"
) )
unset help unset help
@@ -23,7 +27,7 @@ function remove-whitespace {
unset args unset args
unset arguments unset arguments
for arg in $@; do for arg in "$@"; do
if [[ "${arg}" =~ ^-[a-zA-Z]{2,}$ ]]; then if [[ "${arg}" =~ ^-[a-zA-Z]{2,}$ ]]; then
temp=($(echo ${arg} | grep --color=never -o ".")) temp=($(echo ${arg} | grep --color=never -o "."))
args+=(${temp[@]/#/-}) args+=(${temp[@]/#/-})
@@ -32,14 +36,14 @@ function remove-whitespace {
fi fi
done done
for arg in ${args[@]}; do for arg in "${args[@]}"; do
[[ "${arg}" != "--" ]] && arguments+=("${arg}") [[ "${arg}" != "--" ]] && arguments+=("${arg}")
done done
unset args unset args
for arg in ${arguments[@]}; do for arg in "${arguments[@]}"; do
unset arg_found unset arg_found
case ${arg} in case "${arg}" in
"-u" | "--unix") "-u" | "--unix")
arg_found="true" arg_found="true"
ffunix="true" ffunix="true"
@@ -82,26 +86,42 @@ function remove-whitespace {
grep_regex=$'[[:blank:]]+\r?$' grep_regex=$'[[:blank:]]+\r?$'
fi fi
for arg in ${path_args[@]}; do # compile exclude arguments
files+=($( 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}" \ find "${arg}" \
${exclude_paths[@]/#/-not -path } \ ${excludes[@]} \
${exclude_files[@]/#/-not -name } \
-type f \ -type f \
-exec grep -EIq "${grep_regex}" {} \; \ -exec grep -EIq "${grep_regex}" {} \; \
-print -print
)) )")
done
for arg in ${file_args[@]}; do
grep -EIq "${grep_regex}" ${arg} && files+=("${arg}")
done done
if [[ -z "${files[@]}" ]] && [[ -n "${path_args}" ]]; then IFS=$'\n'
echo "no files with trailing whitespace or dos style line endings found" files=(${files})
return 0 unset IFS
fi
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 cat <<EOF
Usage: remove-whitespace [-h] [-n] path [path] Usage: remove-whitespace [-h] [-n] path [path]
@@ -123,12 +143,17 @@ EOF
return 0 return 0
fi 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 if [[ -z "${no_action}" ]]; then
for file in ${files[@]}; do for file in "${files[@]}"; do
if [[ -n "${ffunix}" ]]; then if [[ -n "${ffunix}" ]]; then
sed -i "${sed_cmd}" ${file} sed -i "${sed_cmd}" "${file}"
else else
sed -i "${sed_cmd}" ${file} sed -i "${sed_cmd}" "${file}"
fi fi
done done
fi fi
@@ -139,7 +164,7 @@ EOF
tput smso tput smso
echo "The following files contain whitespace:" echo "The following files contain whitespace:"
tput sgr0 tput sgr0
for file in ${files[@]}; do for file in "${files[@]}"; do
echo "${file}" echo "${file}"
done done
elif [[ -z "${silent}" ]] && elif [[ -z "${silent}" ]] &&
@@ -148,7 +173,7 @@ EOF
tput smso tput smso
echo "The following files contained whitespace:" echo "The following files contained whitespace:"
tput sgr0 tput sgr0
for file in ${files[@]}; do for file in "${files[@]}"; do
echo "${file}" echo "${file}"
done done
fi fi