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 #!/usr/bin/env bash
# Path to the version file # This script reads the version file path from .git/versionpath
versionFile="src/fenrirscreenreader/fenrirVersion.py" # Format of .git/versionpath should be:
# versionFile="path_from_root_of_project/version_file"
# Colors for output # Colors for output
RED='\033[0;31m' colorRed='\033[0;31m'
YELLOW='\033[1;33m' colorYellow='\033[1;33m'
GREEN='\033[0;32m' colorGreen='\033[0;32m'
NC='\033[0m' # No Color 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 # Check if SKIP_VERSION_UPDATE is set
if [ "${SKIP_VERSION_UPDATE}" = "1" ]; then if [[ "${SKIP_VERSION_UPDATE}" = "1" ]]; then
echo -e "${YELLOW}Notice: Skipping version update due to SKIP_VERSION_UPDATE=1${NC}" echo -e "${colorYellow}Notice: Skipping version update due to SKIP_VERSION_UPDATE=1${colorNone}"
popd >/dev/null
exit 0 exit 0
fi 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 # Get current date components
YEAR=$(date +%Y) year=$(date +%Y)
MONTH=$(date +%m) month=$(date +%m)
DAY=$(date +%d) day=$(date +%d)
# Create new version string # Create new version string
newVersion="$YEAR.$MONTH.$DAY" newVersion="$year.$month.$day"
# Get current branch name # Get current branch name
branchName=$(git rev-parse --abbrev-ref HEAD) branchName=$(git rev-parse --abbrev-ref HEAD)
# Check if we're in the middle of a merge # Check if we're in the middle of a merge
if [ -f ".git/MERGE_HEAD" ]; then if [[ -f ".git/MERGE_HEAD" ]]; then
echo -e "${YELLOW}Warning: In the middle of a merge. Skipping version update.${NC}" echo -e "${colorYellow}Warning: In the middle of a merge. Skipping version update.${colorNone}"
popd >/dev/null
exit 0 exit 0
fi fi
# Check if file exists # Check if file exists relative to git root
if [ ! -f "$versionFile" ]; then if [[ ! -f "$versionFile" ]]; then
echo -e "${RED}Error: Version file not found at $versionFile${NC}" echo -e "${colorRed}Error: Version file not found at $versionFile${colorNone}"
popd >/dev/null
exit 1 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 # Store original version file content
originalContent=$(cat "$versionFile") originalContent=$(cat "$versionFile")
@ -73,23 +99,18 @@ else
fi fi
# Check if the file was actually modified # Check if the file was actually modified
if [ "$(cat "$versionFile")" != "$originalContent" ]; then if [[ "$(cat "$versionFile")" != "$originalContent" ]]; then
echo -e "${GREEN}Notice: Version file has been updated to $newVersion${NC}" echo -e "${colorGreen}Notice: Version file has been updated to $newVersion${colorNone}"
if ! git diff --cached --quiet "$versionFile"; then 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 else
git add "$versionFile" git add "$versionFile"
echo -e "${YELLOW}Notice: Version file has been staged${NC}" echo -e "${colorYellow}Notice: Version file has been staged${colorNone}"
fi fi
else else
echo -e "${GREEN}Notice: No updates needed to version file${NC}" echo -e "${colorGreen}Notice: No updates needed to version file${colorNone}"
fi fi
# Check if this is the second commit today echo -e "\n${colorGreen}Note: You can skip version updates by setting SKIP_VERSION_UPDATE=1${colorNone}"
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 popd >/dev/null
echo -e "\n${GREEN}Note: You can skip version updates by setting SKIP_VERSION_UPDATE=1${NC}"