Created
October 17, 2019 14:36
-
-
Save ihnorton/083f64dbbe7f3150efa226e7040a6f80 to your computer and use it in GitHub Desktop.
run clang-format on the current branch, show diff, optionally apply
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
PARENT="origin/dev" | |
clang_format=$(which clang-format || (echo "could not find clang-format!"; return 1)) | |
diff_files=$(git diff --name-only $PARENT) | |
diff_cc_files=$(find $diff_files -name "*.cc" -or -name "*.c" -or -name "*.h") | |
if [[ -z "$diff_cc_files" ]]; then | |
return 0; | |
fi | |
fixcount=$($clang_format -output-replacements-xml $diff_cc_files | grep offset | wc -l) | |
if [[ "$fixcount" -eq 0 ]]; then | |
return 0; | |
else | |
( | |
echo "Found formatting problems -- listing fix diff below!" | |
echo $diff_cc_files | |
echo "----------------------------------------------------" | |
for f in $diff_cc_files; do | |
clang-format $f | git diff -R --no-index -- - $f | |
done | |
) | less | |
fi | |
echo "Apply changes [y/N]? " | |
read APPLY | |
if [[ "$APPLY" -eq "y" ]]; then | |
$clang_format -i $diff_cc_files | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment