Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new GitHub workflow which checks file encodings #6341

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading