From 963283b74e8c2c057a127e723cf412666d7b8f1d Mon Sep 17 00:00:00 2001 From: Joshua Potter Date: Sun, 3 Dec 2023 14:28:55 -0700 Subject: [PATCH] Guard on more exotic filenames. --- specs/clang/template/.githooks/pre-commit | 21 +++++++++---- specs/mix/template/.githooks/pre-commit | 21 +++++++++---- specs/nodejs/template/.githooks/pre-commit | 21 +++++++++---- specs/phoenix/template/.githooks/pre-commit | 35 ++++++++++++--------- specs/poetry/template/.githooks/pre-commit | 21 +++++++++---- 5 files changed, 81 insertions(+), 38 deletions(-) diff --git a/specs/clang/template/.githooks/pre-commit b/specs/clang/template/.githooks/pre-commit index 4deba40..42e68d2 100755 --- a/specs/clang/template/.githooks/pre-commit +++ b/specs/clang/template/.githooks/pre-commit @@ -1,12 +1,21 @@ #!/usr/bin/env bash set -e -FORMAT_FILES=$( - git --no-pager diff --name-status --no-color --cached | \ - awk '$1 != "D" && $2 ~ /\.c$|\.h$/ {print $NF}' +STAGED=$( + git --no-pager diff --name-only --no-color --cached --diff-filter=d | + # Remove quotations used to surrounding filenames with special characters. + sed -e "s/^\"//" -e "s/\"$//g" ) -if [ -n "$FORMAT_FILES" ]; then - clang-format -i "$FORMAT_FILES" - git add "$FORMAT_FILES" +TARGETS=() +while IFS= read -r FILENAME +do + if [[ "$FILENAME" =~ .*\.c ]] || [[ "$FILENAME" == .*\.h ]]; then + TARGETS+=("${FILENAME}") + fi +done <<< "$STAGED" + +if (( ${#TARGETS[@]} )); then + clang-format -i "${TARGETS[@]}" + git add "${TARGETS[@]}" fi diff --git a/specs/mix/template/.githooks/pre-commit b/specs/mix/template/.githooks/pre-commit index f15fd68..ecf4761 100755 --- a/specs/mix/template/.githooks/pre-commit +++ b/specs/mix/template/.githooks/pre-commit @@ -1,12 +1,21 @@ #!/usr/bin/env bash set -e -FORMAT_FILES=$( - git --no-pager diff --name-status --no-color --cached | \ - awk '$1 != "D" && $2 ~ /\.exs?$/ {print $NF}' +STAGED=$( + git --no-pager diff --name-only --no-color --cached --diff-filter=d | + # Remove quotations used to surrounding filenames with special characters. + sed -e "s/^\"//" -e "s/\"$//g" ) -if [ -n "$FORMAT_FILES" ]; then - mix format "$FORMAT_FILES" - git add "$FORMAT_FILES" +TARGETS=() +while IFS= read -r FILENAME +do + if [[ "$FILENAME" =~ .*\.exs? ]]; then + TARGETS+=("${FILENAME}") + fi +done <<< "$STAGED" + +if (( ${#TARGETS[@]} )); then + mix format "${TARGETS[@]}" + git add "${TARGETS[@]}" fi diff --git a/specs/nodejs/template/.githooks/pre-commit b/specs/nodejs/template/.githooks/pre-commit index 4036ff9..0684155 100755 --- a/specs/nodejs/template/.githooks/pre-commit +++ b/specs/nodejs/template/.githooks/pre-commit @@ -1,12 +1,21 @@ #!/usr/bin/env bash set -e -FORMAT_FILES=$( - git --no-pager diff --name-status --no-color --cached | \ - awk '$1 != "D" && $2 ~ /\.jsx?$|\.tsx?$/ {print $NF}' +STAGED=$( + git --no-pager diff --name-only --no-color --cached --diff-filter=d | + # Remove quotations used to surrounding filenames with special characters. + sed -e "s/^\"//" -e "s/\"$//g" ) -if [ -n "$FORMAT_FILES" ]; then - prettier --write "$FORMAT_FILES" - git add "$FORMAT_FILES" +TARGETS=() +while IFS= read -r FILENAME +do + if [[ "$FILENAME" =~ .*\.jsx? ]] || [[ "$FILENAME" == .*\.tsx? ]]; then + TARGETS+=("${FILENAME}") + fi +done <<< "$STAGED" + +if (( ${#TARGETS[@]} )); then + prettier --write "${TARGETS[@]}" + git add "${TARGETS[@]}" fi diff --git a/specs/phoenix/template/.githooks/pre-commit b/specs/phoenix/template/.githooks/pre-commit index 0b93003..be79b38 100755 --- a/specs/phoenix/template/.githooks/pre-commit +++ b/specs/phoenix/template/.githooks/pre-commit @@ -1,22 +1,29 @@ #!/usr/bin/env bash set -e -MIX_FILES=$( - git --no-pager diff --name-status --no-color --cached | \ - awk '$1 != "D" && $2 ~ /\.exs?$/ {print $NF}' +STAGED=$( + git --no-pager diff --name-only --no-color --cached --diff-filter=d | + # Remove quotations used to surrounding filenames with special characters. + sed -e "s/^\"//" -e "s/\"$//g" ) -if [ -n "$MIX_FILES" ]; then - mix format "$MIX_FILES" - git add "$MIX_FILES" +MIX_TARGETS=() +WEB_TARGETS=() +while IFS= read -r FILENAME +do + if [[ "$FILENAME" =~ .*exs? ]]; then + MIX_TARGETS+=("${FILENAME}") + elif [[ "$FILENAME" =~ .*jsx? ]] || [[ "$FILENAME" =~ .*tsx? ]]; then + WEB_TARGETS+=("${FILENAME}") + fi +done <<< "$STAGED" + +if (( ${#MIX_TARGETS[@]} )); then + mix format "${MIX_TARGETS[@]}" + git add "${MIX_TARGETS[@]}" fi -WEB_FILES=$( - git --no-pager diff --name-status --no-color --cached | \ - awk '$1 != "D" && $2 ~ /\.jsx?$|\.tsx?$/ {print $NF}' -) - -if [ -n "$WEB_FILES" ]; then - prettier --write "$WEB_FILES" - git add "$WEB_FILES" +if (( ${#WEB_TARGETS[@]} )); then + prettier --write "${WEB_TARGETS[@]}" + git add "${WEB_TARGETS[@]}" fi diff --git a/specs/poetry/template/.githooks/pre-commit b/specs/poetry/template/.githooks/pre-commit index ffc73b0..a6b2371 100644 --- a/specs/poetry/template/.githooks/pre-commit +++ b/specs/poetry/template/.githooks/pre-commit @@ -1,12 +1,21 @@ #!/usr/bin/env bash set -e -FORMAT_FILES=$( - git --no-pager diff --name-status --no-color --cached | \ - awk '$1 != "D" && $2 ~ /\.py$/ {print $NF}' +STAGED=$( + git --no-pager diff --name-only --no-color --cached --diff-filter=d | + # Remove quotations used to surrounding filenames with special characters. + sed -e "s/^\"//" -e "s/\"$//g" ) -if [ -n "$FORMAT_FILES" ]; then - black --quiet "$FORMAT_FILES" - git add "$FORMAT_FILES" +TARGETS=() +while IFS= read -r FILENAME +do + if [[ "$FILENAME" =~ .*\.py ]]; then + TARGETS+=("${FILENAME}") + fi +done <<< "$STAGED" + +if (( ${#TARGETS[@]} )); then + black --quiet "${TARGETS[@]}" + git add "${TARGETS[@]}" fi