Skip to content

Instantly share code, notes, and snippets.

@Dzordzu
Last active May 11, 2021 22:50
Show Gist options
  • Save Dzordzu/a75ea880d0e1948ae76114a0479cd9ae to your computer and use it in GitHub Desktop.
Save Dzordzu/a75ea880d0e1948ae76114a0479cd9ae to your computer and use it in GitHub Desktop.
Semver - git hook
#!/bin/sh
#
# An example hook script to check the commit log message.
# Called by "git commit" with one argument, the name of the file
# that has the commit message. The hook should exit with non-zero
# status after issuing an appropriate message if it wants to stop the
# commit. The hook is allowed to edit the commit message file.
#
# To enable this hook, rename this file to "commit-msg".
# Uncomment the below to add a Signed-off-by line to the message.
# Doing this in a hook is a bad idea in general, but the prepare-commit-msg
# hook is more suited to it.
#
# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"
# This example catches duplicate Signed-off-by lines.
message="`cat $1`"
root_dir=`git rev-parse --show-toplevel`
version=`grep -e "^VERSION: " $root_dir/README.md | sed 's/VERSION: //g'`
semver_update() {
sed -i "s/VERSION: .*/VERSION: $1/" $root_dir/README.md
echo "v$1" > $root_dir/.git/.semver.newver
return 0
}
major=`echo $version | grep -oe "^[0-9]\+"`
version=`echo $version | sed 's/^[0-9]\+\.//'`
minor=`echo $version | grep -oe "^[0-9]\+"`
version=`echo $version | sed 's/^[0-9]\+\.//'`
patch=`echo $version | grep -oe "^[0-9]\+"`
version=`echo $version | sed 's/^[0-9]\+\.\?//'`
version=`echo $version | sed 's/^-rc//'`
release_candidate=`echo $version | grep -oe "[0-9]\+"`
if [[ "$message" == "rc"*"("* ]]; then
if [[ -z $release_candidate ]]; then
if [[ "$message" == "rc-major("* ]]; then
major="$(( $major + 1 ))"
elif [[ "$message" == "rc-minor("* ]]; then
minor="$(( $minor + 1 ))"
else
patch="$(( $patch + 1 ))"
fi
release_candidate=0
fi
new_version="$major.$minor.$patch-rc$(( $release_candidate+1 ))"
echo "Release candidate: bumped version to $new_version"
semver_update $new_version
fi
new_version="$major.$minor.$patch"
if [[ "$message" == "fix("* ]]; then
if [[ -z $release_candidate ]]; then
new_version="$major.$minor.$(( $patch+1 ))"
fi
echo "Fix: bumped version to $new_version"
semver_update $new_version
fi
if [[ "$message" == "feat("* ]]; then
if [[ -z $release_candidate ]]; then
new_version="$major.$(( $minor+1 )).0"
fi
echo "Feature: bumped version to $new_version"
semver_update $new_version
fi
if [[ "$message" == "perf("* || "$message" == "BREAKING CHANGE: "* || "$message" == "break("* ]]; then
if [[ -z $release_candidate ]]; then
new_version="$(( $major+1 )).0.0"
fi
echo "Breaking: bumped version to $new_version"
semver_update $new_version
fi
#!/bin/sh
#
# An example hook script to prepare a packed repository for use over
# dumb transports.
#
# To enable this hook, rename this file to "post-update".
# exec git update-server-info
root_dir=`git rev-parse --show-toplevel`
if [ -f $root_dir/.git/.semver.newver ]; then
version="`cat $root_dir/.git/.semver.newver`"
rm $root_dir/.git/.semver.newver &> /dev/null
git commit -a -m "Release $version" --no-verify &> /dev/null
git tag "$version"
fi
1. Enter your git repository
2. Enter `.git/hooks`
3. Paste scripts or extend existing ones
You may want to change `semver_update` function within `commit-msg`. Feel free to change versions in other files than README.md
@Dzordzu
Copy link
Author

Dzordzu commented May 11, 2021

RESOLVED Known issue: tagging is broken

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment