pre-commit

text/plain

Filename: pre-commit
Type: text/plain
Part: 0
Message: Re: run pgindent on a regular basis / scripted manner
#!/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