Skip to content

Commit 6c2330f

Browse files
authored
Adding check-branch-name hook
2 parents 92bbf6d + 886f1b5 commit 6c2330f

3 files changed

Lines changed: 39 additions & 0 deletions

File tree

.pre-commit-hooks.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@
1010
entry: pre_commit_hooks/check-comments.sh
1111
language: script
1212
types: [python]
13+
- id: check-branch-name
14+
name: Check the compliance of your branch name
15+
description: Checks that the name of the branch you are committing to is compliant with a given regex
16+
entry: pre_commit_hooks/check-branch-name.sh
17+
language: script
18+
pass_filenames: false
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash
2+
3+
while getopts r: flag
4+
do
5+
case "${flag}" in
6+
r) BRANCH_NAME_REGEX=${OPTARG};;
7+
*) echo "Invalid flag provided ($flag)"; exit 1;
8+
esac
9+
done
10+
11+
if [[ $BRANCH_NAME_REGEX == "" ]]; then
12+
echo "No regular expression provided to validate the branch name..."
13+
echo "Please provide one using the '-r' argument."
14+
echo ""
15+
echo "Sample 'repo' section of your '.pre-commit-config.yaml' file:"
16+
echo "- repo: https://github.com/alma/pre-commit-hooks"
17+
echo " rev: v1.1.0"
18+
echo " hooks:"
19+
echo " - id: check-branch-name"
20+
echo " args:"
21+
echo ' - "-r^(((ci|docs|feature|fix|hotfix|perf|refactor|test)\/.*)|main)$"'
22+
exit 1
23+
fi
24+
25+
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
26+
27+
if ! [[ $BRANCH_NAME =~ $BRANCH_NAME_REGEX ]]; then
28+
echo "*** Commit interrupted ***"
29+
echo "Your branch name does not match the naming convention."
30+
echo "Branch names should match \"$BRANCH_NAME_REGEX\"..."
31+
32+
exit 1
33+
fi

pre_commit_hooks/check-comments.sh

100644100755
File mode changed.

0 commit comments

Comments
 (0)