Configuration

If you look at a captainhook.json configuration, it consists of a config block for each hook and an optional config section to tweak some CaptainHook settings.

{
  "commit-msg": {
    // a hook config block
  },
  "pre-commit": {
    // another hook config block
  },
  // ...
}

Hook Config

Each hook configuration consists of two things an on/off switch and a list of actions to execute.

'commit-msg': {
  "enabled": true,
  "actions": []
}

By setting enabled to false, CaptainHook will not execute the configured actions even if the hook is triggered by git.

Actions

CLI commands

Actions can be two things, firstly you can run any cli command, as long as it uses exit codes to notify the caller about its success. If you, for example, run PHPUnit and a test fails, it will exit with status code 1, so CaptainHook knows that it should stop the currently running git command with an error.

{
  "pre-commit": {
    "enabled": true,
    "actions": [
      {
        "action": "tools/phpunit.phar --configuration=phpunit.git.xml"
      }
    ]
  }
}

Using the example above in a pre-commit hook will prevent you from committing code that breaks any unit test.

Placeholders

You can use placeholders in CLI actions that will get replaced by the Cap'n before execution. This way you can access the the original hook arguments and some other neat stuff.

Placeholder Hook Description
{$FILE} commit-message The path to the file containing the commit message.
{$FILE} prepare-commit-message The path to the file containing the commit message.
{$MODE} prepare-commit-message Source of the commit message e.g. file, template ...
{$HASH} prepare-commit-message Hash of commit e.g. if --amend is used.
{$TARGET} pre-push The push target name.
{$URL} pre-push The target url.
{$PREVIOUSHEAD} post-checkout Old version hash.
{$NEWHEAD} post-checkout New version hash (can be the same as the old head).
{$MODE} post-checkout A flag indicating whether the checkout was a branch checkout.
{$SQUASH} post-merge A flag specifying whether or not the merge being done was a squash merge.
{$STAGED_FILES} pre-commit List of files that get committed. You can use it like this {$STAGED_FILES|of-type:php|separated-by:, }.
{
  "pre-commit": {
    "enabled": true,
    "actions": [
      {
        "action": "tools/phpcs.phar --standard=psr12 {$STAGED_FILES|of-type:php}"
      }
    ]
  }
}

PHP scripts

Secondly you can execute any PHP class implementing an interfaces defined by CaptainHook. The interface you have to implement is \CaptainHook\App\Hook\Action. CaptainHook comes with some built-in, basic validation classes. So let's have a look at how to configure those.

{
  "commit-msg": {
    "enabled": true,
    "actions": [
      {
        "action": "\\CaptainHook\\App\\Hook\\Message\\Action\\Regex",
        "options": {
          "regex": "#.*#"
        }
      }
    ]
  }
}

With this configuration you can validate your commit messages against a regular expression. If your commit message doesn't match your regular expression your commit fails.

Here's a list of all available actions.

You can easily build your own action classes. Have a look on how to extend CaptainHook.

Conditions

You can add conditions to an action configuration. Only if all configured conditions apply the action gets executed. This becomes useful for example if you want to run composer install automatically every time someone changes composer.json or composer.lock.

To make sure you catch every change in your repository you have to add the action to both post-merge and post-checkout hooks. This way you run composer after every merge or pull or after checking out another branch that changes you local composer files.

{
  "post-merge": {
    "enabled": true,
    "actions": [
      {
        "action": "composer install",
        "options": {},
        "conditions": [
          {
            "exec": "\\CaptainHook\\App\\Hook\\Condition\\FileChanged\\Any",
            "args": [
              ["composer.json", "composer.lock"]
            ]
          }
        ]
      }
    ]
  },
  "post-checkout": {
    "enabled": true,
    "actions": [
      {
        "action": "composer install",
        "options": {},
        "conditions": [
          {
            "exec": "\\CaptainHook\\App\\Hook\\Condition\\FileChanged\\Any",
            "args": [
              ["composer.json", "composer.lock"]
            ]
          }
        ]
      }
    ]
  }
}

