Skip to content

Instantly share code, notes, and snippets.

@abhaysood
Created May 18, 2021 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhaysood/6413f10a1ef7ea9a09bcf03762878ba3 to your computer and use it in GitHub Desktop.
Save abhaysood/6413f10a1ef7ea9a09bcf03762878ba3 to your computer and use it in GitHub Desktop.
A pre-commit hook which fails a commit if formatting issues are encountered (Flutter)
#!/bin/bash
red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`
fileList=$(git diff --diff-filter=d --cached --name-only | grep -E '\.(dart)$')
if [ ${#fileList} -lt 1 ]; then
# No changes to dart files continue with git commit.
exit
fi
filesWithIssues=$(flutter format --dry-run ${fileList[*]}) "$@"
if [ $? -ne 0 ]; then
echo "${red}error: pre-commit hook failed. Error running flutter format${reset}"
exit 1
fi
if [ -z "$filesWithIssues" ]; then
# No files found with formatting issues
exit
else
echo "${red}error: pre-commit hook failed. Run flutter format for following files${reset}"
echo -e "\n$filesWithIssues"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment