Skip to content

Commit

Permalink
Merge pull request #1 from krish2718/add_basic_hooks
Browse files Browse the repository at this point in the history
Initial version of pre-commit hooks
  • Loading branch information
bwt-sloanj authored Jan 10, 2024
2 parents 60eeb3e + 9ca13da commit 0937a22
Show file tree
Hide file tree
Showing 13 changed files with 9,253 additions and 1 deletion.
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

0 comments on commit 0937a22

Please sign in to comment.