I successfully fake being a tidy developer with this one weird trick (okay, git hooks)

Linting

Git Hooks

#!/bin/bash
# Written in bash, as it's such a good language for dirty hax like
this
# Some boilerplate
EXIT_CODE_ABORT=1
LINT_FAILURE=0
# Get the list of files that this commit touches
FILES=$(git diff --name-only --staged)
for FILE in ${FILES}; do
# It's no good linting files that have been deleted! Check if
# the file exists
stat "${FILE}" 2>&1 > /dev/null

# If the file does not exist, then skip it. It's been deleted
if [[ $? != 0 ]]; then
echo "Cant' stat file. Skipping!"
else
# Run the actual linter. In this case it's PHPCS
phpcs "${FILE}"
# Capture the failure. We don't actually want to exit here,
# As that will not run any of the subsequent files. It's
# better to run all the lints at once, and notify if any
# of them failed.
if [[ $? != 0 ]]; then
LINT_FAILURE=1
fi
fi
done
# If any of the files failed linting, then terminate. We do not
# want to commit bad files!
if [[ ${LINT_FAILURE} == 1 ]]; then
exit ${EXIT_CODE_ABORT}
fi

Theory vs reality

if [[ -n ${SKIP_LINTS} ]]; then
echo -e '\e[0;31m' # Red
cat <<"EOF"
,-.
___,---.__ /'|`\ __,---,___
,-' \` `-.____,-' | `-.____,-' // `-.
,' | ~'\ /`~ | `.
/ ___// `. ,' , , \___ \
| ,-' `-.__ _ | , __,-' `-. |
| / /\_ ` . | , _/\ \ |
\ | \ \`-.___ \ | / ___,-'/ / | /
\ \ | `._ `\\ | //' _,' | / /
`-.\ /' _ `---'' , . ``---' _ `\ /,-'
`` / \ ,='/ \`=. / \ ''
|__ /|\_,--.,-.--,--._/|\ __|
/ `./ \\`\ | | | /,//' \,' \
/ / ||--+--|--+-/-| \ \ YOU SKIPPED LINTS. I SAW THAT.
| | /'\_\_\ | /_/_/`\ | | GONNA GO EAT A PUPPY.
\ \__, \_ `~' _/ .__/ /
`-._,-' `-._______,-' `-._,-'
EOF
echo -e "\e[0m" # Off
sleep 20
exit 0
fi
SKIP_LINTS=1 git commit -m "haha this is a bad idea"

Lint all the things

{
"linters": {
"php": {
"type": "php",
"include": "(\\.(php|phtml)$)"
},
"phpcs": {
"type": "phpcs",
"bin": "vendor/bin/phpcs",
"include": "(\\.(php|phtml)$)",
"phpcs.standard": "PSR2"
}
}
}
  • We can indicate which files should be run with which lints, and
  • Arcanist will present the output of many linters in a pretty format which is easy to read

Lint all the repos

# ~/.gitconfig
[core]
hooksPath = /opt/git-hooks

Final thoughts

--

--

--

See https://www.andrewhowden.com/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Easy Ways to Improve Your Adalo App Performance

Rails, MVC, and the Most Frequently Used Rails Commands

Import Call Log Data from Ring Central with PHP

SDC Week9Day1 — Further Indexing

Toronto + Coding = My New Story

A beautiful photo of the skyline of the city of Toronto taken at night.

Microservices, a journey worth sharing.

Junior Dev: The Greatest Perk

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andrew Howden

Andrew Howden

See https://www.andrewhowden.com/

More from Medium

SharePoint Comparing Dates Without Workflow or Any Kind Of Trigger Dynamically

The perfect way to Writing Code Documentation

Uncertain classes with my preteens.

The ABCs of Project Management — an ultimate beginner’s guide