📌
ibook
  • README
  • cheatsheet
    • bash
      • builtin
      • syntactic sugar
      • cmd
      • havefun
    • text-processing
      • awk
      • sed
      • html
      • json
      • regex
      • unicode
    • osx
    • curl
    • tricky
    • widget
    • proxy
    • colors
    • math
    • media
    • ssl
      • keystore
      • verification
      • server
      • client
      • tricky
    • windows
      • powershell
      • choco
      • wsl
      • wt
      • shortcut
      • clsid
      • env
      • shell:folder
  • vim
    • nvim
    • install
    • color
    • plugins
      • usage
      • other plugins
      • deprecated
    • tricky
    • viml
    • windows
    • troubleshooting
  • devops
    • admin tools
    • ssh
    • git
      • config
      • alias
      • submodule
      • eol
      • example
      • gerrit
        • gerrit API
      • github
      • troubleshooting
      • tricky
      • statistics
    • pre-commit
    • release-tools
    • tmux
      • cheatsheet
    • ansible
    • vault
    • artifactory
      • api
      • cli
      • aql
      • nginx cert
    • klocwork
      • kwadmin
      • kwserver
      • api
      • q&a
    • elk
    • mongodb
    • android
    • mobile
  • jenkins
    • config
      • windows
    • appearance
    • troubleshooting
    • jenkinsfile
      • utility
      • parallel
      • build
      • envvar
      • properties
      • trigger
      • node
    • script
      • job
      • build
      • stage
      • agent
      • security & authorization
      • exception
      • monitor
      • tricky
    • api
      • blueocean
    • cli
    • plugins
      • kubernetes
      • docker
      • shared-libs
      • lockable-resource
      • ansicolor
      • badge
      • groovy-postbuild
      • simple-theme
      • customizable-header
      • artifactory
      • jira-steps
      • job-dsl
      • build-timeline
      • crumbIssuer
      • coverage
      • uno-choice
      • tricky
  • virtualization
    • kubernetes
      • init
        • kubespray
        • kubeadm
          • environment
          • crio v1.30.4
          • docker v1.15.3
          • HA
        • addons
        • etcd
      • kubectl
        • pod
        • deploy
        • replicasets
        • namespace
        • secrets
      • node
      • certificates
      • events
      • kubeconfig
      • kubelet
      • troubleshooting
      • cheatsheet
      • auth
      • api
      • tools
        • monitor
        • helm
        • network
        • minikube
    • docker
      • run & exec
      • voume
      • remove
      • show info
      • dockerfile
      • dockerd
      • tricky
      • troubleshooting
      • windows
    • crio
    • podman
  • ai
    • prompt
  • osx
    • apps
      • init
      • brew
    • defaults
    • system
    • network
    • script
    • tricky
  • linux
    • devenv
    • util
      • time & date
      • output formatting
      • params
      • tricky
    • nutshell
    • disk
    • network
    • troubleshooting
    • system
      • apt/yum/snap
      • authorization
      • apps
      • x11
    • ubuntu
      • systemctl
      • x
    • rpi
  • programming
    • groovy
    • python
      • config
      • basic
      • list
      • pip
      • q&a
    • others
    • archive
      • angular
      • maven
      • mysql
        • installation
        • logs
      • ruby
        • rubyInstallationQ&A
  • tools
    • fonts
    • html & css
    • Jira & Confluence
    • node & npm
      • gitbook
      • hexo
      • github.page
      • code themes
    • app
      • microsoft office
      • vscode
      • virtualbox
      • iterm2
      • browser
      • skype
      • teamviewer
      • others
  • quotes
  • english
Powered by GitBook
On this page
  • setup
  • run
  • migrate-config
  • hooks

Was this helpful?

  1. devops

pre-commit

PreviousstatisticsNextrelease-tools

Last updated 27 days ago

Was this helpful?

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

copyright manager

[!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 (legacy Mac)

crlf

force use \r(Windows)

lf

force use (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

    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

* iMarslo: cr-manager
setup
install
run
migrate-config
hooks
copyright manager
checker and fixer
convert tab to spaces
typos