From f61a3d9d6cc3d88de64cd92efe4f48c587a27050 Mon Sep 17 00:00:00 2001 From: Eric Berquist Date: Wed, 18 Jul 2018 13:59:24 -0400 Subject: [PATCH 1/4] Optional hooks to prevent committing/pushing to master. --- .hooks/README.md | 13 +++++++++++++ .hooks/install_hooks.bash | 11 +++++++++++ .hooks/pre-commit.hook | 12 ++++++++++++ .hooks/pre-push.hook | 13 +++++++++++++ 4 files changed, 49 insertions(+) create mode 100644 .hooks/README.md create mode 100755 .hooks/install_hooks.bash create mode 100755 .hooks/pre-commit.hook create mode 100755 .hooks/pre-push.hook diff --git a/.hooks/README.md b/.hooks/README.md new file mode 100644 index 000000000..452b910bc --- /dev/null +++ b/.hooks/README.md @@ -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). diff --git a/.hooks/install_hooks.bash b/.hooks/install_hooks.bash new file mode 100755 index 000000000..be586c55d --- /dev/null +++ b/.hooks/install_hooks.bash @@ -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 diff --git a/.hooks/pre-commit.hook b/.hooks/pre-commit.hook new file mode 100755 index 000000000..a30513e4a --- /dev/null +++ b/.hooks/pre-commit.hook @@ -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 diff --git a/.hooks/pre-push.hook b/.hooks/pre-push.hook new file mode 100755 index 000000000..e9cc2654c --- /dev/null +++ b/.hooks/pre-push.hook @@ -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 From 0319dfe90ae8684f22e0c429eaad5b8922e7a023 Mon Sep 17 00:00:00 2001 From: Eric Berquist Date: Wed, 18 Jul 2018 15:18:22 -0400 Subject: [PATCH 2/4] Remove the push hook ...since it has the undesirable behavior of not allowing push after rebasing against upstream, for example. --- .hooks/README.md | 1 - .hooks/pre-push.hook | 13 ------------- 2 files changed, 14 deletions(-) delete mode 100755 .hooks/pre-push.hook diff --git a/.hooks/README.md b/.hooks/README.md index 452b910bc..30a6f6954 100644 --- a/.hooks/README.md +++ b/.hooks/README.md @@ -3,7 +3,6 @@ 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 diff --git a/.hooks/pre-push.hook b/.hooks/pre-push.hook deleted file mode 100755 index e9cc2654c..000000000 --- a/.hooks/pre-push.hook +++ /dev/null @@ -1,13 +0,0 @@ -#!/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 From 52d882fb2b94f3bb994c803b75319a598e140273 Mon Sep 17 00:00:00 2001 From: Eric Berquist Date: Wed, 18 Jul 2018 17:12:27 -0400 Subject: [PATCH 3/4] Move Git hooks into a tools directory. --- {.hooks => tools/hooks}/README.md | 0 {.hooks => tools/hooks}/install_hooks.bash | 5 ++++- {.hooks => tools/hooks}/pre-commit.hook | 0 3 files changed, 4 insertions(+), 1 deletion(-) rename {.hooks => tools/hooks}/README.md (100%) rename {.hooks => tools/hooks}/install_hooks.bash (71%) rename {.hooks => tools/hooks}/pre-commit.hook (100%) diff --git a/.hooks/README.md b/tools/hooks/README.md similarity index 100% rename from .hooks/README.md rename to tools/hooks/README.md diff --git a/.hooks/install_hooks.bash b/tools/hooks/install_hooks.bash similarity index 71% rename from .hooks/install_hooks.bash rename to tools/hooks/install_hooks.bash index be586c55d..c7a7bb923 100755 --- a/.hooks/install_hooks.bash +++ b/tools/hooks/install_hooks.bash @@ -1,11 +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="$(realpath ${SCRIPTDIR}/../.git/hooks)/$(basename ${stub})" + dest="${PROJECTDIR}/.git/hooks/$(basename ${stub})" ln -sv "${hook}" "${dest}" done diff --git a/.hooks/pre-commit.hook b/tools/hooks/pre-commit.hook similarity index 100% rename from .hooks/pre-commit.hook rename to tools/hooks/pre-commit.hook From 7eecad53ce84065e39c21d4d2923a9abc65a1002 Mon Sep 17 00:00:00 2001 From: Eric Berquist Date: Wed, 18 Jul 2018 17:17:23 -0400 Subject: [PATCH 4/4] The pre-commit hook should give some guidance. --- tools/hooks/pre-commit.hook | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/hooks/pre-commit.hook b/tools/hooks/pre-commit.hook index a30513e4a..35ce9d24a 100755 --- a/tools/hooks/pre-commit.hook +++ b/tools/hooks/pre-commit.hook @@ -5,7 +5,9 @@ branch=`git symbolic-ref HEAD` if [ "$branch" = "refs/heads/master" ]; then - echo "pre-commit hook: Can not commit to the local master branch." + 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