carthage-software/mago
Mago is an extremely fast PHP linter, formatter, and static analyzer written in Rust. It helps teams catch issues early, enforce consistent style, and improve code quality across projects, with multiple install options like script, Homebrew, Composer, and Cargo.
+++ title = "Recette pre-commit hooks" description = "Lancer lint, analyze et format sur les fichiers indexés avant chaque commit." nav_order = 40 nav_section = "Recettes" +++
Exécuter Mago automatiquement avant chaque commit git. Les exemples ci-dessous agissent tous uniquement sur les fichiers indexés, donc le hook reste rapide même sur des dépôts volumineux.
Créez .git/hooks/pre-commit et rendez-le exécutable :
chmod +x .git/hooks/pre-commit
Choisissez la configuration qui correspond à votre workflow.
Formater les fichiers PHP indexés et ré-indexer les versions formatées. L'expérience la plus fluide pour les développeurs ; rien à se rappeler.
#!/bin/sh
mago lint --staged
if [ $? -ne 0 ]; then
echo "Linting failed. Please fix the issues before committing."
exit 1
fi
mago analyze --staged
if [ $? -ne 0 ]; then
echo "Static analysis failed. Please fix the issues before committing."
exit 1
fi
mago fmt --staged
if [ $? -ne 0 ]; then
echo "Formatting failed. Please check the error above."
exit 1
fi
exit 0
--staged trouve les fichiers indexés et ne traite que ceux-là. Pour fmt --staged, les fichiers formatés sont automatiquement ré-indexés. Pour lint --staged et analyze --staged, combinés à --fix, les fichiers corrigés sont ré-indexés.
Cela ajoute --fix à l'étape lint. --fail-on-remaining bloque le commit si des problèmes n'ont pas pu être corrigés automatiquement et nécessitent encore une attention manuelle. Sans cela, --fix quitte avec zéro même quand des problèmes non corrigés subsistent.
#!/bin/sh
mago lint --fix --fail-on-remaining --staged
if [ $? -ne 0 ]; then
echo "Linting failed. Please fix the remaining issues before committing."
exit 1
fi
mago analyze --staged
if [ $? -ne 0 ]; then
echo "Static analysis failed. Please fix the issues before committing."
exit 1
fi
mago fmt --staged
if [ $? -ne 0 ]; then
echo "Formatting failed. Please check the error above."
exit 1
fi
exit 0
Pour des corrections plus agressives, utilisez --fix --unsafe ou --fix --potentially-unsafe :
mago lint --fix --potentially-unsafe --fail-on-remaining --staged
Refuser le commit si un fichier indexé n'est pas correctement formaté, obligeant le développeur à formater manuellement.
#!/bin/sh
mago lint --staged
if [ $? -ne 0 ]; then
echo "Linting failed. Please fix the issues before committing."
exit 1
fi
mago analyze --staged
if [ $? -ne 0 ]; then
echo "Static analysis failed. Please fix the issues before committing."
exit 1
fi
mago fmt --check
if [ $? -ne 0 ]; then
echo "Some files are not formatted. Please run 'mago fmt' before committing."
exit 1
fi
exit 0
Si vous utilisez Husky, ajoutez les commandes à .husky/pre-commit :
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
mago lint --staged
mago analyze --staged
mago fmt --staged
Si vous utilisez CaptainHook, ajoutez les actions à captainhook.json :
{
"pre-commit": {
"enabled": true,
"actions": [
{ "action": "mago lint --staged" },
{ "action": "mago analyze --staged" },
{ "action": "mago fmt --staged" }
]
}
}
Pour la variante check-only, remplacez la dernière action par mago fmt --check.
--staged versus --check| Aspect | --staged |
--check |
|---|---|---|
| Comportement | Formate les fichiers indexés et les ré-indexe. | Signale les fichiers non formatés ; échoue s'il y en a. |
| Action développeur | Aucune. | Doit lancer mago fmt manuellement si la vérification échoue. |
| Idéal pour | Équipes qui veulent un formatage transparent. | Équipes qui veulent un contrôle explicite des changements. |
| Indexation partielle | Formate le contenu indexé, laisse l'arbre de travail tranquille. | Fonctionne quel que soit l'état d'indexation. |
How can I help you explore Laravel packages today?