Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial version of pre-commit hooks #1

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Python application

on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up Python 3.10
uses: actions/setup-python@v2
with:
python-version: '3.10'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pre-commit
pip install -e .[dev]

- name: Run pre-commit hooks
run: pre-commit run --all-files

- name: Run tests
run: |
python -m unittest discover
35 changes: 35 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: debug-statements
- id: name-tests-test
args: [--unittest]
- id: requirements-txt-fixer

- repo: https://github.com/psf/black-pre-commit-mirror
rev: 23.9.1
hooks:
- id: black
args: ["--line-length=100"]

- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
args: ["--profile", "black", "--line-length", "100"]

- repo: https://github.com/PyCQA/flake8
rev: 6.1.0
hooks:
- id: flake8
args: [--max-line-length=100]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.7.1
hooks:
- id: mypy
additional_dependencies: [types-all]
15 changes: 15 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
- id: checkpatch
name: checkpatch
entry: checkpatch
language: python
require_serial: true
stages: [pre-commit]
verbose: true

- id: checkpatch-ci
name: checkpatch
entry: checkpatch --ci
language: python
require_serial: true
stages: [manual]
verbose: true
1 change: 1 addition & 0 deletions .version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0.0
110 changes: 109 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,109 @@
# check-commit-hook
# check-commit-hook

Based on [1] this repository contains a set of BW pre-commit hooks to be run on every git commit, the same hooks are used by the CI/CD pipeline to check the files.

# Hooks

## Checkpatch

Uses Linux kernel checkpatch to check for coding style errors in C code, Makefiles, etc. It uses a custom configuration file to ignore some errors and enforce others.

### Custom configuration file

The script uses a default configuration file located in `checkpatch_hook/data/checkpatch.yaml` but it can be overriden by a custom configuration file and passed to the script using the `--config-file` option.

```
checkpatch --config-file <config_file> <file>
```

The configuration file is a YAML file with the following structure:

```yaml
DIR_CONFIGS:
<directory1>:
errors_ignored:
- <checkpatch option>
- <checkpatch option>
max_line_length: <number>
<directory2>:
errors_enabled:
- <checkpatch option>
- <checkpatch option>
max_line_length: <number>
```

Note: Either `errors_enabled` or `errors_ignored` are enforced, not both.

#### Magic keys

The configuration file supports some magic keys that make it easier to configure the script.

##### `__default____`

The `__default____` key can be used to apply the same configuration to all the files in the repository.

```yaml
__default____:
errors_ignored:
- <checkpatch option>
- <checkpatch option>
- ...
max_line_length: <number>
```

##### `ERRORS_COMMON`

The `ERRORS_ENABLED` key can be used as a placeholder for all the errors enabled, and this needs to be part of `errors_enabled` list to be used.

```yaml
ERRORS_ENABLED:
- <checkpatch option>
- <checkpatch option>
- ...

DIR_CONFIGS:
<directory>:
errors_enabled:
- ERRORS_ENABLED
- <checkpatch option>
- <checkpatch option>
- ...
max_line_length: <number>
```

##### `IGNORES_COMMON`

The `IGNORES_COMMON` key can be used as a placeholder for all the errors ignored for all the files, and this needs to be part of `errors_ignored` list to be used.

```yaml
IGNORES_COMMON:
- <checkpatch option>
- <checkpatch option>
- ...

DIR_CONFIGS:
<directory>:
errors_ignored:
- IGNORES_COMMON
- <checkpatch option>
- <checkpatch option>
max_line_length: <number>
```

# Usage

## Pre-commit

To use the hooks in your local repository you need to install pre-commit [1] and add the following to your `.pre-commit-config.yaml` file:

```yaml
- repo: https://github.com/bluwireless/check-commit-hook
rev: xxxx
hooks:
- id: checkpatch
args: [--config-file, checkpatch_custom.yaml]
```

# References

[1] - https://pre-commit.com/
Loading