Last active
September 2, 2024 04:41
-
-
Save duruyao/2ab6a53e94363925036f3ecdf612ffd8 to your computer and use it in GitHub Desktop.
A Git hook example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -euo pipefail | |
function error_ln() { | |
printf "\033[1;32;31m%s\n\033[m" "${1}" | |
} | |
while IFS='' read -r line; do files+=("${line}"); done < <(git diff-index --cached --name-only HEAD --diff-filter=ACM) | |
for file in "${files[@]}"; do | |
if git check-ignore -q --no-index "${file}"; then | |
error_ln "Error: '${file}' is not allowed to be committed (ignored by .gitignore)" >&2 | |
exit 1 | |
fi | |
if [ "$(stat -c %s "${file}")" -gt 1048576 ]; then | |
error_ln "Error: '${file}' is too large (more than 1MB)" >&2 | |
exit 1 | |
fi | |
if file "${file}" | grep -q "ELF"; then | |
error_ln "Error: '${file}' is a compiled executable file and not allowed to be committed" >&2 | |
exit 1 | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cp pre-commit.sh .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit