Skip to content

Commit

Permalink
Add post-commit hook to tag releases from pyproject.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
mk-fg committed Sep 27, 2024
1 parent 606b2ea commit f417b75
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .githook.post-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash
set -eo pipefail

die() {
echo >&2 $'\ngit-post-commit :: ----------------------------------------'
echo >&2 "git-post-commit :: ERROR: $@"
echo >&2 $'git-post-commit :: ----------------------------------------\n'; exit 1; }

ver=$( git show --no-color --diff-filter=M -aU0 pyproject.toml |
gawk '/^\+version\s*=/ {
split(substr($NF,2,length($NF)-2),v,".")
print v[1]+0 "." v[2]+0 "." v[3]+0}' )

[[ "$ver" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || {
ver=$( gawk '/^version\s*=/ {
split(substr($NF,2,length($NF)-2),v,".")
print v[1]+0 "." v[2]+0 "." v[3]+0}' pyproject.toml )
[[ "$ver" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || \
die 'Failed to get version from git-show and pyproject.toml file'
ver_tag=$(git tag --sort=v:refname | tail -1)
[[ -n "$ver" && "$ver" = "$ver_tag" ]] || die 'No new release to tag,'`
`" and last git-tag [ $ver_tag ] does not match pyproject.toml version [ $ver ]"
echo $'\ngit-post-commit: no new tag, last one matches pyproject.toml\n'; exit 0; }

git tag -f "$ver" HEAD # can be reassigning tag after --amend
echo -e "\ngit-post-commit: tagged new release [ $ver ]\n"

0 comments on commit f417b75

Please sign in to comment.