PHPackages                             sweetchuck/git-hooks - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. sweetchuck/git-hooks

ActiveComposer-plugin

sweetchuck/git-hooks
====================

Provides a bridge between Git hooks and scripts under VCS.

v3.0.0(4mo ago)219.7k↓41.7%[1 issues](https://github.com/Sweetchuck/git-hooks/issues)20GPL-2.0-or-laterPHPPHP &gt;=8.4CI passing

Since May 28Pushed 3mo ago3 watchersCompare

[ Source](https://github.com/Sweetchuck/git-hooks)[ Packagist](https://packagist.org/packages/sweetchuck/git-hooks)[ Docs](https://github.com/Sweetchuck/git-hooks)[ RSS](/packages/sweetchuck-git-hooks/feed)WikiDiscussions 3.x Synced 1mo ago

READMEChangelog (10)Dependencies (13)Versions (18)Used By (20)

sweetchuck/git-hooks
====================

[](#sweetchuckgit-hooks)

Triggers custom scripts from Git hooks.

This package provides a bridge between the un-versioned `./.git/hooks/*` scripts and scripts in your Git repository.

[![CircleCI](https://camo.githubusercontent.com/184e1e1ebe6a033e6df8952a687c2ae9545d54ace98b185b2ff66fd6e0a9cde3/68747470733a2f2f646c2e636972636c6563692e636f6d2f7374617475732d62616467652f696d672f67682f5377656574636875636b2f6769742d686f6f6b732f747265652f332e782e7376673f7374796c653d737667)](https://dl.circleci.com/status-badge/redirect/gh/Sweetchuck/git-hooks/tree/3.x)

When to use
-----------

[](#when-to-use)

If you want to put your Git hook scripts under VCS to share them with your teammates, then this is the tool you are looking for.

How to use
----------

[](#how-to-use)

1. Step into your existing package's directory (or create a new one with `git init && composer init`)
2. Run `composer require --dev 'sweetchuck/git-hooks'`
3. Then you have two options
    1. Rely on Git hook scripts which are shipped with this package and implement the logic in your `./.git-hooks.sh` file.
    2. Or create a `./git-hooks` directory and create Git hook files in it. (eg: `./git-hooks/pre-commit`)
4. The deployment script will be automatically triggered by the `post-install-cmd` and `post-update-cmd` Composer event.

How it works
------------

[](#how-it-works)

This package is a **Composer plugin** that automatically manages your Git hooks scripts on `composer install` and `composer update` command events based on the configuration.

Configuration
-------------

[](#configuration)

Example **composer.json** file:

```
{
    "extra": {
        "sweetchuck/git-hooks": {
            "core.hooksPath": "./git-hooks",
            "symlink": true
        }
    }
}
```

### Configuration - core.hooksPath

[](#configuration---corehookspath)

Type: `string`Default value: `vendor/sweetchuck/git-hooks/git-hooks` (dynamically detected)

If the Git version is &gt;= `v2.9` (2016-06), then this value will be used to set `git config core.hooksPath `. If Git is older than `v2.9`, then the content of this directory will be symbolically linked or copied into `./.git/hooks/` directory.

### Configuration - symlink

[](#configuration---symlink)

Type: `boolean`Default value: `false`

This configuration option will be used only if a Git version is older than `v2.9`. Copy or symlink Git hook files from the original location (provided by the `core.hooksPath` configuration) to the `./.git/hooks/`.

Example ./.git-hooks.sh file
----------------------------

[](#example-git-hookssh-file)

If you use the Git hooks script from this package (`vendor/sweetchuck/git-hooks/git-hooks`) you will need custom script which catches Git hooks add triggers something really useful.

Copy the content below into `./.git-hooks.sh`

```
#!/usr/bin/env bash

: "${sghHookName:?'argument is required'}"
: "${sghHasInput:?'argument is required'}"

echo 1>&2 "BEGIN Git hook: ${sghHookName}"

function sghExit ()
{
	if [[ "${2}" != '' ]]; then
		echo 1>&2 "${2}"
	fi

	echo 1>&2 "END   Git hook: ${sghHookName}"

	exit "${1}"
}

# @todo Better detection for executables: php, composer.phar and robo.
robo="$(composer config 'bin-dir')/robo"

# Exit without error if "robo" doesn't exists or it has no corresponding task.
test -x "${robo}" || sghExit 0 'robo executable not found'
"${robo}" help "githook:${sghHookName}" 1> /dev/null 2>&1 || sghExit 0 'robo task does not exist.'

if [ "${sghHasInput}" = 'true' ]; then
	"${robo}" "githook:${sghHookName}" "${@}"
