Skip to content

Commit

Permalink
[ci] Don't diff when running clang-format
Browse files Browse the repository at this point in the history
This takes about 15-20 extra seconds but has the benefit of allowing users to replicate and fix clang format issues locally with ease.
  • Loading branch information
driazati committed Apr 7, 2022
1 parent 5f1f8f3 commit ed3da66
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 46 deletions.
24 changes: 0 additions & 24 deletions tests/lint/clang_format.sh

This file was deleted.

61 changes: 40 additions & 21 deletions tests/lint/git-clang-format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,34 @@ set -e
set -u
set -o pipefail

if [[ "$1" == "-i" ]]; then
INPLACE_FORMAT=1
shift 1
else
INPLACE_FORMAT=0
fi

if [[ "$#" -lt 1 ]]; then
echo "Usage: tests/lint/git-clang-format.sh [-i] <commit>"
echo ""
echo "Run clang-format on files that changed since <commit>"
echo "Examples:"
echo "- Compare last one commit: tests/lint/git-clang-format.sh HEAD~1"
echo "- Compare against upstream/main: tests/lint/git-clang-format.sh upstream/main"
echo "You can also add -i option to do inplace format"
exit 1
fi
INPLACE_FORMAT=false
LINT_ALL_FILES=true
REVISION=

while (( $# )); do
case "$1" in
-i)
INPLACE_FORMAT=true
shift 1
;;
--rev)
LINT_ALL_FILES=false
REVISION=$2
shift 2
;;
*)
echo "Usage: tests/lint/git-clang-format.sh [-i] [--rev <commit>]"
echo ""
echo "Run clang-format on files that changed since <commit> or on all files in the repo"
echo "Examples:"
echo "- Compare last one commit: tests/lint/git-clang-format.sh --rev HEAD~1"
echo "- Compare against upstream/main: tests/lint/git-clang-format.sh --rev upstream/main"
echo "The -i will use black to format files in-place instead of checking them."
exit 1
;;
esac
done

cleanup()
{
Expand All @@ -58,14 +69,22 @@ fi
# Print out specific version
${CLANG_FORMAT} --version

if [[ ${INPLACE_FORMAT} -eq 1 ]]; then
echo "Running inplace git-clang-format against" $1
git-${CLANG_FORMAT} --extensions h,mm,c,cc --binary=${CLANG_FORMAT} $1
if [[ "$INPLACE_FORMAT" == "true" ]]; then
echo "Running inplace git-clang-format against $REVISION"
git-${CLANG_FORMAT} --extensions h,mm,c,cc --binary=${CLANG_FORMAT} "$REVISION"
exit 0
fi

echo "Running git-clang-format against" $1
git-${CLANG_FORMAT} --diff --extensions h,mm,c,cc --binary=${CLANG_FORMAT} $1 1> /tmp/$$.clang-format.txt
if [[ "$LINT_ALL_FILES" == "true" ]]; then
echo "Running git-clang-format against all C++ files"
FILES=$(git ls-files | grep -E '\.(c|cc|cpp|mm|h|hpp)$')
BASE_COMMIT=$(git rev-list --max-parents=0 HEAD)
git-${CLANG_FORMAT} --diff --extensions h,mm,c,cc --binary=${CLANG_FORMAT} "$BASE_COMMIT" ${FILES[@]} 1> /tmp/$$.clang-format.txt
else
echo "Running git-clang-format against $REVISION"
git-${CLANG_FORMAT} --diff --extensions h,mm,c,cc --binary=${CLANG_FORMAT} "$REVISION" 1> /tmp/$$.clang-format.txt
fi

echo "---------clang-format log----------"
cat /tmp/$$.clang-format.txt
echo ""
Expand Down
2 changes: 1 addition & 1 deletion tests/scripts/task_lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ echo "Linting the C++ code..."
tests/lint/cpplint.sh

echo "clang-format check..."
tests/lint/clang_format.sh
./tests/lint/git-clang-format.sh

echo "Rust check..."
tests/lint/rust_format.sh
Expand Down

0 comments on commit ed3da66

Please sign in to comment.