Hello,
during the Kamailio Developers Meeting 2023 in Dusseldorf early this week, Victor Seva shared how one can setup own development environment for facilitating the automatic checks for clang-format and few other useful bits (e.g., training white spaces) using pre-commit tool. So I thought it would be useful to shared further to both developers and users communities (as the later can make patches and pull requests as well).
If I forgot something, Victor should amend me, but the main steps are:
- install pre-commit package (either via apt or via pip3) - install clang-format (debian/ubuntu should have it as a package) - go to the folder with git clone of Kamailio source and run:
pre-commit install
The above command can take some time, but it is needed only once.
After that, on evey commit, the checks should be performed a summary should be presented on screen, like:
$ git commit src/ check yaml...........................................(no files to check)Skipped check xml............................................(no files to check)Skipped fix end of files.........................................................Passed trim trailing whitespace.................................................Passed check for merge conflicts................................................Passed mixed line ending........................................................Passed clang-format.............................................................Passed
If the check of clang-format results in "Failed", like:
clang-format.............................................................Failed
the pre-commit hooks will correct it, so you can just run again the commit command and the 2nd time should be good to go.
If for whatsoever reasons pre-commit is installed but you need to ignore/skip the pre-commit hooks, just provide -n or --no-verify to the commit command, like:
$ git commit -n src/
By using pre-commit hooks, one could easily avoid making pull requests that are failing to meet the code formatting checks on github portal as well as ensure that as developer one does no forget to run clang-format before commit.
Cheers, Daniel