name: Sync
on:
  workflow_dispatch:
  push:
    branches: [ master, "release/**" ]
  pull_request:
    branches: [ master ]

permissions:
  contents: read

jobs:
  sync-autogenerated:
    # Job to verify that the tasks performed by UpdateAlways have been done. It is
    # the committer's responsibility (currently) to run UpdateAlways themselves when
    # making a PR, so that everything is kept in-sync.
    name: Check autogenerated file freshness
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Checkout
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
        with:
          submodules: false
          fetch-depth: 0 # Necessary for maint/UpdateAlways
          fetch-tags: false
          # Check out the unmerged source branch for `pull_request`-triggered runs;
          # otherwise use the tip of the branch for `workflow_dispatch` and `pull` triggers.
          ref: ${{ github.event.pull_request.head.ref || github.ref }}
          repository: ${{ github.event.pull_request.head.repo.full_name || github.repository }}

      - name: UpdateDates
        if: |
          github.event_name != 'pull_request' &&
          (startsWith(github.ref, 'refs/heads/release/') ||
           startsWith(github.ref, 'refs/tags/pcre2-'))
        run: maint/UpdateDates.py

      - name: UpdateAlways
        run: maint/UpdateAlways

      - name: 'Rebuild *.h.generic'
        run: |
          ./autogen.sh && ./configure

          # Workaround for incorrect filesystem permissions on /usr/share/aclocal, which
          # causes the m4 macros to be copied with incorrect permissions.
          # https://github.com/actions/runner-images/issues/11212
          chmod u=rw,go=r m4/*.m4

          rm -f src/*.generic
          make src/config.h.generic src/pcre2.h.generic

      # If we're in a forked repo, it's too onerous to expect contributors to run the
      # checks locally to keep these files up to date (since the tool versions are very
      # fussy and brittle).
      #
      # However, we still want to run the steps above, to check that the UpdateAlways
      # process is able to run to completion, since it can pick up errors in the man pages.

      - name: Commit and push, if not in a forked repo
        if: github.event_name != 'pull_request' || ( ! github.event.pull_request.head.repo.fork && github.actor != 'dependabot[bot]' )
        run: |
          if [ -n "`git status --porcelain`" ] ; then
            # Dirty working tree: push it
            git config user.name "github-actions[bot]"
            git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
            git add -u
            git commit -m "Sync autogenerated files #noupdate"
            git push
          fi

  sync-docs:
    name: Sync content from master to pages
    runs-on: ubuntu-latest
    if: github.event_name != 'pull_request' && github.ref == 'refs/heads/master'
    needs: ['sync-autogenerated']
    permissions:
      contents: write
    steps:
      - name: Checkout
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
        with:
          submodules: false
          fetch-depth: 0 # Necessary to get both the master and pages branches
          fetch-tags: false
          ref: pages

      - name: Commit and push, if docs have changed
        run: |
          if ! git diff --exit-code origin/master -- \
               ./doc ./AUTHORS.md ./LICENCE.md ./SECURITY.md ./README.md \
               ./README ./NON-AUTOTOOLS-BUILD >/dev/null ; then
            # Differences from master: merge and push
            git config user.name "github-actions[bot]"
            git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
            git merge origin/master --no-edit -m"Sync content from master to pages"
            git push
          else
            echo "No content changes to sync"
          fi