-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy path.githook.post-commit
executable file
·30 lines (25 loc) · 1.35 KB
/
.githook.post-commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/bin/bash
set -eo pipefail
# (Re-)Assigns git tags based on version in pyproject.toml file, logs action to stdout
# Current git doesn't have option to auto-push tags, so there's pre-push hook for that too
# (--follow-tags seem to only extend to annotated tags, which these aren't)
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"