Skip to content

Instantly share code, notes, and snippets.

@abhaysood
Created May 18, 2021 19:05
Show Gist options
  • 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