Skip to content

[BUG]: Binary Files Check Throws False Positives #5847

Description

@Rd4dev

Describe the bug

The current binary file detection logic in [pre-commit.sh](# Pre-commit hook to check for binary files.) (introduced in #5525) uses the file --mime command to identify binary files, but this approach leads to false positives for non-binary text-based files like: .textproto, .json, .xml, .txt, .proto, .md.

if file --mime "$file" | grep -q 'binary'; then

Steps To Reproduce

  1. Introduce a new .textproto or .md file locally.
  2. Run the scripts/pre-commit.sh script.
  3. The script will fail with:
Please remove the 1 detected binary file(s).
BINARY FILES CHECK FAILED
  1. Even though the files are valid text files.

This issue was recently observed in a PR #5836 where new .textproto files were introduced but mistakenly identified as binary files.

Image

Expected Behavior

Text files such as .txt, .md, .json, .xml, .textproto should NOT be classified as binary. These files are human-readable and should be allowed in commits without triggering binary file check failures.

Binary files include formats such as:

  • Images: .png, .jpeg, .jpg, .gif, .bmp, .webp
  • Videos: .mp4, .mov, .avi, .mkv
  • Archives and executables: .zip, .tar, .gz, .exe, .bin
  • Compiled objects and libraries: .class, .so, .dll
  • Other binary data files

The check should only flag and fail on actual binary files to prevent unwanted binary files in the repository. Files that are text-based or human-readable should pass the binary check without warnings or failures.

Proposed / Alternate Approach:

An alternative is to use Git’s own diff statistics:

base_commit=$(git merge-base origin/develop HEAD)
binary_files=$(git diff --numstat "$base_commit"...HEAD | awk '$1 == "-" && $2 == "-" { print $3 }')

This approach was tested in the fork PR and appeared to correctly detect only genuine binary files (e.g., images), without false positives on text files.

Image

It’s important to test different edge cases —or to check with team and see maybe introducing a Kotlin script with proper testing could be a more sustainable solution to avoid future regressions.

Additional Context

Additional Findings / Suggestions:

  1. Extending Pre-commit Hook: Currently, the pre-commit script was only setup to check for binary files. It could be refactored to allow running multiple checks by move the existing binary file check into a separate script (e.g., binary-files-check.sh) and have the main pre-commit hook call this script..
  2. Executable Permissions: Some of these scripts may require executable permissions to run properly. It could be useful to clarify whether executable permissions should be automatically set or whether explicit documentation should be provided to enable them.
  3. Formatting colors in CI: The sourcing of the formatting.sh did not embed color codes in the CI, while utilizing color codes right from the file, does work.

Metadata

Metadata

Assignees

Labels

Impact: HighHigh perceived user impact (breaks a critical feature or blocks a release).Work: LowSolution is clear and broken into good-first-issue-sized chunks.bugEnd user-perceivable behaviors which are not desirable.

Type

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions