wtyd/githooks
Composer 安装命令:
composer require wtyd/githooks
包简介
Manage git hooks in a simple and sophisticated way.
README 文档
README
1. Wtyd/GitHooks
GitHooks is a standalone CLI tool (.phar) for managing git hooks and running QA tools in PHP projects. Built with Laravel Zero.
Why GitHooks?
- Standalone binary — distributed as
.phar, so its dependencies don't interfere with your project. - Managed with Composer — no need for Phive or other tools.
- Unified configuration — one file (
githooks.php) configures all QA tools, hooks, and execution options. - Hooks, flows, jobs — map git events to groups of QA tasks with parallel execution, fail-fast, and conditional execution.
- Language agnostic — the
customjob type can run any command (eslint,prettier,composer audit, etc.), so GitHooks manages both backend and frontend QA from a single configuration.
2. Requirements
- PHP >= 7.4
- The QA tools you want to run (phpstan, phpcs, phpmd, etc.)
3. Install
1. Install GitHooks as a dev dependency:
composer require --dev wtyd/githooks
Note: for PHP < 8.1 you must add the following events to the scripts section in your composer.json:
"scripts": { "post-update-cmd": "Wtyd\\GitHooks\\Utils\\ComposerUpdater::phpOldVersions", "post-install-cmd": "Wtyd\\GitHooks\\Utils\\ComposerUpdater::phpOldVersions" }
Then run composer update wtyd/githooks.
2. Initialize the configuration file:
githooks conf:init
In interactive mode, GitHooks detects QA tools in vendor/bin/ and generates a tailored githooks.php. You can also use --no-interaction to copy a template.
3. Install the git hooks:
githooks hook
This creates a .githooks/ directory with universal hook scripts and configures git config core.hooksPath .githooks. The .githooks/ directory should be committed to version control.
To automate hook installation, add it to your composer.json:
"scripts": { "post-update-cmd": [ "vendor/bin/githooks hook" ], "post-install-cmd": [ "vendor/bin/githooks hook" ] }
4. Usage
When you commit, all configured QA tools run automatically. If all checks pass, the commit proceeds. If not, you fix the code and try again.
All checks passed:
parallel_lint - OK. Time: 150ms
phpcs_src - OK. Time: 890ms
phpstan_src - OK. Time: 2.34s
phpmd_src - OK. Time: 1.23s
Results: 4/4 passed in 3.45s
Some checks failed:
parallel_lint - OK. Time: 150ms
phpcs_src - OK. Time: 890ms
phpstan_src - KO. Time: 2.34s
phpmd_src - OK. Time: 1.23s
phpstan_src:
/src/Foo.php:12 Access to undefined property $bar
/src/Foo.php:34 Method doSomething() has no return type
Results: 3/4 passed in 3.45s
Running manually
githooks flow qa # Run a flow (group of jobs) githooks flow qa --fast # Only staged files (accelerable jobs) githooks flow qa --fast-branch # Only files changed vs the base branch githooks flow qa --fast-dirty # Unified working tree (ideal for AI agents) githooks flow qa --fast-dirty --format=json --no-reports # AI-agent friendly: clean JSON, no side-effect files githooks flow qa --only-jobs=phpstan_src # Run specific jobs from a flow githooks flow qa --dry-run # Show commands without executing githooks job phpstan_src # Run a single job githooks job phpstan_src --format=json # JSON output for CI integration
5. Configuration
GitHooks uses a PHP configuration file (githooks.php) with three sections: hooks, flows, and jobs.
<?php return [ // Git hooks: map git events to flows/jobs 'hooks' => [ 'command' => 'php7.4 vendor/bin/githooks', // optional: customize the hook script command 'pre-commit' => ['qa'], 'pre-push' => [ ['flow' => 'full', 'only-on' => ['main', 'develop']], // only on these branches ], ], // Flows: named groups of jobs with shared execution options 'flows' => [ 'options' => ['fail-fast' => false, 'processes' => 2], // global defaults 'qa' => ['jobs' => ['phpcbf_src', 'phpcs_src', 'phpmd_src', 'parallel_lint']], 'full' => ['jobs' => ['phpstan_src', 'phpunit_all']], // CI flow that picks its mode per branch and declares intra-flow dependencies 'ci' => [ 'on' => [ 'master' => ['execution' => 'full'], // full on protected branches '*' => ['execution' => 'fast-branch'], // diff vs base everywhere else ], 'jobs' => [ 'yarn_install', // installs node_modules ['job' => 'eslint_src', 'needs' => ['yarn_install']], // waits for install ['job' => 'phpunit_all', 'only-files' => ['src/**', 'tests/**']], // admission gate ], ], ], // Jobs: individual QA tasks with declarative configuration 'jobs' => [ 'phpcs_src' => [ 'type' => 'phpcs', 'paths' => ['src'], 'standard' => 'PSR12', 'ignore' => ['vendor'], // executable-path omitted: auto-detects vendor/bin/phpcs, then system PATH ], 'phpcbf_src' => [ 'extends' => 'phpcs_src', // inherits paths, standard, ignore from phpcs 'type' => 'phpcbf', // overrides type ], 'phpmd_src' => [ 'type' => 'phpmd', 'executable-path' => 'tools/phpmd', // explicit path when not in vendor/bin 'paths' => ['src'], 'rules' => 'cleancode,codesize,naming,unusedcode', ], 'parallel_lint' => [ 'type' => 'parallel-lint', 'paths' => ['src'], 'exclude' => ['vendor'], ], 'phpstan_src' => [ 'type' => 'phpstan', 'level' => 8, 'paths' => ['src'], ], 'phpunit_all' => [ 'type' => 'phpunit', 'config' => 'phpunit.xml', ], 'composer_audit' => [ 'type' => 'custom', // run any command 'script' => 'composer audit', ], 'yarn_install' => [ 'type' => 'script', // one-liner; no path filtering 'script' => 'yarn install --frozen-lockfile', ], 'eslint_src' => [ 'type' => 'custom', 'executable-path' => 'npx eslint', // structured mode: executable + paths 'paths' => ['resources/js'], 'other-arguments' => '--fix', 'accelerable' => true, // opt-in: filters paths to staged files with --fast ], ], ];
Key concepts
- Hooks map git events (
pre-commit,pre-push, etc.) to flows and jobs. Supports conditional execution by branch (only-on/exclude-on) and staged file patterns (only-files/exclude-files). - Flows are named groups of jobs with shared options (
fail-fast,processes,time-budget,memory-budget,reports). Reusable across hooks and directly executable from CLI. A flow can declareon => [branch_pattern => attrs]to pick its execution mode per branch (e.g.fullonmaster,fast-branchelsewhere). Each entry injobsaccepts the plain job name or an object{job, needs?, only-files?, exclude-files?}to declare intra-flow dependencies and per-entry admission gates. Meta-flows (flows.<X>.flows) compose other flows in a single run. - Jobs are individual QA tasks. Each declares a
typeand its arguments. Jobs can inherit from other jobs withextends. Whenexecutable-pathis omitted, GitHooks auto-detects the binary invendor/bin/. Per-job thresholds (warn-after,fail-after,memory,cores) cap regressions. - Execution modes:
full(default),--fast(only staged files),--fast-branch(only files changed vs base branch),--fast-dirty(unified working tree: dirty vsHEAD∪ untracked — ideal for AI agentic hooks),--files/--files-from(explicit list).
See the documentation site for the full configuration reference.
6. Supported Tools
| Tool | Type | Description |
|---|---|---|
| PHP CodeSniffer | phpcs / phpcbf |
Code style checking and auto-fixing |
| PHPStan | phpstan |
Static analysis |
| Psalm | psalm |
Static analysis |
| PHP Mess Detector | phpmd |
Code quality rules |
| Parallel-lint | parallel-lint |
Syntax checking |
| PHPUnit | phpunit |
Unit testing |
| Paratest | paratest |
Parallel PHPUnit driver |
| PHP CS Fixer | php-cs-fixer |
Code style auto-fixing |
| Rector | rector |
Automated code refactoring |
| PHP Copy Paste Detector | phpcpd |
Duplicate code detection |
| Inline command | script |
Run a single shell command (executable: 'composer audit'). No path filtering. |
| Any tool | custom |
Run any binary with script (one-liner) or executable-path + paths (+ accelerable: true to opt into --fast / --fast-branch filtering) |
| Commit message | commit-msg |
Validate the commit-message subject (declarative rules + conventional-commits preset), wired to Git's commit-msg hook. Runs inline, no shell. |
Two ways to run non-PHP tools:
script— minimal, one-liner command for tools that don't take paths (composer audit,npm audit, etc.).custom— path-aware execution. Two modes: pass the full command inscript, or declareexecutable-path+paths+ optionalother-arguments. Setaccelerable: trueto filter paths down to staged / branch-changed files when running with--fast/--fast-branch.
7. Commands
| Command | Description |
|---|---|
githooks flow <name> |
Run a single flow. |
githooks flows <name1> <name2>… |
Run multiple flows in a combined plan: ad-hoc list, declarative meta-flow, or mixed. Jobs deduped by first occurrence. |
githooks job <name> |
Run a single job. Pass extra args to the underlying tool with --: githooks job phpunit -- --filter=MyTest. |
githooks hook |
Install git hooks via core.hooksPath. |
githooks hook:clean |
Remove installed hooks. |
githooks status |
Show hook installation status. |
githooks cache:clear [jobs…] |
Clear QA tool cache files. Accepts job names, flow names, or no argument (clear all). |
githooks conf:init |
Generate configuration file (interactive or template). |
githooks conf:check |
Validate configuration with deep checks (paths, executables, configs, deprecations, did-you-mean for typos). |
githooks conf:migrate |
Migrate v2 config to v3 format. |
githooks system:info |
Show CPU detection (cgroup-aware) and process configuration. |
githooks profile <flow> |
Chart a metric (time, peak-memory, peak-cores) across the flow's run history as a sparkline with min/p50/p95/max and a trend. Requires recorded runs (see --save-history / history-size). |
githooks profile:list <flow> |
List the runs persisted under .githooks/history/ for a flow. |
Common flags for flow / flows / job
| Flag | Purpose |
|---|---|
--mode=full|fast|fast-branch|fast-dirty / --fast / --fast-branch / --fast-dirty |
Select execution mode. fast filters to staged files, fast-branch to files changed vs base branch, fast-dirty to the unified working tree (dirty vs HEAD ∪ untracked). |
--branch=X (flow only) |
Force branch name used to resolve flows.<X>.on => [branch_pattern => …]. Overrides CI env vars and git rev-parse HEAD. In flows/job, branch resolution falls back to $GITHOOKS_BRANCH / CI env / git. |
--files=a,b,c / --files-from=PATH / --exclude-pattern=glob |
Run against an explicit list of files (CSV, manifest, or with exclusion patterns). |
--processes=N, --fail-fast, --only-jobs=a,b, --exclude-jobs=a,b, --dry-run |
Control parallelism, fail-fast behaviour, and job selection. |
--format=text|json|junit|sarif|codeclimate |
Output format on stdout. JSON v2 schema is stable. |
--report-json=PATH, --report-junit=PATH, --report-sarif=PATH, --report-codeclimate=PATH |
Write structured reports to files (one or many at once). --no-reports skips the config-declared reports. |
--output=PATH |
Write the --format=… payload to a file instead of stdout. |
--show-progress |
Force progress to stderr even with a structured --format (CI dashboards). |
--stats |
Print a 5-column table (Job / Status / Time / Peak Cores / Peak Memory) plus temporal attribution at the end of the flow. Also exposes stats block in JSON v2. |
--save-history (flow / flows) |
Persist this run under .githooks/history/ so profile can chart its trend (opt-in; never on --dry-run). |
--warn-after=N, --fail-after=N, --no-time-budget |
Override flow time-budget thresholds (seconds). |
--memory-warn-above=MB, --memory-fail-above=MB, --no-memory-budget, --allocator=fifo|greedy |
Override memory-budget thresholds and 2D allocator strategy. |
See the documentation site for the full command reference.
8. Documentation
Full documentation is available at wtyd.github.io/githooks, including:
- Getting Started — installation, first config, first hook.
- Configuration Reference — hooks, flows, jobs, options.
- Tools Reference — all keywords for each QA tool.
- CLI Reference — every command with options and examples.
- How-To Guides — parallel execution, CI/CD, frontend tools, etc.
- Migration — from v2, GrumPHP, or CaptainHook.
- Comparison — GitHooks vs GrumPHP vs CaptainHook.
- Changelog — what's new in each version.
9. Contributing
Contributions are welcome! Send a pull request or open an issue. See the Contributing guide.
10. License
The MIT License (MIT). Please see License File for more information.
wtyd/githooks 适用场景与选型建议
wtyd/githooks 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24.16k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2020 年 11 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cli」 「git」 「console」 「hooks」 「qa」 「commit」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 wtyd/githooks 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wtyd/githooks 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 wtyd/githooks 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This class provides you with an easy-to-use interface to progress bars.
A Laravel package to retrieve data from Google Search Console
Additional artisan commands for Laravel
Easily manage git hooks in your composer config
MediaWiki extension that allows embedding external content, specified by URL, into your wiki pages
Nagios plugin to verify a git repository.
统计信息
- 总下载量: 24.16k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-11-06