butschster / laravel-git-hooks
Laravel 应用程序 git hooks 管理工具
Requires
- php: >=7.2
- illuminate/config: ^5.6|^6.0|^7.0
- illuminate/console: ^5.6|^6.0|^7.0
- illuminate/container: ^5.6|^6.0|^7.0
- illuminate/contracts: ^5.6|^6.0|^7.0
- illuminate/pipeline: ^5.6|^6.0|^7.0
- illuminate/support: ^5.6|^6.0|^7.0
Requires (Dev)
- mockery/mockery: ^1.0
- phpunit/phpunit: ^7.0
README
Laravel Git Hooks 管理器是 Laravel 应用程序的一个美观的工具。它提供了一种简单灵活的 API 来管理 git hooks,编辑提交信息
特性
- 管理 git hooks
- 编辑提交信息
- 使用自定义 hooks
- 文档齐全
- 经过充分测试
要求
- Laravel 5.6 到 6.x
- PHP 7.2 及以上
安装
在命令行运行
composer require --dev butschster/laravel-git-hooks
运行 artisan 命令注册 git hooks
php artisan git:register-hooks
这就完成了!
配置
发布配置文件。
php artisan vendor:publish --provider="Butschster\GitHooks\Providers\GitHooksServiceProvider" --tag=config
钩子
配置
您可以为每种类型的 git 钩子设置钩子列表。您可以在 config/git_hooks.php
中这样做。作为钩子,您可以使用类名、服务容器和闭包函数。
// App/Providers/AppServiceProvider class AppServiceProvider extends ServiceProvider { pubflic function register() { $this->app->bind('my-first-hook', function ($app) { return new \App\Console\GitHooks\MyPreCommitHook(); }); } } // config/git_hooks.php return [ ... 'pre-commit' => [ // Just class name \App\Console\GitHooks\MyPreCommitHook::class, // Class name with params, which will be pass in constructor \App\Console\GitHooks\MyPreCommitHook::class => [ 'param1' => 'hello', 'param2' => 'world' ], // Closure function function(ChangedFiles $files, Closure $next) { return $next($files); }, // Service container 'my-first-hook' ], ... ];
pre-commit 钩子
pre-commit
钩子在您甚至输入提交信息之前首先运行。它用于检查即将提交的快照,以查看您是否遗漏了某些内容,确保测试运行,或检查代码中需要检查的任何内容。此钩子退出非零值将中止提交,尽管您可以使用 git commit --no-verify
来绕过它。您可以使用检查代码风格(运行 lint 或类似命令)检查尾随空格(默认钩子正是这样做)或检查新方法上适当的文档。
// config/git_hooks.php return [ ... 'pre-commit' => [ \App\Console\GitHooks\MyPreCommitHook::class, ], ... ]; // App/Console/GitHooks/MyPreCommitHook.php namespace \App\Console\GitHooks; use \Butschster\GitHooks\Git\ChangedFiles; use Closure; class MyPreCommitHook implements \Butschster\GitHooks\Contracts\PreCommitHook { public function getName() : string { return '...'; } public function handle(ChangedFiles $files, Closure $next) { // do something // If you want to cancel commit, you have to throw an exception. // run next hook return $next($files); } }
prepare-commit-msg 钩子
prepare-commit-msg
钩子在提交信息编辑器启动之前但默认信息创建之后运行。它允许您在提交作者看到之前编辑默认信息。此钩子接受一些参数:包含提交信息的文件的路径、提交类型以及如果是修改提交的 SHA-1。此钩子通常对正常提交没有太大用处;相反,它对于默认信息是自动生成的提交很有用,例如模板化提交信息、合并提交、压缩提交和修改提交。您可以与提交模板结合使用,以编程方式插入信息。
// config/git_hooks.php return [ ... 'prepare-commit-msg' => [ \App\Console\GitHooks\MyFirstPrepareCommitHook::class, ], ... ]; // App/Console/GitHooks/MyFirstPrepareCommitHook.php namespace \App\Console\GitHooks; use Butschster\GitHooks\Git\CommitMessage; use Closure; class MyFirstPrepareCommitHook implements \Butschster\GitHooks\Contracts\MessageHook { public function getName() : string { return '...'; } public function handle(CommitMessage $message, Closure $next) { // do something $currentMessage = $message->getMessage(); // You can update commit message text $message->setMessage(str_replace('issue', 'fixed', $currentMessage)); // If you want to cancel commit, you have to throw an exception. // run next hook return $next($message); } }
commit-msg 钩子
commit-msg
钩子接受一个参数,即包含开发者编写的提交信息的临时文件的路径。如果此脚本退出非零值,Git 将中止提交过程,因此您可以在允许提交通过之前使用它来验证项目状态或提交信息。
// config/git_hooks.php return [ ... 'commit-msg' => [ \App\Console\GitHooks\MyFirstCommitMessageHook::class, ], ... ]; // App/Console/GitHooks/MyFirstCommitMessageHook.php namespace \App\Console\GitHooks; use Butschster\GitHooks\Git\CommitMessage; use Closure; class MyFirstCommitMessageHook implements \Butschster\GitHooks\Contracts\MessageHook { public function getName() : string { return '...'; } public function handle(CommitMessage $message, Closure $next) { // do something $currentMessage = $message->getMessage(); // You can update commit message text $message->setMessage(str_replace('issue', 'fixed', $currentMessage)); // If you want to cancel commit, you have to throw an exception. // run next hook return $next($message); } }
post-commit 钩子
在完成整个提交过程之后,将运行 post-commit 钩子。它不接收任何参数,但您可以通过运行 git log -1 HEAD 容易地获取最后一个提交。通常,此脚本用于通知或其他类似功能。
// config/git_hooks.php return [ ... 'post-commit' => [ \App\Console\GitHooks\NotifyAboutNewCommit::class, ], ... ]; // App/Console/GitHooks/NotifyAboutNewCommit.php namespace \App\Console\GitHooks; use Butschster\GitHooks\Git\Log; use Closure; class NotifyAboutNewCommit implements \Butschster\GitHooks\Contracts\PostCommitHook { public function getName() : string { return '...'; } public function handle(Log $log, Closure $next) { $hash = $log->getHash(); $author = $log->getAuthor(); $date = $log->getDate(); $message = $log->getMessage(); // do something // If you want to cancel, you have to throw an exception. // run next hook return $next($log); } }