Skip to content

dixahq/scala-pre-commit-hooks

 
 

Repository files navigation

pre-commit hooks for Scala

Scala has a quite rich build ecosystem, and a myriad of tools to help you writ good code. However, the build system(s) are relatively slow, so any helpers executed via those build system run the risk of interrupting the flow of a developer.

Moreover, some of the code quality tools/features (like unused import checks) are actively in opposition to a "run-often" coding approach.

Moving relevant checks to the pre-commit/pre-push phases partially alleviates those two issues.

How

This is a set of hooks defined in the excellent pre-commit library.

Currently, they include the following:

  • sbt-fatal-warnings - turns on -Xfatal-warnings, runs a clean compilation on the given scope.

  • sbt-unused-imports - as above, but also adds the "unused imports" warning.

  • scalafmt-check - runs scalafmt --mode=changed --test.

  • scalfmt-autofix - runs scalafmt --mode=changed and automatically stages the reformatted files.

Note1: All hooks except for scalafmt-* have the optional scope argument, which allows to define what source scopes are relevant for the hook’s check. The default is {defaultScope}.

Note2: scalafmt-* hooks require [scalafmt-cli to be installed](https://scalameta.org/scalafmt/docs/installation.html#cli).

Usage

To add one or more of the hooks into your repo:

  1. Have everyone on your team install pre-commit

  2. Install scalafmt-cli for scalafmt- hooks: https://scalameta.org/scalafmt/docs/installation.html#cli

  3. Add a .pre-commit-config.yaml file to your repository, with the following syntax:

    .pre-commit-config.yaml
    # See https://pre-commit.com/hooks.html for more hooks
    # See https://pre-commit.com for more information
    repos:
      - repo: https://github.com/pre-commit/pre-commit-hooks
        rev: v4.1.0
        hooks:
          - id: trailing-whitespace
          - id: end-of-file-fixer
      - repo: https://github.com/dixahq/scala-pre-commit-hooks
        hooks:
        rev: {currentVersion}
          - id: sbt-fatal-warnings
            stages: [push] # or [commit, push] etc.
          - id: scalafmt-check
  4. Run pre-commit install to apply your hooks to the repo.

How to control hook run time

By default, the hooks defined here run on both pre-commit and pre-push. They do, however, require some time to get running on large codebases, especially since each hook’s execution is essentially a clean build.

About

Pre-commit/Pre-push hooks for Scala

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 90.9%
  • Shell 9.1%