Skip to content

Commit 1401594

Browse files
berquistButt4cak3
authored andcommitted
Optional hook to prevent committing to master (#270)
1 parent e78892e commit 1401594

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

tools/hooks/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Git Hooks
2+
3+
These are scripts executed by Git at specific steps of the version control workflow, depending on their name.
4+
5+
- `pre-commit` runs before attempting to commit, and prevents committing to the local master branch.
6+
7+
To install,
8+
```bash
9+
$ ./install_hooks.bash
10+
```
11+
12+
To read more about hooks, read the [Pro Git chapter](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks).

tools/hooks/install_hooks.bash

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
3+
set -o errexit
4+
5+
# Grab the directory of _this_ script.
6+
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
7+
PROJECTDIR="$(realpath "${SCRIPTDIR}"/../..)"
8+
9+
# Try to link each hook into the project's .git/hooks directory.
10+
for hook in $(ls "${SCRIPTDIR}"/*.hook); do
11+
stub="${hook%.*}"
12+
dest="${PROJECTDIR}/.git/hooks/$(basename ${stub})"
13+
ln -sv "${hook}" "${dest}"
14+
done

tools/hooks/pre-commit.hook

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env sh
2+
3+
# prevent commit to local master branch
4+
# taken from https://gist.github.com/aaronhoffman/ffbfd36928f9336be2436cffe39feaec#file-pre-commit
5+
6+
branch=`git symbolic-ref HEAD`
7+
if [ "$branch" = "refs/heads/master" ]; then
8+
echo "pre-commit hook: Cannot commit to the local master branch."
9+
echo "Please make all your changes in a new feature branch:"
10+
echo "$ git checkout -b my-new-feature-branch"
11+
exit 1
12+
fi
13+
14+
exit 0

0 commit comments

Comments
 (0)