Skip to content

Instantly share code, notes, and snippets.

@duruyao
Created May 31, 2024 06:13
Show Gist options
  • Save duruyao/2ab6a53e94363925036f3ecdf612ffd8 to your computer and use it in GitHub Desktop.
Save duruyao/2ab6a53e94363925036f3ecdf612ffd8 to your computer and use it in GitHub Desktop.
Git hooks.
#!/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
@duruyao
Copy link
Author

duruyao commented May 31, 2024

cp pre-commit.sh .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit
  • prevent submission of untracked files.
  • prevent submission of files larger than 1MB.
  • prevent submission of ELF files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment