Skip to content

Instantly share code, notes, and snippets.

@fousa
Last active July 15, 2021 07:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fousa/bf38ab0d3e0a0cd5ce1752c5c4932038 to your computer and use it in GitHub Desktop.
Save fousa/bf38ab0d3e0a0cd5ce1752c5c4932038 to your computer and use it in GitHub Desktop.
pre-commit formatting
#!/bin/bash
# Check if the stashed files with swiftformat before commiting.
#
# Installation:
# - Run the following command from your git root:
# `curl https://gist.githubusercontent.com/fousa/bf38ab0d3e0a0cd5ce1752c5c4932038/raw/ce9d02c9038e9f9b4b542a65f16fb94fb2ab352a/pre-commit > .git/hooks/pre-commit`
# - Set hook as executable:
# `chmod +x .git/hooks/pre-commit`
# Fetch the list of staged files.
FILELIST=$(git diff --name-only --cached --diff-filter=d)
if [ "$FILELIST" == '' ]; then
# When no staged files are available we return immediately.
echo "No staged files found that were added or updated."
exit 0
fi
echo "Check formatting for the staged files..."
# Save the files to a text file. This is required by the `swiftformat` command.
git diff --name-only --cached --diff-filter=d > .staged-filelist
# Check the formatting for the given files.
RESULT="Pods/SwiftFormat/CommandLineTool/swiftformat --filelist .staged-filelist --lint --config .swiftformat ."
# Execute the swifformat command and save the result.
$RESULT
RESULT_EXIT_STATUS=$?
# Remove the staged file list.
rm -rf .staged-filelist
# Return the exit status from the swiftlint command.
exit $RESULT_EXIT_STATUS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment