git-hooks/pre-commit-version-increment

96 lines
3.0 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env bash
# Path to the version file
versionFile="src/fenrirscreenreader/fenrirVersion.py"
# Colors for output
RED='\033[0;31m'
YELLOW='\033[1;33m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
# Check if SKIP_VERSION_UPDATE is set
if [ "${SKIP_VERSION_UPDATE}" = "1" ]; then
echo -e "${YELLOW}Notice: Skipping version update due to SKIP_VERSION_UPDATE=1${NC}"
exit 0
fi
# 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 we're in the middle of a merge
if [ -f ".git/MERGE_HEAD" ]; then
echo -e "${YELLOW}Warning: In the middle of a merge. Skipping version update.${NC}"
exit 0
fi
# Check if file exists
if [ ! -f "$versionFile" ]; then
echo -e "${RED}Error: Version file not found at $versionFile${NC}"
exit 1
fi
# Check if the branch name contains "temp" or "temporary"
if [[ "$branchName" =~ temp(orary)? ]]; then
echo -e "${YELLOW}Warning: You appear to be on a temporary branch ($branchName).${NC}"
read -p "Do you want to continue with the version update? (y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
fi
# Store original version file content
originalContent=$(cat "$versionFile")
# Check if version actually needs updating
if ! grep -q "version = \"$newVersion\"" "$versionFile"; then
# Update the version in the file
sed -i "s/version = [\"']\{0,1\}[0-9.]\+[\"']\{0,1\}/version = \"$newVersion\"/" "$versionFile"
fi
# Check if codeName exists and isn't "stable"
if grep -q "codeName.*=.*\"stable\"" "$versionFile"; then
# Don't modify stable codeName
:
elif grep -q "codeName.*=.*\"$branchName\"" "$versionFile"; then
# CodeName already matches branch name, no need to update
:
elif 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
# Check if the file was actually modified
if [ "$(cat "$versionFile")" != "$originalContent" ]; then
echo -e "${GREEN}Notice: Version file has been updated to $newVersion${NC}"
if ! git diff --cached --quiet "$versionFile"; then
echo -e "${YELLOW}Notice: Version file was already staged, updates have been made to staged version${NC}"
else
git add "$versionFile"
echo -e "${YELLOW}Notice: Version file has been staged${NC}"
fi
else
echo -e "${GREEN}Notice: No updates needed to version file${NC}"
fi
# Check if this is the second commit today
lastCommitVersion=$(git log -1 --format=%cd --date=format:'%Y.%m.%d' 2>/dev/null)
if [ "$lastCommitVersion" = "$newVersion" ]; then
echo -e "${YELLOW}Notice: Multiple commits on version $newVersion${NC}"
fi
# Print help information
echo -e "\n${GREEN}Note: You can skip version updates by setting SKIP_VERSION_UPDATE=1${NC}"