git-hooks/pre-push-version-increment

43 lines
1.2 KiB
Plaintext
Raw Normal View History

#!/bin/bash
# Path to the version file
versionFile="src/fenrirscreenreader/fenrirVersion.py"
# Get current date components
YEAR=$(date +%Y)
MONTH=$(date +%m)
DAY=$(date +%d)
# Create new version string
newVersion="$YEAR.$MONTH.$DAY"
# Get current branch name
branchName=$(git rev-parse --abbrev-ref HEAD)
# Check if file exists
if [ ! -f "$versionFile" ]; then
echo "Error: Version file not found at $versionFile"
exit 1
fi
# Update the version in the file
# This handles different possible formats of the version line
sed -i "s/version = [\"']\{0,1\}[0-9.]\+[\"']\{0,1\}/version = \"$newVersion\"/" "$versionFile"
# Check if codeName exists in the file
if grep -q "codeName" "$versionFile"; then
# Update existing codeName
sed -i "s/codeName = [\"']\{0,1\}[^\"']*[\"']\{0,1\}/codeName = \"$branchName\"/" "$versionFile"
else
# Add codeName after the version line
sed -i "/version = / a\codeName = \"$branchName\"" "$versionFile"
fi
# Add the changed file back to the commit
git add "$versionFile"
# Amend the last commit to include the version update
# Use the original commit message
commitMessage=$(git log -1 --pretty=%B)
git commit --amend -m "$commitMessage"