Skip to content

Instantly share code, notes, and snippets.

@miawgogo
Created May 24, 2020 19:18
Show Gist options
  • Save miawgogo/19fe2aa318ff5fda8968bf19190b7e68 to your computer and use it in GitHub Desktop.
Save miawgogo/19fe2aa318ff5fda8968bf19190b7e68 to your computer and use it in GitHub Desktop.
#!/bin/bash
mapfile -t files < <(git diff --name-only | grep "^.*\.(c|h|cpp|hpp|m|mm)$" -E)
dirtyfiles=()
pathtop=$(git rev-parse --show-toplevel)
for i in "${files[@]}"
do
clang-format --dry-run -Werror $1 "$pathtop/$i" &>/dev/null
if [[ $? != 0 ]]; then
dirtyfiles+=( "$i" )
fi
done
set +x
if [[ ${#dirtyfiles[@]} != 0 ]]; then
echo "================================="
echo "Files were not formatted properly"
for i in "${files[@]}"; do echo $i; done
echo "================================="
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment