jobs: # skip-all

  # This is ci/actions-templates/centos-fmt-clippy.yaml
  # Do not edit this file in .github/workflows
  check: # job-name
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
    steps:
      - name: Clone repo
        uses: actions/checkout@v4
        with:
          # v2 defaults to a shallow checkout, but we need at least to the previous tag
          fetch-depth: 0
      - name: Acquire tags for the repo
        run: |
          git fetch --no-tags --prune --depth=1 origin +refs/tags/*:refs/tags/*
      - name: Display the current git status
        run: |
          git status
          git describe
      - name: Run CI workflow generation checks
        run: |
          ./ci/actions-templates/gen-workflows.sh
          git diff --exit-code
      - name: Prep cargo dirs
        run: |
          mkdir -p ~/.cargo/{registry,git}
      - name: Set environment variables appropriately for the build
        run: |
          echo "$HOME/.cargo/bin" >> $GITHUB_PATH
      - name: Cache cargo registry and git trees
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
      - name: Get rustc commit hash
        id: cargo-target-cache
        run: |
          echo "{rust_hash}={$(rustc -Vv | grep commit-hash | awk '{print $2}')}" >> $GITHUB_OUTPUT
        shell: bash
      - name: Cache cargo build
        uses: actions/cache@v4
        with:
          path: target
          key: ${{ github.base_ref }}-${{ github.head_ref }}-${{ runner.os }}-cargo-clippy-target-dir-${{ steps.cargo-target-cache.outputs.rust_hash }}-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ github.base_ref }}-${{ runner.os }}-cargo-clippy-target-dir-${{ steps.cargo-target-cache.outputs.rust_hash }}-${{ hashFiles('**/Cargo.lock') }}
      - name: Install Rustup using ./rustup-init.sh
        run: |
          sh ./rustup-init.sh --default-toolchain=stable --profile=minimal -c=rustfmt -c=clippy -y
      - name: Run the centos check within the docker image
        run: |
          docker run \
            --volume "$PWD":/checkout:ro \
            --workdir /checkout \
            --tty \
            --init \
            --rm \
            centos:7 \
            sh ./ci/raw_init.sh
      - name: Run shell checks
        run: |
          shellcheck -x -s dash -- rustup-init.sh
          git ls-files -- '*.sh' | xargs shellcheck -x -s dash
          git ls-files -- '*.bash' | xargs shellcheck -x -s bash
      - name: Install taplo
        uses: taiki-e/install-action@v2
        with:
          tool: taplo-cli
      - name: Run TOML formatting checks
        run: |
          taplo fmt
          git diff --exit-code
      - name: Run Rust formatting checks
        run: |
          cargo fmt --all --check
      - name: Run cargo check and clippy
        run: |
          cargo check --all --all-targets --features test
          git ls-files -- '*.rs' | xargs touch
          cargo clippy --all-targets --all-features -- -D warnings
