Skip to content

TIP

This example uses macOS 12.1 with the Laravel 10.x framework.

Install the Package

From the project root:

bash
composer require --dev brainmaestro/composer-git-hooks:v3.0.0-alpha.1

Result:

file

Update the Configuration

Edit composer.json

  1. Add the scripts section

    json
    {
      "scripts": {
        "post-update-cmd": [
          "@php artisan vendor:publish --tag=laravel-assets --ansi --force",
          "cghooks update"
        ],
        "post-merge": "composer install", 
        "post-install-cmd": [
    
          "cghooks add --ignore-lock", 
          "cghooks update"
        ], 
        "cghooks": "vendor/bin/cghooks"
      }
    }
  2. Add a hooks entry under extra

    json
    {
      "extra": {
        "laravel": {
          "dont-discover": []
        },
        "hooks": {
    
          "config": {
    
            "stop-on-failure": [
    
              "pre-commit", 
              "pre-push"
            ] 
          }, 
          "pre-commit": [
    
            "echo Running pre-commit hooks...", 
            "./vendor/bin/pint", 
            "echo Running tests...", 
            "php artisan test"
          ], 
          "commit-msg": "grep -qE '^(feat|fix|chore|docs|style|refactor|perf|test|build|ci|revert)\\([^)]+\\):\\s.+\\s?(?:\\(#\\d+\\))?' $1 || (echo 'Commit message must follow Conventional Commits specification!' && exit 1)", 
          "pre-push": [
    
            "echo Running pre-push hooks...", 
            "./vendor/bin/pint --test", 
            "echo Running tests...", 
            "php artisan test"
          ], 
          "post-merge": "composer install"
        } 
      }
    }

Add the Git Hooks

From the project root:

bash
composer cghooks add --force-win

TIP

For more details, check out brainmaestro/composer-git-hooks.

Released under the MIT License.