Giter Site home page Giter Site logo

pre-commit-hook-yamlfmt's Introduction

YAML formatter for pre-commit git hooks

YAML formatter for pre-commit.

This hook formats the indentation of YAML files and optionally aligns top-level colons.
It uses ruamel.yaml to do the heavy lifting and preserve comments within YAML files.

How-to

Configure pre-commit

Use defaults

Add to .pre-commit-config.yaml in your git repo:

- repo: https://github.com/jumanjihouse/pre-commit-hook-yamlfmt
  rev: 0.0.11  # or specific tag
  hooks:
      - id: yamlfmt

๐Ÿ’ก If a pre-commit hook changes a file, the hook fails with a warning that files were changed.

The default settings are:

  • mapping: 4 spaces
  • sequence: 6 spaces
  • offset: 4 spaces
  • colons: do not align top-level colons
  • width: None (use ruamel default)

Given this input:

foo:
  bar:
    - baz1
    - baz2

The default settings result in this output:

---
foo:
    bar:
        - baz1
        - baz2

Combine with yamllint

yamlfmt only works with valid YAML files, so I recommend to use yamllint and yamlfmt together.

- repo: https://github.com/adrienverge/yamllint.git
  rev: v1.21.0  # or higher tag
  hooks:
      - id: yamllint
        args: [--format, parsable, --strict]

- repo: https://github.com/jumanjihouse/pre-commit-hook-yamlfmt
  rev: 0.0.11  # or specific tag
  hooks:
      - id: yamlfmt

Override defaults

Add to .pre-commit-config.yaml in your git repo:

- repo: https://github.com/jumanjihouse/pre-commit-hook-yamlfmt
  rev: 0.0.11  # or specific tag
  hooks:
      - id: yamlfmt
        args: [--mapping, '2', --sequence, '2', --offset, '0', --colons, --width, '150']

Invoke pre-commit

On every commit

If you want to invoke the checks as a git pre-commit hook, run:

# Run on every commit.
pre-commit install

On demand

If you want to run the checks on-demand (outside of git hooks), run:

# Run on-demand.
pre-commit run --all-files

The test harness of this git repo uses this approach.

Contributing

Please see CONTRIBUTING.md.

Testing

Please see TESTING.md.

License

The code in this repo is licensed under the MIT License.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.