language-formatters-pre-commit-hooks 2.16.0
List of pre-commit hooks meant to format your source code.
License: Apache License 2.0
Requires Python: >=3.10

# Language Formatters Pre Commit Hooks

[![Github Actions CI](https://github.com/macisamuele/language-formatters-pre-commit-hooks/workflows/Build/badge.svg)](https://github.com/macisamuele/language-formatters-pre-commit-hooks/actions)
[![Coverage](https://img.shields.io/codecov/c/github/macisamuele/language-formatters-pre-commit-hooks/master.svg)](https://codecov.io/gh/macisamuele/language-formatters-pre-commit-hooks)
[![PyPi version](https://img.shields.io/pypi/v/language-formatters-pre-commit-hooks.svg)](https://pypi.python.org/pypi/language-formatters-pre-commit-hooks/)
[![Supported Python versions](https://img.shields.io/pypi/pyversions/language-formatters-pre-commit-hooks.svg)](https://pypi.python.org/pypi/language-formatters-pre-commit-hooks/)

## About

This package provides utilities for ensuring that your code is nicely formatted by using [`pre-commit`](https://pre-commit.com/) hooks

## List of pretty-format hooks

* `pretty-format-golang`
* `pretty-format-ini`
* `pretty-format-java`
* `pretty-format-kotlin`
* `pretty-format-rust`
* `pretty-format-toml` ⚠️ removes comments under certain conditions
* `pretty-format-yaml`

⚠: the list above could be out-of-sync respect the exposed pre-commit hooks.

Please refer to [`.pre-commit-hooks.yaml`](.pre-commit-hooks.yaml) for a more updated list.

## Example Usage

Add a similar snippet into your `.pre-commit-config.yaml` file

```yaml

- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
  rev: ${LATEST_SHA_OR_VERSION}
  hooks:
  - id: pretty-format-java
    args: [--autofix]
  - id: pretty-format-kotlin
    args: [--autofix]
  - id: pretty-format-yaml
    args: [--autofix, --indent, '2']
```

## Development

This tool uses tox as main tool to build virtual environments.

To get started will be enough to run `make development`.

If you have [`aactivator`](https://github.com/Yelp/aactivator) installed this step will happen automatically.

### Contributing

Contributions are _always_ welcome.

1. [Fork the project](http://github.com/macisamuele/language-formatters-pre-commit-hooks/fork)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Add your modifications
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request

## FAQ

### How to deal with different Google Java Formatter versions?

```yaml
  - repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
    rev: ...
    hooks:
      - id: pretty-format-java
        args: [--autofix, --aosp, --google-java-formatter-version=1.16.0]
```

### How to deal with multiple Java versions?

This might be relevant for `pretty-format-java` and `pretty-format-kotlin` hooks.
The hooks depends on having `java` on version **11** or greater installed on your machine.

As you're working with _compiled-to-JVM_ languages, we assume that you have `java` installed on your system. You might not have the minimum required version installed.

To work-around such scenario you have 2 approaches available:

1. Have multiple `java` versions installed on your system and ensure that while running the pre-commit hooks JRE 11+ is available on your `PATH` variable (ie. `PATH=${JRE_11_PATH}:${PATH} pre-commit run`).

2. Work around the issue by using [`docker`](https://www.docker.com/).
    ⚠: This approach has been tested (at the time of writing) on Linux and MacOS.

    The latter approach should be preferred if you cannot install an additional JRE version on your system or if doing is unfeasible (e.g. on a CI system). Please note you need to have `docker` installed on your system.

    Add the following `Dockerfile` on your repository root (same directory where `.pre-commit-config.yaml` is stored)

    ```Dockerfile
    FROM python:3.7-alpine

    # Install JRE-11 as we will run pre-commit hooks that depends an Java 11+
    RUN apk add --no-cache openjdk11-jre

    ENV PRE_COMMIT_HOME /pre-commit-docker-cache
    ENV PRE_COMMIT_LANGUAGE_FORMATTERS_VERSION ${version of the library to install}

    RUN set -x \
        && pip install --no-cache-dir language-formatters-pre-commit-hooks==${PRE_COMMIT_LANGUAGE_FORMATTERS_VERSION} \

        # Run pre-commit-hook to ensure that jars are downloaded and stored in the docker image
        # Run the hooks that you're planning to run within docker.
        # This reduces premission issues as well has makes all the run fast as the lazy-dependencies are pre-fetched
        && pretty-format-java  \

        # Update permissions as hooks will be run as your host-system user (your username) but the image is built as root
        && chmod a+r ${PRE_COMMIT_HOME}/*
    ```

    and the following hook into your `.pre-commit-config.yaml` file

    ```yaml
    repos:
    - repo: local
      hooks:
      - id: pretty-format-java-in-docker    # Useful to eventually SKIP pre-commit hooks
        name: pretty-format-java-in-docker  # This is required, put something sensible
        language: docker                    # Self explanatory
        entry: pretty-format-java           # Hook that you want to run in docker
        args: [...]                         # Arguments that would would pass to the hook (as if it was local)
        files: ^.*\.java$                   # File filter has to be added ;)
    ```

    By doing the following, the selected hook (`pretty-format-java` in the example) will be executed within the docker container.

    Side note: We're not embedding the Dockerfile in the repository as this is more a workaround to support whom cannot of installing a more recent Java version on the library-user system and as such we are not planning to fully support this other than giving possible solutions (Java 11+ was released in September, 2018).

### How to use a pre-downloaded [google-java-format jar](https://github.com/google/google-java-format) file?

You can pass the jar file path to `--google-java-formatter-jar` argument:

```yaml
  - repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
    rev: ...
    hooks:
      - id: pretty-format-java
        args: [--google-java-formatter-jar=/usr/bin/google-java-format-1.17.0-all-deps.jar]
```

### How to use a pre-downloaded [ktlint jar](https://github.com/pinterest/ktlint) file?

You can pass the jar file path to the `--ktlint-jar` argument:

```yaml
  - repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
    rev: ...
    hooks:
      - id: pretty-format-kotlin
        args: [--ktlint-jar=/usr/bin/ktlint.jar]
```

### How to use a pre-downloaded [ktfmt jar](https://github.com/facebook/ktfmt) file?

You can pass the jar file path to the `--ktfmt-jar` argument:

```yaml
  - repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
    rev: ...
    hooks:
      - id: pretty-format-kotlin
        args: [--ktfmt, --ktfmt-jar=/usr/bin/ktfmt-0.47.jar]
```

### How can I verify the checksum of the jar?

_Only supported for the `pretty-format-java` and `pretty-format-kotlin-hooks`_

Use the corresponding `[...]-checksum` argument

```yaml
  - repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
    rev: ...
    hooks:
      - id: pretty-format-java
        args: [
          --google-java-formatter-version=1.17.0,
          --formatter-jar-checksum=33068bbbdce1099982ec1171f5e202898eb35f2919cf486141e439fc6e3a4203,
        ]
```

```yaml
  - repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
    rev: ...
    hooks:
      - id: pretty-format-kotlin
        args: [
          --ktlint-version=1.2.1,
          --formatter-jar-checksum=2e28cf46c27d38076bf63beeba0bdef6a845688d6c5dccd26505ce876094eb92,
        ]
```

```yaml
  - repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
    rev: ...
    hooks:
      - id: pretty-format-kotlin
        args: [
          --ktfmt,
          --ktfmt-version=0.47,
          --formatter-jar-checksum=af61161faacd74ac56374e0b43003dbe742ddc0d6a7e2c1fe43e15415e65ffbd,
        ]
```

### How to use ktfmt instead of ktlint?

```yaml
  - repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
    rev: ...
    hooks:
      - id: pretty-format-kotlin
        args: [--ktfmt, --ktfmt-style=google]
```

Supported styles are google (default), dropbox and kotlinlang

## License

`language-formatters-pre-commit-hooks` is licensed with [`Apache License version 2.0`](http://www.apache.org/licenses/LICENSE-2.0.html).

Changelog
=========

2.16.0 (2026-01-11)
-------------------
- Drop Python 3.8 support, add Python 3.11, 3.13 and 3.14 support
- Update GoogleJavaFormatter to 1.33.0
- Update ktfmt to version 0.61
- Update ktlint to version 1.8.0
- Update palantir to version 2.83.0
- [internal] Add Dependabot configuration for automated dependency updates (GitHub Actions and pip)
- [internal] Add auto-merge workflow for Dependabot PRs
- [internal] Update pre-commit hooks, github actions, tox and mypy configurations
- [internal] Apply pyupgrade modernizations (remove encoding pragmas, use modern Python syntax)

2.15.0 (2025-06-25)
-------------------
- Update GoogleJavaFormatter to 1.27.0
- Update ktfmt to version 0.55
- Update ktlint to version 1.6.0
- Update palantir to version 2.67.0
- Enhance documentation to highlight that TOML formatting might be removing comments. [PR #249](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/249) - [@maresb](https://github.com/maresb) - [@corneliusroemer](httos://github.com/corneliusroemer)
- Improve `pretty-format-rust` to better manage workspaces [PR #263](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/263) - [@GUIpsp](https://githib.com/GUIpsp)

2.14.0 (2024-07-14)
-------------------
- Update GoogleJavaFormatter to 1.22.0
- Update ktfmt to version 0.51
  **NOTE**: This updates removes dropbbox style formatting ([here](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/235/commits/0ba8757d94104c62f2431160f3316e51203bd60d)).
- Update KTLint to version 1.3.1
- [Kotlin] Improved support for ktfmt - [PR #224](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/224) - [@mxr](https://github.com/mxr)
- [Java/Kotlin] Enable checksum verification of download artifacts - [PR #222](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/222) - [@mxr](https://github.com/mxr)
- [Rust] Add support for non-root cargo projects - [PR #217](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/217) - [@AJIOB](https://github.com/AJIOB)
- [Java] Add support for palantir-cli - [PR #213](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/213) - [@eirnym](https://github.com/eirnym)
- [internal] Update pre-commit hooks, github actions and removed deprecated usage of `pkg_resources`

2.13.0 (2024-03-31)
-------------------

- Update KTLint to version 1.2.1
- Update GoogleJavaFormatter to version 1.21.0
- Ensure that TOML processing assumes that the files have UTF-8 encoding - [PR #208](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/208) - [@Jayman2000](https://github.com/Jayman2000) thanks for your contribution
- Pretty TOML allows for customisation of inline-comment-space - [PR #205](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/205) - [@alkatar21](https://github.com/alkatar21) thanks for your contribution
- [internal] Update pre-commit hooks, github actions

2.12.0 (2023-12-19)
-------------------

- Add support for [ktfmt](https://github.com/facebook/ktfmt) Kotlin code formatter - [PR #196](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/196) - [@aandres](https://github.com/aandres) thanks for your contribution
- Update KTLint to version 1.0.1

2.11.0 (2023-10-11)
-------------------

- Update KTLint to version 1.0.0
- Update GoogleJavaFormatter to 1.18.1
- Make library compatible with Python 3.12 - [PR #182](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/182) - [@Delgan](https://github.com/Delgan) thanks for your contribution

2.10.0 (2023-07-13)
------------------

- Add `--indent` and `--trailing-commas` arguments for `pretty-format-toml` - [PR #160](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/143) - [@maresb](https://github.com/maresb) thanks for your contribution
- Improve `pretty-format-kotlin` interaction with `ktlint` to prevent attempts to format not kotlin files, [(Issue #162)](https://github.com/macisamuele/language-formatters-pre-commit-hooks/issues/162) - [PR #163](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/163) - [@languitar](https://github.com/languitar) thanks for your contribution
- Improve `pretty-format-kotlin` to reduce log verbosity - [PR #177](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/177) - [@Velfi](https://github.com/Velfi) thanks for your contribution
- Allow `pretty-format-java` and `pretty-format-kotlin` to leverage pre downloaded JAR instead of fetching it - [PR #156](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/156) / [PR #???](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/156) - [@fabasoad](https://github.com/fabasoad) thanks for your contribution
- Update KTLint to 0.50.0


2.9.0 (2023-05-13)
------------------

- Update GoogleJavaFormatter to 1.17.0
- Update KTLint to 0.49.1
- Bug fix pretty-format-yaml
  Sequecence item indentation should condider offset as part of the indentation, [#154 (comment)](https://github.com/macisamuele/language-formatters-pre-commit-hooks/issues/154#issuecomment-1546778156) has more details.
  Thanks [@datalogics-kam](https://github.com/datalogics-kam) and [@fmigneault](https://github.com/fmigneault) for reporting the issue and helping me identify the underlying root cause.

2.8.0 (2023-03-17)
------------------

- Update GoogleJavaFormatter to 1.16.0

2.7.0 (2023-02-18)
------------------

- Add support for customisable offset in `pretty-format-yaml` - [PR #143](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/143)
- Update KTLint to 0.48.2

2.6.0 (2023-01-20)
------------------

- Fix `pretty-format-toml` to be compatible with latest `toml-sort` libraries - Thanks [@liblaf](https://github.com/liblaf) and [@stewartHutchins](https://github.com/stewartHutchins) for the support on having toml prettification working again
  The fix has been carried over multiple PRs ([PR #134](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/134), [PR #136](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/136) and [PR #137](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/137)).
- Internal build fix (failures caused by `tox` major release) - [PR #141](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/141), inspired from [PR #135](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/135) - Thanks [@malmans2](https://github.com/malmans2) for the support
- Update KTlint to 0.48.1 - [PR #140](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/140) - Thanks [@detouched](https://github.com/detouched) for the upgrade

2.5.0 (2022-12-05)
------------------

- Lift JDK 16+ restriction - [PR #123](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/123) - [@harti2006](https://github.com/harti2006) thanks for your contribution
- Update KTlint to 0.47.1 - [PR #125](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/125)
- pretty_format_rust does no longer use explicit rust versions - [PR #126](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/126)

2.4.0 (2022-07-01)
------------------

- Update GoogleJavaFormatter to 1.15.0
- Update KTlint to 0.45.1
- Ensure Python 3.10 support and drop Python3.6 guaranteed support - [PR #114](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/114) / [PR #115](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/115)
- Updated prettifier library for INI files (from `configobj` to `config_formatter`) to provide more deterministic output and proper comments handling - [PR #113](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/113) - [@Delgan](https://github.com/Delgan) thanks for your contribution
- `pretty-format-yaml` allows customization of max line length - [PR #104](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/104)
- More explicit error messages in case of prettifier failires - [PR #116](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/116)
- Use explicit encoding within INI prettifier - [PR #102](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/102) - [@hbre](https://github.com/hbre) thanks for your contribution

2.3.0 (2022-02-17)
------------------

- Update GoogleJavaFormatter to 1.14.0
- Update KTlint to 0.44.0
- Use explicit encoding within YAML prettifier - [PR #92](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/92) - [@passionsfrucht](https://github.com/passionsfrucht) thanks for your contribution

2.2.0 (2021-08-08)
------------------

- Make download of external artifacts resilient to systems with temporary directory on different disk partitions - [@psmit](https://github.com/psmit) and [@kbalston](https://github.com/kbalston) thanks for your contribution
- Make usage of Google Java Formatter compatible with JDK16+ - [@ostrya](https://github.com/ostrya) thanks for your contribution
- Update GoogleJavaFormatter to 1.11.0
- Bump KTlint to 0.42.1
- Misc github workflow updates (testing on Python 3.9, better tracking of tool versions tested, etc.)
- Improved error message in case of Google Java Formatter and KTLint not supported Java Version

ℹ: `pretty-format-java` now supports Java 16+
⚠: `pretty-format-kotlin` supports Java up to Java 15

2.1.0 (2021-05-28)
------------------

- Bump KTlint to 0.40.0
- Update GoogleJavaFormatter to 1.10.0

2.0.0 (2021-01-16)
------------------

- Preserve comments in while formatting `ini` files. [PR #45](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/45) - [@Skylion007](https://github.com/Skylion007) thanks for your contribution
- Preserve comments in while formatting `toml` files. [PR #46](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/46) - [@Skylion007](https://github.com/Skylion007) thanks for your contribution
- ⚠ Drop Python2 support. [PR #48](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/48)
- Update KTLint to 0.40.0

1.6.1 (2020-10-31)
-----------------

- Internal fix of downloaded files path. [PR #43](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/43)

1.6.0 (2020-10-24)
------------------

- Update KTLint to 0.39.0
- Update GoogleJavaFormatter to 1.9
- Run `pretty-format-java` serially to prevent multiple-downloads of the same Java artifact. [PR #23](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/23) - [@ineiti](https://github.com/ineiti) thanks for your contribution
- Internal update of download logic to reduce race coditions while download big artifacts from network. [PR #24](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/24)
- Bump min `pre-commit` supported version. [PR #27](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/27)
- Allow `pretty-format-java` to modify the Google Java Formatter to use (`--google-java-formatter-version` CLI argument). [PR #30](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/30)
- Allow `pretty-format-kotlin` to modify the KTLint to use (`--ktlint-version` CLI argument). [PR #30](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/30)
- Enhance security in commands execution (prevent shell-injection). [PR #38](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/38)

1.5.0 (2020-06-16)
------------------

- Add `--preserve-quotes` argument into `pretty-format-yaml`. [PR #16](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/16) - [@vbisserie](https://github.com/vbisserie) thanks for your contribution

1.4.2 (2020-06-09)
------------------

- Update KTLint to 0.37.1

1.4.1 (2020-06-03)
------------------

- Update KTLint to 0.37.0

1.4.0 (2020-05-20)
------------------

- Improve handling of multi-document YAML files. [PR #3](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/3) - [@dan-cohn](https://github.com/dan-cohn) thanks for your contribution
- `pretty-format-java` does default to Google style. Add `--aosp` argument for Android Open Source Project style. [PR #8](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/8) - [@ChenAndrew](https://github.com/ChenAndrew) thanks for your contribution.
- Update GoogleJavaFormatter to 1.8

1.3.2 (2020-01-25)
------------------

- Definitive packaging fix

1.3.1 (2020-01-24)
------------------

- Update Packaging informations

:warning: This version **broke module retrieval** (:disappointed:) while improving quality of PyPi uploaded information. You're recommended to use a more recent version of the library.

1.3.0 (2020-01-24)
------------------

- Update KTLint to 0.36.0
- Enhange `pretty-format-yaml` to deal with YAML files containing primitive types only - [PR #1](https://github.com/macisamuele/language-formatters-pre-commit-hooks/pull/1) - [@dan-cohn](https://github.com/dan-cohn) thanks for your contribution

1.2.5 (2019-11-22)
------------------

- Update KTLint to 0.35.0

1.2.4 (2019-07-19)
------------------

- Update KTLint to 0.34.0 and fix KTLint GitHub link

1.2.3 (2019-02-14)
------------------

- Update Google Java Formatter to 1.7 and KTlint to 0.30.0

1.2.2 (2018-11-20)
------------------

- pretty-format-rust fails if ``cargo fmt`` fails

1.2.1 (2018-11-20)
------------------

- no-diff release

1.2.0 (2018-11-20)
------------------

- Bump KTlint to 0.29.0
- Remove duplicated filenames from command execution

1.1.3 (2018-09-02)
------------------

- Last fix to cargo invocations to use the environmentally defined toolchain

1.1.2 (2018-09-02)
------------------

- Bump KTlint to 0.27.0

1.1.1 (2018-09-02)
------------------

- Ensure that generated files end with a new line
- Allow rust toolchain customization via `RUST_TOOLCHAIN` environment variable

1.1.0 (2018-07-29)
------------------

- Add pretty formatters for INI, Rust and TOML files

1.0.1 (2018-07-20)
------------------

- Improve detection of modified files from kotlin formatter

1.0.0 (2018-07-20)
------------------

- Initial release: added pretty formatters for Golang, Java, Kotlin and YAML