Skip to content

Commit 67ef836

Browse files
author
Nikita Korotkih
committed
Add pre-push hook example
1 parent 5d21c5d commit 67ef836

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
3+
# Sometimes it's better to not allow users to make force pushes to some branches.
4+
# It can cause losing of some commits and can be pain for a team.
5+
# If you want to protect from doing such a harmful action then add protected branches to "protected_branches" variable.
6+
7+
protected_branch='(master|dev)' # you can set up multiple protected branches
8+
9+
policy='[Policy] Never force push or delete the '$protected_branch' branch! (Prevented with pre-push hook.)'
10+
11+
parent_pid=$(ps -oppid= -p $PPID)
12+
push_command=$(ps -ocommand= -p $parent_pid)
13+
14+
is_destructive='force|delete|\-f'
15+
will_remove_protected_branch=':'$protected_branch
16+
17+
if ([[ $push_command =~ $is_destructive ]] && [[ $push_command =~ $protected_branch ]]) \
18+
|| [[ $push_command =~ $will_remove_protected_branch ]]
19+
then
20+
echo $policy
21+
exit 1
22+
fi

0 commit comments

Comments
 (0)