Skip to content

Instantly share code, notes, and snippets.

@danielgomezrico
Created January 20, 2023 16:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielgomezrico/6f88ceb468162385fc7075e73817ec67 to your computer and use it in GitHub Desktop.
Save danielgomezrico/6f88ceb468162385fc7075e73817ec67 to your computer and use it in GitHub Desktop.
Git - Hook - Pre Commit - Flutter: format files before they are commited
#!/bin/sh
#
# Autoformat all modified files before commit
#
function format_if_required {
FILES=$1
flutter format $FILES | grep -Ev '^(Unchanged)'
echo "--> Autoformat was applied/checked ✅"
}
echo "----------------------------------------------------------"
echo " Checking code format on staged files"
echo "----------------------------------------------------------"
CHANGED_FILES=$(git diff --diff-filter=d --cached --name-only | grep .dart)
if [ -z "$CHANGED_FILES" ];
then
echo "--> No files added to check"
else
format_if_required $CHANGED_FILES
fi
echo "--> Commit is ok ✅"
echo "----------------------------------------------------------"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment