name: Check For Changed Files
description: Checks for changed files compared to specific git reference and glob expression
inputs:
  baseRef:
    description: Git reference to check against
    required: true
  ref:
    description: Git reference to check with
    required: false
    default: HEAD
  checkGlob:
    description: Glob expression to limit check to specific files
    required: false
  useFallback:
    description: Use fallback compare against prior commit
    required: false
    default: 'true'
outputs:
  hasChangedFiles:
    value: ${{ steps.checks.outputs.hasChangedFiles }}
    description: True if specified files were changed in comparison to specified git reference
  changedFiles:
    value: ${{ toJSON(steps.checks.outputs.changedFiles) }}
    description: List of changed files
runs:
  using: composite
  steps:
    - name: Check For Changed Files ✅
      shell: bash
      id: checks
      env:
        GIT_BASE_REF: ${{ inputs.baseRef }}
        GIT_REF: ${{ inputs.ref }}
        USE_FALLBACK: ${{ inputs.useFallback }}
      run: |
        : Check for Changed Files ✅
        if [[ "${RUNNER_DEBUG}" ]]; then set -x; fi
        shopt -s extglob
        shopt -s dotglob

        if ! git cat-file -e "${GIT_BASE_REF}" &> /dev/null; then
          echo "::warning::Provided base reference ${GIT_BASE_REF} is invalid"
          if [[ "${USE_FALLBACK}" == 'true' ]]; then
            GIT_BASE_REF='HEAD~1'
          fi
        fi

        changes=($(git diff --name-only ${GIT_BASE_REF} ${GIT_REF} -- ${{ inputs.checkGlob }}))

        if (( ${#changes[@]} )); then
          file_string="${changes[*]}"
          echo "hasChangedFiles=true" >> $GITHUB_OUTPUT
          echo "changedFiles=[${file_string// /,}]" >> GITHUB_OUTPUT
        else
          echo "hasChangedFiles=false" >> $GITHUB_OUTPUT
          echo "changedFiles=[]" >> GITHUB_OUTPUT
        fi
