Skip to content

Commit

Permalink
Add CI workflow for clang-format (#610)
Browse files Browse the repository at this point in the history
Should fail if the contribution doesn't match the format and provide a
diff that someone can download and apply locally.

Signed-off-by: Tristan Partin <[email protected]>
  • Loading branch information
tristan957 authored Feb 13, 2023
1 parent 3990cd4 commit 3912217
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 9 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/clang-format.ignore.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# SPDX-License-Identifier: Apache-2.0 OR MIT
#
# SPDX-FileCopyrightText: Copyright 2023 Micron Technology, Inc.

name: ClangFormat

on:
pull_request:
types: [opened, synchronize, reopened]
paths-ignore:
- "**.c"
- "**.h"
- "**.h.in"
- "!subprojects/*"
- .github/workflows/clang-format.yaml

concurrency:
group: ${{ github.workflow }}-ignore-${{ github.head_ref }}
cancel-in-progress: true

permissions: {}

jobs:
clang-format:
runs-on: ubuntu-latest
steps:
- run: |
echo "Skipping ${{ github.workflow }}/${{ github.job }}"
57 changes: 57 additions & 0 deletions .github/workflows/clang-format.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# SPDX-License-Identifier: Apache-2.0 OR MIT
#
# SPDX-FileCopyrightText: Copyright 2023 Micron Technology, Inc.

name: ClangFormat

on:
pull_request:
types: [opened, synchronize, reopened]
branches:
- master
- "v[0-9]+.[0-9]+"
paths:
- "**.c"
- "**.h"
- "**.h.in"
- "!subprojects/*"
- .github/workflows/clang-format.yaml

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref }}
cancel-in-progress: true

permissions: {}

jobs:
clang-format:
runs-on: ubuntu-latest
container:
image: ghcr.io/hse-project/ci-images/fedora-37:${{ github.base_ref }}

steps:
- name: Checkout HSE
uses: actions/checkout@v3

- name: Run clang-format
run: |
./scripts/dev/clang-format.sh -c "$GITHUB_WORKSPACE"
- name: Generate diff
if: failure()
run: |
git config --global --add safe.directory "$GITHUB_WORKSPACE"
git fetch origin --depth=1 "$GITHUB_BASE_REF" "$GITHUB_HEAD_REF"
git diff -U0 --no-color --relative \
"origin/$GITHUB_BASE_REF" "origin/$GITHUB_HEAD_REF" |
clang-format-diff -p1 -sort-includes -regex \
'(cli|include|lib|samples|tests|tools)\/.*\.(c|h|h\.in)$' |
tee clang-format.patch
- name: Upload diff
if: failure()
uses: actions/upload-artifact@v3
with:
name: diff
path: clang-format.patch
18 changes: 9 additions & 9 deletions scripts/dev/format.sh → scripts/dev/clang-format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

CMD=${0##*/}

CLANG_FORMAT_VERSION=13
CLANG_FORMAT_REGEX='(1[3-9]|[2-9][0-9])'
CLANG_FORMAT_VERSION=15
CLANG_FORMAT_REGEX='(1[5-9]|[2-9][0-9])'

set -u

Expand Down Expand Up @@ -44,8 +44,8 @@ EOF

cat <<EOF
$CMD works recursively on all '*.c' and '*.h' files under each given path. If
no path is given, the current working directory is used.
$CMD works recursively on all '*.c', '*.h', and '*.h.in' files under each given
path. If no path is given, the current working directory is used.
Notes:
- $CMD must be executed from the root of the HSE source tree.
Expand Down Expand Up @@ -81,7 +81,7 @@ if ! clang-format --version 2>&1 | grep -qPi "^clang-format version $CLANG_FORMA
err "Need clang-format version $CLANG_FORMAT_VERSION or higher"
fi

if [[ ! -f lib/kvdb/meson.build ]]; then
if [[ ! -f meson_options.txt ]]; then
err "Running from a directory that doesn't seem to be the top of an HSE source tree"
fi

Expand All @@ -93,10 +93,10 @@ for p in "$@"; do
if [[ -d "$p" ]]; then
:
elif [[ -f "$p" ]]; then
if [[ "$p" =~ .*\.[ch] ]]; then
if [[ "$p" =~ .*\.(c|h|h\.in)$ ]]; then
:
else
err "File '$p' is not a C source file"
err "File '$p' is not a C file"
fi
else
err "Path '$p' is neither a directory or a file"
Expand All @@ -114,8 +114,8 @@ fi
# need pipefail in unexpected case that find errors out
set -o pipefail
find "$@" -name .git -prune -o -name subprojects -prune -o \
-type f \( -name '*.[ch]' -o -name '*..h.in' \) -print0 \
-type f \( -name '*.[ch]' -o -name '*.h.in' \) -print0 \
| xargs -r0 clang-format "${clang_format_extra_flags[@]}" \
--Werror -style=file -i -fallback-style=none
--Werror -style=file -i -fallback-style=none

exit $?

0 comments on commit 3912217

Please sign in to comment.