From 67ef836cceeb8ec54020e599e1c7c018e0da42b2 Mon Sep 17 00:00:00 2001 From: Nikita Korotkih Date: Sat, 30 Jan 2016 22:55:28 +0300 Subject: [PATCH] Add pre-push hook example --- .../pre-push/prevent-force-push-to-master | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100755 examples/pre-push/prevent-force-push-to-master diff --git a/examples/pre-push/prevent-force-push-to-master b/examples/pre-push/prevent-force-push-to-master new file mode 100755 index 0000000..3726168 --- /dev/null +++ b/examples/pre-push/prevent-force-push-to-master @@ -0,0 +1,22 @@ +#!/bin/bash + +# Sometimes it's better to not allow users to make force pushes to some branches. +# It can cause losing of some commits and can be pain for a team. +# If you want to protect from doing such a harmful action then add protected branches to "protected_branches" variable. + +protected_branch='(master|dev)' # you can set up multiple protected branches + +policy='[Policy] Never force push or delete the '$protected_branch' branch! (Prevented with pre-push hook.)' + +parent_pid=$(ps -oppid= -p $PPID) +push_command=$(ps -ocommand= -p $parent_pid) + +is_destructive='force|delete|\-f' +will_remove_protected_branch=':'$protected_branch + +if ([[ $push_command =~ $is_destructive ]] && [[ $push_command =~ $protected_branch ]]) \ + || [[ $push_command =~ $will_remove_protected_branch ]] +then + echo $policy + exit 1 +fi