#!/bin/sh
# Installed by `php artisan crm:version --install-hook`.
#
# Every commit carries a version. A version that has been written but not yet
# committed — a deliberate `--minor` or `--major` — belongs to this commit, so
# it is staged as it is. Anything else gets a patch bump, staged into the same
# commit. Remove this with `php artisan crm:version --remove-hook`.

if git diff --cached --name-only | grep -qx 'VERSION'; then
    exit 0
fi

head_version=$(git show HEAD:VERSION 2>/dev/null | tr -d '[:space:]')
current_version=$(tr -d '[:space:]' < VERSION 2>/dev/null)

if [ "$head_version" != "$current_version" ]; then
    git add VERSION docs/releases.md >/dev/null 2>&1 || exit 0
    exit 0
fi

php artisan crm:version --auto >/dev/null 2>&1 || exit 0
git add VERSION docs/releases.md >/dev/null 2>&1 || exit 0

exit 0