Skip to content

Commit 0424962

Browse files
jokramjonico
authored andcommitted
Fixed pattern matching in block_branch_names.sh (#128)
Fixed pattern matching to accept only lower case characters and not to block tags [Fix] Pattern matching [a-z] did match also upper case characters [Fix] Tags were always blocked
1 parent 7090ff0 commit 0424962

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

pre-receive-hooks/block_branch_names.sh

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,19 @@
1010

1111
zero_commit="0000000000000000000000000000000000000000"
1212

13+
# Ensure that [a-z] means only lower case ASCII characters => set LC_COLLATE to 'C'
14+
# See http://unix.stackexchange.com/questions/227070/why-does-a-z-match-lowercase-letters-in-bash
15+
# See https://www.gnu.org/software/bash/manual/bashref.html#Pattern-Matching
16+
LC_COLLATE='C'
17+
1318
while read oldrev newrev refname; do
14-
# Prevent creation of new branches that don't match `^refs/heads/[a-z]+$`
15-
if [[ $oldrev == $zero_commit && ! $refname =~ ^refs/heads/[a-z]+$ ]]; then
16-
echo "Blocking creation of new branch $refname because it must only contain lower-case alphabetical characters."
17-
exit 1
19+
# Only check new branches ($oldrev is zero commit), don't block tags
20+
if [[ $oldrev == $zero_commit && $refname =~ ^refs/heads/ ]]; then
21+
# Check if the branch name is lower case characters (ASCII only), '-', '_', "/" or numbers
22+
if [[ ! $refname =~ ^refs/heads/[-a-z0-9_/]+$ ]]; then
23+
echo "Blocking creation of new branch $refname because it must only contain lower-case alpha-numeric characters, '-', '_' or '/'."
24+
exit 1
25+
fi
1826
fi
1927
done
2028
exit 0

0 commit comments

Comments
 (0)