Skip to content

Commit

Permalink
Merge Use fixed clang-format
Browse files Browse the repository at this point in the history
This merge forces contributors to use the same version of clang-format as our CI. This is done by installing pre-commit (which can be done easily through pip/pipx). pre-commit allows us to fix the used version of clang-format, so that contributors will use exactly the same version as our CI.

If pre-commit can't be detected during cmake an error is thrown. Otherwise, the pre-commit hooks are installed.

Since this replaces our git-cmake-format, that dependency was removed.

Additional changes:

- change not supported values in .clang-format
- run pre-commit checks over all files

Related PR: #1461
  • Loading branch information
MarcelKoch authored Nov 16, 2023
2 parents 23c06ea + 1cce135 commit fa71990
Show file tree
Hide file tree
Showing 29 changed files with 311 additions and 335 deletions.
14 changes: 7 additions & 7 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ Language: Cpp
# BasedOnStyle: Google
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignConsecutiveAssignments: None
AlignConsecutiveDeclarations: None
AlignEscapedNewlines: Left
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: true
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: true
AfterNamespace: false
Expand Down Expand Up @@ -88,7 +88,7 @@ PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
ReflowComments: true
SortIncludes: true
SortIncludes: CaseInsensitive
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
Expand All @@ -101,7 +101,7 @@ SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
Standard: c++14
TabWidth: 8
UseTab: Never
...
Expand Down
28 changes: 10 additions & 18 deletions .github/bot-pr-format-base.sh
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
#!/usr/bin/env bash

source .github/bot-pr-base.sh
cp .github/bot-pr-base.sh /tmp
source /tmp/bot-pr-base.sh

echo "Retrieving PR file list"
PR_FILES=$(bot_get_all_changed_files ${PR_URL})
NUM=$(echo "${PR_FILES}" | wc -l)
echo "PR has ${NUM} changed files"

TO_FORMAT="$(echo "$PR_FILES" | grep -E $EXTENSION_REGEX || true)"
echo "Set-up working tree"

git remote add fork "$HEAD_URL"
git fetch fork "$HEAD_BRANCH"
git fetch origin "$BASE_BRANCH"

# checkout current PR head
LOCAL_BRANCH=format-tmp-$HEAD_BRANCH
git checkout -b $LOCAL_BRANCH fork/$HEAD_BRANCH

git config user.email "ginkgo.library@gmail.com"
git config user.name "ginkgo-bot"

# save scripts from develop
pushd dev_tools/scripts
pushd dev_tools/scripts || exit 1
cp add_license.sh format_header.sh update_ginkgo_header.sh /tmp
popd
popd || exit 1

# checkout current PR head
LOCAL_BRANCH=format-tmp-$HEAD_BRANCH
Expand All @@ -28,12 +29,3 @@ git checkout -b $LOCAL_BRANCH fork/$HEAD_BRANCH
cp /tmp/add_license.sh dev_tools/scripts/
cp /tmp/format_header.sh dev_tools/scripts/
cp /tmp/update_ginkgo_header.sh dev_tools/scripts/

# format files
dev_tools/scripts/add_license.sh
dev_tools/scripts/update_ginkgo_header.sh
for f in $(echo "$TO_FORMAT" | grep -E $FORMAT_HEADER_REGEX); do dev_tools/scripts/format_header.sh "$f"; done
for f in $(echo "$TO_FORMAT" | grep -E $FORMAT_REGEX); do "$CLANG_FORMAT" -i -style=file "$f"; done

