name: Export LSPatch source archive

on:
  workflow_dispatch:

jobs:
  export-source:
    name: Create LSPatch source tar.gz
    runs-on: ubuntu-latest

    steps:
      - name: Check out selected revision
        uses: actions/checkout@v4
        with:
          fetch-depth: 1
          persist-credentials: false

      - name: Create source archive
        shell: bash
        run: |
          set -euo pipefail

          commit="$(git rev-parse HEAD)"
          short_sha="$(git rev-parse --short=12 HEAD)"
          archive_root="lspatch-${short_sha}"
          output_dir="${PWD}/artifacts"
          archive_path="${output_dir}/${archive_root}.tar.gz"

          mkdir -p "${output_dir}"

          # Archive all tracked files from the revision selected when the
          # workflow was started. Git metadata is intentionally excluded.
          git archive \
            --format=tar \
            --prefix="${archive_root}/" \
            HEAD | gzip -9 > "${archive_path}"

          (
            cd "${output_dir}"
            sha256sum "${archive_root}.tar.gz" > "${archive_root}.tar.gz.sha256"
          )

          echo "Created LSPatch source archive for commit ${commit}:"
          ls -lh "${output_dir}"

      - name: Upload source archive
        uses: actions/upload-artifact@v4
        with:
          name: lspatch-source
          path: artifacts/
          if-no-files-found: error
          compression-level: 0
          retention-days: 30
