# Release helpers.
#
# `make patch|minor|major` bumps the version in Cargo.toml and Cargo.lock, folds
# that bump into the last commit, and tags it. Everything is one action so a
# release cannot end up half-done: no version bump left untagged, no tag without
# a bump.

.PHONY: help check patch minor major version release install

help:
	@echo "make check    fmt, clippy and the suite, in that order"
	@echo "make patch    bump $(CURRENT) -> next patch, amend into HEAD, tag"
	@echo "make minor    bump $(CURRENT) -> next minor, amend into HEAD, tag"
	@echo "make major    bump $(CURRENT) -> next major, amend into HEAD, tag"
	@echo "make version  print the current version"
	@echo "make install  cargo install --path . && worklane install-skill"

# The one command to run before committing. docs/testing.md pins fmt and clippy as clean and
# 83d31cd is what happens when only memory enforces that: fmt drifted to about 160 places
# across every file with the tool installed and configured the whole time. This is not CI,
# which a single-author tool with no pipeline anywhere does not want, and not a hook, which
# is a per-clone opt-in the way `blame.ignoreRevsFile` is. It is one name to type, so
# "remember to run fmt, clippy and the tests" becomes "run make check".
#
# Cheapest first, so a formatting slip is reported in a second rather than after the suite.
# `--all-targets` on clippy, matching the rule in docs/testing.md, so the tests are linted too.
# `-D warnings` is what makes clippy's opinion an exit code: the rule says clean, and a
# warning printed into a wall of cargo output is not clean, it is unread.
#
# Completions are in the suite rather than a step of their own. `cargo test` sources the
# generated script into a real fish and asks it for candidates, so "check tests, check
# completions" is one command already, and a second target would be a second thing to
# remember. The fish tests skip when fish is not installed, printing that they did.
check:
	cargo fmt --check
	cargo clippy --all-targets -- -D warnings
	cargo test

# Read from the [package] table of Cargo.toml, which is the version the binary
# reports through `--version`, so it is the single source of truth. The name is
# what finds this crate's own entry in Cargo.lock.
PACKAGE = $(shell awk -F'"' '/^\[package\]/{p=1;next} /^\[/{p=0} p && /^name *= *"/{print $$2; exit}' Cargo.toml)
CURRENT = $(shell awk -F'"' '/^\[package\]/{p=1;next} /^\[/{p=0} p && /^version *= *"/{print $$2; exit}' Cargo.toml)

# 1 appends ", and bump to <version>" to the amended commit's subject; empty
# leaves the message alone.
AMEND_SUBJECT = 1

version:
	@echo $(CURRENT)

patch: BUMP = patch
minor: BUMP = minor
major: BUMP = major
patch minor major: release

# A release rewrites the last commit, so refuse anything that would make that
# rewrite lose work or orphan a tag.
release:
	@set -e; \
	if ! git rev-parse -q --verify HEAD >/dev/null 2>&1; then \
		echo "error: no commits yet, so there is no commit to fold a version bump into. Commit first." >&2; \
		exit 1; \
	fi; \
	if [ -n "$$(git status --porcelain)" ]; then \
		echo "error: working tree is dirty. Commit or stash first, and decide whether that work belongs in this version:" >&2; \
		git status --short >&2; \
		exit 1; \
	fi; \
	if [ -n "$$(git tag --points-at HEAD)" ]; then \
		echo "error: HEAD is already tagged $$(git tag --points-at HEAD | paste -sd' '). Amending it would leave that tag on an orphaned commit. Commit something first." >&2; \
		exit 1; \
	fi; \
	if upstream=$$(git rev-parse --abbrev-ref --symbolic-full-name '@{u}' 2>/dev/null) && \
	   git merge-base --is-ancestor HEAD "$$upstream" 2>/dev/null; then \
		echo "error: HEAD is already pushed to $$upstream. Amending it would need a force push. Commit something first." >&2; \
		exit 1; \
	fi; \
	current="$(CURRENT)"; \
	if [ -z "$$current" ]; then echo "error: no version found in the [package] table of Cargo.toml" >&2; exit 1; fi; \
	new=$$(echo "$$current" | awk -F. -v kind=$(BUMP) '{ \
		if (NF != 3) exit 1; \
		if (kind == "patch") printf "%d.%d.%d\n", $$1, $$2, $$3 + 1; \
		else if (kind == "minor") printf "%d.%d.0\n", $$1, $$2 + 1; \
		else printf "%d.0.0\n", $$1 + 1 }'); \
	if [ -z "$$new" ]; then echo "error: cannot bump version '$$current': expected major.minor.patch" >&2; exit 1; fi; \
	if git rev-parse -q --verify "refs/tags/v$$new" >/dev/null; then \
		echo "error: tag v$$new already exists" >&2; exit 1; \
	fi; \
	awk -v v="$$new" '/^\[package\]/{p=1} /^\[/ && !/^\[package\]/{p=0} p && /^version *= *"/{sub(/"[^"]*"/, "\"" v "\"")} {print}' Cargo.toml > Cargo.toml.bump; \
	mv Cargo.toml.bump Cargo.toml; \
	git add Cargo.toml; \
	if [ -f Cargo.lock ] && git ls-files --error-unmatch Cargo.lock >/dev/null 2>&1; then \
		awk -v v="$$new" -v pkg="$(PACKAGE)" '$$0 == "name = \"" pkg "\""{f=1} f && /^version = "/{sub(/"[^"]*"/, "\"" v "\""); f=0} {print}' Cargo.lock > Cargo.lock.bump; \
		mv Cargo.lock.bump Cargo.lock; \
		git add Cargo.lock; \
	fi; \
	if [ -n "$(AMEND_SUBJECT)" ]; then \
		subject=$$(git log -1 --format=%s | sed -E 's/, and bump to [0-9]+\.[0-9]+\.[0-9]+$$//'); \
		body=$$(git log -1 --format=%b); \
		if [ -n "$$body" ]; then \
			git commit --quiet --amend -m "$$subject, and bump to $$new" -m "$$body"; \
		else \
			git commit --quiet --amend -m "$$subject, and bump to $$new"; \
		fi; \
	else \
		git commit --quiet --amend --no-edit; \
	fi; \
	git tag -a "v$$new" -m "v$$new"; \
	echo "$$current -> $$new, tagged v$$new on $$(git rev-parse --short HEAD) ($$(git log -1 --format=%s))"

install:
	cargo install --path .
	worklane install-skill
