#!/bin/bash # I, Ian Kelling, follow the GNU license recommendations at # https://www.gnu.org/licenses/license-recommendations.en.html. They # recommend that small programs, < 300 lines, be licensed under the # Apache License 2.0. This file contains or is part of one or more small # programs. If a small program grows beyond 300 lines, I plan to switch # its license to GPL. # Copyright 2024 Ian Kelling # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # http://www.apache.org/licenses/LICENSE-2.0 # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # Copyright (C) 2016 Ian Kelling # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # http://www.apache.org/licenses/LICENSE-2.0 # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ian: This section is the git upstream example hook, but it's actually useful. # An example hook script to verify what is about to be committed. # Called by "git commit" with no arguments. The hook should # exit with non-zero status after issuing an appropriate message if # it wants to stop the commit. # # To enable this hook, rename this file to "pre-commit". if git rev-parse --verify HEAD >/dev/null 2>&1 then against=HEAD else # Initial commit: diff against an empty tree object against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 fi # If you want to allow non-ASCII filenames set this variable to true. allownonascii=$(git config --bool hooks.allownonascii) # Redirect output to stderr. exec 1>&2 # Cross platform projects tend to avoid non-ASCII filenames; prevent # them from being added to the repository. We exploit the fact that the # printable range starts at the space character and ends with tilde. if [[ $allownonascii != true ]] && # Note that the use of brackets around a tr range is ok here, (it's # even required, for portability to Solaris 10's /usr/bin/tr), since # the square bracket bytes happen to fall in the designated range. test "$(git diff --cached --name-only --diff-filter=A -z $against | LC_ALL=C tr -d '[ -~]\0' | wc -c)" != 0 then cat </dev/null; then cat <<'EOF' Whitespace issues found. We can't fix in a pre-commit hook for the first commit. Either fix on your own. I suggest https://github.com/dlenski/wtf, from git root: git ls-files --exclude-standard -cmo --no-empty-directory | while read -r f; do if [[ -L $f ]] || ! grep -Iq . "$f"; then continue; fi; wtf.py -i -E lf "$f"; done or allow whitespace with: git config hooks.allowwhitespace true, or export GIT_ALLOWWHITESPACE=true EOF echo "$ws_issues" exit 1 fi echo "Fixing whitespace lines. To avoid this, do: git config hooks.allowwhitespace true, or export GIT_ALLOWWHITESPACE=true" export GIT_ALLOWWHITESPACE=ignore if ! git diff-files --quiet . && \ ! git diff-index --quiet --cached HEAD; then # - dirty tree and dirty index git commit -m "temporary commit of index while fixing whitespace" git add -u :/ git commit -m "temporary commit of non-index while fixing whitespace" git rebase --whitespace=fix HEAD~2 git reset HEAD~ && git reset --soft HEAD~ elif ! git diff-files --quiet .; then # - dirty tree and clean index git add -u :/ git commit -m "temporary commit of non-index while fixing whitespace" git rebase --whitespace=fix HEAD~ git reset HEAD~ elif ! git diff-index --quiet --cached HEAD; then # - clean tree and dirty index git commit -m "temporary commit of index while fixing whitespace" git rebase --whitespace=fix HEAD~ git reset --soft HEAD~ fi