Approval-based snapshot testing library for Go (mirror)
1#!/usr/bin/env bash
2set -euo pipefail
3
4# Determine next semver based on conventional commits since main.
5# Usage: ./scripts/version.sh [--dry-run]
6
7DRY_RUN=false
8if [[ "${1:-}" == "--dry-run" ]]; then
9 DRY_RUN=true
10fi
11
12# Get the latest root module tag (ignore cmd/shutter/ prefixed tags)
13LATEST_TAG=$(jj tag list | grep -E '^v[0-9]' | sort -V -t: -k1,1 | tail -1 | awk '{print $1}' | tr -d ':')
14
15if [[ -z "$LATEST_TAG" ]]; then
16 echo "error: no existing version tags found"
17 exit 1
18fi
19
20echo "Current version: $LATEST_TAG"
21
22# Parse current version
23VERSION="${LATEST_TAG#v}"
24IFS='.' read -r MAJOR MINOR PATCH <<< "$VERSION"
25
26# Get commit messages since main
27COMMITS=$(jj log -r 'main..@' --no-graph -T 'description ++ "---\n"' 2>/dev/null)
28
29if [[ -z "$COMMITS" || "$COMMITS" == $'---\n' ]]; then
30 echo "No commits since main."
31 exit 0
32fi
33
34echo ""
35echo "Commits since main:"
36echo "$COMMITS" | grep -v '^---$' | grep -v '^$' | sed 's/^/ /'
37echo ""
38
39# Determine bump type from conventional commits
40BUMP="patch"
41
42while IFS= read -r line; do
43 # Skip empty lines and delimiters
44 [[ -z "$line" || "$line" == "---" ]] && continue
45
46 # Check for breaking changes
47 if echo "$line" | grep -qiE '^[a-z]+(\(.+\))?!:|BREAKING CHANGE'; then
48 BUMP="major"
49 break
50 fi
51
52 # Check for feat -> minor
53 if echo "$line" | grep -qE '^feat(\(.+\))?:'; then
54 BUMP="minor"
55 fi
56done <<< "$COMMITS"
57
58# Calculate new version
59case "$BUMP" in
60 major) MAJOR=$((MAJOR + 1)); MINOR=0; PATCH=0 ;;
61 minor) MINOR=$((MINOR + 1)); PATCH=0 ;;
62 patch) PATCH=$((PATCH + 1)) ;;
63esac
64
65NEW_VERSION="v${MAJOR}.${MINOR}.${PATCH}"
66
67echo "Bump type: $BUMP"
68echo "New version: $NEW_VERSION"
69echo "Tags: $NEW_VERSION, cmd/shutter/$NEW_VERSION"
70
71if $DRY_RUN; then
72 echo ""
73 echo "(dry run — no tags created)"
74 exit 0
75fi
76
77echo ""
78read -p "Create tags and push? [y/N] " -n 1 -r
79echo ""
80
81if [[ $REPLY =~ ^[Yy]$ ]]; then
82 jj tag set "$NEW_VERSION" "cmd/shutter/$NEW_VERSION"
83 # jj git push doesn't support tags, so export to git and push via git
84 jj git export
85 GIT_DIR=$(jj git root)
86 git --git-dir="$GIT_DIR" push origin "$NEW_VERSION" "cmd/shutter/$NEW_VERSION"
87 echo "Done. Tagged and pushed $NEW_VERSION"
88else
89 echo "Aborted."
90fi