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
This commit is contained in:
Carlos Diaz 2022-02-24 11:44:39 -06:00 committed by GitHub
parent 4ed0f011f6
commit a83cae012d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 5 deletions

30
.pre-commit-config.yaml Normal file
View File

@ -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"]

View File

@ -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
```

View File

@ -19,7 +19,6 @@
--max-continuation-indent=120
--mode=c
--lineend=linux
--recursive
--suffix=none
--preserve-date
--formatted

View File

@ -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"')