-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new GitHub workflow which checks file encodings
It checks whether files related to a push or pull request contain encodings which are not UTF-8. The workflow was created with Claude.AI. Signed-off-by: Stefan Weil <[email protected]>
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
name: Check File Encodings | ||
|
||
on: | ||
pull_request: | ||
types: [opened, synchronize] | ||
push: | ||
branches: | ||
- '**' | ||
|
||
jobs: | ||
check-encoding: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Get changed files | ||
id: changed-files | ||
uses: tj-actions/changed-files@v45 | ||
|
||
- name: Check file encodings | ||
run: | | ||
#!/bin/bash | ||
set -euo pipefail | ||
EXIT_CODE=0 | ||
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do | ||
# Skip if file is binary or doesn't exist (was deleted) | ||
if [[ ! -f "$file" ]] || [[ -z "$(grep -Il '.' "$file")" ]]; then | ||
continue | ||
fi | ||
# Try to detect encoding using file command | ||
encoding=$(file -i "$file" | grep -oP "charset=\K.*") | ||
if [ "$encoding" != "utf-8" ] && [ "$encoding" != "us-ascii" ]; then | ||
echo "::error file=${file}::File is not UTF-8 encoded (detected: ${encoding})" | ||
EXIT_CODE=1 | ||
else | ||
# Double-check with iconv | ||
if ! iconv -f utf-8 -t utf-8 "$file" > /dev/null 2>&1; then | ||
echo "::error file=${file}::File contains invalid UTF-8 sequences" | ||
EXIT_CODE=1 | ||
fi | ||
fi | ||
done | ||
exit $EXIT_CODE |