name: Checkout PyTorch

description: Clean workspace and check out PyTorch

inputs:
  no-sudo:
    description: If set to any value, don't use sudo to clean the workspace
    required: false
  submodules:
    description: Works as stated in actions/checkout, but the default value is recursive
    required: false
    default: recursive
  fetch-depth:
    description: Works as stated in actions/checkout, but the default value is 0
    required: false
    default: "0"

runs:
  using: composite
  steps:
    - name: Check if in a container runner
      shell: bash
      id: check_container_runner
      run: echo "IN_CONTAINER_RUNNER=$(if [ -f /.inarc ] || [ -f /.incontainer ]; then echo true ; else echo false; fi)" >> "$GITHUB_OUTPUT"

    - name: Clean workspace
      shell: bash
      if: ${{ steps.check_container_runner.outputs.IN_CONTAINER_RUNNER == 'false' }}
      env:
        NO_SUDO: ${{ inputs.no-sudo }}
      run: |
        retry () {
          $* || (sleep 1 && $*) || (sleep 2 && $*) || (sleep 4 && $*) || (sleep 8 && $*)
        }
        echo "${GITHUB_WORKSPACE}"
        if [ -z "${NO_SUDO}" ]; then
          retry sudo rm -rf "${GITHUB_WORKSPACE}"
        else
          retry rm -rf "${GITHUB_WORKSPACE}"
        fi
        mkdir "${GITHUB_WORKSPACE}"

        # Use all available CPUs for fetching
        cd "${GITHUB_WORKSPACE}"
        git config --global fetch.parallel 0
        git config --global submodule.fetchJobs 0

    - name: Checkout PyTorch
      uses: actions/checkout@v4
      with:
        ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
        # --depth=1 for speed, manually fetch history and other refs as necessary
        fetch-depth: ${{ inputs.fetch-depth }}
        submodules: ${{ inputs.submodules }}
        show-progress: false
