Skip to content

Instantly share code, notes, and snippets.

@neomantra
Created May 6, 2026 23:42
Show Gist options
  • Select an option

  • Save neomantra/bbcaf189347221ee70bef493cb20f40c to your computer and use it in GitHub Desktop.

Select an option

Save neomantra/bbcaf189347221ee70bef493cb20f40c to your computer and use it in GitHub Desktop.
go mod replace version-timestamp-hash extractor
#!/bin/bash
#
# This script is used to generate a version string for the go.mod replace directive.
# This is of the format version-timestamp-hash, where version is the current version of the module,
# timestamp is the current time in UTC, and hash is the short git hash of the current commit.
#
# This script emits the "version-timestamp-hash" based on the current git HEAD.
#
# If passed with an argument, that string is used as the version,
# otherwise "v0.0.0" is used as the default version.
#
#
VERSION=${1:-"v0.0.0"}
DT=$(TZ=UTC git show -s --format=%cd --date='format-local:%Y%m%d%H%M%S' HEAD)
HASHISH=$(git rev-parse --short=12 HEAD)
echo "$VERSION-$DT-$HASHISH"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment