From a3c06384b52f56c454ac7f08dfeae26bfb6b52c9 Mon Sep 17 00:00:00 2001 From: goerlibe Date: Thu, 28 Sep 2023 10:45:51 +0200 Subject: [PATCH 1/6] feat: automatically enforce commit message format --- .pre-commit-config.yaml | 14 ++++++++++++-- docs/How_to_contribute.md | 12 ++++++------ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8ea96a8c8..4335f5338 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -6,6 +6,8 @@ # the 3-Clause BSD License. See the LICENSE file in the package base # directory for details. +default_install_hook_types: [pre-commit, commit-msg] + default_language_version: python: python3 # python >= 3.8 should be used. @@ -37,9 +39,9 @@ repos: # Using this mirror lets us use mypyc-compiled black, which is about 2x faster # more info: https://github.com/psf/black/blob/main/docs/integrations/source_version_control.md - repo: https://github.com/psf/black-pre-commit-mirror - rev: 23.7.0 + rev: 23.9.1 hooks: - - id: black # run black fromatter on all files + - id: black # run black formatter on all files args: [--line-length=120] @@ -50,3 +52,11 @@ repos: entry: scripts/dev/check-license.sh language: script # exclude: we could exclude files here, but we do it in the script instead + +- repo: local + hooks: + - id: check-commit-msg + name: Check commit message + entry: python scripts/dev/check-commit-msg.py + language: python + stages: [commit-msg] diff --git a/docs/How_to_contribute.md b/docs/How_to_contribute.md index 573ba0279..f2b675132 100644 --- a/docs/How_to_contribute.md +++ b/docs/How_to_contribute.md @@ -29,12 +29,12 @@ In general it is sufficient to follow the general installation instructions. How - Install the python programs in development mode by executing `pip install -e .[dev]` from the project main directory - 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 - ``` + - 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 some checks: + - black is run to **format** python source code + - python **type safety** is improved with mypy + - we enforce [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/) + - other checks like looking for unneccesary whitespace and preventing large files to be added + ## Commit messages Commit messages should follow the following format: From ea0ada137bd01b434297693b0417444dd69c6dfd Mon Sep 17 00:00:00 2001 From: goerlibe Date: Thu, 28 Sep 2023 10:55:44 +0200 Subject: [PATCH 2/6] feat: allow more commit message types --- scripts/dev/check-commit-msg.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/dev/check-commit-msg.py b/scripts/dev/check-commit-msg.py index b8c45f2b9..81649e82d 100644 --- a/scripts/dev/check-commit-msg.py +++ b/scripts/dev/check-commit-msg.py @@ -20,7 +20,7 @@ commit_msg = f.read() commit_msg = commit_msg.replace("\n", "") -pattern = re.compile("^(feat|fix|test|chore|wip)(\(.+\))?(\[.+\])?:.+$") +pattern = re.compile("^(feat|fix|test|chore|wip|refactor|doc|docs|perf|ci|build|style)(\(.+\))?(\[.+\])?:.+$") matches = bool(pattern.match(commit_msg)) if matches: @@ -31,5 +31,5 @@ print("\t", commit_msg) print("Please use the following format:") print("\t(scope)[optional info]: commit message") - print("where `` can be any of `feat,fix,test,chore,wip`.") + print("where `` can be any of `feat,fix,test,chore,wip,refactor,doc,docs,perf,ci,build,style`.") sys.exit(1) From 12c84d5c48819488d800837b08db174a420641f5 Mon Sep 17 00:00:00 2001 From: goerlibe Date: Thu, 28 Sep 2023 11:05:11 +0200 Subject: [PATCH 3/6] feat: use commitlint instead of custom script chore: cleanup (remove unused script) --- .pre-commit-config.yaml | 11 ++++++----- commitlint.config.js | 10 ++++++++++ scripts/dev/check-commit-msg.py | 35 --------------------------------- 3 files changed, 16 insertions(+), 40 deletions(-) create mode 100644 commitlint.config.js delete mode 100644 scripts/dev/check-commit-msg.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4335f5338..1492b4104 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -53,10 +53,11 @@ repos: language: script # exclude: we could exclude files here, but we do it in the script instead -- repo: local + +- repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook + rev: v9.5.0 hooks: - - id: check-commit-msg - name: Check commit message - entry: python scripts/dev/check-commit-msg.py - language: python + - id: commitlint + name: Check commit message with commitlint stages: [commit-msg] + additional_dependencies: ['@commitlint/config-conventional'] diff --git a/commitlint.config.js b/commitlint.config.js new file mode 100644 index 000000000..d7dd88b88 --- /dev/null +++ b/commitlint.config.js @@ -0,0 +1,10 @@ +/* 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. + */ + +module.exports = { extends: ['@commitlint/config-conventional'] }; diff --git a/scripts/dev/check-commit-msg.py b/scripts/dev/check-commit-msg.py deleted file mode 100644 index 81649e82d..000000000 --- a/scripts/dev/check-commit-msg.py +++ /dev/null @@ -1,35 +0,0 @@ -# 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. - -import re - -# checks the commit message for correct formatting. -# invoked by the git commit-msg hook, if configured. -import sys - -# 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|refactor|doc|docs|perf|ci|build|style)(\(.+\))?(\[.+\])?:.+$") -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(scope)[optional info]: commit message") - print("where `` can be any of `feat,fix,test,chore,wip,refactor,doc,docs,perf,ci,build,style`.") - sys.exit(1) From e39c375d230c55233889f3230b6519bab5d52493 Mon Sep 17 00:00:00 2001 From: goerlibe Date: Thu, 28 Sep 2023 11:25:33 +0200 Subject: [PATCH 4/6] docs: refer to conventional commits instead of custom definition --- docs/How_to_contribute.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/docs/How_to_contribute.md b/docs/How_to_contribute.md index f2b675132..7d9e51017 100644 --- a/docs/How_to_contribute.md +++ b/docs/How_to_contribute.md @@ -37,11 +37,7 @@ In general it is sufficient to follow the general installation instructions. How ## Commit messages -Commit messages should follow the following format: -``` -(scope)[optional info]: commit message -``` -where `` can be any of `feat,fix,test,chore,wip`. +Commit messages should follow the conventional commits format: [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/) ## Creating a new release Execute the following steps in order to create a new DiscoPoP release: From 80fa73b600e320b0fab42b1ad16a31df05943dc1 Mon Sep 17 00:00:00 2001 From: goerlibe Date: Fri, 29 Sep 2023 13:01:02 +0200 Subject: [PATCH 5/6] fix: remove commitlint as it did not work on all devices --- .pre-commit-config.yaml | 39 +++++++++++++++++++++++---------- commitlint.config.js | 1 + scripts/dev/check-commit-msg.py | 35 +++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 12 deletions(-) create mode 100644 scripts/dev/check-commit-msg.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1492b4104..65ad11f08 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -6,6 +6,10 @@ # the 3-Clause BSD License. See the LICENSE file in the package base # directory for details. +# pre-commit checks are defined in this file +# See https://pre-commit.com for more information + + default_install_hook_types: [pre-commit, commit-msg] default_language_version: @@ -14,8 +18,7 @@ default_language_version: # python3 used for compatibility reasons between different systems -# See https://pre-commit.com for more information -# See https://pre-commit.com/hooks.html for more hooks +# standard hooks -- more info: https://pre-commit.com/hooks.html repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.4.0 @@ -27,24 +30,25 @@ repos: - id: check-merge-conflict # check for merge conflict strings -# more info: https://github.com/pre-commit/mirrors-mypy +# mypy -- more info: https://github.com/pre-commit/mirrors-mypy - repo: https://github.com/pre-commit/mirrors-mypy rev: 'v1.5.1' # Use the sha / tag you want to point at hooks: - - id: mypy # run mypy + - id: mypy args: [--config-file=mypy.ini, --ignore-missing-imports] #additional_dependencies: [dep==version.version.version, ...] # NOTE: pre-commit runs mypy in a virtualenv, so dependencies are not installed unless explicitly listed here -# Using this mirror lets us use mypyc-compiled black, which is about 2x faster -# more info: https://github.com/psf/black/blob/main/docs/integrations/source_version_control.md + +# black -- more info: https://github.com/psf/black/blob/main/docs/integrations/source_version_control.md - repo: https://github.com/psf/black-pre-commit-mirror rev: 23.9.1 hooks: - - id: black # run black formatter on all files + - id: black args: [--line-length=120] +# check for discopop license tag - repo: local hooks: - id: licensetag @@ -54,10 +58,21 @@ repos: # exclude: we could exclude files here, but we do it in the script instead -- repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook - rev: v9.5.0 +# lint commit messages -- idea based on: https://www.conventionalcommits.org/en/v1.0.0/ +- repo: local hooks: - - id: commitlint - name: Check commit message with commitlint + - id: check-commit-msg + name: Check commit message + entry: python scripts/dev/check-commit-msg.py + language: python stages: [commit-msg] - additional_dependencies: ['@commitlint/config-conventional'] + + +# currently we use a custom script for commit message lintining because commitlint did not work consistently on all devices +#- repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook +# rev: v9.5.0 +# hooks: +# - id: commitlint +# name: Check commit message with commitlint +# stages: [commit-msg] +# additional_dependencies: ['@commitlint/config-conventional'] diff --git a/commitlint.config.js b/commitlint.config.js index d7dd88b88..0c577c0aa 100644 --- a/commitlint.config.js +++ b/commitlint.config.js @@ -7,4 +7,5 @@ * directory for details. */ +// currently we use a custom script for lintining because commitlint did not work consistently on all devices module.exports = { extends: ['@commitlint/config-conventional'] }; diff --git a/scripts/dev/check-commit-msg.py b/scripts/dev/check-commit-msg.py new file mode 100644 index 000000000..81649e82d --- /dev/null +++ b/scripts/dev/check-commit-msg.py @@ -0,0 +1,35 @@ +# 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. + +import re + +# checks the commit message for correct formatting. +# invoked by the git commit-msg hook, if configured. +import sys + +# 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|refactor|doc|docs|perf|ci|build|style)(\(.+\))?(\[.+\])?:.+$") +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(scope)[optional info]: commit message") + print("where `` can be any of `feat,fix,test,chore,wip,refactor,doc,docs,perf,ci,build,style`.") + sys.exit(1) From 6d3eddfb965006eb1b1dc9798e947abfc34fb43f Mon Sep 17 00:00:00 2001 From: Lukas Rothenberger Date: Fri, 29 Sep 2023 14:11:38 +0200 Subject: [PATCH 6/6] ci[commit-msg]: removed "wip" type --- scripts/dev/check-commit-msg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/dev/check-commit-msg.py b/scripts/dev/check-commit-msg.py index 81649e82d..f07141d76 100644 --- a/scripts/dev/check-commit-msg.py +++ b/scripts/dev/check-commit-msg.py @@ -20,7 +20,7 @@ commit_msg = f.read() commit_msg = commit_msg.replace("\n", "") -pattern = re.compile("^(feat|fix|test|chore|wip|refactor|doc|docs|perf|ci|build|style)(\(.+\))?(\[.+\])?:.+$") +pattern = re.compile("^(feat|fix|test|chore|refactor|doc|docs|perf|ci|build|style)(\(.+\))?(\[.+\])?:.+$") matches = bool(pattern.match(commit_msg)) if matches: