2017 © Pedro Peláez
 

library composer-git-hooks

Easily manage git hooks in your composer config

image

brainmaestro/composer-git-hooks

Easily manage git hooks in your composer config

  • Friday, July 20, 2018
  • by brainmaestro
  • Repository
  • 5 Watchers
  • 179 Stars
  • 37,457 Installations
  • PHP
  • 28 Dependents
  • 0 Suggesters
  • 15 Forks
  • 1 Open issues
  • 23 Versions
  • 36 % Grown

The README.md

composer-git-hooks

Software License ![Travis][badge-travis] ![Packagist][badge-packagist] Download, (*1)

Manage git hooks easily in your composer configuration. This command line tool makes it easy to implement a consistent project-wide usage of git hooks. Specifying hooks in the composer file makes them available for every member of the project team. This provides a consistent environment and behavior for everyone which is great. It is also possible to use to manage git hooks globally for every repository on your computer. That way you have a reliable set of hooks crafted by yourself for every project you choose to work on., (*2)

Install

Add a hooks section to the extra section of your composer.json and add the hooks there., (*3)

{
    "extra": {
        "hooks": {
            "pre-commit": [
                "echo committing as $(git config user.name)",
                "php-cs-fixer fix ." // fix style
            ],
            // verify commit message. ex: ABC-123: Fix everything
            "commit-msg": "grep -q '[A-Z]+-[0-9]+.*' $1",
            "pre-push": [
                "php-cs-fixer fix --dry-run ." // check style
                "phpunit"
            ],
            "post-merge": "composer install"
            "...": "..."
        }
    }
}

Then install with, (*4)

composer require --dev brainmaestro/composer-git-hooks

This installs the cghooks binary to your vendor/bin folder. If this folder is not in your path, you will need to preface every command with vendor/bin/., (*5)

Note: hooks declared in the scripts or hooks root sections of composer.json are no longer supported in v3., (*6)

Global support

You can also install it globally. This feels much more natural when cghooks is used with the newly added support for managing global git hooks., (*7)

composer global require --dev brainmaestro/composer-git-hooks

All commands have global support (besides testing the hooks. Still requires being in the directory with the composer.json file)., (*8)

Optional Configuration

Stop on failure

When a hook is a sequence of commands, it can be useful to stop the execution when a command fails., (*9)

Specify the impacted hooks in the stop-on-failure config section., (*10)

{
    "extra": {
        "hooks": {
            "config": {
                "stop-on-failure": ["pre-push"]
            },
            "pre-push": [
                "php-cs-fixer fix --dry-run --stop-on-violation .",
                "phpunit"
            ],
        }
    }
}

Always be sure to run the update command after changing the stop-on-failure config section., (*11)

Custom hooks

