Pre-commit and python virtual environments

Pre-commit https://pre-commit.com/ provides a framework for configuring and running various lint checks, like clang-format, cmake-format, git-lint, and so forth, either ad hoc, as part of CI, or as actual git hooks – the original reason for the project. It grew out of the python ecosystem and community, but has very good support for many C++ tools. After all, setup.py is one of the most common C++ build systems.
The existence of a python3 doesn’t seem like a huge ask, configuring a local venv is a bit higher, but fairly straightforward.
Carbon-lang has been using pre-commit for a long time now, and has reported being happy with it. It helps keep people on the same versions of linting tools, and makes updates consistent for everyone.
Having a python virtual env also can make other tools, such as gcovr, available.
I’ve started setting it up at GitHub - steve-downey/expected: std::expected implementation for further work on the lint branch where I’m starting work towards a std::expected<T&, E> - little work there yet in code. Although I’m seeing ways of organizing things so the synopsis and summaries are more cut/paste than they are now.

pre-commit can also be run as a github action and report lint checks in a PR on github. This would mean that contributors, who don’t want to setup python3 + virtual environment locally, could also benefit from automated linter checks.

It also looks like it’s reasonably straightforward to make sure the checks are done the same way in a GH action as well as locally. It’s otherwise a frustrating problem trying to appease the check that is subtly different than what you have in your environment.

1 Like

I’ve used pre-commit before and have been generally pleased with it.