#!/bin/bash set -euo pipefail # logpile parser bump script # Moves logpile to the latest published git.mchus.pro/reanimator/parser tag, # commits, tags the next logpile release as vC.P (C = release counter, # P = parser major), # pushes the branch and tag, and builds bin/logpile. # # Env: # PARSER_VERSION=v38.0.0 use this parser tag instead of the latest one # PARSER_DIR=../reanimator/parser local parser checkout to pull (optional) # DRY_RUN=1 update code, test and build, but do not commit/tag/push # NO_PUSH=1 commit and tag locally, do not push # FORCE=1 release even if the parser version is unchanged RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' die() { echo -e "${RED}Error: $*${NC}" >&2; exit 1; } step() { echo -e "${YELLOW}==> $*${NC}"; } cd "$(git rev-parse --show-toplevel)" PARSER_MODULE_BASE="git.mchus.pro/reanimator/parser" PARSER_REMOTE="https://git.mchus.pro/reanimator/parser.git" PARSER_DIR="${PARSER_DIR:-../reanimator/parser}" export GOPRIVATE="${GOPRIVATE:-git.mchus.pro/*}" export GOTOOLCHAIN="${GOTOOLCHAIN:-local}" # 1. Preconditions if [[ -n "$(git status --porcelain)" ]] && [[ "${DRY_RUN:-}" != "1" ]]; then die "working tree has uncommitted changes (commit/stash them, or use DRY_RUN=1)" fi BRANCH=$(git branch --show-current) [[ -n "$BRANCH" ]] || die "detached HEAD" # 2. Pull the local parser checkout (best effort; versions come from the remote) if [[ -d "$PARSER_DIR/.git" ]]; then step "Pulling parser checkout $PARSER_DIR" if [[ -z "$(git -C "$PARSER_DIR" status --porcelain)" ]]; then git -C "$PARSER_DIR" pull --ff-only --tags -q || echo " (pull failed, continuing)" else echo " (dirty, skipped)" fi fi # 3. Resolve the target parser version if [[ -z "${PARSER_VERSION:-}" ]]; then step "Looking up latest parser tag" PARSER_VERSION=$(git ls-remote --tags --refs "$PARSER_REMOTE" \ | sed -n 's#.*refs/tags/\(v[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)$#\1#p' \ | sort -t. -k1.2,1n -k2,2n -k3,3n | tail -1) [[ -n "$PARSER_VERSION" ]] || die "no parser tags found at $PARSER_REMOTE" fi NEW_MAJOR=$(echo "$PARSER_VERSION" | sed 's/^v\([0-9]*\)\..*/\1/') NEW_MODULE="$PARSER_MODULE_BASE/v$NEW_MAJOR" OLD_LINE=$(go list -m -f '{{.Path}} {{.Version}}' all | grep "^$PARSER_MODULE_BASE/v[0-9]* " || true) [[ -n "$OLD_LINE" ]] || die "parser module not found in go.mod" OLD_MODULE=${OLD_LINE% *} OLD_VERSION=${OLD_LINE#* } OLD_MAJOR=${OLD_MODULE##*/v} echo " parser: $OLD_VERSION -> $PARSER_VERSION" if [[ "$OLD_VERSION" == "$PARSER_VERSION" ]] && [[ "${FORCE:-}" != "1" ]]; then echo -e "${GREEN}Already on $PARSER_VERSION, nothing to do (FORCE=1 to release anyway)${NC}" exit 0 fi # 4. Rewrite module references if [[ "$OLD_MODULE" != "$NEW_MODULE" ]]; then step "Rewriting imports $OLD_MODULE -> $NEW_MODULE" GO_FILES=$(grep -rl --include='*.go' "$OLD_MODULE" cmd internal | grep -v '^internal/chart/' || true) DOC_FILES=$(grep -rl "$OLD_MODULE" bible-local | grep -v '10-decisions.md' || true) for f in $GO_FILES $DOC_FILES; do sed -i.bak "s#$OLD_MODULE#$NEW_MODULE#g" "$f" && rm -f "$f.bak" done echo " $(echo "$GO_FILES" | grep -c . || true) Go files, $(echo "$DOC_FILES" | grep -c . || true) docs" go mod edit -droprequire "$OLD_MODULE" -require "$NEW_MODULE@$PARSER_VERSION" else go mod edit -require "$NEW_MODULE@$PARSER_VERSION" fi go mod tidy if [[ -n "${GO_FILES:-}" ]]; then UNFORMATTED=$(gofmt -l $GO_FILES) [[ -z "$UNFORMATTED" ]] || die "gofmt reports issues in: $UNFORMATTED" fi # 5. Verify step "go build / vet / test" go build ./... go vet ./... go test ./... # 6. Next logpile tag: v. # counter = highest N among vN.M tags + 1 (v2.49 -> v3.38 -> v4.38 -> v5.39 ...) REMOTE_TAGS=$(git ls-remote --tags --refs origin | sed 's#.*refs/tags/##') \ || die "cannot list tags on origin" LAST_COUNTER=$({ git tag -l; echo "$REMOTE_TAGS"; } \ | sed -n 's/^v\([0-9][0-9]*\)\.[0-9][0-9]*$/\1/p' | sort -n | tail -1) [[ -n "$LAST_COUNTER" ]] || die "no vN.M tags found" TAG="v$(( LAST_COUNTER + 1 )).$NEW_MAJOR" if git rev-parse -q --verify "refs/tags/$TAG" >/dev/null || echo "$REMOTE_TAGS" | grep -qx "$TAG"; then die "tag $TAG already exists" fi echo " logpile tag: $TAG" if [[ "${DRY_RUN:-}" == "1" ]]; then step "DRY_RUN: building without commit/tag/push" make build echo -e "${GREEN}Dry run done. Would commit, tag $TAG and push $BRANCH.${NC}" exit 0 fi # 7. Commit, tag, push step "Committing and tagging $TAG" git add go.mod go.sum cmd internal bible-local git commit -q -m "chore: update parser module to $PARSER_VERSION" git tag -a "$TAG" -m "logpile $TAG (parser $PARSER_VERSION)" if [[ "${NO_PUSH:-}" != "1" ]]; then step "Pushing $BRANCH and $TAG" git push origin "$BRANCH" git push origin "$TAG" fi # 8. Build with the new tag baked in step "Building" make build echo -e "${GREEN}Done: $TAG (parser $PARSER_VERSION) -> bin/logpile${NC}"