PHP code example of igorsgm / laravel-git-hooks

1. Go to this page and download the library: Download igorsgm/laravel-git-hooks library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

igorsgm / laravel-git-hooks example snippets


'pre-commit' => [
    \Igorsgm\GitHooks\Console\Commands\Hooks\PintPreCommitHook::class, // Laravel Pint
    \Igorsgm\GitHooks\Console\Commands\Hooks\PHPCodeSnifferPreCommitHook::class, // PHPCS (with PHPCBF autofixer) 
    \Igorsgm\GitHooks\Console\Commands\Hooks\LarastanPreCommitHook::class, // Larastan
    \Igorsgm\GitHooks\Console\Commands\Hooks\EnlightnPreCommitHook::class, // Enlightn
    \Igorsgm\GitHooks\Console\Commands\Hooks\ESLintPreCommitHook::class, // ESLint
    \Igorsgm\GitHooks\Console\Commands\Hooks\PrettierPreCommitHook::class, // Prettier
],

'pre-commit' => [
    // Other pre-commit hooks...
    \App\Console\GitHooks\MyCustomPreCommitHook::class,
],

// config/git-hooks.php
return [
    ...
    'pre-commit' => [
        \App\Console\GitHooks\MyPreCommitHook::class,
    ],
    ...
];

// App/Console/GitHooks/MyPreCommitHook.php

namespace App\Console\GitHooks;

use Closure;
use Igorsgm\GitHooks\Git\ChangedFiles;

class MyPreCommitHook implements \Igorsgm\GitHooks\Contracts\PreCommitHook
{
    // ...

    public function handle(ChangedFiles $files, Closure $next)
    {
        // TODO: Implement your pre commit hook logic here.

        // If you want to cancel the commit, you have to throw an exception.
        // i.e: throw new HookFailException();

        // Run the next hook in the chain
        return $next($files);
    }
}

// config/git-hooks.php
return [
    ...
    'prepare-commit-msg' => [
        \App\Console\GitHooks\MyPrepareCommitMessageHook::class,
    ],
    ...
];

// App/Console/GitHooks/MyPrepareCommitMessageHook.php

namespace App\Console\GitHooks;

use Closure;
use Igorsgm\GitHooks\Git\CommitMessage;
use Igorsgm\GitHooks\Contracts\MessageHook;

class MyPrepareCommitMessageHook implements \Igorsgm\GitHooks\Contracts\MessageHook
{
    // ...

    public function handle(CommitMessage $message, Closure $next)
    {
        // TODO: Implement your prepare commit msg hook logic here.

        $currentMessage = $message->getMessage();
        // You can update commit message text
        $message->setMessage(str_replace('issue', 'fixed', $currentMessage));

        // If you want to cancel the commit, you have to throw an exception.
        // i.e: throw new HookFailException();

        // Run the next hook in the chain
        return $next($message);
    }
}

// config/git-hooks.php
return [
    ...
    'commit-msg' => [
        \App\Console\GitHooks\MyCommitMessageHook::class,
    ],
    ...
];

// config/git-hooks.php
return [
    ...
    'post-commit' => [
        \App\Console\GitHooks\MyPostCommitHook::class,
    ],
    ...
];

// App/Console/GitHooks/MyPrepareCommitMessageHook.php

namespace App\Console\GitHooks;

use Closure;
use Igorsgm\GitHooks\Git\CommitMessage;
use Igorsgm\GitHooks\Contracts\PostCommitHook;

class MyPostCommitHook implements \Igorsgm\GitHooks\Contracts\PostCommitHook
{
    // ...

    public function handle(Log $log, Closure $next)
    {
        // TODO: Implement post commit hook logic here.

        // You can interact with the commit log
        $hash = $log->getHash();
        $author = $log->getAuthor();
        $date = $log->getDate();
        $message = $log->getMessage();

        // If you want to cancel the commit, you have to throw an exception.
        // i.e: throw new HookFailException();

        // Run the next hook in the chain
        return $next($log);
    }
}

// config/git-hooks.php
return [
    ...
    'pre-push' => [
        \App\Console\GitHooks\MyPrePushHook::class,
    ],
    ...
];
bash
php artisan vendor:publish --tag=laravel-git-hooks
bash
php artisan git-hooks:register
bash
    php artisan git-hooks:make
    
bash
php artisan git-hooks:register