Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: icefox/git-hooks
base: master
...
head fork: endorama/git-hooks
compare: master
Checking mergeability… Don’t worry, you can still create the pull request.
  • 18 commits
  • 5 files changed
  • 0 commit comments
  • 2 contributors
View
55 README
@@ -1,55 +0,0 @@
-git-hooks - A tool to manage project, user, and global Git hooks for multiple git repositories.
-
-git-hooks lets hooks be installed inside git repositories, users home directory, and globally. When a hook is called by git, git-hooks will check each of these locations for the hooks to run.
-
-************************************
-Install
-
-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).
-
-************************************
-Overview
-
-Hooks can be very powerful and useful. Some common hooks include:
-
-- Spell check the commit message.
-- Check that the code builds.
-- Verify that any new files contain a copyright with the current year in it.
-
-Hooks can be very project specific such as:
-
-- Verify that the project still builds
-- Verify that autotests matching the modified files still pass with no errors.
-- Pre-populate the commit message with the 'standard' format.
-- Verify that any new code follows the 'standard' coding style.
-
-or very specific to one person such as:
-
-- Don't allow a push to a remote repository after 1am in case I break something and will be asleep.
-- Don't let me commit between 9-5 for projects in ~/personal/ as I shouldn't be working on them during work hours.
-
-For more details on the different Git hooks that can be installed checkout the online documentation at http://www.kernel.org/pub/software/scm/git/docs/githooks.html
-
-************************************
-Locations
-
-git-hooks provide a way to manage and share your hooks by allowing for the installation of hooks in three locations:
- - User hooks that are installed in ~/.git_hooks/
- - Project hooks that are installed in the git_hooks directory inside of each project.
- - Global hooks that are specified with the hooks.global configuration option.
- The contrib directory includes a number of useful hooks and can be set by doing the following:
- git config --global hooks.global $PWD/contrib/
-
-Multiple global hooks directories can be specified by using a space between each path.
-
-************************************
-Creating hooks
-
-To keep things organized git-hooks looks for scripts in sub directories named after the git hook name. For example this project has the following pre-commit script in the following location:
-
-git_hooks/pre-commit/bsd
-
-When you run 'git hooks' with no arguments it will list all of the hooks installed on the system. It will run the hooks with the --about argument to generate the description that is shown. Checkout the hooks in contrib for some examples.
-
View
105 README.md
@@ -0,0 +1,105 @@
+#git-hooks
+
+A tool to manage project, user, and global Git hooks for multiple git repositories.
+
+git-hooks lets hooks be installed inside git repositories, users home directory, and globally. When a hook is called by git, git-hooks will check each of these locations for the hooks to run.
+
+************************************
+##Install
+
+Add git-hooks to your PATH environment variable so 'git hooks' can be run. In `~/.bashrc` ( for example ):
+
+ # Load Git-hook
+ export PATH=$PATH:/opt/git-hooks
+
+This assume you have installed git-hooks in /opt folder.
+
+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).
+
+************************************
+##Overview
+
+Hooks can be very powerful and useful. Some common hooks include:
+
+- Spell check the commit message.
+- Check that the code builds.
+- Verify that any new files contain a copyright with the current year in it.
+
+Hooks can be very project specific such as:
+
+- Verify that the project still builds
+- Verify that autotests matching the modified files still pass with no errors.
+- Pre-populate the commit message with the 'standard' format.
+- Verify that any new code follows the 'standard' coding style.
+
+or very specific to one person such as:
+
+- Don't allow a push to a remote repository after 1am in case I break something and will be asleep.
+- Don't let me commit between 9-5 for projects in ~/personal/ as I shouldn't be working on them during work hours.
+
+For more details on the different Git hooks that can be installed checkout the online documentation at http://www.kernel.org/pub/software/scm/git/docs/githooks.html
+
+************************************
+##Locations
+
+git-hooks provide a way to manage and share your hooks by allowing for the installation of hooks in three locations:
+
+- User hooks that are installed in ~/.git_hooks/
+- Project hooks that are installed in the .githooks directory inside of each project.
+- Global hooks that are specified with the hooks.global configuration option.
+ The contrib directory includes a number of useful hooks and can be set by doing the following:
+ `git config --global hooks.global {git-hooks folder}/contrib/`
+ Global hooks can be also specified 'per project' ( this options will override the global one ):
+ `git config hooks.global {git-hooks folder}/contrib/`
+
+Multiple global hooks directories can be specified by using a space between each path.
+
+************************************
+##Creating hooks
+
+To keep things organized git-hooks looks for scripts in sub directories named after the git hook name. For example this project has the following pre-commit script in the following location:
+
+git_hooks/pre-commit/bsd
+
+When you run `git hooks list` it will list all of the hooks installed on the system. It will run the hooks with the --about argument to generate the description that is shown. Checkout the hooks in contrib for some examples.
+
+************************************
+##Enabling / Disabling hooks
+
+Sometimes could be great to just have some hooks enabled, while others are not useful. To reach this I've included two new function: `--enable` and `--disable` ( see `git hooks -h` for more ). This functions can be used to enable or disable specific hook scripts.
+Example:
+
+ $ git hooks list
+ Git hooks ARE installed in this repository.
+
+ Listing User, Project, and Global hooks:
+ ---
+ /home/$USER/.git_hooks:
+
+ /home/$USER/code/git/project/.githooks:
+ E post-checkout/db-import - Import the database linked to the specified branch
+ E post-commit/db-dump - Dump the database after each commit
+
+Now this 2 hooks are enabled ( notice the 'E' at the beginning of the line ). Now I can disable one of them using:
+
+ $ git hooks -d post-checkout/db-import
+
+This will make git hooks search in the hooks 'post-checkout' subfolders for a hook with name 'db-import' and disable it ( this means simply a chmod -x on the file ).
+Now this is wath you get:
+
+ $ git hooks list
+ Git hooks ARE installed in this repository.
+
+ Listing User, Project, and Global hooks:
+ ---
+ /home/$USER/.git_hooks:
+
+ /home/$USER/code/git/project/.githooks:
+ D post-checkout/db-import - Import the database linked to the specified branch
+ E post-commit/db-dump - Dump the database after each commit
+
+You can also disable ALL HOOKS with a single command:
+
+ $ git hooks -d
+
+To enable a hook use `git hooks -e [HOOK NAME]`, or to enable ALL HOOKS use `git hooks -e`.
View
78 contrib/post-checkout/db-import
@@ -0,0 +1,78 @@
+#!/bin/bash
+
+# db-import - Git Post Checkout Hook
+# This scripts import a dump of the selected database after each checkout. Can be useful to restore
+# a stored state of an application.
+# Can be useful used in conjunction with db-dump hook.
+#
+# Author : Edoardo Tenani < edoardo.tenani@gmail.com >
+# Copyright: (c) 2011 - Edoardo Tenani
+# License : Public Domain
+#
+# Config
+# hooks.db-hooks.path : the path from which db dump will be imported ( use a absolute path! )
+# hooks.db-hooks.uname : the username to connect to the database
+# hooks.db-hooks.passwd : the password to connect to the database
+# hooks.db-hooks.dbname : the name of the database to be dumped
+#
+# To set a config you can use `git config hooks.db-hook.{param}`. This configs are specified
+# per-project
+
+
+function run() {
+ # $1 => From ref
+ # $2 => To ref
+ # $3 => 0 for file checkout, 1 for branch checkout
+
+ if [[ $3 -eq 1 ]]; then
+ if [[ -z $(git config hooks.db-hook.path) || \
+ -z $(git config hooks.db-hook.uname) || \
+ -z $(git config hooks.db-hook.passwd) || \
+ -z $(git config hooks.db-hook.dbname) ]]; then
+ echo "[post-checkout::db-import] Invalid parameters, aborting!"
+ return
+ fi
+
+ SAVE_FOLDER=$(git config hooks.db-hook.path)
+
+ branch=$(git br | grep "*" | cut -c3-)
+
+ # http://stackoverflow.com/questions/229551/string-contains-in-bash/229606#229606
+ if [[ ${branch} == */* ]]; then
+ root_branch=$(echo ${branch} | cut -d/ -f1)
+ file=$(echo ${branch} | cut -d/ -f2)
+
+ save_file=${SAVE_FOLDER}/${root_branch}/${file}.sql
+ else
+ save_file=${SAVE_FOLDER}/${branch}.sql
+ fi
+
+ # Check if main branch folder exists
+ if [[ ! -d $(dirname ${save_file}) ]]; then
+ echo "[post-checkout::db-import] No dumps exists for this branch!"
+ return
+ fi
+
+ # Dump the database
+ uname=$(git config hooks.db-hook.uname)
+ passwd=$(git config hooks.db-hook.passwd)
+ dbname=$(git config hooks.db-hook.dbname)
+ mysql -u ${uname} -p${passwd} ${dbname} < ${save_file}
+
+ # Check for error and commit file ( amending previous commit)
+ if [[ $? -eq 0 ]]; then
+ echo "[post-checkout::db-import] Successfull imported < ${save_file}"
+ else
+ echo "[post-checkout::db-import] Error occurred"
+ fi
+ fi
+}
+
+case "${1}" in
+ --about )
+ echo "Import a database after each checkout; the database imported is related to the name of the branch"
+ ;;
+ * )
+ run $@
+ ;;
+esac
View
81 contrib/post-commit/db-dump
@@ -0,0 +1,81 @@
+#!/bin/bash
+
+# db-dump - Git Post Commit Hook
+# This scripts create a dump of the selected database on each commit, adding the file to the
+# commit itself. The file is named with the branch name, and overridden every dump. His history can
+# be found in git history!
+#
+# Author : Edoardo Tenani < edoardo.tenani@gmail.com >
+# Copyright: (c) 2011 - Edoardo Tenani
+# License : Public Domain
+#
+# Config
+# hooks.db-hooks.path : the path in which db dump will be saved ( use a absolute path! )
+# hooks.db-hooks.uname : the username to connect to the database
+# hooks.db-hooks.passwd : the password to connect to the database
+# hooks.db-hooks.dbname : the name of the database to be dumped
+#
+# To set a config you can use `git config hooks.db-hook.{param}`. This configs are specified
+# per-project
+
+function run() {
+ # The absolute path to the directory in which save the db dump ( no trailing slash! )
+ # SAVE_FOLDER="$HOME/code/git/radioohm/sites/default/db-dump"
+ #SAVE_FOLDER="$HOME/code/git/git-hooks/asd/db-dump"
+
+ if [[ -z $(git config hooks.db-hook.path) || \
+ -z $(git config hooks.db-hook.uname) || \
+ -z $(git config hooks.db-hook.passwd) || \
+ -z $(git config hooks.db-hook.dbname) ]]; then
+ echo "[post-commit::db-dump] Invalid parameters, aborting!"
+ return
+ fi
+
+ SAVE_FOLDER=$(git config hooks.db-hook.path)
+
+ branch=$(git br | grep "*" | cut -c3-)
+
+# http://stackoverflow.com/questions/229551/string-contains-in-bash/229606#229606
+ if [[ ${branch} == */* ]]; then
+ root_branch=$(echo ${branch} | cut -d/ -f1)
+ file=$(echo ${branch} | cut -d/ -f2)
+
+ save_file=${SAVE_FOLDER}/${root_branch}/${file}.sql
+ else
+ save_file=${SAVE_FOLDER}/${branch}.sql
+ fi
+
+ # Check if main branch folder exists
+ if [[ ! -d $(dirname ${save_file}) ]]; then
+ mkdir -p $(dirname ${save_file})
+ fi
+
+ # Dump the database
+ uname=$(git config hooks.db-hook.uname)
+ passwd=$(git config hooks.db-hook.passwd)
+ dbname=$(git config hooks.db-hook.dbname)
+ mysqldump -u ${uname} -p${passwd} ${dbname} > ${save_file}
+
+ # Check for error and commit file ( amending previous commit)
+ if [[ $? -eq 0 ]]; then
+ echo "[post-commit::db-dump] Successfull dumped > ${save_file}"
+ git add ${save_file}
+ git hooks -d db-dump > /dev/null 2>&1
+
+ reflog=$(git log -n 1 --pretty=format:"%s\n%b")
+ git ci --amend -m "${reflog}" > /dev/null 2>&1
+
+ git hooks -e db-dump > /dev/null 2>&1
+ else
+ echo "[post-commit::db-dump] Error occurred"
+ fi
+}
+
+case "${1}" in
+ --about )
+ echo "Dump the Database after each commit, saving in a dump related to the current branch"
+ ;;
+ * )
+ run $@
+ ;;
+esac
View
166 git-hooks
@@ -26,30 +26,26 @@
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
-function hook_dirs
-{
- if [ ! -z "${1}" ] ; then
- hook="/${1}"
- else
- hook=""
- fi
- echo "${HOME}/.git_hooks${hook}"
- GITDIR=`git rev-parse --git-dir`
- cd $GITDIR/..
- echo "${PWD}/git_hooks${hook}"
- echo "${PWD}/.githooks${hook}"
- eval echo "`git config hooks.global`"${hook}
-}
-function list_hooks_in_dir
-{
- if [[ $OSTYPE =~ 'darwin' ]] ; then
- find "${1}/" -type f 2>/dev/null | grep -v "^.$" | sort
- else
- find "${1}/" -executable -type f 2>/dev/null | grep -v "^.$" | sort -V
- fi
+function usage {
+ echo 'Git Hooks'
+ echo 'git hook [option] [cmd]'
+ 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 ' --enable, -e [HOOK] Enable the specified hook, if no hook name is specified, all'
+ echo ' hooks will be enabled'
+ echo ' --disable, -d [HOOK] Disable the specified hook, if no hook name is specified, all'
+ echo ' hooks will be disabled'
+ echo 'Commands:'
+ echo ' run [cmd] Run the hooks for cmd (such as pre-commit)'
+ echo ' list Show currently installed hooks'
}
+
function run_hooks
{
dir="${1}"
@@ -66,10 +62,21 @@ function run_hooks
echo -n `basename \`dirname "${hook}"\``
echo "/`basename "${hook}"`"
fi
- ${hook} "$@"
+ if [[ -x ${hook} ]]; then
+ ${hook} "$@"
+ fi
done
}
+
+##
+# Run hooks
+##
+function report_error {
+ echo "Hook failed: $last_run_hook"
+ exit 1
+}
+
function run_hook
{
set -e
@@ -88,6 +95,9 @@ function run_hook
set +e
}
+##
+# Install / Uninstall hooks in the git repository
+##
function install_hooks
{
GITDIR=`git rev-parse --git-dir`
@@ -106,6 +116,9 @@ function install_hooks
git-hooks run "$0" "$@"' > "${file}"
chmod +x "${file}"
done
+ if [[ ! -e "../../.githooks" ]]; then
+ mkdir "../../.githooks"
+ fi
else
if [ ! -d hooks.old ] ; then
echo "Error, hooks.old doesn't exists, aborting uninstall to not destroy something"
@@ -116,6 +129,39 @@ git-hooks run "$0" "$@"' > "${file}"
fi
}
+##
+# List function
+##
+
+# Return the directories in which search hooks
+# $1 ??
+function hook_dirs
+{
+ if [ ! -z "${1}" ] ; then
+ hook="/${1}"
+ else
+ hook=""
+ fi
+ echo "${HOME}/.git_hooks${hook}"
+ GITDIR=`git rev-parse --git-dir`
+ cd $GITDIR/..
+ echo "${PWD}/.githooks${hook}"
+ eval echo "`git config hooks.global`"${hook}
+}
+
+# List all hooks in the specified folder
+# $1 - the folder supposed to contain hooks
+function list_hooks_in_dir
+{
+ if [[ $OSTYPE =~ 'darwin' ]] ; then
+ find -L "${1}/" -type f 2>/dev/null | grep -v "^.$" | sort
+ else
+ # find "${1}" -executable -type f 2>/dev/null | grep -v "^.$" | sort -V
+ find -L "${1}" -type f 2>/dev/null | grep -v "^.$" | sort -V
+ fi
+}
+
+# List all available hooks
function list_hooks
{
GITDIR=`git rev-parse --git-dir`
@@ -133,19 +179,69 @@ function list_hooks
for dir in `hook_dirs`; do
echo "${dir}:"
for hook in `list_hooks_in_dir "${dir}"` ; do
+ # Display if hook is enable or not ( is executable or not )
+ if [[ -x ${hook} ]]; then
+ echo -en "\x1b[32m E \x1b[0m\t" # output green E
+ else
+ echo -en "\x1b[31m D \x1b[0m\t" # output red D
+ fi
echo -n `basename \`dirname "${hook}"\``
- echo -e "/`basename "${hook}"` \t- `${hook} --about`"
+ echo -ne "/`basename "${hook}"`"
+
+ if [[ -x ${hook} ]]; then
+ echo -e "\t- `${hook} --about 2>/dev/null`"
+ else
+ echo -e ""
+ fi
done
echo ""
done
}
-function report_error {
- echo "Hook failed: $last_run_hook"
- exit 1
+##
+# Enable / Disable hooks
+##
+
+# Enable the specified hook
+# $1 - the file name of the hook to be enabled
+function enable_hook {
+ for dir in `hook_dirs`; do
+ for hook in `list_hooks_in_dir "${dir}"` ; do
+ if [[ $# -eq 0 ]]; then
+ echo "Enabling ${hook}"
+ chmod +x ${hook}
+ else
+ if [[ $1 = `echo "${hook}" | rev | cut -d"/" -f1,2 | rev` ]]; then
+ echo "Enabling ${hook}"
+ chmod +x ${hook}
+ fi
+ fi
+ done
+ done
+}
+
+# Disable the specified hook
+# $1 - the file name of the hook to be disabled
+function disable_hook {
+ for dir in `hook_dirs`; do
+ for hook in `list_hooks_in_dir "${dir}"` ; do
+ if [[ $# -eq 0 ]]; then
+ echo "Disabling ${hook}"
+ chmod -x ${hook}
+ else
+ if [[ $1 = `echo "${hook}" | rev | cut -d"/" -f1,2 | rev` ]]; then
+ echo "Disabling ${hook}"
+ chmod -x ${hook}
+ fi
+ fi
+ done
+ done
}
case $1 in
+ list )
+ list_hooks
+ ;;
run )
if [ ! -z "${GIT_DIR}" ] ; then
unset GIT_DIR
@@ -157,18 +253,16 @@ case $1 in
--install|--uninstall )
install_hooks "$1"
;;
+ -e|--enable )
+ enable_hook $2
+ ;;
+ -d|--disable )
+ disable_hook $2
+ ;;
-h|--help )
- echo '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 " run [cmd] Run the hooks for cmd (such as pre-commit)"
- echo " (no arguments) Show currently installed hooks"
+ usage
;;
* )
- list_hooks
+ usage
;;
esac

No commit comments for this range

Something went wrong with that request. Please try again.