For a full list of all available Conditions and some example configurations check the Conditions Section.

Include other configuration files

If you have a lot of projects you maybe want to define a set of default hooks for all of them. To maintain such a default configuration CaptainHook version 4.4.0 introduced the possibility to include other CaptainHook configurations like this.

{
  "config": {
    "includes": [
      "some-other-captainhook-config.json"
    ]
  },
  "pre-commit": {
    "enabled": true,
    "actions": [
      {
        "action"0: "\\CaptainHook\\App\\Hook\\Message\\Action\\Regex",
        "options": {
          "regex": "#.*#"
        }
      }
    ]
  }
}

The idea is to create a separate composer package e.g. my-git-hooks that is structured like this.

my-git-hooks/
├── composer.json
├── msg-validation.json
└── crazy-workflow.json

Inside your composer.json you require all libs and plugins you need to execute the configured actions plus captainhook/captainhook or captainhook/plugin-composer. The only thing you have to do in your projects is to require your my-git-hooks package and add something like this to your local project captainhook.json file.

{
  "config": {
    "includes": [
      "vendor/my-company/my-git-hooks/msg-validation.json",
      "vendor/my-company/my-git-hooks/crazy-workflow.json"
    ]
  }
}

To update the base configuration you create a new version for your my-git-hooks package and run composer update in your projects.

Include Level

By default you can't include configurations in included configurations. But if you really want to shoot yourself in the foot, who am I to judge you. You can increase the maximum levels of inclusion in your project configuration. Be aware, that setting this in any included configuration will have no effect.

{
  "config": {
    "includes-level": 2,
    "includes": [
      "vendor/my-company/my-git-hooks/msg-validation.json",
      "vendor/my-company/my-git-hooks/crazy-workflow.json"
    ]
  }
}

Settings

You can configure some basic CaptainHook behaviour by overwriting the defaults in your configuration file. For example if your .git directory is not located on the same level as your captainhook.json or you like a more verbose output.

{
  "config": {
    "bootstrap": "../vendor/autoload.php",
    "git-directory": "../../.git",
    "fail-on-first-error": false,
    "verbosity": "verbose",
    "ansi-colors": false,
    "includes-level": 2,
    "includes": [],
    "run-mode": "docker",
    "run-exec": "docker exec CONTAINER_NAME",
    "run-path": "vendor/bin/captainhook
  }
}
Setting Description Default
bootstrap Path to your bootstrap / autoloader file. vendor/autoload.php
git-directory Custom path to your repositories .git directory (relative from the configuration file) .git
fail-on-first-error Stop hook execution on first error (true) or execute all actions and collect all errors (false) true
verbosity Define the output verbosity [quiet|normal|verbose|debug] verbose
ansi-colors Enable or disable ANSI color output true
includes A list of CaptainHook configuration files to include -
includes-level Nesting level of allowed config file inclusion 0
run-mode CaptainHook execution mode (local|docker) local
run-exec Docker command to spin up the container and execute a script -
run-path Path to the command inside your docker container -

Overwrite settings

If you have different preferences as your team does and you want to tweak some settings to your needs but you don't want to change the captainhook.json that is under version control, there is a way. All you have to do is to create a captainhook.config.json file right beside your captainhook.json file.

ATTENTION: The file must be named captainhook.config.json and it must be located in the same directory as your captainhook.json configuration file.

In this file you can overwrite all settings to your personal needs. Here's an example captainhook.config.json file.

{
  "verbosity": "quiet",
  "ansi-colors": false,
  "fail-on-first-error": false,
  "run-mode": "docker",
  "run-exec": "docker exec MY_CONTAINER_NAME",
  "run-path": "vendor/bin/captainhook
}

ATTENTION: Remember to exclude the file from version control ;)