Skip to content
quarry Artifact [20725e0d7f]
Login

Artifact [20725e0d7f]

Artifact 20725e0d7f0e3944ac016311033577e15efd2379505fbe8e9cef8c783051a8e4:


#!/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 as `zit tag list --raw CHECKIN`
# prints them, then a line `--`, then the same for its primary parent.
# A tag counts when the check-in carries it itself: a sym- name, `+` or
# a propagating `*` on this check-in, not one inherited from the parent,
# and not a branch's own name. That is the rule the CI trigger's tag
# event uses (index.TagsAdded), so what starts a release is what stamps
# it. Plain `zit tag list` leaves propagating tags out.
#
# 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 == "--" { parent = 1; next }
    parent { inherited[$1] = 1; next }
    $1 == "branch" { branch["sym-" $2] = 1 }
    $1 ~ /^sym-/ { own[$1] = 1 }
    END { for (t in own) if (!(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"