~johnhamelink/git-hooks

81623bb0a643565719170db01e54082630b9f593 — Admin 7 months ago
Add `git-branch-validator` - a `git-flow-avh` git hook

`git-branch-validator` checks that working branches (`bugfix/*`
`feature/*` `support/*` `hotfix/*`) are prefixed with a JIRA ticket
ID.

If the branch is a release branch, it checks that it is named in
way that somewhat resembles a version number.
1 files changed, 43 insertions(+), 0 deletions(-)

A git-branch-validator
A  => git-branch-validator +43 -0
@@ 1,43 @@
#!/bin/sh

# This script is designed to work with git-flow-avh git hooks.
#
# You can either put this file on your path and call the script from a
# git hook, or you can symlink it into your git hooks directory
# directly if you prefer.
#
# The version numbering regex is designed to be loose and forgiving -
# even allowing for a "v" prefix. You can tweak the regex by
# overriding the environment variables' defaults below:
#
#    export TICKET_PATTERN="^(JIRA-[[:digit:]])+"
#
#    git-branch-validator JIRA-999-add-widget origin \
#                        feature/JIRA-999-add-widget \
#                        develop

set -euo pipefail

NAME=$1
ORIGIN=$2
BRANCH=$3
BASE=$4

TICKET_PATTERN="${TICKET_PATTERN="^([[:upper:]]+-[[:digit:]]+)"}"
RELEASE_BRANCH_PATTERN="^release/*"
RELEASE_VERSION_PATTERN="${RELEASE_VERSION_PATTERN="^[vV]?[0-9]+(.[0-9]+){0,2}([-+]+[a-z]+)*$"}"
DEVELOP_BRANCH="${DEVELOP_BRANCH="develop"}"
MASTER_BRANCH="${MASTER_BRANCH="master"}"

if [[ $BRANCH =~ $RELEASE_BRANCH_PATTERN ]]; then
    if [[ ! $NAME =~ $RELEASE_VERSION_PATTERN ]]; then
        printf "\nInvalid release branch name:\n\n\t%s\n\n" "${BRANCH}"
        exit 1
    fi
else if [[ ! ( "${BRANCH}" == "${DEVELOP_BRANCH}" || "${BRANCH}" == "${MASTER_BRANCH}" || $NAME =~ $TICKET_PATTERN ) ]]; then
        printf "\nError: A JIRA ticket ID is required for the naming of branches like:\n\n\t%s\n\n" "${BRANCH}"
        exit 2
    fi
fi

exit 0