Skip to content

Instantly share code, notes, and snippets.

@Ogaday
Created April 6, 2023 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ogaday/252f27c22f20d6aa92126354f27e678b to your computer and use it in GitHub Desktop.
Save Ogaday/252f27c22f20d6aa92126354f27e678b to your computer and use it in GitHub Desktop.
Bump git tag
#!/bin/bash
function log_error() {
msg=$1
echo $msg >&2
}
function bump() {
target_version=$1
if [ -z $target_version ]
then
log_error "No tag provided. Please provide a semver tag as the first argument."
return 1
fi
local_changes=$(git diff-index --name-only HEAD)
if [ ! -z $local_changes ]
then
log_error "Warning: You have unstaged changes. Please commit or stash them."
return 1
fi
current_branch=$(git rev-parse --abbrev-ref HEAD)
if [ ! $current_branch = "main" ]
then
log_error "Warning: Not on main branch. Please switch branches and try again."
return 1
fi
#TODO check version format.
last_tag=$(git describe --tags --abbrev=0 2> /dev/null)
first_commit=$(git rev-list --max-parents=0 HEAD | tail -n 1)
last_ref=${last_tag:-$first_commit}
tag_body=$(git shortlog --no-merges ${last_ref}..HEAD)
git tag --annotate $target_version --message="Changes:" --message="${tag_body}" --edit
return 0
}
bump $@
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment