ramsey / conventional-commits
A PHP library for creating and validating commit messages according to the Conventional Commits specification. Includes a CaptainHook action!
Fund package maintenance!
ramsey
Installs: 662 936
Dependents: 82
Suggesters: 0
Security: 0
Stars: 184
Watchers: 5
Forks: 21
Open Issues: 12
Requires
- php: ^8.1
- composer-runtime-api: ^2.0
- ext-json: *
- composer/composer: ^2.4
- jawira/case-converter: ^3.5
- opis/json-schema: ^2.3
- symfony/console: ^6.0 || ^7.0
- symfony/filesystem: ^6.0 || ^7.0
Requires (Dev)
- captainhook/captainhook: ^5.15
- captainhook/plugin-composer: ^5.3
- ergebnis/composer-normalize: ^2.30
- hamcrest/hamcrest-php: ^2.0
- mockery/mockery: ^1.5
- php-parallel-lint/php-console-highlighter: ^1.0
- php-parallel-lint/php-parallel-lint: ^1.3
- phpstan/extension-installer: ^1.2
- phpstan/phpstan: ^1.10
- phpstan/phpstan-mockery: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- phpunit/phpunit: ^10.1
- ramsey/coding-standard: ^2.2
- ramsey/composer-repl: ^1.4
- roave/security-advisories: dev-latest
- sebastianfeldmann/cli: ^3.4
- sebastianfeldmann/git: ^3.8
- spatie/phpunit-snapshot-assertions: ^5.1
- symfony/process: ^6.0 || ^7.0
Suggests
- captainhook/captainhook: Manage your project's Git hooks with CaptainHook, and use ramsey/conventional-commits in your commit-msg and prepare-commit-msg hooks.
README
A PHP library for creating and validating commit messages.
About
ramsey/conventional-commits is a PHP library for creating and validating commit messages according to the Conventional Commits specification. It also includes a CaptainHook action!
This project adheres to a code of conduct. By participating in this project and its community, you are expected to uphold this code.
Installation
Install this package as a development dependency using Composer.
composer require --dev ramsey/conventional-commits
Usage
To use the conventional-commits
console command to help you prepare commit
messages according to Conventional Commits, enter the following in your console:
./vendor/bin/conventional-commits prepare
You can also validate the commit message using the following command:
./vendor/bin/conventional-commits validate "[commit message]"
If you don't provide a commit message in the command line, the command will prompt you for it.
To see all the features of the console command, enter:
./vendor/bin/conventional-commits
CaptainHook Action
To use ramsey/conventional-commits with CaptainHook as part of your commit-msg
and/or prepare-commit-msg
Git hooks, be sure to require CaptainHook as a
development dependency.
Check out the CaptainHook documentation for more information on installing and configuring CaptainHook.
Validating Commit Messages
To use the CaptainHook action to validate commit messages according to the
Conventional Commits specification, add the following to the commit-msg
property in your captainhook.json
file:
{ "commit-msg": { "enabled": true, "actions": [ { "action": "\\Ramsey\\CaptainHook\\ValidateConventionalCommit" } ] } }
Preparing Commit Messages
You can set up this library to prompt you to prepare commit messages when you
use git commit
!
To use the CaptainHook action to prepare commit messages according to the
Conventional Commits specification, add the following to the prepare-commit-msg
property in your captainhook.json
file:
{ "prepare-commit-msg": { "enabled": true, "actions": [ { "action": "\\Ramsey\\CaptainHook\\PrepareConventionalCommit" } ] } }
Configuration
Configuring ramsey/conventional-commits offers control over a few more aspects of commit messages, such as letter case (i.e. lower, upper), allowed types and scopes, required footers, and more.
We look for configuration in one of two places:
composer.json
captainhook.json
⚠️ Please note: if your
composer.json
file is not in the same location as thevendor/
directory, we might have trouble locating it. Feel free to open an issue, and we'll work with you to see if we can find a solution.
Configuration Properties
Configuration for ramsey/conventional-commits consists of the following properties:
When specifying configuration, if you wish to use the default behavior for a property, it is not necessary to list the property in your configuration.
Recognized letter cases are:
Configuration in composer.json
If you choose to put your configuration in composer.json
, place it within the
extra
property, namespaced under ramsey/conventional-commits
, like this:
{ "extra": { "ramsey/conventional-commits": { "config": { "typeCase": null, "types": [], "scopeCase": null, "scopeRequired": false, "scopes": [], "descriptionCase": null, "descriptionEndMark": null, "bodyRequired": false, "bodyWrapWidth": null, "requiredFooters": [] } } } }
📝 The properties in this example represent the default values.
Configuration in captainhook.json
If you choose to put your configuration in captainhook.json
, you must provide
it for each action you configure, like this:
{ "commit-msg": { "enabled": true, "actions": [ { "action": "\\Ramsey\\CaptainHook\\ValidateConventionalCommit", "options": { "config": { "typeCase": null, "types": [], "scopeCase": null, "scopeRequired": false, "scopes": [], "descriptionCase": null, "descriptionEndMark": null, "bodyRequired": false, "bodyWrapWidth": null, "requiredFooters": [] } } } ] }, "prepare-commit-msg": { "enabled": true, "actions": [ { "action": "\\Ramsey\\CaptainHook\\PrepareConventionalCommit", "options": { "config": { "typeCase": null, "types": [], "scopeCase": null, "scopeRequired": false, "scopes": [], "descriptionCase": null, "descriptionEndMark": null, "bodyRequired": false, "bodyWrapWidth": null, "requiredFooters": [] } } } ] } }
However, if you provide your configuration in composer.json
, it is not
necessary to also provide it in captainhook.json
.
🚨 If using the Git commit hook functionality of Captain Hook, any configuration provided in
captainhook.json
will override configuration incomposer.json
.⚠️ When using the standalone command (i.e.
./vendor/bin/conventional-commits
), only configuration incomposer.json
will apply, unless providing the--config
option.
Configuration in a Separate File
You may also store your configuration in a separate file. For example, you may
store it in conventional-commits.json
, like this:
{ "typeCase": "kebab", "types": [ "ci", "deps", "docs", "refactor", "style", "test" ], "scopeCase": "kebab", "scopeRequired": false, "scopes": [], "descriptionCase": "lower", "descriptionEndMark": "", "bodyRequired": true, "bodyWrapWidth": 72, "requiredFooters": ["Signed-off-by"] }
When stored in a separate file, we won't know where to look for your
configuration, unless you tell us, so you must still provide a small amount of
configuration in either composer.json
or captainhook.json
, so we can find
it.
Here's what this looks like in composer.json
:
{ "extra": { "ramsey/conventional-commits": { "configFile": "./conventional-commits.json" } } }
And here's what this looks like in captainhook.json
:
{ "commit-msg": { "enabled": true, "actions": [ { "action": "\\Ramsey\\CaptainHook\\ValidateConventionalCommit", "options": { "configFile": "./conventional-commits.json" } } ] }, "prepare-commit-msg": { "enabled": true, "actions": [ { "action": "\\Ramsey\\CaptainHook\\PrepareConventionalCommit", "options": { "configFile": "./conventional-commits.json" } } ] } }
Contributing
Contributions are welcome! To contribute, please familiarize yourself with CONTRIBUTING.md.
Coordinated Disclosure
Keeping user information safe and secure is a top priority, and we welcome the contribution of external security researchers. If you believe you've found a security issue in software that is maintained in this repository, please read SECURITY.md for instructions on submitting a vulnerability report.
Copyright and License
The ramsey/conventional-commits library is copyright © Ben Ramsey and licensed for use under the terms of the MIT License (MIT). Please see LICENSE for more information.