Custom hooks can be added to the custom-hooks array of the `config section., (*12)

{
    "extra": {
        "hooks": {
            "config": {
                "custom-hooks": ["pre-flow-feature-start"]
            },
            "pre-flow-feature-start": [
                "echo 'Starting a new feature...'"
            ]
        }
    }
}

Always be sure to run the update command after changing the custom-hooks config section. Note: config is not valid custom hook value., (*13)

Shortcut

Add a cghooks script to the scripts section of your composer.json file. That way, commands can be run with composer cghooks ${command}. This is ideal if you would rather not edit your system path., (*14)

{
    "scripts": {
        "cghooks": "vendor/bin/cghooks",
        "...": "..."
    }
}

Composer Events

Add the following events to your composer.json file. The cghooks commands will be run every time the events occur. Go to Composer Command Events for more details about composer's event system., (*15)

{
    "scripts": {
        "post-install-cmd": "cghooks add --ignore-lock",
        "post-update-cmd": "cghooks update",
        "...": "..."
    }
}

Usage

All the following commands have to be run either in the same folder as your composer.json file or by specifying the --git-dir option to point to a folder with a composer.json file., (*16)

Adding Hooks

After installation is complete, run cghooks add to add all the valid git hooks that have been specified in the composer config., (*17)

Option Description Command
no-lock Do not create a lock file cghooks add --no-lock
ignore-lock Add the lock file to .gitignore cghooks add --ignore-lock
force-win Force windows bash compatibility cghooks add --force-win

The lock file contains a list of all added hooks., (*18)

If the --global flag is used, the hooks will be added globally, and the global git config will also be modified. If no directory is provided, there is a fallback to the current core.hooksPath in the global config. If that value is not set, it defaults to $COMPOSER_HOME (this specific fallback only happens for the add command). It will fail with an error if there is still no path after the fallbacks., (*19)

Updating Hooks

The update command which is run with cghooks update basically ignores the lock file and tries to add hooks from the composer file. This is similar to what the --force option for the add command did. This command is useful if the hooks in the composer.json file have changed since the first time the hooks were added., (*20)

This works similarly when used with --global except that there is no fallback to $COMPOSER_HOME if no directory is provided., (*21)

Removing Hooks

Hooks can be easily removed with cghooks remove. This will remove all the hooks that were specified in the composer config., (*22)

Hooks can also be removed by passing them as arguments. The command cghooks remove pre-commit post-commit which will remove the pre-commit and post-commit hooks., (*23)

Option Description Command
force Delete hooks without checking the lock file cghooks remove --force

CAREFUL: If the lock file was tampered with or the force option was used, hooks that already existed before using this package, but were specified in the composer scripts config will be removed as well. That is, if you had a previous pre-commit hook, but your current composer config also has a pre-commit hook, this option will cause the command to remove your initial hook., (*24)

This also does not have a fallback to $COMPOSER_HOME if no directory is provided when used with --global., (*25)

Listing hooks

Hooks can be listed with the cghooks list-hooks command. This basically checks composer config and list the hooks that actually have files., (*26)

Common Options

The following options are common to all commands., (*27)

Option Description Command
git-dir Path to git directory cghooks ${command} --git-dir='/path/to/.git'
lock-dir Path to lock file directory cghooks ${command} --lock-dir='/path/to/lock'
global Runs the specified command globally cghooks ${command} --global

Each command also has a flag -v to control verbosity for more detailed logs. Currently, only one level is supported., (*28)

Testing Hooks

Hooks can be tested with cghooks ${hook} before adding them. Example cghooks pre-commit runs the pre-commit hook., (*29)

Contributing

Please see CONTRIBUTING for details., (*30)

Credits

License

The MIT License (MIT). Please see License File for more information., (*31)

The Versions

20/07 2018

dev-master

9999999-dev

Easily manage git hooks in your composer config

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ezinwa Okpoechi

hook git composer

16/07 2018

dev-dependabot/composer/phpunit/phpunit-7.2.7

dev-dependabot/composer/phpunit/phpunit-7.2.7

Easily manage git hooks in your composer config

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ezinwa Okpoechi

hook git composer

06/06 2018

v2.4.5

2.4.5.0

Easily manage git hooks in your composer config

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ezinwa Okpoechi

hook git composer

01/06 2018

v2.4.4

2.4.4.0

Easily manage git hooks in your composer config

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ezinwa Okpoechi

hook git composer

27/02 2018

v2.4.3

2.4.3.0

Easily manage git hooks in your composer config

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ezinwa Okpoechi

hook git composer

29/01 2018

v2.4.2

2.4.2.0

Easily manage git hooks in your composer config

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ezinwa Okpoechi

hook git composer

29/01 2018

dev-add-php-7.2

dev-add-php-7.2

Easily manage git hooks in your composer config

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ezinwa Okpoechi

hook git composer

15/01 2018

v2.4.1

2.4.1.0

Easily manage git hooks in your composer config

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ezinwa Okpoechi

hook git composer

20/10 2017

v2.4.0

2.4.0.0

Easily manage git hooks in your composer config

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ezinwa Okpoechi

hook git composer

26/07 2017

v2.3.1

2.3.1.0

Easily manage git hooks in your composer config

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ezinwa Okpoechi

hook git composer

25/07 2017

v2.3.0

2.3.0.0

Easily manage git hooks in your composer config

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ezinwa Okpoechi

hook git composer

26/06 2017

v2.2.0

2.2.0.0

Easily manage git hooks in your composer config

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ezinwa Okpoechi

hook git composer

07/06 2017

v2.1.1

2.1.1.0

Easily manage git hooks in your composer config

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ezinwa Okpoechi

hook git composer

07/06 2017

v2.1.0

2.1.0.0

Easily manage git hooks in your composer config

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ezinwa Okpoechi

hook git composer

22/05 2017

v2.0.1

2.0.1.0

Easily manage git hooks in your composer config

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ezinwa Okpoechi

hook git composer

17/05 2017

v2.0.0

2.0.0.0

Easily manage git hooks in your composer config

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ezinwa Okpoechi

hook git composer

08/05 2017

dev-ensure-valid-schema

dev-ensure-valid-schema

Easily manage git hooks in your composer config

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ezinwa Okpoechi

hook git composer

13/03 2017

v1.1.0

1.1.0.0

Easily manage git hooks in your composer config

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ezinwa Okpoechi

hook git composer

11/03 2017

v1.0.0

1.0.0.0

Easily manage git hooks in your composer config

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ezinwa Okpoechi

hook git composer

10/03 2017

v0.3.0

0.3.0.0

Easily manage git hooks in your composer config

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ezinwa Okpoechi

hook git composer

09/03 2017

v0.2.1

0.2.1.0

Easily manage git hooks in your composer config

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ezinwa Okpoechi

hook git composer

08/03 2017

v0.2.0

0.2.0.0

Easily manage git hooks in your composer config

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ezinwa Okpoechi

hook git composer

08/03 2017

v0.1.0

0.1.0.0

Easily manage git hooks in your composer config

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ezinwa Okpoechi

hook git composer