Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions examples/pre-push/prevent-force-push-to-master
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a little description what this hook for.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

# 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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain what it is for? Looks a little bit weird)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the current process is a node.js one. So to get the original git command we need to run ps with parent pid. It looks trivial or maybe I didn't understand the question


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