Skip to content

Commit 2b192f3

Browse files
author
Anis
authored
Merge pull request #8 from lumapps/chore/precommit_hook
chore(script): check message during commit
2 parents 3c34309 + d0de179 commit 2b192f3

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
---
22
repos:
3+
- repo: ./
4+
rev: master
5+
hooks:
6+
- id: commit-message-validator
7+
stages: [commit-msg]
8+
args: [--no-jira, --allow-temp]
9+
310
- repo: https://github.com/adrienverge/yamllint.git
411
rev: v1.24.2
512
hooks:

.pre-commit-hooks.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
- id: commit-message-validator
3+
name: Commit Message Validator
4+
description: Checks that commit messages are compliant with Lumapps rules.
5+
entry: check_message.sh
6+
language: script

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pre-commit=./venv/bin/pre-commit
1313
venv:
1414
python3.7 -m venv venv
1515
$(pip) install pre-commit
16+
$(pre-commit) install -t commit-msg
1617

1718
lint:
1819
$(pre-commit) run --all-files

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,30 @@ jobs:
247247
- if `no_jira` is not empty, no validation is done on JIRA refs.
248248
- if `allow_temp` is not empty, no validation is done on `fixup!` and `squash!` commits.
249249

250+
## Add pre-commit plugin
251+
252+
If you are using [pre-commit](https://pre-commit.com/) in you repository, you can add this to your configuration so commit messages are checked locally:
253+
254+
Into `.pre-commit-config.yaml`:
255+
```yaml
256+
...
257+
repos:
258+
...
259+
- repo: https://github.com/lumapps/commit-message-validator
260+
rev: master
261+
hooks:
262+
- id: commit-message-validator
263+
stages: [commit-msg]
264+
args: [--allow-temp]
265+
...
266+
```
267+
Then run `pre-commit install --hook-type commit-msg` to install the `commit-message-validator`
268+
269+
### OPTIONS
270+
271+
- if `no_jira` is set, no validation is done on JIRA refs.
272+
- if `allow_temp` is set, no validation is done on `fixup!` and `squash!` commits.
273+
250274
<!-- ROADMAP -->
251275

252276
## Roadmap

check_message.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env bash
2+
3+
set -eu
4+
5+
OPTIONS=$(getopt --long no-jira allow-temp -- "$@")
6+
[ $? -eq 0 ] || {
7+
echo "Incorrect options provided"
8+
exit 1
9+
}
10+
11+
COMMIT_VALIDATOR_ALLOW_TEMP=
12+
COMMIT_VALIDATOR_NO_JIRA=
13+
14+
while true; do
15+
case "$1" in
16+
--no-jira ) COMMIT_VALIDATOR_NO_JIRA=1; shift ;;
17+
--allow-temp ) COMMIT_VALIDATOR_ALLOW_TEMP=1; shift ;;
18+
-- ) shift; break ;;
19+
* ) break ;;
20+
esac
21+
done
22+
23+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
24+
# shellcheck source=validator.sh
25+
source "$DIR/validator.sh"
26+
27+
28+
if [[ "$1" == *MERGE_MSG ]]
29+
then
30+
# ignore merge message (merge with --no-ff without conflict)
31+
exit
32+
fi
33+
34+
MESSAGE=$(<"$1")
35+
36+
FIRST_WORD=${MESSAGE%% *}
37+
if [[ "${FIRST_WORD,,}" == merge ]]
38+
then
39+
# ignore merge commits (merge after conflict resolution)
40+
exit
41+
42+
fi
43+
44+
# print message so you don't lose it in case of errors
45+
# (in case you are not using `-m` option)
46+
echo "Options: JIRA=$COMMIT_VALIDATOR_NO_JIRA, TEMP=$COMMIT_VALIDATOR_ALLOW_TEMP"
47+
printf "checking commit message:\n\n#BEGIN#\n%s\n#END#\n\n" "$(grep -v "#" <<< "$MESSAGE")"
48+
49+
validate "$MESSAGE"

0 commit comments

Comments
 (0)