On 2023-01-23 Mo 09:49, Jelte Fennema wrote:
> Indeed the flags you added are enough. Attached is a patch
> that adds an updated pre-commit hook with the same behaviour
> as the one before. I definitely think having a pre-commit hook
> in the repo is beneficial, since writing one that works in all
> cases definitely takes some time.
I didn't really like your hook, as it forces a reindent, and many people
won't want that (for reasons given elsewhere in this thread).
Here's an extract from my pre-commit hook that does that if PGAUTOINDENT
is set to "yes", and otherwise just warns you that you need to run pgindent.
cheers
andrew
--
Andrew Dunstan
EDB: https://www.enterprisedb.com
#!/bin/sh
set -u
: ${PGAUTOINDENT:=no}
branch=$(git rev-parse --abbrev-ref HEAD)
files=$(git diff --cached --name-only --diff-filter=ACMR)
check_indent () {
# only do this on master
test "$branch" = "master" || return 0
# no need to filter files - pgindent ignores everything that isn't a
# .c or .h file
src/tools/pgindent/pgindent --silent-diff $files && return 0
exec 2>&1
if [ "$PGAUTOINDENT" = yes ] ; then
echo "Running pgindent on changed files"
src/tools/pgindent/pgindent $files
echo "Commit abandoned. Rerun git commit to adopt pgindent
changes"
else
echo 'You need a pgindent run, e.g:'
echo -n 'src/tools/pgindent/pgindent '
echo '`git diff --name-only --diff-filter=ACMR`'
fi
exit 1
}
# nothing to do if there are no files
test -z "$files" && exit 0
check_indent