6 ms·
Pre-commit: framework for managing/maintaining multi-language pre-commit hooks
- maleldil 5y agoThis looks very interesting, but I have a question: how to deal with multiple independent projects in the same repo? Say I have projects A, B, and C, and I made a change to a file in C. How can I make mypy run inside C only? The command would be `mypy C` from the repo root, but how can I configure pre-commit to do that? This question also applies to GitHub Actions, which is something I'd like to implement too.
- mwt 5y agoYou can point a hook towards a specific path, i.e. adding `files: ^C` to a hook will cause it to only run on the path of project/path C. There's also `exclude: ` to do to the opposite and other ways to customize each hook with granularity. https://pre-commit.com/#pre-commit-configyaml---hooks https://pre-commit.com/#pre-commit-configyaml---hooks
- maleldil 5y agoThis works, thanks. I had to set up one mypy hook for each project, however. Is there a better way?
- timost 5y agoOne obvious but often forgotten side effect of using pre-commit is that you need to be within a valid git repository with git installed.
- l0b0 5y agoMy favourite pre-commit feature is how it'll stash any unstaged changes before running checks when committing. No more irrelevant linter errors when committing part of a change. (And yes, it does the right thing when doing `commit -a`.) My least favourite bug is that it doesn't always play nicely with NixOS [1], and the maintainer locked me out of the issue for pointing it out. [1] https://github.com/pre-commit/pre-commit/pull/1938 https://github.com/pre-commit/pre-commit/pull/1938
- chriswarbo 5y ago> My least favourite bug is that it doesn't always play nicely with NixOS [1], and the maintainer locked me out of the issue for pointing it out. Oof. Does this solve the problem https://github.com/cachix/pre-commit-hooks.nix https://github.com/cachix/pre-commit-hooks.nix (using Nix to manage dependencies)? I was looking at pre-commit the other day, and wanted to incorporate it into the Nix setup of my projects.
- l0b0 5y agoThank you for the tip. The issue is that I want the project to be usable by people who are not using Nix. IIUC I'd have to duplicate the configuration to do that with pre-commit-hooks.nix. Is that correct?
- chriswarbo 5y agoNot sure (I still haven't used it!), but I wouldn't be surprised :(
- EdwardDiego 5y agoThat thorn in the side bit was a little OTT. He's doing this for free, yeah?
- l0b0 5y agoDid you actually read the comment thread? My comment: > NixOS isn't broken. I'm running into the same problem (link above). The response: > if you don't have a functioning ld or libstdc++ that's on you, sorry So they basically consider the way NixOS works to be "broken", and then go out of their way to block me from responding further to the discussion. I've been maintaining and contributing to FOSS for many years, and this is one of the most rude responses I've seen so far.
- l0b0 5y agoIf you're worried about reproducible builds, make sure to use a commit ID for the `rev` property rather than a (movable) tag, optionally with a tag comment: rev: 148ec47877499e3d671f6366f9eed812db181b40 # v1.2.0
- jokull 5y agoI suggest only doing pre-commit stuff for things that run really fast. Don't put your whole test suite in there.
- LittlePeter 5y agoWhatever test runs during pre-commit must also run during normal CI/CD run. In case of Python: black formatter. It must run during normal CI/CD because pre-commit hooks can be skipped. So now I have two different black calls: in the pre-commit hook and in the CI/CD. And they must be of the same version. Ad infinitum for all other tests. This is the reason I don't use pre-commit framework. It leads to "double accounting". I have to sync the pre-commit tests and the CI/CD tests. Or am I missing something? Can the framework run off the venv dir?
- woodruffw 5y agoThis is why I stuff everything into a top-level Makefile with `.PHONY` rules instead. Nearly every developer knows how to invoke `make` and already has tab completion for `make` rules, to boot. The CI in turn calls `make` as well, so there's no duplication. For example: https://github.com/trailofbits/pip-audit/blob/main/Makefile https://github.com/trailofbits/pip-audit/blob/main/Makefile
- joshuamorton 5y agoIt seems like you're still assuming the hard part, ensuring that isort etc. Are installed and have the same configuration (.rc files).
- woodruffw 5y agoNope. The `make dev` target does that, and the configuration is checked in. You might have missed it, since most of it is embedded in the "pyproject.toml" file.
- OakNinja 5y agoThe no-brainer basic usage of any pre-commit hook is to make sure you don’t waste minutes while waiting for your build to finish and then end up with a failure because you don’t have a new line at the end of the file. So, adding lint(ers). Making sure every commit have a ticket reference (and auto add it from the branch name if possible) is a fantastic convenience that also avoids commits referencing the wrong ticket. You can also have a sanity check on what branches you don’t want to cowboy commit on unless you deliberately want to, like the main branch. Black is a tool that is better to run on save. The key to a good pre-commit hook is that it has to be _fast_ otherwise parts of the team will stop using it.
- xenator 5y agoI use it on every day basis for most of my projects. It really helps during onboarding of new team members. Before I need to expain code styles, etc. With pre-commits it simplifies whole process. Ofc, as for every tool there is good and bad practice. For example, don't put long running tasks (eg. tests) into the pipeline. Things like code formatters, linter or import sorter is just simplify coders life.
- mellosouls 5y agoOT: the creator has a fun live-coding and chat twitch channel worth checking out. https://www.twitch.tv/anthonywritescode https://www.twitch.tv/anthonywritescode
- pyuser583 5y agoA very nice tool, but its important to learn the details. For example, how to require multiple args in one command/one arg in multiple command. Also, I’ve found it very helpful to always have command run from local venv, rather than remote downloads. Downloading can lead to import errors. Overall, a very, very nice tool, but one you have to put some effort into learning.
- autarch 5y agoI wrote a blog post comparing tools in this family a while back that may be of interest - https://blog.urth.org/2020/05/08/comparing-code-quality-meta-tools/ https://blog.urth.org/2020/05/08/comparing-code-quality-meta...