Skip to content

Commit

Permalink
add pre-push hook to verify formatting (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbohanon authored Dec 22, 2023
1 parent c54bae7 commit 73c2cbc
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: pre-commit

on:
push:
branches: [main]
branches: [main, master]
pull_request:
branches: [main]
branches: [main, master]

jobs:
pre-commit:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.bin
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ If you are in the root directory that has the `.devcontainer/` folder, you can r
## Contributing

Lua code is formatted in a pre-commit hook using [stylelua](https://github.com/JohnnyMorganz/StyLua). Please install this as part of contributing to the project.
Contributors using Linux may use the pre-push hook to ensure formatting. Install the pre-push hook by running `tools/init.sh`.
49 changes: 49 additions & 0 deletions tools/hooks/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/sh

echo "checking formatting with stylua."
echo "use NO_VERIFY=1 or --no-verify to skip pre-push check"
echo ""

# Get the version defined in the CI stylua check config
STYLUA_VERSION="$(awk '/rev:/ {print $2;}' .pre-commit-config.yaml)"

# Use stylua installed on the $PATH if exists
STYLUA_BIN="$(which stylua 2>/dev/null)"


if [ ! "$STYLUA_BIN" ]; then
# There may already be a stylua in .bin from a prior run
if [ -d .bin/ ] && [ -f .bin/stylua ]; then
echo "found stylua in .bin/"
echo ""
STYLUA_BIN=".bin/stylua"
else
echo "stylua must be installed. ${STYLUA_VERSION} is used by this repo."
echo "attempt to download and install it? y/N"
read -r input
if [ "$input" = 'y' ]; then
mkdir .bin/
wget -O .bin/stylua.zip "https://github.com/JohnnyMorganz/StyLua/releases/download/$STYLUA_VERSION/stylua-linux.zip"
cd .bin || exit 1
unzip stylua.zip 1>/dev/null && rm stylua.zip
cd .. || exit 1
STYLUA_BIN=".bin/stylua"
else
exit 1
fi
fi
elif [ "$($STYLUA_BIN --version)" != "$STYLUA_VERSION" ]; then
echo "repo uses stylua $STYLUA_VERSION, but $($STYLUA_BIN --version) was found on PATH."
echo "this may cause discrepancies with the CI check."
echo "continuing anyway..."
echo ""
fi

# Use stylua to check formatting; disallow push on fail
CHECK_OUTPUT="$($STYLUA_BIN --check ./)"
if [ "$CHECK_OUTPUT" ]; then
echo "run 'stylua ./' before pushing"
echo "$CHECK_OUTPUT"
exit 1
fi
exit 0
16 changes: 16 additions & 0 deletions tools/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/sh

if [ ! -d .git ]; then
echo "not in a git-managed repo."
exit 1
fi

mkdir -p .git/hooks
if [ -f .git/hooks/pre-push ]; then
echo "pre-push hook already exists in this repo."
echo "consider manually merging your custom hook with the one defined here."
exit 1
fi

cp tools/hooks/pre-push .git/hooks/ || exit 1
exit 0

0 comments on commit 73c2cbc

Please sign in to comment.