From a83cae012d0fa15b9ef5c7cbdfb5a185a8fbd864 Mon Sep 17 00:00:00 2001 From: Carlos Diaz Date: Thu, 24 Feb 2022 11:44:39 -0600 Subject: [PATCH] feat(script): add pre-commit configuration for code formatting (#3092) * Add initial pre-commit configuration for code formatting * chore: Move --recursive switch from cfg file to script * pre-commit: Update format-source hook to use code-format.cfg Also remove the code-format-per-file.cfg file as it's now unused * docs: Add section about pre-commit --- .pre-commit-config.yaml | 30 ++++++++++++++++++++++++++++++ docs/CODING_STYLE.md | 35 +++++++++++++++++++++++++++++++++++ scripts/code-format.cfg | 1 - scripts/code-format.py | 8 ++++---- 4 files changed, 69 insertions(+), 5 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 000000000000..9d8088a89459 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,30 @@ +# See https://pre-commit.com for more information +# See https://pre-commit.com/hooks.html for more hooks +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.2.0 + hooks: + - id: trailing-whitespace + +- repo: local + hooks: + # Run astyle over the staged files with c and h extension found in the directories + # listed in the files regex pattern. Ignoring the files in the exclude pattern. + - id: format-source + name: Formatting source files + entry: astyle --options=scripts/code-format.cfg --ignore-exclude-errors + stages: [ commit ] + language: system + pass_filenames: true + verbose: true + files: | + (?x)^( + src/ | + tests/src/test_cases/ + ) + exclude: | + (?x)^( + src/extra/libs/ | + src/lv_conf_internal.h + ) + types_or: ["c", "header"] \ No newline at end of file diff --git a/docs/CODING_STYLE.md b/docs/CODING_STYLE.md index f1a6c8b5fb97..d45f0fc4c1c5 100644 --- a/docs/CODING_STYLE.md +++ b/docs/CODING_STYLE.md @@ -87,3 +87,38 @@ void lv_label_set_text(lv_obj_t * label, const char * text) Use 4 spaces indentation instead of tab. You can use **astyle** to format the code. Run `code-formatter.sh` from the `scrips` folder. + +#### pre-commit + +[pre-commit](https://pre-commit.com/) is a multi-language package manager for pre-commit hooks. +See the [instalation guide](https://pre-commit.com/#installation) to get pre-commit python package +installed into your development machine. + +Once you have `pre-commit` installed you will need to [set up the git hook scripts](https://pre-commit.com/#3-install-the-git-hook-scripts) with: +```console +pre-commit install +``` + +now `pre-commit` will run automatically on `git commit`! + +##### Hooks + +The `format-source` local hook (see `.pre-commit-config.yaml`) runs **astyle** on all the staged source and header +files (that are not excluded, see `exclude` key of the hook configuration) before entering the commit message, +if any file gets formatted by **astyle** you will need to add the change to the staging area and run `git commit` again. + +The `trailing-whitespace` hook fixes trailing whitespaces on all of the files. + +##### Skipping hooks + +If you want to skip any particular hook you can do so with: +```console +SKIP=name-of-the-hook git commit +``` + +##### Testing hooks + +It's no necessary to do a commit to test the hooks, you can test hooks by adding the files into the staging area and run: +```console +pre-commit run name-of-the-hook +``` \ No newline at end of file diff --git a/scripts/code-format.cfg b/scripts/code-format.cfg index fd0ab62f62f8..a92a84962e0f 100644 --- a/scripts/code-format.cfg +++ b/scripts/code-format.cfg @@ -19,7 +19,6 @@ --max-continuation-indent=120 --mode=c --lineend=linux ---recursive --suffix=none --preserve-date --formatted diff --git a/scripts/code-format.py b/scripts/code-format.py index fef9eb66a9dd..42faee6eec18 100755 --- a/scripts/code-format.py +++ b/scripts/code-format.py @@ -2,7 +2,7 @@ import os -os.system('astyle --options=code-format.cfg "../src/*.c,*.h"') -os.system('astyle --options=code-format.cfg "../demos/*.c,*.h"') -os.system('astyle --options=code-format.cfg "../examples/*.c,*.h"') -os.system('astyle --options=code-format.cfg "../tests/src/test_cases/*.c"') +os.system('astyle --options=code-format.cfg --recursive "../src/*.c,*.h"') +os.system('astyle --options=code-format.cfg --recursive "../demos/*.c,*.h"') +os.system('astyle --options=code-format.cfg --recursive "../examples/*.c,*.h"') +os.system('astyle --options=code-format.cfg --recursive "../tests/src/test_cases/*.c"')