name: clang-format

on: [push, pull_request]

jobs:
  check:
    runs-on: ubuntu-22.04
    env:
      CLANG_VERSION: 10
    steps:
    - uses: actions/checkout@v3
    # - name: Install clang-format
    #   run: |
    #     codename=$( lsb_release --codename --short )
    #     sudo tee /etc/apt/sources.list.d/llvm.list >/dev/null <<EOF
    #     deb http://apt.llvm.org/${codename}/ llvm-toolchain-${codename}-${CLANG_VERSION} main
    #     deb-src http://apt.llvm.org/${codename}/ llvm-toolchain-${codename}-${CLANG_VERSION} main
    #     EOF
    #     wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add
    #     sudo apt-get update -y
    #     sudo apt-get install -y clang-format-${CLANG_VERSION}

    # Temporary fix until this commit is merged
    # https://github.com/XRPLF/rippled/commit/552377c76f55b403a1c876df873a23d780fcc81c
    - name: Download and install clang-format
      run: |
        sudo apt-get update -y
        sudo apt-get install -y libtinfo5
        curl -LO https://github.com/llvm/llvm-project/releases/download/llvmorg-10.0.1/clang+llvm-10.0.1-x86_64-linux-gnu-ubuntu-16.04.tar.xz
        tar -xf clang+llvm-10.0.1-x86_64-linux-gnu-ubuntu-16.04.tar.xz
        sudo mv clang+llvm-10.0.1-x86_64-linux-gnu-ubuntu-16.04 /opt/clang-10
        sudo ln -s /opt/clang-10/bin/clang-format /usr/local/bin/clang-format-10
    - name: Format src/ripple
      run: find src/ripple -type f \( -name '*.cpp' -o -name '*.h' -o -name '*.ipp' \) -print0 | xargs -0 clang-format-${CLANG_VERSION} -i
    - name: Format src/test
      run: find src/test -type f \( -name '*.cpp' -o -name '*.h' -o -name '*.ipp' \) -print0 | xargs -0 clang-format-${CLANG_VERSION} -i
    - name: Check for differences
      id: assert
      run: |
        set -o pipefail
        git diff --exit-code | tee "clang-format.patch"
    - name: Upload patch
      if: failure() && steps.assert.outcome == 'failure'
      uses: actions/upload-artifact@v4
      continue-on-error: true
      with:
        name: clang-format.patch
        if-no-files-found: ignore
        path: clang-format.patch
    - name: What happened?
      if: failure() && steps.assert.outcome == 'failure'
      env:
        PREAMBLE: |
          If you are reading this, you are looking at a failed Github Actions
          job.  That means you pushed one or more files that did not conform
          to the formatting specified in .clang-format. That may be because
          you neglected to run 'git clang-format' or 'clang-format' before
          committing, or that your version of clang-format has an
          incompatibility with the one on this
          machine, which is:
        SUGGESTION: |

          To fix it, you can do one of two things:
          1. Download and apply the patch generated as an artifact of this
             job to your repo, commit, and push.
          2. Run 'git-clang-format --extensions c,cpp,h,cxx,ipp dev'
             in your repo, commit, and push.
      run: |
        echo "${PREAMBLE}"
        clang-format-${CLANG_VERSION} --version
        echo "${SUGGESTION}"
        exit 1
