Precommit
Tool per runnare controlli in automatico prima di fare commit.
Prerequisiti
- repo git
Installazione
-
Runnare
pip install pre-commit
-
Aggiungere al file
requirements.txt
la vocepre-commit
-
Nella repo, creare un file
.pre-commit-config.yaml
. Una configurazione interessante può essere questa:
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
-
(opzionale) runna
pre-commit install
se vuoi triggerare i controlli ogni volta che fai commit -
Runna pre-commit su tutti i file:
pre-commit run --all-files
Configurazione pre-commit
- Questa configurazione: ignora a livello git il Vagrantfile in caso ci sia una chiave custom e controlla che la chiave privata non sia nell’inventory di Ansible:
repos:
...
- repo: local
hooks:
- id: update-vagrantfile
name: Ignore Vagrantfile in case of custom key.
entry: /usr/bin/bash
args:
- -c
- |
#!/bin/bash
if grep -q 'config.vm.provision "file", source: "~/.ssh/id_rsa.pub", destination: "~/.ssh/authorized_keys2"' Vagrantfile; then
git update-index --no-skip-worktree Vagrantfile
else
git update-index --skip-worktree Vagrantfile
fi
language: script
files: Vagrantfile
- id: ansible_ssh_check
name: Check that ansible_ssh_private_key_file is not in inventories
types: [file]
files: \.(json|ini)$
args: [--multiline]
entry: '(?i)ansible_ssh_private_key_file'
language: pygrep
stages: [commit]