Skip to content

Instantly share code, notes, and snippets.

@zcyemi
Created July 22, 2025 08:45
Show Gist options
  • Save zcyemi/1cfbe5db7c16966e7b4c6d04888ef687 to your computer and use it in GitHub Desktop.
Save zcyemi/1cfbe5db7c16966e7b4c6d04888ef687 to your computer and use it in GitHub Desktop.
Git Pre-Commit Hook for filter non UTF-8 encoding.
#!/usr/bin/env bash
#
# .git/hooks/pre-commit
# Prevent committing non–UTF‑8 files, but only for certain extensions
set -e # exit on any error
# Define which file extensions to check (without the leading dot)
EXTENSIONS=(cs)
# Collect all added/copied/modified files in the index
files=$(git diff --cached --name-only --diff-filter=ACM)
# If no files to check, exit cleanly
[ -z "$files" ] && exit 0
# Helper: join array for grep
ext_pattern=$(printf '|%s' "${EXTENSIONS[@]}")
ext_pattern="(${ext_pattern:1})$"
# Array to hold non‑UTF‑8 files
non_utf8=()
for file in $files; do
# Skip if it's not a regular file
[ -f "$file" ] || continue
# Skip files that don't match our extensions
if [[ ! "$file" =~ \.${ext_pattern} ]]; then
continue
fi
# Check the staged content for UTF‑8 validity
if ! git show :"$file" | iconv -f utf-8 -t utf-8 >/dev/null 2>&1; then
non_utf8+=( "$file" )
fi
done
# If any non‑UTF‑8 files were found, list them and fail
if [ ${#non_utf8[@]} -ne 0 ]; then
echo "✖ Error: the following files are not UTF-8 encoded. Please convert them before committing:" >&2
for f in "${non_utf8[@]}"; do
echo " - $f" >&2
done
exit 1
fi
# All good
exit 0
@zcyemi
Copy link
Author

zcyemi commented Jul 22, 2025

Modify EXTENSIONS=(cs) for your file extensions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment