name: CI

on:
  push:
    branches: [main]
  pull_request:

env:
  CARGO_TERM_COLOR: always

jobs:
  build-and-test:
    name: Build & test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt, clippy

      - name: Cache cargo build
        uses: Swatinem/rust-cache@v2

      - name: Check formatting
        run: cargo fmt --all --check

      - name: Clippy
        run: cargo clippy --all-targets -- -D warnings

      - name: Build
        run: cargo build --verbose

      - name: Test
        run: cargo test --verbose

      # Guard the pure-Rust `git` CLI backend (no libgit2 C build) - the path
      # recommended for Windows installs without a C toolchain.
      - name: Test (no default features, git-CLI backend)
        run: cargo test --no-default-features --verbose

      # Guard the pure-Rust gitoxide backend (the `gix` crate, no C toolchain).
      - name: Clippy (gitoxide backend)
        run: cargo clippy --no-default-features --features gitoxide --all-targets -- -D warnings

      - name: Test (gitoxide backend)
        run: cargo test --no-default-features --features gitoxide --verbose

  windows:
    name: Build & test (Windows)
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v5

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy

      - name: Cache cargo build
        uses: Swatinem/rust-cache@v2

      - name: Clippy
        run: cargo clippy --all-targets -- -D warnings

      # Default features build libgit2 from C source; windows-latest ships the
      # MSVC toolchain + cmake that `cc`/`libgit2-sys` need.
      - name: Build
        run: cargo build --verbose

      # Runs the unit tests plus the real-`pwsh` end-to-end prompt test
      # (PowerShell 7 is preinstalled on windows-latest).
      - name: Test
        run: cargo test --verbose

      - name: Build (no default features, git-CLI backend)
        run: cargo build --no-default-features --verbose

      # gitoxide is pure Rust, so it should build on Windows without the C
      # toolchain that the default libgit2 backend needs.
      - name: Build (gitoxide backend)
        run: cargo build --no-default-features --features gitoxide --verbose

      # The steps above build the committed Cargo.lock. `cargo install` instead
      # resolves dependencies fresh, so a published release can pull newer
      # transitive crates than the locked build ever sees. That is exactly how
      # the advapi32 link failure shipped: libgit2-sys never links advapi32, and
      # the locked build only succeeds because the pinned windows-sys 0.52 (via
      # chrono) drags its umbrella Win32 import library onto the link line and
      # incidentally resolves those symbols. Newer windows-sys uses per-function
      # raw-dylib imports and drops that coverage. Resolve to the latest
      # compatible versions and rebuild so this job also mirrors the
      # `cargo install` view and fails loudly if a transitive bump removes a
      # system library we depend on. Kept last because it rewrites Cargo.lock.
      - name: Update to latest compatible dependencies
        run: cargo update

      - name: Build (latest deps)
        run: cargo build --verbose
