https://github.com/macisamuele/language-formatters-pre-commit-hooks
Collection of custom pre-commit hooks.
https://github.com/macisamuele/language-formatters-pre-commit-hooks
formatting pre-commit
Last synced: 6 months ago
JSON representation
Collection of custom pre-commit hooks.
- Host: GitHub
- URL: https://github.com/macisamuele/language-formatters-pre-commit-hooks
- Owner: macisamuele
- License: apache-2.0
- Created: 2018-07-15T15:55:49.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2025-03-29T01:22:19.000Z (6 months ago)
- Last Synced: 2025-04-01T08:42:51.773Z (6 months ago)
- Topics: formatting, pre-commit
- Language: Python
- Size: 394 KB
- Stars: 125
- Watchers: 6
- Forks: 62
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Language Formatters Pre Commit Hooks
[](https://github.com/macisamuele/language-formatters-pre-commit-hooks/actions)
[](https://codecov.io/gh/macisamuele/language-formatters-pre-commit-hooks)
[](https://pypi.python.org/pypi/language-formatters-pre-commit-hooks/)
[](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`
* `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-jreENV 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).