Skip to content

Instantly share code, notes, and snippets.

@ginkgomzd
Created February 9, 2021 20:11
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 ginkgomzd/589e20274a8410f4e9811a64647043c5 to your computer and use it in GitHub Desktop.
Save ginkgomzd/589e20274a8410f4e9811a64647043c5 to your computer and use it in GitHub Desktop.
Rename tags matching a pattern
# # #
#
# CHANGE RELEASE TAG SCHEME
#
# re-name tags to remove the civicrm release version
# set env DEBUG=1 to preview the changes
#
# USAGE
#
# `> git tag -ln1 | awk -f mv-tags.awk`
#
BEGIN {
DEBUG = ENVIRON["DEBUG"]
}
/^v[^-\s]+-[\d\.]*/ {
# Exclude rc and beta tags
if (match($1, /-beta|-rc|.beta/)) {
next
}
# Catch some invalid tags that slip-through
# due to dashes in the commit msg
if (match($1, /-/) == 0) {
next
}
old_tag = $1
n = 2
msg = ""
while (n <= NF) {
msg = msg " " $n
n++
}
cmd = "git tag -m \"" trim(msg) "\" " new_tag(old_tag) " " old_tag "^{}"
shell(cmd)
# Delete local tag
cmd = "git tag -d " old_tag
shell(cmd)
# Delete remote tag
cmd = "git push origin --delete " old_tag
shell(cmd)
}
function new_tag(old_tag) {
idx = match(old_tag, /[-]+(.*)/, matches)
if (idx > 0) {
return "v" matches[1]
}
}
function ltrim(s) { sub(/^[ \t\r\n]+/, "", s); return s }
function rtrim(s) { sub(/[ \t\r\n]+$/, "", s); return s }
function trim(s) { return rtrim(ltrim(s)); }
# # #
# Checks a string for "Error"
# and dies, outputing the string
function on_error(response, cmd) {
if (match(response, /Error/)) {
print response
if (length(cmd) != 0) {
print "in response to:"
print cmd
}
exit 1
}
}
# # #
# Execute a shell using awk's pipe feature
# expects a single line of output; saves to SHELL_RESPONSE
# ensures pipe is closed
# catches Errors using on_error()
function shell(SHELL_CMD ,SHELL_RESPONSE) {
print SHELL_CMD
if (DEBUG == "" || DEBUG == 0) {
SHELL_CMD " 2>&1 " | getline SHELL_RESPONSE
close(SHELL_CMD " 2>&1 ")
on_error(SHELL_RESPONSE, SHELL_CMD)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment