Skip to content

Commit 89e1c7e

Browse files
authored
Merge pull request #6341 from stweil/add_encoding_test
Add new GitHub workflow which checks file encodings
2 parents 7337abb + c96cdbd commit 89e1c7e

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Check File Encodings
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize]
6+
push:
7+
branches:
8+
- '**'
9+
10+
jobs:
11+
check-encoding:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v4
15+
16+
- name: Get changed files
17+
id: changed-files
18+
uses: tj-actions/changed-files@v45
19+
20+
- name: Check file encodings
21+
run: |
22+
#!/bin/bash
23+
set -euo pipefail
24+
25+
EXIT_CODE=0
26+
27+
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
28+
# Skip if file is binary or doesn't exist (was deleted)
29+
if [[ ! -f "$file" ]] || [[ -z "$(grep -Il '.' "$file")" ]]; then
30+
continue
31+
fi
32+
33+
# Try to detect encoding using file command
34+
encoding=$(file -i "$file" | grep -oP "charset=\K.*")
35+
36+
if [ "$encoding" != "utf-8" ] && [ "$encoding" != "us-ascii" ]; then
37+
echo "::error file=${file}::File is not UTF-8 encoded (detected: ${encoding})"
38+
EXIT_CODE=1
39+
else
40+
# Double-check with iconv
41+
if ! iconv -f utf-8 -t utf-8 "$file" > /dev/null 2>&1; then
42+
echo "::error file=${file}::File contains invalid UTF-8 sequences"
43+
EXIT_CODE=1
44+
fi
45+
fi
46+
done
47+
48+
exit $EXIT_CODE

0 commit comments

Comments
 (0)