Skip to content

Draft: git: add pre-commit hook using clang-format-diff

Christian Taedcke requested to merge chrta/ModemManager:add-pre-commit-hook into main

Pre-Commit hook

See https://pre-commit.com/ on how to install these pre-commit hooks. On linux this can be installed with:

# install pre-commit tool
pip install pre-commit

# install hooks specified in .pre-commit-config.yaml
pre-commit install

clang-format

The current clang-format config is borrowed and adapted from !478 (comment 862387) It may be necessary to tweak this file, but this config should be aligned with the coding conventions. The result is not perfect, since it also touches code that is ok or even better without clang-format. See https://clang.llvm.org/docs/ClangFormatStyleOptions.html for available options.

To check the clang-format config, i manually ran it for this complete repo (this edits all files inline):

find . -iname "*.h" -o -iname "*.c" | xargs clang-format-14 -i -style=file

The hook currently uses clang-format-diff to only check the diff. This uses clang-format-15.

To use clang-format on the complete files that are edited in a commit, modify .pre-commit-config.yaml to include the clang-format hook.

Adding commit message checks

We could easily add https://jorisroovers.com/gitlint/ to check the format of the commit messages. gitlint works pretty good and it should be possible to ensure that the commit message follows https://modemmanager.org/docs/contribution-guidelines/git-commit-style/#basic-message-formatting

Links

For clang-format-diff integrations see https://github.com/jlebar/pre-commit-hooks

For clang-format integration see https://github.com/pre-commit/mirrors-clang-format and https://github.com/ssciwr/clang-format-wheel regarding the clang-format integration.

Edited by Christian Taedcke

Merge request reports