Using pre-commit

I have a bunch of small repositories I maintain, for scripts I use at work, or other bits and pieces, like the one for this blog.

I'm not using github or other large repositories, they're stored on machines I control, and accessed via ssh and tailscale. Simple, straight forward and reliable.

Inkeeping with that simple and reliable theme, I'm not doing CI/CD pipelines, but I do have a few tasks I run using git's built in 'hooks'.__ For example, if I update a script I use on several different computers, a post-commit hook copies the latest version of the script to those machines, when I commit it.

I like using linters to help me spot my missteps 1 but don't always remember to run them. Then I found precommit, and I'm quite enjoying it.

The instructions on the site are ok, but pretty developer focused. They undersell the results.

Install pre-commit. It's a python thing, but before you run away screaming, try pipx install pre-commit Worked fine for me, even on Windows. If you don't have pipx installed, try sudo apt install pipx or sudo dnf install pipx. pipx is a great way to get python command line tools installed and available, without dealing directly with virtual envs and setting up paths. If you don't have/want pipx, then I'll redirect you back to the pre-commit website to get that installed.

In your repository, create a file called .pre-commit-config.yaml and add 'projects' and 'hooks' for the issues and languages you want to check. If I'm adding this to a repository with a bash script and a README.md file, then I'd create a file that looks like this

repos:
- repo: https://github.com/shellcheck-py/shellcheck-py
  hooks:
  - id: shellcheck
- repo: https://github.com/pre-commit/pre-commit-hooks
  hooks:
  - id: check-added-large-files
  - id: check-case-conflict
  - id: check-illegal-windows-names
  - id: check-merge-conflict
  - id: check-shebang-scripts-are-executable
  - id: end-of-file-fixer
  - id: trailing-whitespace 
- repo: https://github.com/igorshubovych/markdownlint-cli
  rev: v0.43.0
  hooks:
  - id: markdownlint
  - args: ["--disable","MD013", "MD041", "--"]
- repo: https://github.com/thlorenz/doctoc
  hooks:
  - id: doctoc
    args: ["--notitle"]

Each listed id under hooks, is a test it will run. Pre-commit themselves have a whole series of them, and links to other projects with more specific tests, like shellcheck etc.

Now for some magic... in that folder, run pre-commit install That will set up the pre-commit packaget to run when you commit a file, you don't have to edit the hook files.

(optional) run pre-commit autoupdate This will get all the latest versions of the linting and checking tools.

(optional) To check all the existing files with a one time pass, run pre-commit run --all-files

From here on out, whenever you run git commit it will run the checks for you, and prevent the commit if it finds issues.

For example:

»»»» git commit -v
check for added large files..............................................Passed
check for case conflicts.................................................Passed
check illegal windows names..........................(no files to check)Skipped
check for merge conflicts................................................Passed
check that scripts with shebangs are executable..........................Passed
fix end of files.........................................................Passed
trim trailing whitespace.................................................Passed
doctoc...................................................................Passed

I didn't have to install anything else, just that yaml file, and it dealt with the rest. Very slick.
Go through the website for more info, checks you can implement, and to see what's available for your programming language or project.

As always, if you have questions or comments or info to add, hit me up on the fediverse.

links

social