#!/bin/sh

# Update hook to decline changes to the Tracker parser without bumps to the
# parser version. These are necessary for user setups to detect such changes
# and rebuild parsed data, so they are important not to miss.

# --- Command line
refname="$1"
oldrev="$2"
newrev="$3"

# --- Safety check
if [ -z "$GIT_DIR" ]; then
	echo "Don't run this script from the command line." >&2
	echo " (if you want, you could supply GIT_DIR then run" >&2
	echo "  $0 <ref> <oldrev> <newrev>)" >&2
	exit 1
fi

if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then
	echo "usage: $0 <ref> <oldrev> <newrev>" >&2
	exit 1
fi

z40=0000000000000000000000000000000000000000

parser_files="src/libtracker-common/tracker-parser.h
  src/libtracker-common/tracker-parser-utils.c
  src/libtracker-common/tracker-parser-utils.h
  src/libtracker-common/tracker-parser-libicu.c
  src/libtracker-common/tracker-parser-libunistring.c"

# --- Check types
# if $newrev is 0000...0000, it's a commit to delete a ref.
if [ $newrev = $z40 ]; then
    newrev_type=delete
else
    newrev_type=$(git cat-file -t $newrev)
fi

if [ "$newrev_type" != "commit" ]; then
    exit 0
fi

if [ $oldrev = $z40 ]; then
    # New branch, nothing to check here
    exit 0
fi

case $refname in
    refs/heads/*)
        # Perform further checks when modifying a branch
	;;
    *)
	# Early exit for anything else (tags, remotes, ...)
	exit 0
	;;
esac

case $refname in
    refs/heads/wip/*)
	# Allow breaking version in WIP branches
	exit 0
	;;
    *)
	;;
esac

if [ $oldrev = $z40 ]; then
    range="$newrev"
else
    range="$oldrev..$newrev"
fi

parser_changes=`git rev-list "$range" -- $parser_files`

if [ -n "$parser_changes" ]
then
    version_bump=`git diff $range -G" TRACKER_PARSER_VERSION "`
    if [ -z "$version_bump" ]
    then
	echo >&2 "Parser changes found without bump to TRACKER_PARSER_VERSION, not pushing"
	echo >&2 "Revision(s) affected:"
	echo >&2 "$parser_changes"
	exit 1
    fi
fi

exit 0
