<commit_before>name: Cache query compilation
description: Caches CodeQL compilation caches - should be run both on PRs and pushes to main.

inputs:
  key:
    description: 'The cache key to use - should be unique to the workflow'
    required: true

outputs:
  cache-dir:
    description: "The directory where the cache was stored"
    value: ${{ steps.fill-compilation-dir.outputs.compdir }}

runs:
  using: composite
  steps:
    # Cache the query compilation caches.
    # calculate the merge-base with main, in a way that works both on PRs and pushes to main. 
    - name: Calculate merge-base
      shell: bash
      if: ${{ github.event_name == 'pull_request' }}
      env:
        BASE_BRANCH: ${{ github.base_ref }}
      run: |
        MERGE_BASE=$(git cat-file commit $GITHUB_SHA | grep '^parent ' | head -1 | cut -f 2 -d " ")
        echo "merge_base=$MERGE_BASE" >> $GITHUB_ENV
    - name: Read CodeQL query compilation - PR
      if: ${{ github.event_name == 'pull_request' }}
      uses: actions/cache@v3
      with:
        path: '**/.cache'
        key: codeql-compile-${{ inputs.key }}-pr-${{ github.sha }} # deliberately not using the `compile-compile-main` keys here.
        restore-keys: |
          codeql-compile-${{ inputs.key }}-${{ github.base_ref }}-${{ env.merge_base }}
          codeql-compile-${{ inputs.key }}-${{ github.base_ref }}-
          codeql-compile-${{ inputs.key }}-main-
    - name: Fill CodeQL query compilation cache - main
      if: ${{ github.event_name != 'pull_request' }}
      uses: actions/cache@v3
      with:
        path: '**/.cache'
        key: codeql-compile-${{ inputs.key }}-${{ github.ref_name }}-${{ github.sha }} # just fill on main
        restore-keys: | # restore from another random commit, to speed up compilation.
          codeql-compile-${{ inputs.key }}-${{ github.ref_name }}- 
          codeql-compile-${{ inputs.key }}-main-
    - name: Fill compilation cache directory
      id: fill-compilation-dir
      shell: bash 
      run: |
        # Move all the existing cache into another folder, so we only preserve the cache for the current queries.
        mkdir -p ${COMBINED_CACHE_DIR}
        rm -f **/.cache/{lock,size} # -f to avoid errors if the cache is empty.
        # copy the contents of the .cache folders into the combined cache folder.
        cp -r **/.cache/* ${COMBINED_CACHE_DIR}/ || : # ignore missing files
        # clean up the .cache folders
        rm -rf **/.cache/*

        echo "compdir=${COMBINED_CACHE_DIR}" >> $GITHUB_OUTPUT
      env: 
        COMBINED_CACHE_DIR: ${{ github.workspace }}/compilation-dir<commit_msg>CI: use read-only-cache when running on a PR<commit_after>name: Cache query compilation
description: Caches CodeQL compilation caches - should be run both on PRs and pushes to main.

inputs:
  key:
    description: 'The cache key to use - should be unique to the workflow'
    required: true

outputs:
  cache-dir:
    description: "The directory where the cache was stored"
    value: ${{ steps.fill-compilation-dir.outputs.compdir }}

runs:
  using: composite
  steps:
    # Cache the query compilation caches.
    # calculate the merge-base with main, in a way that works both on PRs and pushes to main. 
    - name: Calculate merge-base
      shell: bash
      if: ${{ github.event_name == 'pull_request' }}
      env:
        BASE_BRANCH: ${{ github.base_ref }}
      run: |
        MERGE_BASE=$(git cat-file commit $GITHUB_SHA | grep '^parent ' | head -1 | cut -f 2 -d " ")
        echo "merge_base=$MERGE_BASE" >> $GITHUB_ENV
    - name: Read CodeQL query compilation - PR
      if: ${{ github.event_name == 'pull_request' }}
      uses: erik-krogh/actions-cache@a88d0603fe5fb5606db9f002dfcadeb32b5f84c6
      with:
        path: '**/.cache'
        read-only: true
        key: codeql-compile-${{ inputs.key }}-pr-${{ github.sha }} # deliberately not using the `compile-compile-main` keys here.
        restore-keys: |
          codeql-compile-${{ inputs.key }}-${{ github.base_ref }}-${{ env.merge_base }}
          codeql-compile-${{ inputs.key }}-${{ github.base_ref }}-
          codeql-compile-${{ inputs.key }}-main-
    - name: Fill CodeQL query compilation cache - main
      if: ${{ github.event_name != 'pull_request' }}
      uses: erik-krogh/actions-cache@a88d0603fe5fb5606db9f002dfcadeb32b5f84c6
      with:
        path: '**/.cache'
        key: codeql-compile-${{ inputs.key }}-${{ github.ref_name }}-${{ github.sha }} # just fill on main
        restore-keys: | # restore from another random commit, to speed up compilation.
          codeql-compile-${{ inputs.key }}-${{ github.ref_name }}- 
          codeql-compile-${{ inputs.key }}-main-
    - name: Fill compilation cache directory
      id: fill-compilation-dir
      shell: bash 
      run: |
        # Move all the existing cache into another folder, so we only preserve the cache for the current queries.
        mkdir -p ${COMBINED_CACHE_DIR}
        rm -f **/.cache/{lock,size} # -f to avoid errors if the cache is empty.
        # copy the contents of the .cache folders into the combined cache folder.
        cp -r **/.cache/* ${COMBINED_CACHE_DIR}/ || : # ignore missing files
        # clean up the .cache folders
        rm -rf **/.cache/*

        echo "compdir=${COMBINED_CACHE_DIR}" >> $GITHUB_OUTPUT
      env: 
        COMBINED_CACHE_DIR: ${{ github.workspace }}/compilation-dir
{'fraction_non_alphanumeric': 0.12845227062094533, 'fraction_numerical': 0.008897126969416125, 'mean_word_length': 2.538360655737705, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6713729', 'n_tokens_mistral': 1740, 'n_tokens_neox': 1615, 'n_words': 516}