From 5c00bd86e0046a5c6767f60a0d67d0bc21a45b51 Mon Sep 17 00:00:00 2001
From: Moul <moul@moul.re>
Date: Sun, 9 May 2021 15:59:26 +0200
Subject: [PATCH] [enh] #132: Introduce Black pre-commit

Introduce .pre-commit-config.yml
Document its usage in CONTRIBUTING.md
Add pre-commit dependency as a dev dependency
---
 .pre-commit-config.yaml |  5 +++++
 CONTRIBUTING.md         | 26 +++++++++++++++++++++++++-
 README.md               |  2 --
 pyproject.toml          |  1 +
 4 files changed, 31 insertions(+), 3 deletions(-)
 create mode 100644 .pre-commit-config.yaml

diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
new file mode 100644
index 00000000..47fe30ad
--- /dev/null
+++ b/.pre-commit-config.yaml
@@ -0,0 +1,5 @@
+repos:
+-   repo: https://github.com/psf/black
+    rev: 21.5b0
+    hooks:
+    - id: black
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 044ff64a..bc695d8c 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -1,7 +1,31 @@
 # Contribute guide
 
-## Release workflow
+## Pre-commit
+We are using [`pre-commit`](https://pre-commit.com/) tool to perform checks on staged changes before committing. Such as black formatting.
+Depending whether you prefer to work ouside or inside Poetry virtual environment, you should stick to one usage or use both to still have this usefull checks.
+`pre-commit` can be used from the installation on your system or from inside Poetry virtual environment where it is installed.
+
+To install the `git-hooks`, run:
+```bash
+pip3 install --user pre-commit
+duniterpy> pre-commit install
+```
 
+```bash
+duniterpy> poetry shell
+(duniterpy-UyTOfZjU-py3.9) pre-commit install
+```
+
+### Black formatting
+We are using [Black](https://github.com/psf/black) formatter tool.
+Run Black on a Python file to format it:
+```bash
+poetry run black duniterpy/file.py
+```
+With `pre-commit`, Black is called on staged files, so the commit should fail in case black would make changes.
+You will have to add Black changes in order to commit your changes.
+
+## Release workflow
 To handle a release, you have to respect this workflow:
 
 * Verify all features and bug fixes are merged in the `dev` branch.
diff --git a/README.md b/README.md
index f1e74b4b..3e70265d 100644
--- a/README.md
+++ b/README.md
@@ -73,8 +73,6 @@ poetry run make docs
 * When writing docstrings, use the reStructuredText format recommended by https://www.python.org/dev/peps/pep-0287/#docstring-significant-features
 * Use `make` commands to check the code and the format.
 
-Black, the formatting tool, requires Python 3.6 or higher.
-
 * Install runtime dependencies
 ```bash
 poetry install --no-dev
diff --git a/pyproject.toml b/pyproject.toml
index b4da4b69..3c4e5491 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -38,5 +38,6 @@ websocket-client = "^0.57"
 black = {version = "^21.4b2", allow-prereleases = true}
 mypy = "^0.790"
 pylint = "^2.7.2"
+pre-commit = "^2.12.1"
 sphinx = "^3.2.1"
 sphinx_rtd_theme = "^0.5.0"
-- 
GitLab