🏡 index : quine-global/pocketbase.git

name: Sync upstream

on:
  schedule:
    - cron: "17 4 * * *"
  workflow_dispatch:

permissions:
  contents: write

concurrency:
  group: sync-upstream

jobs:
  sync:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - repo: pocketbase
            upstream: pocketbase/pocketbase
          - repo: dbx
            upstream: pocketbase/dbx
          - repo: fexpr
            upstream: fexpr/fexpr
          - repo: ozzo-validation
            upstream: pocketbase/ozzo-validation
          - repo: tygoja
            upstream: pocketbase/tygoja
    name: ${{ matrix.repo }}
    steps:
      - name: Mirror upstream master and tags
        env:
          UPSTREAM: https://ganigeorgiev.com/git/${{ matrix.upstream }}
          # github.token cannot push to other repos or push tags that change .github/workflows, so a PAT is required.
          FORK: https://x-access-token:${{ secrets.SYNC_TOKEN }}@github.com/${{ github.repository_owner }}/${{ matrix.repo }}.git
        run: |
          git init -q mirror
          cd mirror
          git remote add upstream "$UPSTREAM"
          git remote add fork "$FORK"

          # Fetch the fork first so upstream only has to send the new commits.
          git fetch --no-tags fork master
          git fetch --tags upstream master

          # No --force: if master ever diverges from upstream, the push fails instead of rewriting history.
          git push fork FETCH_HEAD:refs/heads/master
          git ls-remote --tags --refs fork | awk '{print $2}' | sort > existing
          git for-each-ref --format='%(refname)' refs/tags | sort | comm -23 - existing | xargs -r -n 50 git push fork