Skip to content

Instantly share code, notes, and snippets.

@Dari4sho
Forked from CSTDev/auto-increment-version.sh
Last active May 20, 2023 23:36
Show Gist options
  • Select an option

  • Save Dari4sho/1a0991ec7c15f32725abc308b5253f6b to your computer and use it in GitHub Desktop.

Select an option

Save Dari4sho/1a0991ec7c15f32725abc308b5253f6b to your computer and use it in GitHub Desktop.
Script that will find the last Git Tag and increment it. It will only increment when the latest commit does not already have a tag. By default it increments the patch number, you can tell it to change the major or minor versions by adding #major or #minor to the commit message.
#!/bin/bash
tag_prefix="client-v"
# Get the highest tag number
latest_tag=$(git describe --abbrev=0 --tags)
# Extract the prefix and version using regular expression
if [[ $latest_tag =~ ^(.*$tag_prefix)(.*)$ ]]; then
prefix="${BASH_REMATCH[1]}"
version="${BASH_REMATCH[2]}"
else
echo "Invalid version format"
exit 1
fi
echo "Prefix: $prefix"
echo "Version: $version"
# Replace dots with spaces to split into an array
version_parts=(${version//./ })
# Extract individual version numbers
major=${version_parts[0]}
minor=${version_parts[1]}
patch=${version_parts[2]}
major=${major//v/} # Remove 'v' prefix if present
# Check for #major or #minor in commit message and increment the relevant version number
commit_message=$(git log --format=%B -n 1 HEAD)
major_update=$(echo "$commit_message" | grep '#major')
minor_update=$(echo "$commit_message" | grep '#minor')
# Check if an argument is provided
if [ $# -eq 0 ]; then
echo "No argument provided, relying on git log to decide which version to push (available arguments: minor/major)"
fi
# Process the arguments
for argument in "$@"; do
case "$argument" in
--push)
push_update=true
;;
major)
major_update=true
;;
minor)
minor_update=true
;;
*)
echo "Unknown argument: $argument"
;;
esac
done
# Decide version update
if [ "$major_update" ]; then
echo "Update major version"
major=$((major + 1))
minor=0
patch=0
elif [ "$minor_update" ]; then
echo "Update minor version"
minor=$((minor + 1))
patch=0
else
echo "Update patch version"
patch=$((patch + 1))
fi
# Create new tag
new_tag="$prefix$major.$minor.$patch"
echo "Updating $latest_tag to $new_tag"
# Get current commit hash and check if it already has a tag
commit_hash=$(git rev-parse HEAD)
existing_tag=$(git describe --contains "$commit_hash" 2>/dev/null)
# Only tag if no tag already exists
if [ -z "$existing_tag" ]; then
echo "Tagged with $new_tag"
if [ "$push_update" ]; then
echo "Setting & pushing tag..."
git tag "$new_tag"
git push --tags
else
echo "Dry run - doing nothing. Use --push to actually set the tag and push it"
fi
else
echo "Already a tag on this commit"
exit 1
fi
@Dari4sho
Copy link
Author

Dari4sho commented May 20, 2023

Based on https://gist.github.com/CSTDev/08c127680e3b5fae38c051da3e489351
Features added / namings & comments optimized with ChatGPT to support the following:

  • Support custom prefix (e.g. my tag version name format is client-v1.0.0)
  • Add minor / major arguments to overwrite auto #minor / #major detection (e.g. ./auto-increment-version.sh major)
  • Support dry run for evaluation / --push argument to apply tag increment (e.g. ./auto-increment-version --push)
  • Arguments can be used together (e.g. ./auto-increment-version minor --push)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment