magdalena

interactive shell navigation and history
Log | Files | Refs | README | LICENSE

release (1372B)


      1 #!/usr/bin/env bash
      2 set -euo pipefail
      3 cd "$(dirname "$0")/.."
      4 
      5 BRANCH="master"
      6 REMOTE="origin"
      7 
      8 die() {
      9     echo "error: $*" >&2
     10     exit 1
     11 }
     12 
     13 update_version_file() {
     14     local version="$1"
     15     [ -f build.zig.zon ] || return 0
     16     sed -i "s/\.version = \"[^\"]*\"/.version = \"$version\"/" build.zig.zon
     17     git add build.zig.zon
     18 }
     19 
     20 command -v git >/dev/null 2>&1 || die "git not found"
     21 
     22 current=$(git tag --list 'v[0-9]*' --sort=-v:refname | head -n1)
     23 current=${current#v}
     24 usage="$(basename "$0") <version> [--push] (${current:-none})"
     25 
     26 version=""
     27 push=0
     28 for arg in "$@"; do
     29     case "$arg" in
     30     --push) push=1 ;;
     31     -*) die "unknown flag: $arg" ;;
     32     *)
     33         [ -z "$version" ] || die "unexpected argument: $arg"
     34         version="$arg"
     35         ;;
     36     esac
     37 done
     38 
     39 [ -n "$version" ] || die "$usage"
     40 echo "$version" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+([-+][0-9A-Za-z.-]+)*$' ||
     41     die "'$version' is not a semantic version"
     42 
     43 [ "$(git rev-parse --abbrev-ref HEAD)" = "$BRANCH" ] || die "not on $BRANCH branch"
     44 [ -z "$(git status --porcelain)" ] || die "working tree is dirty"
     45 git rev-parse -q --verify "refs/tags/v$version" >/dev/null 2>&1 && die "tag v$version already exists"
     46 
     47 update_version_file "$version"
     48 
     49 git commit -m "chore: release v$version"
     50 git tag -a "v$version" -m "v$version"
     51 
     52 if [ "$push" -eq 1 ]; then
     53     git push "$REMOTE" "$BRANCH" --follow-tags
     54 fi