Skip to content

Commit

Permalink
Improve clang-format detection & enforcement (#3462)
Browse files Browse the repository at this point in the history
* Improve the detection of non-clang-format-compliant commits/diffs

* Fix formatting

* Add .clang-format to version control; have it consistent and fixed across environments
  • Loading branch information
theohax committed Sep 22, 2021
1 parent 61b3347 commit 8ecbb1d
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 700 deletions.
1 change: 0 additions & 1 deletion .clang-format.base → .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ BraceWrapping:
AfterClass: true
BeforeCatch: true
BeforeElse: true
BeforeLambdaBody: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
SortIncludes: true
Expand Down
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,3 @@ Testing

# Autogenerated Build Prep artifacts
util/build_prep/*/prep.sh

# Autogenerated Tooling
.clang-format
22 changes: 12 additions & 10 deletions ci/check-commit-format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

set -e

source "$(dirname "$BASH_SOURCE")/detect-clang-format.sh"
if [[ ! -z $(git status --untracked-files=no --porcelain) ]]; then
echo "Unable to run script: working directory not clean (see git status)"
exit 1
fi

source "$(dirname "$BASH_SOURCE")/common.sh"

"$REPO_ROOT/ci/update-clang-format"
"$REPO_ROOT/ci/clang-format-all.sh"

clang_format_result=$(python "$REPO_ROOT/ci/git-clang-format.py" --diff --extensions "hpp,cpp")
if [[ $clang_format_result != "no modified files to format" ]] &&
[[ $clang_format_result != "clang-format did not modify any files" ]]; then
python "$REPO_ROOT/ci/git-clang-format.py" --diff --extensions "hpp,cpp"
echo
if [[ ! -z $(git status --untracked-files=no --porcelain) ]]; then
echo "Code formatting differs from expected - please run ci/clang-format-all.sh"
git diff
git reset --hard HEAD > /dev/null
exit 1
else
echo "clang-format passed"
exit 0
fi

echo "clang-format passed"
exit 0
8 changes: 3 additions & 5 deletions ci/clang-format-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

set -e

source $(dirname $BASH_SOURCE)/detect-clang-format.sh
source $(dirname $BASH_SOURCE)/common.sh
source "$(dirname "$BASH_SOURCE")/detect-clang-format.sh"
source "$(dirname "$BASH_SOURCE")/common.sh"

cd "$REPO_ROOT"
./ci/update-clang-format
find nano -iname '*.h' -o -iname '*.hpp' -o -iname '*.cpp' | xargs "$CLANG_FORMAT" -i
find "$REPO_ROOT/nano" -iname '*.h' -o -iname '*.hpp' -o -iname '*.cpp' | xargs "$CLANG_FORMAT" -i -style=file
2 changes: 2 additions & 0 deletions ci/detect-clang-format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ if [[ -z $CLANG_FORMAT ]]; then
echo "No 'clang-format' of version '$CLANG_FORMAT_VERSION' could be detected in your PATH."
exit 1
fi

echo "Using '$CLANG_FORMAT' version '$CLANG_FORMAT_VERSION'"
Loading

0 comments on commit 8ecbb1d

Please sign in to comment.