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 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
12 changes: 12 additions & 0 deletions tools/hooks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 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.

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).
14 changes: 14 additions & 0 deletions tools/hooks/install_hooks.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash

set -o errexit

# Grab the directory of _this_ script.
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROJECTDIR="$(realpath "${SCRIPTDIR}"/../..)"

# Try to link each hook into the project's .git/hooks directory.
for hook in $(ls "${SCRIPTDIR}"/*.hook); do
stub="${hook%.*}"
dest="${PROJECTDIR}/.git/hooks/$(basename ${stub})"
ln -sv "${hook}" "${dest}"
done
14 changes: 14 additions & 0 deletions tools/hooks/pre-commit.hook
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/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: Cannot commit to the local master branch."
echo "Please make all your changes in a new feature branch:"
echo "$ git checkout -b my-new-feature-branch"
exit 1
fi

exit 0