Moved the version incrementer from pre-push to pre-commit. It's a better way that doesn't cause branch divergence.

This commit is contained in:
Storm Dragon 2024-12-07 20:00:31 -05:00
parent 1f837964da
commit 5ac7a8f5f6
2 changed files with 105 additions and 42 deletions

105
pre-commit-version-increment Executable file
View File

@ -0,0 +1,105 @@
#!/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 for uncommitted changes (excluding version file)
if [ -n "$(git diff --name-only | grep -v "$versionFile")" ]; then
echo -e "${YELLOW}Warning: You have unstaged changes in other files.${NC}"
read -p "Do you want to continue with the commit? (y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
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}"

View File

@ -1,42 +0,0 @@
#!/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"