2024-12-07 20:00:31 -05:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2024-12-12 21:38:37 -05:00
|
|
|
# This script reads the version file path from .git/versionpath
|
|
|
|
# Format of .git/versionpath should be:
|
|
|
|
# versionFile="path_from_root_of_project/version_file"
|
2024-12-07 20:00:31 -05:00
|
|
|
|
|
|
|
# Colors for output
|
2024-12-12 21:38:37 -05:00
|
|
|
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
|
|
|
|
}
|
2024-12-07 20:00:31 -05:00
|
|
|
|
|
|
|
# Check if SKIP_VERSION_UPDATE is set
|
2024-12-12 21:38:37 -05:00
|
|
|
if [[ "${SKIP_VERSION_UPDATE}" = "1" ]]; then
|
|
|
|
echo -e "${colorYellow}Notice: Skipping version update due to SKIP_VERSION_UPDATE=1${colorNone}"
|
|
|
|
popd >/dev/null
|
2024-12-07 20:00:31 -05:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2024-12-12 21:38:37 -05:00
|
|
|
# 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
|
|
|
|
|
2024-12-07 20:00:31 -05:00
|
|
|
# Get current date components
|
2024-12-12 21:38:37 -05:00
|
|
|
year=$(date +%Y)
|
|
|
|
month=$(date +%m)
|
|
|
|
day=$(date +%d)
|
2024-12-07 20:00:31 -05:00
|
|
|
|
|
|
|
# Create new version string
|
2024-12-12 21:38:37 -05:00
|
|
|
newVersion="$year.$month.$day"
|
2024-12-07 20:00:31 -05:00
|
|
|
|
|
|
|
# Get current branch name
|
|
|
|
branchName=$(git rev-parse --abbrev-ref HEAD)
|
|
|
|
|
|
|
|
# Check if we're in the middle of a merge
|
2024-12-12 21:38:37 -05:00
|
|
|
if [[ -f ".git/MERGE_HEAD" ]]; then
|
|
|
|
echo -e "${colorYellow}Warning: In the middle of a merge. Skipping version update.${colorNone}"
|
|
|
|
popd >/dev/null
|
2024-12-07 20:00:31 -05:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2024-12-12 21:38:37 -05:00
|
|
|
# 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
|
2024-12-07 20:00:31 -05:00
|
|
|
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
|
2024-12-12 21:38:37 -05:00
|
|
|
if [[ "$(cat "$versionFile")" != "$originalContent" ]]; then
|
|
|
|
echo -e "${colorGreen}Notice: Version file has been updated to $newVersion${colorNone}"
|
2024-12-07 20:00:31 -05:00
|
|
|
if ! git diff --cached --quiet "$versionFile"; then
|
2024-12-12 21:38:37 -05:00
|
|
|
echo -e "${colorYellow}Notice: Version file was already staged, updates have been made to staged version${colorNone}"
|
2024-12-07 20:00:31 -05:00
|
|
|
else
|
|
|
|
git add "$versionFile"
|
2024-12-12 21:38:37 -05:00
|
|
|
echo -e "${colorYellow}Notice: Version file has been staged${colorNone}"
|
2024-12-07 20:00:31 -05:00
|
|
|
fi
|
|
|
|
else
|
2024-12-12 21:38:37 -05:00
|
|
|
echo -e "${colorGreen}Notice: No updates needed to version file${colorNone}"
|
2024-12-07 20:00:31 -05:00
|
|
|
fi
|
|
|
|
|
2024-12-12 21:38:37 -05:00
|
|
|
echo -e "\n${colorGreen}Note: You can skip version updates by setting SKIP_VERSION_UPDATE=1${colorNone}"
|
2024-12-07 20:00:31 -05:00
|
|
|
|
2024-12-12 21:38:37 -05:00
|
|
|
popd >/dev/null
|