# restore formatting scripts so they don't appear in the diff
git checkout -- dev_tools/scripts/*.sh
10 changes: 10 additions & 0 deletions .github/check-format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,27 @@
cp .github/bot-pr-format-base.sh /tmp
source /tmp/bot-pr-format-base.sh

echo -n "Run Pre-Commit checks"

pipx run pre-commit run --show-diff-on-failure --color=always --from-ref "origin/$BASE_BRANCH" --to-ref HEAD || true

echo -n "Collecting information on changed files"

# check for changed files, replace newlines by \n
LIST_FILES=$(git diff --name-only | sed '$!s/$/\\n/' | tr -d '\n')
echo -n .

git diff > /tmp/format.patch
mv /tmp/format.patch .
echo -n .

bot_delete_comments_matching "Error: The following files need to be formatted"
echo -n .

if [[ "$LIST_FILES" != "" ]]; then
MESSAGE="The following files need to be formatted:\n"'```'"\n$LIST_FILES\n"'```'
MESSAGE="$MESSAGE\nYou can find a formatting patch under **Artifacts** [here]"
MESSAGE="$MESSAGE($JOB_URL) or run "'`format!` if you have write access to Ginkgo'
bot_error "$MESSAGE"
fi
echo .
24 changes: 3 additions & 21 deletions .github/format-rebase.sh
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
#!/usr/bin/env bash

source .github/bot-pr-base.sh

git remote add base "$BASE_URL"
git remote add fork "$HEAD_URL"

git remote -v

git fetch base $BASE_BRANCH
git fetch fork $HEAD_BRANCH

git config user.email "$USER_EMAIL"
git config user.name "$USER_NAME"

LOCAL_BRANCH=rebase-tmp-$HEAD_BRANCH
git checkout -b $LOCAL_BRANCH fork/$HEAD_BRANCH

# save scripts from develop
pushd dev_tools/scripts
cp add_license.sh format_header.sh update_ginkgo_header.sh /tmp
popd
cp .github/bot-pr-format-base.sh /tmp
source /tmp/bot-pr-format-base.sh

bot_delete_comments_matching "Error: Rebase failed"

Expand All @@ -30,7 +12,7 @@ git rebase --rebase-merges --empty=drop --no-keep-empty \
--exec "cp /tmp/add_license.sh /tmp/format_header.sh /tmp/update_ginkgo_header.sh dev_tools/scripts/ && \
dev_tools/scripts/add_license.sh && dev_tools/scripts/update_ginkgo_header.sh && \
for f in \$($DIFF_COMMAND | grep -E '$FORMAT_HEADER_REGEX'); do dev_tools/scripts/format_header.sh \$f; done && \
for f in \$($DIFF_COMMAND | grep -E '$FORMAT_REGEX'); do $CLANG_FORMAT -i \$f; done && \
pipx run pre-commit run && \
git checkout dev_tools/scripts && (git diff >> /tmp/difflog; true) && (git diff --quiet || git commit -a --amend --no-edit --allow-empty)" \
base/$BASE_BRANCH 2>&1 || bot_error "Rebase failed, see the related [Action]($JOB_URL) for details"

Expand Down
22 changes: 20 additions & 2 deletions .github/format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,29 @@
cp .github/bot-pr-format-base.sh /tmp
source /tmp/bot-pr-format-base.sh

echo "Retrieving PR file list"
PR_FILES=$(bot_get_all_changed_files ${PR_URL})
NUM=$(echo "${PR_FILES}" | wc -l)
echo "PR has ${NUM} changed files"

TO_FORMAT="$(echo "$PR_FILES" | grep -E $EXTENSION_REGEX || true)"

# format files
dev_tools/scripts/add_license.sh
dev_tools/scripts/update_ginkgo_header.sh
for f in $(echo "$TO_FORMAT" | grep -E $FORMAT_HEADER_REGEX); do dev_tools/scripts/format_header.sh "$f"; done
pipx run pre-commit run --files $TO_FORMAT || true

# restore formatting scripts so they don't appear in the diff
git checkout -- dev_tools/scripts/*.sh

# check for changed files, replace newlines by \n
LIST_FILES=$(git diff --name-only | sed '$!s/$/\\n/' | tr -d '\n')
CHANGES=$(git diff --name-only | sed '$!s/$/\\n/' | tr -d '\n')

echo "$CHANGES"

# commit changes if necessary
if [[ "$LIST_FILES" != "" ]]; then
if [[ "$CHANGES" != "" ]]; then
git commit -a -m "Format files
Co-authored-by: $USER_COMBINED"
Expand Down
33 changes: 12 additions & 21 deletions .github/workflows/bot-pr-comment.yml
Original file line number Diff line number Diff line change
@@ -1,60 +1,50 @@
name: OnCommentPR

on:
issue_comment:
types: [created]
name: OnCommentPR

jobs:
label:
runs-on: ubuntu-latest
if: github.event.issue.pull_request != '' && github.event.comment.body == 'label!' && (github.event.comment.author_association == 'COLLABORATOR' || github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER')
steps:
- name: Checkout the latest code (shallow clone)
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
ref: develop
- name: Add appropriate labels
env:
GITHUB_TOKEN: ${{ secrets.BOT_TOKEN }}
run: cp --preserve .github/label.sh /tmp && /tmp/label.sh

check_format:
name: check-format
runs-on: ubuntu-22.04
if: github.event.issue.pull_request != '' && github.event.comment.body == 'check-format!' && (github.event.comment.author_association == 'COLLABORATOR' || github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER')
steps:
- name: Checkout the latest code (shallow clone)
uses: actions/checkout@v3
with:
ref: develop
- name: Check for formatting changes
env:
GITHUB_TOKEN: ${{ secrets.BOT_TOKEN }}
run: cp --preserve .github/check-format.sh /tmp && /tmp/check-format.sh
- name: Upload code formatting patch
if: failure()
uses: actions/upload-artifact@v3
with:
name: patch
path: format.patch
uses: ./.github/workflows/check-formatting.yml

format:
name: format
runs-on: ubuntu-22.04
if: github.event.issue.pull_request != '' && github.event.comment.body == 'format!' && (github.event.comment.author_association == 'COLLABORATOR' || github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER')
steps:
- name: Checkout the latest code (shallow clone)
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
ref: develop
persist-credentials: false
- name: Commit formatting changes
env:
GITHUB_TOKEN: ${{ secrets.BOT_TOKEN }}
run: cp --preserve .github/format.sh /tmp && /tmp/format.sh

rebase:
name: rebase
if: github.event.issue.pull_request != '' && github.event.comment.body == 'rebase!' && (github.event.comment.author_association == 'COLLABORATOR' || github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER')
runs-on: ubuntu-latest
steps:
- name: Checkout the latest code
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
ref: develop
fetch-depth: 0
Expand All @@ -63,13 +53,14 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.BOT_TOKEN }}
run: cp --preserve .github/rebase.sh /tmp && /tmp/rebase.sh

format-rebase:
name: format-rebase
if: github.event.issue.pull_request != '' && github.event.comment.body == 'format-rebase!' && (github.event.comment.author_association == 'COLLABORATOR' || github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER')
runs-on: ubuntu-22.04
steps:
- name: Checkout the latest code
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
ref: develop
fetch-depth: 0
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/bot-pr-created.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
if: github.event.pull_request.author_association == 'COLLABORATOR' || github.event.pull_request.author_association == 'MEMBER' || github.event.pull_request.author_association == 'OWNER'
steps:
- name: Checkout the latest code (shallow clone)
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
ref: develop
- name: Add appropriate labels
Expand Down
34 changes: 12 additions & 22 deletions .github/workflows/bot-pr-updated.yml
Original file line number Diff line number Diff line change
@@ -1,42 +1,31 @@
name: OnSyncPR

on:
pull_request_target:
types: [opened,synchronize]
name: OnSyncPR

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
cancel-in-progress: true

jobs:
check-format:
runs-on: ubuntu-22.04
if: github.event.pull_request.author_association == 'COLLABORATOR' || github.event.pull_request.author_association == 'MEMBER' || github.event.pull_request.author_association == 'OWNER'
steps:
- name: Checkout the latest code (shallow clone)
uses: actions/checkout@v3
with:
ref: develop
- name: Check for formatting changes
env:
GITHUB_TOKEN: ${{ secrets.BOT_TOKEN }}
run: cp .github/check-format.sh /tmp && /tmp/check-format.sh
- name: Upload code formatting patch
if: failure()
uses: actions/upload-artifact@v3
with:
name: patch
path: format.patch
uses: ./.github/workflows/check-formatting.yml

abidiff:
runs-on: ubuntu-latest
if: github.event.pull_request.author_association == 'COLLABORATOR' || github.event.pull_request.author_association == 'MEMBER' || github.event.pull_request.author_association == 'OWNER'
env:
CMAKE_FLAGS: -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=DEBUG -DGINKGO_BUILD_TESTS=OFF -DGINKGO_BUILD_EXAMPLES=OFF -DGINKGO_BUILD_BENCHMARKS=OFF -DGINKGO_BUILD_HWLOC=OFF -DGINKGO_BUILD_REFERENCE=OFF -DGINKGO_BUILD_OMP=OFF -DGINKGO_BUILD_CUDA=OFF -DGINKGO_BUILD_HIP=OFF -DGINKGO_BUILD_SYCL=OFF
steps:
- name: Checkout the new code (shallow clone)
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
path: new
ref: ${{ github.event.pull_request.head.ref }}
- name: Checkout the old code (shallow clone)
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
path: old
ref: ${{ github.event.pull_request.base.ref }}
Expand All @@ -58,14 +47,15 @@ jobs:
if: failure()
uses: actions/upload-artifact@v3
with:
name: abi
path: abi.diff
name: abi
path: abi.diff

check-wiki-changelog:
runs-on: ubuntu-latest
if: github.event.pull_request.author_association == 'COLLABORATOR' || github.event.pull_request.author_association == 'MEMBER' || github.event.pull_request.author_association == 'OWNER'
steps:
- name: Checkout the latest code (shallow clone)
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
ref: develop
- name: Check if PR number exists in wiki/Changelog
Expand Down
21 changes: 21 additions & 0 deletions .github/workflows/check-formatting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Check formatting
on: workflow_call

jobs:
pre-commit:
name: Run pre-commit hooks
runs-on: ubuntu-22.04
steps:
- name: Checkout the latest code (shallow clone)
uses: actions/checkout@v4
- name: Run Pre-Commit checks
run: cp .github/check-format.sh /tmp && /tmp/check-format.sh
id: pre-commit
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Upload code formatting patch
if: ${{ failure() }}
uses: actions/upload-artifact@v3
with:
name: patch
path: format.patch
2 changes: 1 addition & 1 deletion .github/workflows/intel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:

steps:
- name: Checkout the latest code (shallow clone)
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: configure
run: |
Expand Down
Loading

0 comments on commit fa71990

Please sign in to comment.