pre-commit-sample

text/plain

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