Skip to content

Commit

Permalink
feat(ci)[commit-msg hook]: automatic check of commit message formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasrothenberger committed Sep 27, 2023
1 parent d570b15 commit 7082506
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docs/How_to_contribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ In general it is sufficient to follow the general installation instructions. How
- The `-e` switch ensures that changes in the python source code are immediately active.
- `[dev]` also installs some development requirements (e.g. mypy, black, pre-commit).
- Install some git hooks by running `pre-commit install` from the main directory of this project. These hooks help to ensure a good quality of the commited code by automatically running the black **formatter** and checking for **type safety** with mypy on every commit.
- Activate the git `commit-msg` hook to validate commit message formatting by creating a file named `.git/hooks/commit-msg` with the following contents:
```
#!/bin/sh
python scripts/dev/check-commit-msg.py $1
```
## Commit messages
Commit messages should follow the following format:
```
<type>(scope)[optional info]: commit message
```
where `<type>` can be any of `feat,fix,test,chore,wip`.
## Creating a new release
Execute the following steps in order to create a new DiscoPoP release:
Expand Down
34 changes: 34 additions & 0 deletions scripts/dev/check-commit-msg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This file is part of the DiscoPoP software (http://www.discopop.tu-darmstadt.de)
#
# Copyright (c) 2020, Technische Universitaet Darmstadt, Germany
#
# This software may be modified and distributed under the terms of
# the 3-Clause BSD License. See the LICENSE file in the package base
# directory for details.

# checks the commit message for correct formatting.
# invoked by the git commit-msg hook, if configured.
import sys
import re

# todo translate to python

path_to_commit_msg = sys.argv[1]

with open(path_to_commit_msg, "r") as f:
commit_msg = f.read()
commit_msg = commit_msg.replace("\n", "")

pattern = re.compile("^(feat|fix|test|chore|wip)(\(\S+\))?(\[\S+\])?:.+$")
matches = bool(pattern.match(commit_msg))

if matches:
print("VALID commit message: ", commit_msg)
sys.exit(0)
else:
print("INVALID commit message: ")
print("\t", commit_msg)
print("Please use the following format:")
print("\t<type>(scope)[optional info]: commit message")
print("where `<type>` can be any of `feat,fix,test,chore,wip`.")
sys.exit(1)

0 comments on commit 7082506

Please sign in to comment.