Jul. 15th, 2023

momijizukamori: Grey tabby cat with paws on keyboard and mouse. The text reads 'code cat is on the job', lolcats-style (CODE CAT)
[personal profile] momijizukamori
So we've required files to be tidied using perltidy since 2019, buuuuut I am terrible at remembering to manually do it, so! I threw together a quick'n'dirty way of automatically running tidy on commit. It's based on this example from the Prettier docs (Prettier is like perltidy but for CSS/JS/HTML).

Here's the perl variant of the script:
#!/bin/sh
FILES=$(git diff --cached --name-only --diff-filter=ACMR | sed 's| |\\ |g')
[ -z "$FILES" ] && exit 0

# Prettify all selected files
echo "$FILES" | xargs tidyall

# Add back the modified/prettified files to staging
echo "$FILES" | xargs git add

exit 0


This also has the bonus that it only tidies files you added to your commit, so you don't end up with rogue changes in files you didn't touch because someone else forgot to tidy before a branch got merged!

EDIT: [personal profile] kareila rightfully pointed out that I didn't actually specify the install instructions (they're in the linked Prettier docs but, uh, that's not that intuitive). This script should go in the file .git/hooks/pre-commit, and that file needs to be executable, which you can do on the commandline by running chmod +x .git/hooks/pre-commit

Profile

dw_dev: The word "develop" using the Swirly D logo.  (Default)
Dreamwidth Open Source Development

September 2025

S M T W T F S
 123456
78910111213
14151617181920
21222324252627
282930    

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Oct. 12th, 2025 08:49 am
Powered by Dreamwidth Studios