Skip to content

Instantly share code, notes, and snippets.

@ihnorton
Created October 17, 2019 14:36
Show Gist options
  • Save ihnorton/083f64dbbe7f3150efa226e7040a6f80 to your computer and use it in GitHub Desktop.
Save ihnorton/083f64dbbe7f3150efa226e7040a6f80 to your computer and use it in GitHub Desktop.
run clang-format on the current branch, show diff, optionally apply
#!/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