Skip to content

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 4 commits into from
Jul 19, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions .hooks/README.md
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).
11 changes: 11 additions & 0 deletions .hooks/install_hooks.bash
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
12 changes: 12 additions & 0 deletions .hooks/pre-commit.hook
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
13 changes: 13 additions & 0 deletions .hooks/pre-push.hook
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
Copy link
Contributor

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.

Copy link
Member Author

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 and refs/heads/master are equal, but if the remote isn't upstream it fails, and making this bulletproof is simply not worth it.

echo "pre-push hook: Can not push to remote master branch."
exit 1
fi
done

exit 0