Conditions

Here's a list of all built-in conditions:

All hooks
\CaptainHook\App\Hook\Condition\Branch\On Check if we are on a particular branch
\CaptainHook\App\Hook\Condition\Branch\NotOn Check if we are not on a particular branch
\CaptainHook\App\Hook\Condition\Branch\OnMatching Check if we are on a particular branch that matches a given regex
\CaptainHook\App\Hook\Condition\Branch\NotOnMatching Check if we are not on a particular branch that matches a given regex
\CaptainHook\App\Hook\Condition\Config\CustomValueIsTruthy Check if a custom config is true, 1, yes or on
\CaptainHook\App\Hook\Condition\Config\CustomValueIsFalsy Check if a custom config is false, 0, no or off
\CaptainHook\App\Hook\Condition\Branch\Files Check if the current branch has changed files
pre-commit
\CaptainHook\App\Hook\Condition\FileStaged\All Check if all of the configured files are staged for commit
\CaptainHook\App\Hook\Condition\FileStaged\Any Check if any of the configured files is staged for commit
\CaptainHook\App\Hook\Condition\FileStaged\OfType Check if a file of a given type is staged for commit
\CaptainHook\App\Hook\Condition\FileStaged\InDirectory Check if a file in a directory is changed
post-checkout / post-merge / post-rewrite / pre-push
\CaptainHook\App\Hook\Condition\FileChanged\All Check if all of the configured files are changed
\CaptainHook\App\Hook\Condition\FileChanged\Any Check if any of the configured files is changed
\CaptainHook\App\Hook\Condition\FileChanged\OfType Check if changes to a typy of file will be pushed
logic conditions
\CaptainHook\App\Hook\Condition\Logic\LogicAnd Combine multiple conditions. Only if all conditions apply the action will be executed
\CaptainHook\App\Hook\Condition\Logic\LogicOr Combine multiple conditions. If one condition applies the action will be executed

You can build your own conditions. You can either use simple scripts or binaries which must return an exit code. The exit code 0 means the condition applies; any exit code other than 0 means the condition does not apply.

Or you can build custom PHP Conditions. For an exact guide have a look at the "how to extend CaptainHook" section.

On

all hooks

This Condition expects just one argument, the name of the branch.

{
  "conditions": [
    {
      "exec": "\\CaptainHook\\App\\Hook\\Condition\\Branch\\On",
      "args": [
        "main"
      ]
    }
  ]
}

NotOn

all hooks

This Condition expects just one argument, the name of the branch.

{
  "conditions": [
    {
      "exec": "\\CaptainHook\\App\\Hook\\Condition\\Branch\\NotOn",
      "args": [
        "main"
      ]
    }
  ]
}

OnMatching

all hooks

This Condition expects just one argument, the regex to match the branch.

{
  "conditions": [
    {
      "exec": "\\CaptainHook\\App\\Hook\\Condition\\Branch\\OnMatching",
      "args": [
        "#feature/[a-z0-9\-_]+#i"
      ]
    }
  ]
}

NotOnMatching

all hooks

This Condition expects just one argument, the regex to match the branch.

{
  "conditions": [
    {
      "exec": "\\CaptainHook\\App\\Hook\\Condition\\Branch\\NotOnMatching",
      "args": [
        "#feature/[a-z0-9\-_]+#i"
      ]
    }
  ]
}

CustomValueIsTruthy

all hooks

This Condition expects just one argument, the name of the custom config value to check.

{
  "conditions": [
    {
      "exec": "\\CaptainHook\\App\\Hook\\Condition\\Config\\CustomConfigValueIsTruthy",
      "args": [
        "MY_CONFIG_VALUE"
      ]
    }
  ]
}

CustomValueIsFalsy

all hooks

This Condition expects just one argument, the name of the custom config value to check.

{
  "conditions": [
    {
      "exec": "\\CaptainHook\\App\\Hook\\Condition\\Config\\CustomConfigValueIsFalsy",
      "args": [
        "MY_CONFIG_VALUE"
      ]
    }
  ]
}

Branch files changed

all hooks

This Condition accepts an options object, all values are optional.

This will not work in the main branch and should most likely combined with a NotOn condition

If you don't provide the compare-to option, it will try to figure out the branching point using the reflog.

{
  "conditions": [
    {
      "exec": "\\CaptainHook\\App\\Hook\\Condition\\Branch\\Files",
      "args": [
        {"compared-to": "main", "of-type": "php", "in-dir": "foo/"}
      ]
    }
  ]
}

FileStaged\All

pre-commit

Expecting a list of file names or pattern.

{
  "conditions": [
    {
      "exec": "\\CaptainHook\\App\\Hook\\Condition\\FileStaged\\All",
      "args": [
        ["foo.html", "bar.html", "*.php"]
      ]
    }
  ]
}

FileStaged\Any

pre-commit

Expecting a list of file names or pattern.

{
  "conditions": [
    {
      "exec": "\\CaptainHook\\App\\Hook\\Condition\\FileStaged\\Any",
      "args": [
        ["foo.html", "bar.html", "*.php"]
      ]
    }
  ]
}

FileStaged\OfType

pre-commit

Expecting just the type of file you want to watch as string.

{
  "conditions": [
    {
      "exec": "\\CaptainHook\\App\\Hook\\Condition\\FileStaged\\OfType",
      "args": [
        "php"
      ]
    }
  ]
}

FileStaged\InDirectory

pre-commit

Expecting just the type of file you want to watch as string.

{
  "conditions": [
    {
      "exec": "\\CaptainHook\\App\\Hook\\Condition\\FileStaged\\InDirectory",
      "args": [
        "src/Foo/"
      ]
    }
  ]
}

FileChanged\All

post-checkout post-merge

Expecting a list of file names or pattern.

{
  "conditions": [
    {
      "exec": "\\CaptainHook\\App\\Hook\\Condition\\FileChanged\\All",
      "args": [
        ["foo.html", "bar.html", "*.php"]
      ]
    }
  ]
}

FileChanged\Any

post-checkout post-merge

Expecting a list of file names or pattern.

{
  "conditions": [
    {
      "exec": "\\CaptainHook\\App\\Hook\\Condition\\FileChanged\\Any‚",
      "args": [
        ["foo.html", "bar.html", "*.php"]
      ]
    }
  ]
}

FileChanged\OfType

pre-push

Expecting just the type of file you want to watch as string.

{
  "conditions": [
    {
      "exec": "\\CaptainHook\\App\\Hook\\Condition\\FileChanged\\OfType",
      "args": [
        "php"
      ]
    }
  ]
}

Logic\LogicAnd

Combining multiple conditions. All must pass to execute the action.

{
  "conditions": [
    {
      "exec": "and",
      "args": [{
        "exec": "\\CaptainHook\\App\\Hook\\Condition\\FileStaged\\InDirectory",
        "args": [
          ["src/new"]
        ]
      },{
        "exec": "\\CaptainHook\\App\\Hook\\Condition\\FileStaged\\OfType",
        "args": [
          ["php"]
        ]
      }]
    }
  ]
}

Logic\LogicOr

Combining multiple conditions. Only one condition must apply to execute the action.

{
  "conditions": [
    {
      "exec": "or",
      "args": [{
        "exec": "\\CaptainHook\\App\\Hook\\Condition\\FileStaged\\InDirectory",
        "args": [
          ["src/new"]
        ]
      },{
        "exec": "\\CaptainHook\\App\\Hook\\Condition\\FileStaged\\OfType",
        "args": [
          ["php"]
        ]
      }]
    }
  ]
}