Skip to content

Commit f29a3da

Browse files
committed
Add documentation for global git hooks support
1 parent 27a21cd commit f29a3da

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

README.md

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,51 @@
55
[![Packagist][badge-packagist]][link-packagist]
66
[![Download][badge-downloads]][link-packagist]
77

8-
> Manage git hooks easily in your composer configuration. This package 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.
8+
> 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.
99
1010
## Install
1111

1212
Add a `hooks` section to the `extra` section of your `composer.json` and add the hooks there. The previous way of adding hooks to the `scripts` section of your `composer.json` is still supported, but this way is cleaner if you have many scripts.
1313

14-
```json
14+
```javascript
1515
{
1616
"extra": {
1717
"hooks": {
18-
"pre-commit": "phpunit",
19-
"post-commit": "echo committed",
20-
"pre-push": ["phpunit", "echo pushing!"],
18+
"pre-commit": [
19+
"echo committing as $(git config user.name)",
20+
"php-cs-fixer fix ." // fix style
21+
],
22+
// verify commit message. ex: ABC-123: Fix everything
23+
"commit-msg": "grep -q '[A-Z]+-[0-9]+.*' $1",
24+
"pre-push": [
25+
"php-cs-fixer fix --dry-run ." // check style
26+
"phpunit"
27+
],
28+
"post-merge": "composer update"
2129
"...": "..."
2230
}
2331
}
2432
}
2533
```
2634

27-
Then install the library with
35+
Then install with
2836

2937
```sh
3038
composer require --dev brainmaestro/composer-git-hooks
3139
```
3240

3341
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/`.
3442

43+
### Global support
44+
45+
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.
46+
47+
```sh
48+
composer global require --dev brainmaestro/composer-git-hooks
49+
```
50+
51+
All commands have global support (besides testing the hooks. Still requires being in the directory with the `composer.json` file).
52+
3553
### Optional Configuration
3654

3755
#### Shortcut
@@ -63,7 +81,7 @@ Add the following events to your `composer.json` file. The `cghooks` commands wi
6381

6482
## Usage
6583

66-
All the following commands have to be run in the same folder as your `composer.json` file.
84+
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.
6785

6886
### Adding Hooks
6987

@@ -78,10 +96,14 @@ to add all the valid git hooks that have been specified in the composer config.
7896

7997
The `lock` file contains a list of all added hooks.
8098

99+
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.
100+
81101
### Updating Hooks
82102

83103
The update command which is run with `cghooks update` basically ignores the lock file and tries to add hooks from the composer lock. 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.
84104

105+
This works similarly when used with `--global` except that there is no fallback to `$COMPOSER_HOME` if no directory is provided.
106+
85107
### Removing Hooks
86108

87109
Hooks can be easily removed with `cghooks remove`. This will remove all the hooks that were specified in the composer config.
@@ -94,6 +116,8 @@ Hooks can also be removed by passing them as arguments. The command `cghooks rem
94116

95117
**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.
96118

119+
This also does not have a fallback to `$COMPOSER_HOME` if no directory is provided when used with `--global`.
120+
97121
### Listing hooks
98122

99123
Hooks can be listed with the `cghooks list-hooks` command. This basically checks composer config and list the hooks that actually have files.
@@ -102,9 +126,12 @@ Hooks can be listed with the `cghooks list-hooks` command. This basically checks
102126

103127
The following options are common to all commands.
104128

105-
| Option | Description | Command |
106-
| --------- | --------------------- | ---------------------------------------------- |
107-
| `git-dir` | Path to git directory | `cghooks ${command} --git-dir='/path/to/.git'` |
129+
| Option | Description | Command |
130+
| --------- | ----------------------------------- | ---------------------------------------------- |
131+
| `git-dir` | Path to git directory | `cghooks ${command} --git-dir='/path/to/.git'` |
132+
| `global` | Runs the specified command globally | `cghooks ${command} --global` |
133+
134+
Each command also has a flag `-v` to control verbosity for more detailed logs. Currently, only one level is supported.
108135

109136
### Testing Hooks
110137

cghooks

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use BrainMaestro\GitHooks\Commands\ListCommand;
2222
use BrainMaestro\GitHooks\Commands\HookCommand;
2323
use Symfony\Component\Console\Application;
2424

25-
$application = new Application('Composer Git Hooks', '2.4.5');
25+
$application = new Application('Composer Git Hooks', '2.5.0');
2626

2727
$application->add(new AddCommand());
2828
$application->add(new UpdateCommand());

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,13 @@
5151
"pre-commit": "composer check-style",
5252
"pre-push": [
5353
"composer test",
54-
"grep -q $(git tag --sort=-v:refname | head -n 1) cghooks"
54+
"appver=$(grep -o -P '\\d.\\d.\\d' cghooks)",
55+
"tag=$(git tag --sort=-v:refname | head -n 1 | tr -d v)",
56+
"if [ \"$tag\" != \"$appver\" ]; then",
57+
"echo \"The most recent tag v$tag does not match the application version $appver\n\"",
58+
"sed -i -E \"s/$appver/$tag/\" cghooks",
59+
"exit 1",
60+
"fi"
5561
]
5662
}
5763
}

tests/helpersTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace BrainMaestro\GitHooks\Tests;
44

5-
use PHPUnit\Framework\TestCase;
5+
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
66

7-
class HelpersTest extends TestCase
7+
class HelpersTest extends PHPUnitTestCase
88
{
99
/**
1010
* @test

0 commit comments

Comments
 (0)