LAST_TAG := $(shell git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")

## Create a new semver tag based on conventional commits since last tag
tag:
	@BUMP=$$(git log $(LAST_TAG)..HEAD --pretty=format:'%s' 2>/dev/null \
		| grep -qE '!:|BREAKING CHANGE' && echo major \
		|| git log $(LAST_TAG)..HEAD --pretty=format:'%s' 2>/dev/null \
		| grep -qE '^feat' && echo minor \
		|| echo patch); \
	OLD=$$(echo $(LAST_TAG) | sed 's/^v//'); \
	NEW=$$(echo $$OLD | awk -v b=$$BUMP -F. '{ \
		if(b=="major") print $$1+1".0.0"; \
		else if(b=="minor") print $$1"."$$2+1".0"; \
		else print $$1"."$$2"."$$3+1}'); \
	echo "$$OLD -> $$NEW ($$BUMP)"; \
	git tag "v$$NEW"

## Push the latest tag to origin
push:
	@git push origin $(LAST_TAG)
