Skip to content

Instantly share code, notes, and snippets.

@ernstki
Last active November 3, 2022 21:20
Show Gist options
  • Save ernstki/d687669aa3100f8bac9984ab02106871 to your computer and use it in GitHub Desktop.
Save ernstki/d687669aa3100f8bac9984ab02106871 to your computer and use it in GitHub Desktop.
Bump a SemVer-like version string
#!/usr/bin/awk -f
##
## Bump a version number in x.y.z (semver) format; defaults to incrementing
## the patchlevel (the 'z' part in the example) by one.
##
## Usage: $ bumpver x.y.z [major|minor|patch]
##
## Author: Kevin Ernst <ernstki -at- mail.uc.edu>
## Date: 11 February 2020
## License: WTFPL
##
function bail(msg) {
printf "ERROR: %s\n", msg > "/dev/stderr"
exit 1
}
BEGIN {
FS = "."
split(ARGV[1], parts)
if (length(parts) != 3)
bail("expected a SemVer-like version string (x.y.z)")
#for (i=1; i<=length(parts); i++)
# printf "\tparts[%d]\t= %d\n", i, parts[i]
which = ARGV[2]
if (which == "")
which = "patch"
if (which ~ /^maj/) {
parts[1] += 1
parts[2] = 0
parts[3] = 0
} else if (which ~ /^min/) {
parts[2] += 1
parts[3] = 0
} else if (which ~ /^patch/) {
parts[3] += 1
} else {
bail("expected one of 'major', 'minor', or 'patch'")
}
printf "%d.%d.%d\n", parts[1], parts[2], parts[3]
}
# bumpver
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment