setup

install

$ python -m pip install --user pipx
$ pipx ensurepath
$ pipx install pre-commit

# or
$ brew install --HEAD pre-commit

run

# -- all files --
$ pre-commit run --all-files

# -- all files with specific hook --
$ pre-commit run <hook_id> --all-files
# i.e.:
$ pre-commit run trailing-whitespace --all-files

migrate-config

$ pre-commit migrate-config

hooks

[!NOTE|labels:reference:]

# yamllint disable rule:indentation
---
repos:
  - repo: https://github.com/marslo/cr-manager
    rev: v0.0.4
    hooks:
      - id: update-copyright
        args: ["--update"]

checker and fixer

# yamllint disable rule:indentation
---
repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v5.0.0
    hooks:
      - id: trailing-whitespace
        name: Trim Trailing Whitespace
      - id: end-of-file-fixer
        name: End Of File Fixer
      - id: check-yaml
        name: Check YAML
        args: ["--unsafe"]
      - id: check-json
        name: Check JSON
      - id: check-merge-conflict
        name: Check Merge Conflict
      - id: check-case-conflict
        name: Check Case Conflict
      - id: mixed-line-ending
        name: Mixed Line Ending
        args: ["--fix=lf"]

mixed-line-ending

--fix OPTIONS COMMENTS
auto auto-detect line-ending
no no change line-ending
cr force use \r(legacy Mac)
crlf force use \r\n(Windows)
lf force use \n(Unix/Linux/macOS)

convert tab to spaces

  • expand + sponge

    # yamllint disable rule:indentation
    ---
    repos:
      - repo: local
        hooks:
          - id: tab-to-space
            name: Convert Tabs to 2 Spaces
            entry: bash -c 'expand -t 2 "$@" | sponge "$@"' --
            language: system
            types: [text]
            exclude: \.(py|groovy|jenkinsfile/.*)$
    
          - id: tab-to-4-spaces
            name: Convert Tabs to 4 Spaces
            entry: bash -c 'expand -t 4 "$@" | sponge "$@"' --
            language: system
            files: \.py$
    
          - id: tab-to-2-spaces
            name: Convert Tabs to 2 Spaces
            entry: bash -c 'expand -t 2 "$@" | sponge "$@"' --
            language: system
            files: (\.groovy$|jenkinsfile/.*)
    
  • python solution

    tab_converter.py
    import argparse
    import fileinput
    
    def convert_tabs(file_path, spaces):
        with fileinput.FileInput(file_path, inplace=True) as file:
            for line in file:
                print(line.expandtabs(spaces), end='')
    
    if __name__ == '__main__':
        parser = argparse.ArgumentParser()
        parser.add_argument('--spaces', type=int, required=True)
        parser.add_argument('files', nargs='*')
        args = parser.parse_args()
    
        for file in args.files:
            convert_tabs(file, args.spaces)
    
    repos:
      - repo: local
        hooks:
          - id: tab-to-space
            name: Convert Tabs to 2 Spaces [ DEFAULT ]
            entry: python tab_converter.py --spaces 2
            language: system
            types: [text]
            exclude: \.(py|groovy)$
            pass_filenames: true
    
          - id: tab-to-4-spaces
            name: Convert Tabs to 4 Spaces
            entry: python tab_converter.py --spaces 4
            language: system
            files: \.py$
            pass_filenames: true
    
          - id: tab-to-2-spaces
            name: Convert Tabs to 2 Spaces
            entry: python tab_converter.py --spaces 2
            language: system
            files: (\.groovy$|jenkinsfile/.*)
            pass_filenames: true
    

typos

# yamllint disable rule:indentation
---
repos:
  - repo: https://github.com/crate-ci/typos
    rev: v1.31.1
    hooks:
        - id: typos
          name: Typos
          description: Finds and corrects spelling mistakes among source code.
          exclude: \.git
Copyright © marslo 2020-2024 all right reserved,powered by GitbookLast Modified: 2025-04-25 15:47:26

results matching ""

    No results matching ""