Add a script which can be called in git pre-commit hooks for formatting files. Currently allows for autoformatting python files using black
parent
e6ed36ffbe
commit
9530e2107d
@ -0,0 +1,20 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# This is a script which can be included in a pre-commit hook to auto format code using specified
|
||||||
|
# tools. In this script we utilize black for python and intelliJ for Java.
|
||||||
|
|
||||||
|
# Only run if black is installed
|
||||||
|
if hash black 2>/dev/null; then
|
||||||
|
|
||||||
|
# Find all of the .py files in the current git repo, apply black formatting to them and then
|
||||||
|
# add them again
|
||||||
|
git diff --cached --name-status | \
|
||||||
|
grep -v '^D' | grep '\.py' | \
|
||||||
|
sed 's/[A-Z][ \t]*//' | \
|
||||||
|
xargs black 2>&1| \
|
||||||
|
grep '^reformatted' | \
|
||||||
|
sed 's/reformatted[ \t]//' | \
|
||||||
|
xargs git add
|
||||||
|
|
||||||
|
else
|
||||||
|
"Black isn't installed so not formatting python code"
|
||||||
|
fi
|
Loading…
Reference in new issue