Skip to content

Commit 391c1a4

Browse files
committed
Check commits for binary data
While some binary data is acceptable, excessive binary files can slow down repository operations and impact performance. This commit adds a check to detect and flag binary data in commits, ensuring the repository remains lean and efficient. Reference: https://www.redhat.com/en/blog/git-hooks Change-Id: I39100df9317c32b58a1877a945314d5773bda0ae
1 parent 082cb08 commit 391c1a4

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

scripts/pre-commit.hook

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,26 @@ else
117117
exit 1
118118
fi
119119

120+
# Get staged filenames (added, copied, or modified) into an array.
121+
FILES=($(git diff --cached --name-only --diff-filter=ACM))
122+
binary_files=()
123+
124+
for file in "${FILES[@]}"; do
125+
# Get MIME info for the file.
126+
mime_info=$(file --mime "$file")
127+
# Extract a friendly filename (everything before the colon).
128+
name=$(file "$file" | cut -d ":" -f1)
129+
130+
if echo "$mime_info" | grep -qi binary; then
131+
binary_files+=("$name")
132+
echo "[!] '$name' appears to be a binary blob."
133+
fi
134+
done
135+
136+
if [ "${#binary_files[@]}" -gt 0 ]; then
137+
echo "WARNING: Binary data found"
138+
fi
139+
120140
FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep -E "\.(c|cpp|h)$")
121141
for FILE in $FILES; do
122142
nf=$(git checkout-index --temp $FILE | cut -f 1)

0 commit comments

Comments
 (0)