On 2023-10-27 Fr 03:14, Étienne BERSAC wrote:
Hello,


Yes, there's a lot to look out for, and you're a damn sight better at
it
than I am. But we should try to automate the things that can be
automated, even if that leaves many tasks that can't be. I have three
things in my pre-commit hook: a check for catalog updates, a check
for
new typedefs, and an indent check.
Could you share your configuration ? Could we provide more helper and
integration to help produce consistent code ?


Sure. pre-commit hook file attached. I'm sure this could be improved on.


cheers


andrew


--
Andrew Dunstan
EDB: https://www.enterprisedb.com
#!/bin/sh

set -u

: ${PGAUTOINDENT:=no}
: ${PGTYPEDEFCHECK:=no}

branch=$(git rev-parse --abbrev-ref HEAD)
files=$(git diff --cached --name-only --diff-filter=ACMR)

check_typedef() {

        # only do this on master
        test  "$branch" = "master" || return 0

        test $PGTYPEDEFCHECK = yes || return 0

        tdmod=`git status --porcelain src/tools/pgindent/typedefs.list`

        test -z "$tdmod" && return 0

        tdfiles=`git diff --cached --name-only -Stypedef $files | grep 
'[.][ch]$'`

        test -z "$tdfiles" && return 0

        # changes include typedef but list not changed
        {
                echo 'Commit on master contains a typedef but typedefs.list not 
changed'
                echo 'It can be forced with git commit --no-verify'
                echo 'or PGTYPEDEFCHECK=no'
        } >&2

        exit 1

}

check_catalog_version () {

        # only do this on master
        test  "$branch" = "master" || return 0

        case "$files" in
                *src/include/catalog/catversion.h*)
                        return 0;
                        ;;
                *src/include/catalog/*)
                        ;;
                *)
                        return 0;
                        ;;
        esac

        # changes include catalog but not catversion.h, so warn about it
        {
                echo 'Commit on master alters catalog but catversion not bumped'
                echo 'It can be forced with git commit --no-verify'
        } >&2

        exit 1
}

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

        # but make sure we have some

        perl -e 'foreach (@ARGV) { exit 0 if /\.[ch]$/; }; exit 1;' $files || \
            return 0;

        echo Indenting $files

        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 "You need to rerun git commit to pick up pgindent changes"
        else
                echo '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_catalog_version
check_indent
check_typedef

Reply via email to