Skip to content

Instantly share code, notes, and snippets.

@nilium nilium/gomodver
Created Apr 12, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.