Skip to content

Commit

Permalink
feat(script): add pre-commit configuration for code formatting (lvgl#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
C47D authored Feb 24, 2022
1 parent 4ed0f01 commit a83cae0
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
30 changes: 30 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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"]
35 changes: 35 additions & 0 deletions docs/CODING_STYLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
1 change: 0 additions & 1 deletion scripts/code-format.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
--max-continuation-indent=120
--mode=c
--lineend=linux
--recursive
--suffix=none
--preserve-date
--formatted
Expand Down
8 changes: 4 additions & 4 deletions scripts/code-format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"')

0 comments on commit a83cae0

Please sign in to comment.