Skip to content

Instantly share code, notes, and snippets.

@nilium
Created April 12, 2019 16:59
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save nilium/bd3adf2bd985d2d2edec5b63287d693c to your computer and use it in GitHub Desktop.
Script to print Go module v0.0.0-DATE-SHA version strings
#!/bin/bash
# Usage: gomodver [REF...]
# Prints a Go modules v0.0.0-DATE-SHA version string for the given Git refs.
# If no REF is given, gomodver defaults to HEAD.
datearg=d@
case "$(uname -s | tr A-Z a-z)" in
*bsd|darwin) datearg=r;
esac
if [ $# -eq 0 ]; then
set -- HEAD
fi
nl=''
for ref; do
head="$(git rev-parse "$ref")"
shortsha="${head:0:12}"
ts="$(git log -n1 --date=raw --format='%cd' "$head" | cut -f1 -d' ')"
committed="$(date "-${datearg}${ts}" -u '+%Y%m%d%H%M%S')"
printf "%sv0.0.0-%s-%s" "${nl}" "${committed}" "${shortsha}"
nl=$'\n'
done
if [ -t 1 ]; then
printf '\n'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment