git-hooks/pre-commit-version-increment

117 lines
3.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# 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
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 "${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)
# 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 "${colorYellow}Warning: In the middle of a merge. Skipping version update.${colorNone}"
popd >/dev/null
exit 0
fi
# 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
# 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 "${colorGreen}Notice: Version file has been updated to $newVersion${colorNone}"
if ! git diff --cached --quiet "$versionFile"; then
echo -e "${colorYellow}Notice: Version file was already staged, updates have been made to staged version${colorNone}"
else
git add "$versionFile"
echo -e "${colorYellow}Notice: Version file has been staged${colorNone}"
fi
else
echo -e "${colorGreen}Notice: No updates needed to version file${colorNone}"
fi
echo -e "\n${colorGreen}Note: You can skip version updates by setting SKIP_VERSION_UPDATE=1${colorNone}"
popd >/dev/null