Files
Storm Dragon f0352878f7 updated hook.'
git commit updated hook.
2026-10-25 15:36:09 -04:00

56 lines
1.5 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
if [[ ${SKIP_VERSION_UPDATE:-0} == 1 ]]; then
exit 0
fi
gitRoot=$(git rev-parse --show-toplevel)
cd "$gitRoot"
if [[ -f $(git rev-parse --git-path MERGE_HEAD) ]]; then
exit 0
fi
versionFile=internal/version/version.go
if ! git cat-file -e ":$versionFile" 2>/dev/null; then
echo "Error: $versionFile must be present in the index." >&2
exit 1
fi
if ! branchName=$(git symbolic-ref --quiet --short HEAD); then
echo "Error: Check out a branch or set SKIP_VERSION_UPDATE=1." >&2
exit 1
fi
newVersion=$(date +%Y.%m.%d)
escapedChannel=${branchName//\\/\\\\}
escapedChannel=${escapedChannel//\"/\\\"}
if grep -Fxq "const Version = \"$newVersion\"" "$versionFile" &&
grep -Fxq "const Channel = \"$escapedChannel\"" "$versionFile"; then
exit 0
fi
if ! git diff --quiet -- "$versionFile"; then
echo "Error: Stage or save unstaged changes in $versionFile before its version update." >&2
exit 1
fi
if [[ $(grep -Ec '^const Version = ".*"$' "$versionFile") != 1 ]] ||
[[ $(grep -Ec '^const Channel = ".*"$' "$versionFile") != 1 ]]; then
echo "Error: Expected one Version constant and one Channel constant in $versionFile." >&2
exit 1
fi
sedChannel=${escapedChannel//\\/\\\\}
sedChannel=${sedChannel//&/\\&}
sedChannel=${sedChannel//|/\\|}
sed -i \
-e "s|^const Version = \".*\"$|const Version = \"$newVersion\"|" \
-e "s|^const Channel = \".*\"$|const Channel = \"$sedChannel\"|" \
"$versionFile"
git add -- "$versionFile"
echo "Updated Cthulhu version to $newVersion-$branchName."