Created
November 3, 2016 14:35
-
-
Save brookst/c286f4ce10aa4ac6ed96df2bfbc0d101 to your computer and use it in GitHub Desktop.
Demo using a hook to restrict branch names
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Demo using a hook to restrict branch names | |
# !Run in a temp dir! | |
set -ue | |
main () { | |
echo \> Setup | |
mkdir work | |
cd work | |
git init | |
echo "Initial state" > file | |
git add file | |
git commit -m "Initial commit" | |
cd - | |
git clone --bare work server | |
cd server | |
echo \> Write ref update hook | |
cat <<-EOF > hooks/update | |
#!/bin/bash | |
ref_name=\$1 | |
old_ref=\$2 | |
new_ref=\$3 | |
echo "Update \${ref_name}: \$old_ref → \$new_ref" | |
if [ "\$old_ref" != "0000000000000000000000000000000000000000" ]; then | |
echo "Existing branches may be updated" | |
exit 0 | |
fi | |
if [[ "\$ref_name" =~ "refs/heads/" ]]; then | |
# Check branch name matches policy | |
if [[ "\${ref_name}" =~ "/jira" ]]; then | |
echo "Branch OK" | |
exit 0 | |
else | |
echo "Policy: Branch must start with \"jira\"" | |
exit 1 | |
fi | |
else | |
echo "Not a branch: \$ref_name" | |
fi | |
EOF | |
chmod +x hooks/update | |
cd - | |
cd work | |
git remote add server ../server | |
cd - | |
echo | |
echo \> Make a change | |
cd work | |
echo "Modified state" > file | |
git commit -m "Modification" file | |
git push server master | |
cd - | |
echo | |
echo \> Submit a tag | |
cd work | |
git tag testtag | |
git push server testtag | |
cd - | |
echo | |
echo \> Make good and bad branches | |
cd work | |
git checkout -b jira42 | |
git checkout -b nope24 | |
git push server nope24 jira42 || true | |
cd - | |
} | |
if [ "${1:-}" = "clean" ]; then | |
rm -rf work server | |
else | |
main | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment