4 ms·
How do you enforce the use of a formatter in a project -- pre-commit hook? pre-receive hook? both? And is there a convention to put a specific dotfile at the pr
by roma1n 7y ago
How do you enforce the use of a formatter in a project -- pre-commit hook? pre-receive hook? both? And is there a convention to put a specific dotfile at the project root to hint at your IDE/editor that it should use a certain tool for automatic formatting?
- weberc2 7y agoWe are using a pre-commit hook but I also have an on-save hook in my editor. Further, we also have a CI job that runs `black --check` which returns 1 (failing the build) if the files weren't completely formatted with black. In Go, the convention is that everyone's editor runs gofmt on change, and because the convention is so strong, that's usually enough (although many larger projects also have a CI job similar to the one described above). Having used both extensively, my biggest grievances (in order) are that there aren't more editors that with good on-save support, that black is relatively slow (compared to gofmt), and that black isn't more opinionated and less configurable.
- im_new_here 7y agoCI runs a linter and fails the build if the output is non-empty. PRs can't be merged unless the build passes on the branch.
- raverbashing 7y agoYeah no because this changes the code and does not merely complain
- IanCal 7y agoOne, that shouldn't matter on CI anyway. Two, you can tell black to just print the difference with --diff and tell it to just check the files with --check.
- raverbashing 7y agoYeah, true, though ideally you would do it before the commit so you wouldn't have one extra lint commit per code commit
- theptip 7y agoI run this as a job in my build pipeline: black --diff black --check The first one shows you what's different (so you have logs in your build job), the second one fails your job if there are any diffs. You could just do the second one if you're a fan of the whole brevity thing.
- misiti3780 7y agoI use the format on save for black + prettier.
- acdha 7y agoWe use pre-commit.com – configure your project with dotfiles for various tools so things like editor auto-format & lint, pre-commit, etc. can pick them up, and then have a Git pre-commit hook which is also run by your CI tool for consistency. Here’s a reference: https://github.com/LibraryOfCongress/coding-standards https://github.com/LibraryOfCongress/coding-standards The CI check can be pretty simple — for example: https://github.com/LibraryOfCongress/concordia/blob/2f813f1813e69e24a7fe2230ccb945661ec55951/.travis.yml#L36 https://github.com/LibraryOfCongress/concordia/blob/2f813f18...
- coolaj86 7y agoI recommend the server-side `update` hook (much like `pre-receive`, but a little easier to use and more flexible). Here's an example with Prettier: https://coolaj86.com/articles/server-side-git-hooks-for-code-formatting/ https://coolaj86.com/articles/server-side-git-hooks-for-code...