Browse Source

github: cmake-format lint check on PR [skip ci]

Victor Seva 5 months ago
parent
commit
4552cec0df
2 changed files with 70 additions and 1 deletions
  1. 52 0
      .github/scripts/check-cmake-format.sh
  2. 18 1
      .github/workflows/pull_request.yml

+ 52 - 0
.github/scripts/check-cmake-format.sh

@@ -0,0 +1,52 @@
+#!/bin/bash
+COMMIT_MESSAGE_SUBJECT_FORMAT="%s"
+
+set -eu
+
+res=0
+
+git_log_format() {
+  local pattern="$1"
+  local reference="$2"
+  git log -1 --pretty=format:"$pattern" "$reference"
+}
+
+check_cmake-format() {
+  while read -r file ; do
+    if ! [ -f "${file}" ] ; then
+      continue
+    fi
+    if [[ "${file}" =~ CMakeLists.txt$ ]] || [[ "${file}" =~ \.cmake$ ]] ; then
+      echo "Checking ${file}"
+      cmake-format --check -c cmake/cmake-format.py "${file}" || res=1
+    fi
+  done < <(git diff-tree --no-commit-id --name-only -r "${1}")
+}
+
+target="${GITHUB_BASE_REF:-master}"
+if [ "${CI:-}" ]; then
+    git rev-parse -q --no-revs --verify "origin/${target}" || \
+        git rev-parse -q --no-revs --verify "${target}" || \
+        git fetch origin --depth=1 "${target}"
+    git rev-parse -q --no-revs --verify "origin/${target}" || \
+        git rev-parse -q --no-revs --verify "${target}" || \
+        git fetch origin --depth=1 tag "${target}"
+    # Ensure that the target revision has some history
+    target_sha=$(git rev-parse -q --verify "origin/${target}" || git rev-parse -q --verify "${target}")
+    git fetch -q "--depth=${FETCH_DEPTH:-50}" origin "+${target_sha}"
+else
+    target_sha=$(git rev-parse -q --verify "${target}") || die "fatal: couldn't find ref ${target}"
+fi
+
+ref=${ref:-HEAD}
+src_sha=$(git rev-parse -q --verify "${ref}") || die "fatal: couldn't find ref ${ref}"
+echo "Checking $(git rev-list --count "${src_sha}" "^${target_sha}") commits since revision ${target_sha}"
+for commit in $(git rev-list --reverse "${src_sha}" "^${target_sha}"); do
+  commit_msg=$(git_log_format "${COMMIT_MESSAGE_SUBJECT_FORMAT}" "${commit}")
+  echo "[${commit}] ${commit_msg}"
+  check_cmake-format "${commit}"
+  echo "===================== done ========= "
+done
+
+echo "Result: ${res}"
+exit ${res}

+ 18 - 1
.github/workflows/pull_request.yml

@@ -14,9 +14,26 @@ jobs:
     steps:
       - name: Check out the repository to the runner
         uses: actions/checkout@v4
-      - uses: wolletd/[email protected]
+      - name: Run clang-format check
+        uses: wolletd/[email protected]
         with:
           target-ref: master
+  check-cmake-format:
+    runs-on: ubuntu-latest
+    permissions:
+      contents: read # to fetch code (actions/checkout)
+    steps:
+      - name: Check out the repository to the runner
+        uses: actions/checkout@v4
+        with:
+          ref: ${{ github.event.pull_request.head.sha }}
+      - uses: awalsh128/[email protected]
+        with:
+          packages: cmake-format
+      - name: Run scripts/check-cmake-format.sh
+        run: ./.github/scripts/check-cmake-format.sh
+        env:
+          ref: ${{ github.event.pull_request.head.sha }}
   check-commit:
     runs-on: ubuntu-latest
     permissions: