-
-
Notifications
You must be signed in to change notification settings - Fork 360
Optional hooks to prevent committing/pushing to master #270
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,13 @@ | ||
# Git Hooks | ||
|
||
These are scripts executed by Git at specific steps of the version control workflow, depending on their name. | ||
|
||
- `pre-commit` runs before attempting to commit, and prevents committing to the local master branch. | ||
- `pre-push` runs before attempting to push, and prevents pushing to the remote master branch. | ||
|
||
To install, | ||
```bash | ||
$ ./install_hooks.bash | ||
``` | ||
|
||
To read more about hooks, read the [Pro Git chapter](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks). |
This file contains hidden or 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,11 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Grab the directory of _this_ script. | ||
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | ||
|
||
# Try to link each hook into the project's .git/hooks directory. | ||
for hook in $(ls "${SCRIPTDIR}"/*.hook); do | ||
stub="${hook%.*}" | ||
dest="$(realpath ${SCRIPTDIR}/../.git/hooks)/$(basename ${stub})" | ||
ln -sv "${hook}" "${dest}" | ||
done |
This file contains hidden or 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,12 @@ | ||
#!/usr/bin/env sh | ||
|
||
# prevent commit to local master branch | ||
# taken from https://gist.github.com/aaronhoffman/ffbfd36928f9336be2436cffe39feaec#file-pre-commit | ||
|
||
branch=`git symbolic-ref HEAD` | ||
if [ "$branch" = "refs/heads/master" ]; then | ||
echo "pre-commit hook: Can not commit to the local master branch." | ||
exit 1 | ||
fi | ||
|
||
exit 0 |
This file contains hidden or 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,13 @@ | ||
#!/usr/bin/env sh | ||
# prevent push to remote master branch | ||
# taken from https://gist.github.com/aaronhoffman/ffbfd36928f9336be2436cffe39feaec#file-pre-push | ||
|
||
while read local_ref local_sha remote_ref remote_sha | ||
do | ||
if [ "$remote_ref" = "refs/heads/master" ]; then | ||
echo "pre-push hook: Can not push to remote master branch." | ||
exit 1 | ||
fi | ||
done | ||
|
||
exit 0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think this will cause trouble when you have two remotes, origin and upstream where origin points to a personal fork and upstream points to this repository. When you pull changes from upstream and want to push them to origin to keep your fork up-to-date, this will block the push if I understand correctly.
It should be enough to block commits to master in my opinion.
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.
You are correct. It could be changed to allow the push if
refs/remotes/upstream/master
andrefs/heads/master
are equal, but if the remote isn'tupstream
it fails, and making this bulletproof is simply not worth it.