-
Notifications
You must be signed in to change notification settings - Fork 30
Add pre-push hook example #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you explain what it is for? Looks a little bit weird) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done