Skip to content

Instantly share code, notes, and snippets.

@kevmoo
Last active September 2, 2020 11:19
Show Gist options
  • Save kevmoo/f1a104e31a4ba6092349 to your computer and use it in GitHub Desktop.
Save kevmoo/f1a104e31a4ba6092349 to your computer and use it in GitHub Desktop.
Pre-commit hook to format Dart code
# Inspired by https://robots.thoughtbot.com/use-git-hooks-to-automate-annoying-tasks
dart_files=$(git diff --cached --name-only --diff-filter=ACM | grep '.dart$')
[ -z "$dart_files" ] && exit 0
function checkfmt() {
unformatted=$(dartfmt -n $dart_files)
[ -z "$unformatted" ] && return 0
echo >&2 "Dart files must be formatted with dartfmt. Please run:"
for fn in $unformatted; do
echo >&2 " dartfmt -w $PWD/$fn"
done
return 1
}
checkfmt || fail=yes
[ -z "$fail" ] || exit 1
echo 'all okay?'
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment