set -o errexit
changed_files()
{
files=""
while IFS= read -r line; do
files+=("$line")
done < <(git diff --cached --name-only --diff-filter=ACMR)
}
exit_() {
echo ""
echo "$1"
echo ""
echo "This hook can be skipped if needed with 'git commit --no-verify'"
echo "See '.git/hooks/pre-commit', installed from 'scripts/pre-commit'"
exit 1
}
if [[ $(git config --get user.name | wc -w) -lt 2 ]]; then
exit_ "Commit failed: please fix your Git user name (see docs/Contributing.md)"
fi
if ! git diff-index --check --cached HEAD --; then
exit_ "Commit failed: please fix the conflict markers or whitespace errors"
fi
changed_files
if [[ ${#files[@]} -eq 0 ]]; then
exit 0
fi
scripts=$(git rev-parse --show-toplevel)/scripts
if ! "$scripts/format-code" --quiet --whatif --files="${files[*]}"; then
exit_ "Commit failed: to fix the formatting please run './scripts/format-code --staged' in bash or '.\\scripts\\format-code.ps1 --staged' in powershell"
fi
if ! "$scripts/check-license.sh" "${files[@]}"; then
exit_ "Commit failed: please add license headers to the above files"
fi