osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Never get spotless errors with this one weird trick


Hi all,

Scott just separated the spotless check from the Java unit test precommit job, so you get faster feedback on spotless errors.

I wondered if there was a good place to just always reformat, and whether it was fast enough to be OK. The answer is yes, and yes.

You can set up a git precommit hook to always autoformat code, by putting this in .git/hooks/pre-commit and setting the executable bit.

    #!/bin/sh
    set -e
    ./gradlew spotlessApply

If you haven't used git hooks, the docs are here: https://git-scm.com/docs/githooks. I'll call out that --no-verify will skip it and `chmod u-x` will disable it.

Then testing the time:

 - From a fresh checkout ./gradlew spotlessJavaApply took 24s configuration and 49s spotlessApply
 - Then I modified one file in nexmark, messed up the formatting, and committed
 - The re-run took 1s in configuration and 4s in spotlessApply

So this will add ~5s of waiting each time you `git commit`. You can decide if it is worth it to you. If you are a "push a bunch of commits to be squashed" GitHub user, you could amortize it by making it a pre-push hook that adds a spotless commit (`git commit --fixup HEAD`).

Kenn