Skip to content

Commit

Permalink
Add new GitHub workflow which checks file encodings
Browse files Browse the repository at this point in the history
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
stweil committed Dec 6, 2024
1 parent 68b5975 commit 1973135
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/check_file_encodings.yml
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

0 comments on commit 1973135

Please sign in to comment.