|
| 1 | +[](https://goreportcard.com/report/github.com/Oppodelldog/git-commit-hook) [](https://raw.githubusercontent.com/Oppodelldog/git-commit-hook/master/LICENSE) [](http://nulldog.de:12080/Oppodelldog/git-commit-hook) [](https://coveralls.io/github/Oppodelldog/git-commit-hook?branch=master) |
| 2 | + |
1 | 3 | # git-commit-hook
|
| 4 | +> configureable git commit hook |
| 5 | +
|
| 6 | + |
| 7 | +**Customize commit messages dynamically with templating** |
| 8 | + |
| 9 | +**Validate commit message** |
| 10 | + |
| 11 | +### 1. install |
| 12 | +#### Download |
| 13 | +downlod the binary and ensure your user has execution permissions |
| 14 | +#### Install |
| 15 | +Copy or link the binary into your git repositories ```hooks``` folder |
| 16 | +(Project/.git/hooks), rename it to ```commit-msg``` |
| 17 | + |
| 18 | +Or create a symlink: |
| 19 | +```ln -sf ~/Downloads/git-commit-hook ~/MyProject/.git/hooks/commit-msg``` |
| 20 | + |
| 21 | +### 2. Configure |
| 22 | +```yaml |
| 23 | + "project xyz": |
| 24 | + # path to the git repository |
| 25 | + path: "/home/nils/projects/xyz/.git" |
| 26 | + |
| 27 | + # define types of branch and a pattern to identify the type for the current branch you are working on |
| 28 | + branch: |
| 29 | + master: "^(origin\\/)*master" |
| 30 | + |
| 31 | + # define a commit message template per branch type, or as here for all (*) branch types |
| 32 | + template: |
| 33 | + "*": "{.BranchName}: {.CommitMessage}" |
| 34 | + |
| 35 | + # define validation rules per branch type |
| 36 | + validation: |
| 37 | + master: |
| 38 | + "(?m)(?:\\s|^|/)(([A-Z](_)*)+-[0-9]+)([\\s,;:!.-]|$)" : "valid ticket ID" |
| 39 | + ``` |
| 40 | + There are several places the configuration will be searched at, but one thing is for sure, the config file |
| 41 | + must be named ```git-commit-hook.yaml```. |
2 | 42 |
|
3 |
| -[](https://goreportcard.com/report/github.com/Oppodelldog/git-commit-hook) [](https://raw.githubusercontent.com/Oppodelldog/git-commit-hook/master/LICENSE) [](http://nulldog.de:12080/Oppodelldog/git-commit-hook) [](https://ci.appveyor.com/project/Oppodelldog/git-commit-hook/branch/master) [](https://coveralls.io/github/Oppodelldog/git-commit-hook?branch=master) |
| 43 | + Here are some places the configuration will be searched at: |
| 44 | + * **~/.config/git-commit-hook** |
| 45 | + * inside the **git repository** you commit in (also in subfolders **.git**, **.git/hooks**) |
4 | 46 |
|
5 |
| -**Problem:** I forget to add ticket numbers to commit messages or I take the wrong ticket ID. |
| 47 | +Watch out the test [fixture](config/test-data.yaml) for full feature sample |
6 | 48 |
|
7 |
| -**Solution:** a custom git-hook that prepends the current branch-name to every commit message. |
| 49 | +### 3. Commit |
| 50 | +You can do it on your own, I know that!! |
8 | 51 |
|
9 |
| -The implementation and rules are tightly coupled to **git flow** used with **jira**. |
| 52 | +--- |
10 | 53 |
|
11 |
| -The intention is on the one hand to make life easier when working on features. |
12 |
| -On the other hand the intention must be to not allow commits to non-fetaure branches without having a valid ticket reference. |
| 54 | +### Configuration check |
| 55 | +to check if the git hook is installed and configured correctly, just call the command from |
| 56 | +your repository like this: |
| 57 | +```.git/hooks/commit-msg``` |
13 | 58 |
|
14 |
| -### So here are the rules |
| 59 | +The output will be something like this: |
15 | 60 |
|
16 |
| -* At least there must always be a non empty commit message. |
| 61 | + git-commit-hook - parsed configuration |
17 | 62 |
|
18 |
| -* When you are committing to a **feature** branch |
19 | 63 |
|
20 |
| - the hook will preprend the branch name to the commit message |
| 64 | + branch types: |
| 65 | + master : ^(origin\/)*master |
21 | 66 |
|
22 |
| -* When you are committing to a non feature branch, lets say **develop** for a quick fix, **release/v0.1.1** for a release fix or a **hotfix** |
| 67 | + branch type templates: |
| 68 | + master : {{.CommitMessage}} - whatever !!! {{.BranchName}} |
23 | 69 |
|
24 |
| - you have to enter a valid feature reference. |
| 70 | + branch type validation: |
| 71 | + master : |
| 72 | + (?m)^.*(#\d*)+.*$ : must have a ticket reference (eg. #267) |
0 commit comments