Pre-commit hooks are fundamentally broken

(jyn.dev)

49 points | by todsacerdoti 6 hours ago

10 comments

  • thomashabets2 33 minutes ago
    I feel like I found better git commands for this, that don't have these problems. It's not perfect, sure, but works for me.

    The pre commit script (https://github.com/ThomasHabets/rustradio/blob/main/extra/pr...) triggers my executor which sets up the pre commit environment like so: https://github.com/ThomasHabets/rustradio/blob/main/tickbox/...

    I run this on every commit. Sure, I have probably gone overboard, but it has prevented problems, and I may be too picky about not having a broken HEAD. But if you want to contribute, you don't have to run any pre commit. It'll run on every PR too.

    I don't send myself PRs, so this works for me.

    Of course I always welcome suggestions and critique on how to improve my workflow.

    And least nothing is stateful (well, it caches build artefacts), and aside from "cargo deny" no external deps.

    • 000ooo000 25 minutes ago
      Only a minor suggestion: git worktrees is a semi-recent addition that may be nicer than your git archive setup
  • conradludgate 9 minutes ago
    I've worked in several projects where running the tests locally automatically install pre-commit hooks and I've wanted to commit warcrimes because of it.

    Don't do that, just dont.

  • Simplita 1 hour ago
    I’ve seen similar issues once hooks start doing more than fast checks. The moment they become stateful or depend on external context, they stop being guardrails and start being a source of friction. In practice, keeping them boring and deterministic seems to matter more than catching everything early.
  • nrclark 1 hour ago
    This was a really interesting read. I'd highly recommend it for anybody who's setting up (or currently maintains) a pre-commit workflow for their developers.

    I want to add one other note: in any large organization, some developers will use tools in ways nobody can predict. This includes Git. Don't try to force any particular workflow, including mandatory or automatically-enabled hooks.

    Instead, put what you want in an optional pre-push hook and also put it into an early CI/CD step for your pull request checker. You'll get the same end result but your fussiest developers will be happier.

    • Mic92 1 hour ago
      I can second that. If there are multiple commits: https://github.com/tummychow/git-absorb is handy to add formatting changes into the right commit after commits already happened.
    • eru 28 minutes ago
      > This includes Git. Don't try to force any particular workflow, including mandatory or automatically-enabled hooks.

      And with git, you can even make anything that happens on the dev machines mandatory.

      Anything you want to be mandatory needs to go into your CI. Pre-commit and pre-push hooks are just there to lower CI churn, not to guarantee anything.

      (With the exception of people accidentally pushing secrets. The CI is too late for that, and a pre-push hook is a good idea.)

      • normie3000 22 minutes ago
        > with git, you can even make anything that happens on the dev machines mandatory

        s/can/can't?

  • wolfi1 3 minutes ago
    why do people rebase so often? shouldn't it be excluded from the usual workflows as you are losing commit history as well?
    • hbogert 1 minute ago
      why would you lose commit history? You are just picking up a set of commits and reapplying them. Of course you can use rebase for more things, but rebase does not equal losing commit history.
  • lemonlime227 24 minutes ago
    To bring up jujutsu, `jj fix` (https://docs.jj-vcs.dev/latest/cli-reference/#jj-fix) is a more refined way of ensuring formatting in commits. It runs a formatting command with the diff in stdin and uses the results printed to stdout. It can simplify merges and rebases history to ensure all your commits remain formatted (so if you enable a new formatting option, it can remove the need for a special format/style fix commit in your mutable set). Hard to go back to pre-commit hooks after using jj fix (also hard to use git after using jj ;) ).
    • conradludgate 13 minutes ago
      The downside currently (although I've been assured this will be fixed one day) is that it doesn't support running static analysis over each commit you want to fix.

      My git rebase workflow often involves running `git rebase -x "cargo clippy -- --deny=warnings"`. This needs a full checkout to work and not just a single file input

    • dbt00 18 minutes ago
      Came here to mention jj fix. It is a fundamentally more elegant way of doing things.
  • 000ooo000 29 minutes ago
    Your hook can't observe a simple env var, if you are stepping off the happy path of your workflow? E.g. `GIT_HOOK_BYEBYE` = early return in hook script. Article seems a little dramatic. If you write a pre-commit hook that is a pain in your own arse, how does that make them fundamentally broken?
  • tkzed49 1 hour ago
    Thank you. I don't need to "fix" a commit before it ends up on a remote branch. Sometimes I expect a commit to pass checks and sometimes I don't. Frankly, don't even run pre-push hooks. Just run the checks in CI when I push. You'd better be doing that anyway before I'm allowed to push to a production branch, so stop breaking my git workflows and save me the time of running duplicate checks locally.

    Also, if most developers are using one editor, configure that editor to run format and auto-fix lint errors. That probably cleans up the majority of unexpected CI failures.

    • eru 26 minutes ago
      Pre-commit and pre-push hooks are something developers can voluntarily add (or enable) to shorten the latency until they get feedback: instead of the CI rejecting their PR, they can (optionally!) get a local message about it.

      Otherwise, I agree, your project can not rely on any checks running on the dev machine with git.

  • nine_k 34 minutes ago
    A bit less enraged: pre-commit hooks should be pure functions. They must not mutate the files being committed. At best, they should generate a report. At worst, they could reject a commit (e.g. if it contains a private key file included by mistake).
    • Ferret7446 5 minutes ago
      In my experience pre-commit hooks are most often used to generate a starting commit message.

      To put it more bluntly, pre-commit hooks are pre-commit hooks, exactly what it says on the tin. Not linting hooks or checking hooks or content filters. Depending on what exactly you want to do, they may or may not be the best tool for the job.

      To put it even more bluntly, if you are trying to enforce proper formatting, pre-commit hooks are absolutely the wrong tool for the job, as hooks are trivially bypassable, and not shared when cloning a repo, by design.

    • normie3000 20 minutes ago
      > e.g. if it contains a private key file included by mistake

      Thanks - this is the first example of a pre-commit hook that I can see value in.

  • badgersnake 1 hour ago
    Yep, all that and they’re also annoying. Version control tools are not supposed to argue - do what you’re told. If I messed up, the branch build will tell me.
    • thomashabets2 29 minutes ago
      Is that the difference between forced pre commits vs opt in? I don't want to commit something that doesn't build. If nothing else it makes future bisects annoying.

      But if I intend to squash and merge, then who cares about intermediate state.

      • normie3000 18 minutes ago
        > I don't want to commit something that doesn't build.

        This is a really interesting perspective. Personally I commit code that will fail the build multiple times per day. I only care that something builds at the point it gets merged to master.