Skip to content
quarry Artifact [ed68cf8dd8]
Login

Artifact [ed68cf8dd8]

Artifact ed68cf8dd88283da8492a69e87732c6eeeff16a3404a4505831fc9ac96dc34b1:


#!/bin/sh
# version-stamp.sh VERSIONFILE CHECKIN [TAG...]
# version-stamp.sh VERSIONFILE CHECKIN -
#
# The version quarry is stamped with (RFC 0020 §releases). VERSIONFILE
# holds X.Y.Z, the version the tree is working toward; CHECKIN is the
# built check-in (12 hex characters or more, or "local" for a tree with
# no checkout); the TAGs are the names it carries. A check-in tagged
# version-X.Y.Z is that release and prints X.Y.Z; anything else is on
# its way there and prints X.Y.Z-dev+<the check-in's first 12 characters>.
#
# With -, the tags are read from stdin in three parts split by a line
# `--`: what `zit tag list --raw CHECKIN` prints, the same for its
# primary parent, and what plain `zit tag list CHECKIN` prints. A tag
# counts when the check-in carries it itself, the rule the CI trigger's
# tag event uses (index.TagsAdded), so what starts a release is what
# stamps it: a `+` name on it (the plain list, which holds exactly
# those), or a sym- name it has that its parent does not (a propagating
# `*` it carries, never one inherited), and never a branch's own name.
# zit prints no sign, so one case reads differently from the trigger: a
# check-in carrying its own `*sym-NAME` while its parent also has NAME
# stamps -dev. Nobody propagates a version tag down a line that already
# carries it.
#
# build-linux.sh stamps with this when VERSION is unset, and deploy.sh
# runs the tree's copy, so the rule has one home.
set -eu
[ $# -ge 2 ] || { echo "usage: version-stamp.sh VERSIONFILE CHECKIN [TAG...]" >&2; exit 2; }
file=$1 checkin=$2
shift 2
if [ "$*" = - ]; then
  # One tag per word: a fossil tag name holds no space.
  # shellcheck disable=SC2046
  set -- $(awk '$0 == "--" { part++; next }
    part == 1 { inherited[$1] = 1; next }
    part == 2 { if (NF) single["sym-" $1] = 1; next }
    $1 == "branch" { branch["sym-" $2] = 1 }
    $1 ~ /^sym-/ { own[$1] = 1 }
    END {
      for (t in own) if ((t in single || !(t in inherited)) && !(t in branch)) print substr(t, 5)
    }')
fi
v=$(tr -d ' \t\r\n' <"$file")
printf '%s\n' "$v" | grep -Eqx '[0-9]+\.[0-9]+\.[0-9]+' || {
  echo "version-stamp: $file holds \"$v\", not X.Y.Z" >&2; exit 1; }
case "$checkin" in
  local) ;;
  *[!0-9a-f]* | "") echo "version-stamp: \"$checkin\" is not a check-in hash" >&2; exit 1 ;;
  *) [ "${#checkin}" -ge 12 ] || { echo "version-stamp: \"$checkin\" is shorter than 12 hex characters" >&2; exit 1; }
     checkin=$(printf '%s' "$checkin" | cut -c1-12) ;;
esac
for t in "$@"; do
  if [ "$t" = "version-$v" ]; then
    echo "$v"
    exit 0
  fi
done
echo "$v-dev+$checkin"