jschaedl/composer-git-hooks
Composer 安装命令:
composer require jschaedl/composer-git-hooks
包简介
Easily manage git hooks in your composer config
README 文档
README
Manage git hooks easily in your composer configuration. This command line tool makes it easy to implement a consistent project-wide usage of git hooks. Specifying hooks in the composer file makes them available for every member of the project team. This provides a consistent environment and behavior for everyone which is great. It is also possible to use to manage git hooks globally for every repository on your computer. That way you have a reliable set of hooks crafted by yourself for every project you choose to work on.
Install
Add a hooks section to the extra section of your composer.json and add the hooks there.
{ "extra": { "hooks": { "pre-commit": [ "echo committing as $(git config user.name)", "php-cs-fixer fix ." // fix style ], // verify commit message. ex: ABC-123: Fix everything "commit-msg": "grep -q '[A-Z]+-[0-9]+.*' $1", "pre-push": [ "php-cs-fixer fix --dry-run ." // check style "phpunit" ], "post-merge": "composer install" "...": "..." } } }
Then install with
composer require --dev brainmaestro/composer-git-hooks
This installs the cghooks binary to your vendor/bin folder. If this folder is not in your path, you will need to preface every command with vendor/bin/.
Note: hooks declared in the scripts or hooks root sections of composer.json are no longer supported in v3.
Global support
You can also install it globally. This feels much more natural when cghooks is used with the newly added support for managing global git hooks.
composer global require --dev brainmaestro/composer-git-hooks
All commands have global support (besides testing the hooks. Still requires being in the directory with the composer.json file).
Optional Configuration
Stop on failure
When a hook is a sequence of commands, it can be useful to stop the execution when a command fails.
Specify the impacted hooks in the stop-on-failure config section.
{
"extra": {
"hooks": {
"config": {
"stop-on-failure": ["pre-push"]
},
"pre-push": [
"php-cs-fixer fix --dry-run --stop-on-violation .",
"phpunit"
],
}
}
}
Always be sure to run the update command after changing the stop-on-failure config section.
Custom hooks
Custom hooks can be added to the custom-hooks array of the `config section.
{
"extra": {
"hooks": {
"config": {
"custom-hooks": ["pre-flow-feature-start"]
},
"pre-flow-feature-start": [
"echo 'Starting a new feature...'"
]
}
}
}
Always be sure to run the update command after changing the custom-hooks config section.
Note: config is not valid custom hook value.
Shortcut
Add a cghooks script to the scripts section of your composer.json file. That way, commands can be run with composer cghooks ${command}. This is ideal if you would rather not edit your system path.
{
"scripts": {
"cghooks": "vendor/bin/cghooks",
"...": "..."
}
}
Composer Events
Add the following events to your composer.json file. The cghooks commands will be run every time the events occur. Go to Composer Command Events for more details about composer's event system.
{
"scripts": {
"post-install-cmd": "cghooks add --ignore-lock",
"post-update-cmd": "cghooks update",
"...": "..."
}
}
Usage
All the following commands have to be run either in the same folder as your composer.json file or by specifying the --git-dir option to point to a folder with a composer.json file.
Adding Hooks
After installation is complete, run cghooks add
to add all the valid git hooks that have been specified in the composer config.
| Option | Description | Command |
|---|---|---|
no-lock |
Do not create a lock file | cghooks add --no-lock |
ignore-lock |
Add the lock file to .gitignore | cghooks add --ignore-lock |
force-win |
Force windows bash compatibility | cghooks add --force-win |
The lock file contains a list of all added hooks.
If the --global flag is used, the hooks will be added globally, and the global git config will also be modified. If no directory is provided, there is a fallback to the current core.hooksPath in the global config. If that value is not set, it defaults to $COMPOSER_HOME (this specific fallback only happens for the add command). It will fail with an error if there is still no path after the fallbacks.
Updating Hooks
The update command which is run with cghooks update basically ignores the lock file and tries to add hooks from the composer file. This is similar to what the --force option for the add command did. This command is useful if the hooks in the composer.json file have changed since the first time the hooks were added.
This works similarly when used with --global except that there is no fallback to $COMPOSER_HOME if no directory is provided.
Removing Hooks
Hooks can be easily removed with cghooks remove. This will remove all the hooks that were specified in the composer config.
Hooks can also be removed by passing them as arguments. The command cghooks remove pre-commit post-commit which will remove the pre-commit and post-commit hooks.
| Option | Description | Command |
|---|---|---|
force |
Delete hooks without checking the lock file | cghooks remove --force |
CAREFUL: If the lock file was tampered with or the force option was used, hooks that already existed before using this package, but were specified in the composer scripts config will be removed as well. That is, if you had a previous pre-commit hook, but your current composer config also has a pre-commit hook, this option will cause the command to remove your initial hook.
This also does not have a fallback to $COMPOSER_HOME if no directory is provided when used with --global.
Listing hooks
Hooks can be listed with the cghooks list-hooks command. This basically checks composer config and list the hooks that actually have files.
Common Options
The following options are common to all commands.
| Option | Description | Command |
|---|---|---|
git-dir |
Path to git directory | cghooks ${command} --git-dir='/path/to/.git' |
lock-dir |
Path to lock file directory | cghooks ${command} --lock-dir='/path/to/lock' |
global |
Runs the specified command globally | cghooks ${command} --global |
Each command also has a flag -v to control verbosity for more detailed logs. Currently, only one level is supported.
Testing Hooks
Hooks can be tested with cghooks ${hook} before adding them. Example cghooks pre-commit runs the pre-commit hook.
Contributing
Please see CONTRIBUTING for details.
Credits
Related
License
The MIT License (MIT). Please see License File for more information.
jschaedl/composer-git-hooks 适用场景与选型建议
jschaedl/composer-git-hooks 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 67.87k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 12 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「git」 「composer」 「HOOK」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jschaedl/composer-git-hooks 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jschaedl/composer-git-hooks 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jschaedl/composer-git-hooks 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
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.
A system for organizing WordPress hooks
This composer plugin enables installation of GravityForms WordPress plugin and its addons.
Composer plugin for running post-install/post-update scripts of your project's dependencies
统计信息
- 总下载量: 67.87k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-12-01