# Copyright (C) 2025 Sebastian Pipping <sebastian@pipping.org>
# Licensed under the MIT license

name: Enforce clang-format clean code

on:
  pull_request:
  push:
  schedule:
    - cron: '0 2 * * 5'  # Every Friday at 2am
  workflow_dispatch:

permissions:
  contents: read

jobs:
  clang_format:
    runs-on: ubuntu-24.04
    steps:
    - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd  # v6.0.2

    - name: Install Clang 21
      run: |-
        set -x
        source /etc/os-release
        wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
        sudo add-apt-repository "deb https://apt.llvm.org/${UBUNTU_CODENAME}/ llvm-toolchain-${UBUNTU_CODENAME}-21 main"
        sudo apt-get update  # due to new repository
        sudo apt-get install --yes --no-install-recommends -V \
            clang-format-21
        echo /usr/lib/llvm-21/bin >>"${GITHUB_PATH}"

    - name: Enforce clang-format clean code
      run: |
        set -x -o pipefail
        clang-format --version
        git ls-files \*.cpp \*.c \*.h \*.h.in | xargs clang-format -i
        sed 's,#\( \+\)cmakedefine,#cmakedefine,' -i.bak src/UriConfig.h.in  # this helps Meson
        git -c color.ui=always diff --exit-code  # i.e. fail CI for non-empty diff
