-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from filiprojek/githooks
feat: githooks
- Loading branch information
Showing
4 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/bin/bash | ||
|
||
# @link https://gist.github.com/mattscilipoti/8424018 | ||
# | ||
# Called by "git push" after it has checked the remote status, | ||
# but before anything has been pushed. | ||
# | ||
# If this script exits with a non-zero status nothing will be pushed. | ||
# | ||
# Steps to install, from the root directory of your repo... | ||
# 1. Copy the file into your repo at `.git/hooks/pre-push` | ||
# 2. Set executable permissions, run `chmod +x .git/hooks/pre-push` | ||
# 3. Or, use `rake hooks:pre_push` to install | ||
# | ||
# Try a push to master, you should get a message `*** [Policy] Never push code directly to...` | ||
# | ||
# The commands below will not be allowed... | ||
# `git push origin master` | ||
# `git push --force origin master` | ||
# `git push --delete origin master` | ||
|
||
|
||
protected_branch='master' | ||
|
||
policy="\n\n[Policy] Never push code directly to the "$protected_branch" branch! (Prevented with pre-push hook.)\n\n" | ||
|
||
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,') | ||
|
||
push_command=$(ps -ocommand= -p $PPID) | ||
|
||
is_destructive='force|delete|\-f' | ||
|
||
will_remove_protected_branch=':'$protected_branch | ||
|
||
do_exit(){ | ||
echo -e $policy | ||
exit 1 | ||
} | ||
|
||
if [[ $push_command =~ $is_destructive ]] && [ $current_branch = $protected_branch ]; then | ||
do_exit | ||
fi | ||
|
||
if [[ $push_command =~ $is_destructive ]] && [[ $push_command =~ $protected_branch ]]; then | ||
do_exit | ||
fi | ||
|
||
if [[ $push_command =~ $will_remove_protected_branch ]]; then | ||
do_exit | ||
fi | ||
|
||
# Prevent ALL pushes to protected_branch | ||
if [[ $push_command =~ $protected_branch ]] || [ $current_branch = $protected_branch ]; then | ||
do_exit | ||
fi | ||
|
||
unset do_exit | ||
|
||
exit 0 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/bash | ||
|
||
cp .githooks/* .git/hooks | ||
|
||
echo "hooks have been copied" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/bin/bash | ||
|
||
# @link https://gist.github.com/mattscilipoti/8424018 | ||
# | ||
# Called by "git push" after it has checked the remote status, | ||
# but before anything has been pushed. | ||
# | ||
# If this script exits with a non-zero status nothing will be pushed. | ||
# | ||
# Steps to install, from the root directory of your repo... | ||
# 1. Copy the file into your repo at `.git/hooks/pre-push` | ||
# 2. Set executable permissions, run `chmod +x .git/hooks/pre-push` | ||
# 3. Or, use `rake hooks:pre_push` to install | ||
# | ||
# Try a push to master, you should get a message `*** [Policy] Never push code directly to...` | ||
# | ||
# The commands below will not be allowed... | ||
# `git push origin master` | ||
# `git push --force origin master` | ||
# `git push --delete origin master` | ||
|
||
|
||
protected_branch='master' | ||
|
||
policy="\n\n[Policy] Never push code directly to the "$protected_branch" branch! (Prevented with pre-push hook.)\n\n" | ||
|
||
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,') | ||
|
||
push_command=$(ps -ocommand= -p $PPID) | ||
|
||
is_destructive='force|delete|\-f' | ||
|
||
will_remove_protected_branch=':'$protected_branch | ||
|
||
do_exit(){ | ||
echo -e $policy | ||
exit 1 | ||
} | ||
|
||
if [[ $push_command =~ $is_destructive ]] && [ $current_branch = $protected_branch ]; then | ||
do_exit | ||
fi | ||
|
||
if [[ $push_command =~ $is_destructive ]] && [[ $push_command =~ $protected_branch ]]; then | ||
do_exit | ||
fi | ||
|
||
if [[ $push_command =~ $will_remove_protected_branch ]]; then | ||
do_exit | ||
fi | ||
|
||
# Prevent ALL pushes to protected_branch | ||
if [[ $push_command =~ $protected_branch ]] || [ $current_branch = $protected_branch ]; then | ||
do_exit | ||
fi | ||
|
||
unset do_exit | ||
|
||
exit 0 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/bash | ||
|
||
cp .githooks/* .git/hooks | ||
|
||
echo "hooks have been copied" |