]> gitweb.fluxo.info Git - git-hooks.git/commitdiff
Add the ability to remind users when committing to any repo without git-hooks.
authorBenjamin C Meyer <ben@meyerhome.net>
Thu, 7 Nov 2013 02:12:26 +0000 (21:12 -0500)
committerBenjamin C Meyer <ben@meyerhome.net>
Thu, 7 Nov 2013 02:51:19 +0000 (21:51 -0500)
It is easy to forget to install git-hooks in a repository you are using.
A commit with no error message could both mean you don't have git-hooks
installed and it can mean that you do have it installed, but your
commit was perfect and there was no errors.

The new option --installglobal creates a default .git template
that is used by all future cloned repositories (and git init'd)
that remind users that the repository doesn't have git-hooks
installed.

A number of projects I have been involved with used git-hooks,
but founds its weakest point was that eventually someone would
re-clone a repo and make commits with errors before turning
git-hooks back on.  Another common problem was when we had
multiple git repositories and git-hooks was only installed in
some of them, but not all

While it would be nice to be able to use --instalglobal to
automatically turn on git-hooks on every future repo eventually
some git repository on github would include a hook that would
email <secret stuff> back home and all you would have to do
would be to clone it to activate it which would be a "bad thing",
but it can pester you without harm.

Signed-off-by: Benjamin C Meyer <ben@meyerhome.net>
README
git-hooks

diff --git a/README b/README
index 6784bb1244e1063eb5ac7eaf1c9bf01a12591b58..530e5e72e3a2532e88178281927559af09da6863 100644 (file)
--- a/README
+++ b/README
@@ -9,6 +9,8 @@ Add git-hooks to your PATH environment variable so 'git hooks' can be run.
 
 Run 'git hooks --install' Inside a git project to change that projects git hooks to use git-hooks hooks.  'git hooks --uninstall' can be run at any time to go back to the old hooks that were installed before (typically the default which does nothing).
 
+Run 'git hooks --installglobal' to force any new git repository or any git repository you clone to have a reminders to install git hooksi. (It can't be on by default for security reasons of course)
+
 ************************************
 Overview
 
index fa080019afa34fc4d3a191c8de69d9d9c588ab1f..2d75c8b2f1519bb3b50f820553ba521284f64e92 100755 (executable)
--- a/git-hooks
+++ b/git-hooks
@@ -95,6 +95,23 @@ function run_hook
     set +e
 }
 
+function install_hooks_into
+{
+    DIR=$1
+    cd "${DIR}"
+
+    set -e
+    mv hooks hooks.old
+    set +e
+    mkdir hooks
+    cd hooks
+    for file in applypatch-msg commit-msg post-applypatch post-checkout post-commit post-merge post-receive pre-applypatch pre-auto-gc pre-commit prepare-commit-msg pre-rebase pre-receive update pre-push
+    do
+        echo "${2}" > "${file}"
+        chmod +x "${file}"
+    done
+}
+
 function install_hooks
 {
     GITDIR=`git rev-parse --git-dir`
@@ -108,17 +125,9 @@ function install_hooks
             echo "hooks.old already exists, perhaps you already installed?"
             return 1
         fi
-        set -e
-        mv hooks hooks.old
-        set +e
-        mkdir hooks
-        cd hooks
-        for file in applypatch-msg commit-msg post-applypatch post-checkout post-commit post-merge post-receive pre-applypatch pre-auto-gc pre-commit prepare-commit-msg pre-rebase pre-receive update pre-push
-        do
-            echo '#!/usr/bin/env bash
-git-hooks run "$0" "$@"' > "${file}"
-            chmod +x "${file}"
-        done
+    cmd='#!/usr/bin/env bash
+git-hooks run "$0" "$@"';
+    install_hooks_into "${PWD}" "${cmd}"
     else
         if [ ! -d hooks.old ] ; then
             echo "Error, hooks.old doesn't exists, aborting uninstall to not destroy something"
@@ -158,6 +167,28 @@ function report_error {
     exit 1
 }
 
+function installglobal {
+    TEMPLATE="$HOME/.git-template-with-git-hooks"
+    if [ ! -d "${TEMPLATE}" ] ; then
+        DEFAULT=/usr/share/git-core/templates
+        if [ -d ${DEFAULT} ] ; then
+            cp -rf /usr/share/git-core/templates "${TEMPLATE}"
+        else
+            mkdir -p "${TEMPLATE}/hooks"
+        fi
+        cmd="#!/usr/bin/env bash
+echo \"git hooks not installed in this repository.  Run 'git hooks --install' to install it or 'git hooks -h' for more information.\"";
+        install_hooks_into "${TEMPLATE}" "${cmd}"
+        mv "${TEMPLATE}/hooks.old" "${TEMPLATE}/hooks.original"
+    fi
+    git config --global init.templatedir "${TEMPLATE}"
+    echo "Git global config init.templatedir is now set to ${TEMPLATE}"
+}
+
+function uninstallglobal {
+    git config --global --unset init.templatedir
+}
+
 case $1 in
     run )
         if [ ! -z "${GIT_DIR}" ] ; then
@@ -170,14 +201,28 @@ case $1 in
     --install|--uninstall )
         install_hooks "$1"
         ;;
+    --installglobal )
+        installglobal
+        ;;
+    --uninstallglobal )
+        uninstallglobal
+        ;;
     -h|--help|-? )
         echo 'Git Hooks'
+        echo '    A tool to manage project, user, and global Git hooks for multiple git repositories.'
+        echo '    https://github.com/icefox/git-hooks'
         echo ''
         echo 'Options:'
         echo '    --install      Replace existing hooks in this repository with a call to'
         echo '                   git hooks run [hook].  Move old hooks directory to hooks.old'
         echo '    --uninstall    Remove existing hooks in this repository and rename hooks.old'
         echo '                   back to hooks'
+        echo '    --installglobal'
+        echo '                   Create a template .git directory that that will be used whenever'
+        echo '                   a git repository is created or cloned that will remind the user'
+        echo '                   to install git-hooks.'
+        echo '    --uninstallglobal'
+        echo '                   Turn off the global .git directory template that has the reminder.'
         echo "    run [cmd]      Run the hooks for cmd (such as pre-commit)"
         echo "    (no arguments) Show currently installed hooks"
         ;;