Set version file path in file in repositories .git directory. This makes it easy to use with multiple projects while keeping the hook up to date.

This commit is contained in:
Storm Dragon 2024-12-12 21:38:37 -05:00
parent 96f819a8f8
commit c73a264694

View File

@ -1,53 +1,79 @@
#!/usr/bin/env bash
# Path to the version file
versionFile="src/fenrirscreenreader/fenrirVersion.py"
# This script reads the version file path from .git/versionpath
# Format of .git/versionpath should be:
# versionFile="path_from_root_of_project/version_file"
# Colors for output
RED='\033[0;31m'
YELLOW='\033[1;33m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
colorRed='\033[0;31m'
colorYellow='\033[1;33m'
colorGreen='\033[0;32m'
colorNone='\033[0m' # No Color
# Get the git root directory
gitRoot=$(git rev-parse --show-toplevel)
if [[ $? -ne 0 ]]; then
echo -e "${colorRed}Error: Not in a git repository${colorNone}"
exit 1
fi
# Temporarily move to the git root directory
pushd "$gitRoot" >/dev/null || {
echo -e "${colorRed}Error: Could not change to git root directory${colorNone}"
exit 1
}
# 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}"
if [[ "${SKIP_VERSION_UPDATE}" = "1" ]]; then
echo -e "${colorYellow}Notice: Skipping version update due to SKIP_VERSION_UPDATE=1${colorNone}"
popd >/dev/null
exit 0
fi
# Verify .git/versionpath exists
if [[ ! -f ".git/versionpath" ]]; then
echo -e "${colorRed}Error: .git/versionpath not found. Please create it with contents:${colorNone}"
echo -e "${colorYellow}versionFile=\"path/to/your/version/file\"${colorNone}"
popd >/dev/null
exit 1
fi
# Source the version path file
# shellcheck disable=SC1091
source ".git/versionpath"
# Validate that versionFile variable was set
if [[ -z "$versionFile" ]]; then
echo -e "${colorRed}Error: versionFile variable not set in .git/versionpath${colorNone}"
popd >/dev/null
exit 1
fi
# Get current date components
YEAR=$(date +%Y)
MONTH=$(date +%m)
DAY=$(date +%d)
year=$(date +%Y)
month=$(date +%m)
day=$(date +%d)
# Create new version string
newVersion="$YEAR.$MONTH.$DAY"
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}"
if [[ -f ".git/MERGE_HEAD" ]]; then
echo -e "${colorYellow}Warning: In the middle of a merge. Skipping version update.${colorNone}"
popd >/dev/null
exit 0
fi
# Check if file exists
if [ ! -f "$versionFile" ]; then
echo -e "${RED}Error: Version file not found at $versionFile${NC}"
# Check if file exists relative to git root
if [[ ! -f "$versionFile" ]]; then
echo -e "${colorRed}Error: Version file not found at $versionFile${colorNone}"
popd >/dev/null
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")
@ -73,23 +99,18 @@ else
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 [[ "$(cat "$versionFile")" != "$originalContent" ]]; then
echo -e "${colorGreen}Notice: Version file has been updated to $newVersion${colorNone}"
if ! git diff --cached --quiet "$versionFile"; then
echo -e "${YELLOW}Notice: Version file was already staged, updates have been made to staged version${NC}"
echo -e "${colorYellow}Notice: Version file was already staged, updates have been made to staged version${colorNone}"
else
git add "$versionFile"
echo -e "${YELLOW}Notice: Version file has been staged${NC}"
echo -e "${colorYellow}Notice: Version file has been staged${colorNone}"
fi
else
echo -e "${GREEN}Notice: No updates needed to version file${NC}"
echo -e "${colorGreen}Notice: No updates needed to version file${colorNone}"
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
echo -e "\n${colorGreen}Note: You can skip version updates by setting SKIP_VERSION_UPDATE=1${colorNone}"
# Print help information
echo -e "\n${GREEN}Note: You can skip version updates by setting SKIP_VERSION_UPDATE=1${NC}"
popd >/dev/null