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

Conversation

stweil
Copy link
Member

@stweil stweil commented Dec 6, 2024

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.

@stweil
Copy link
Member Author

stweil commented Dec 6, 2024

Examples: https://github.com/stweil/kitodo-production/actions/workflows/check_file_encodings.yml. See annotations for these actions, escpecially for the ones which failed.

@stweil
Copy link
Member Author

stweil commented Dec 6, 2024

A manual test which checks all files in the current directory structure can be done with similar bash code on Linux:

for file in $(find * -type f); 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

The test uses file to detect the trivial cases for which it also reports the wrong encoding.
If file does not find a wrong encoding, a full check with iconv then checks the whole file.

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]>
@stweil stweil force-pushed the add_encoding_test branch from 1973135 to 0683269 Compare January 10, 2025 21:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant