|
| 1 | +# Monorepo-style subsplit: replays every commit that touches turbo-ext/ onto |
| 2 | +# https://github.com/phpstan/turbo-ext, where the extension is distributed |
| 3 | +# standalone (the phpstan/turbo PIE package on Packagist). |
| 4 | +# |
| 5 | +# Each replayed commit keeps the original message, author, committer and |
| 6 | +# dates, and its tree is the commit's turbo-ext/ subtree — plus a |
| 7 | +# "Split-From: <monorepo sha>" trailer and a generated VERSION.txt carrying |
| 8 | +# the extension version at that commit (the monorepo short SHA the enabler |
| 9 | +# pins; builds in the split cannot compute it — replayed commits have |
| 10 | +# different SHAs, and PIE tarballs have no git at all). The trailer is what |
| 11 | +# makes the replay incremental and idempotent: a run reads the most recent |
| 12 | +# trailer from the split repository and replays only the commits made since, |
| 13 | +# on top of the split branch's current head. A failed or skipped run is |
| 14 | +# simply caught up by the next one (or by a manual workflow_dispatch). |
| 15 | +# git subtree split is not used because it walks the entire monorepo history |
| 16 | +# on every run, which takes tens of minutes at this repository's size. |
| 17 | +# |
| 18 | +# When a new maintained branch (2.3.x, ...) starts, update the branch trigger |
| 19 | +# below (like phar.yml) and create the same-named branch in phpstan/turbo-ext |
| 20 | +# from the previous branch's head — that head's trailer keeps the replay |
| 21 | +# incremental across the branch point. |
| 22 | + |
| 23 | +name: "Subsplit turbo-ext" |
| 24 | + |
| 25 | +on: |
| 26 | + workflow_dispatch: |
| 27 | + push: |
| 28 | + branches: |
| 29 | + - "2.2.x" |
| 30 | + paths: |
| 31 | + - "turbo-ext/**" |
| 32 | + - ".github/workflows/subsplit-turbo-ext.yml" |
| 33 | + |
| 34 | +# The replay chain builds on the split branch's current head; two runs |
| 35 | +# building on the same head would race each other, so queue them instead. |
| 36 | +concurrency: |
| 37 | + group: subsplit-turbo-ext |
| 38 | + cancel-in-progress: false |
| 39 | + |
| 40 | +permissions: {} |
| 41 | + |
| 42 | +jobs: |
| 43 | + subsplit: |
| 44 | + name: "Replay turbo-ext commits" |
| 45 | + runs-on: "ubuntu-latest" |
| 46 | + timeout-minutes: 10 |
| 47 | + |
| 48 | + steps: |
| 49 | + - name: Harden the runner (Audit all outbound calls) |
| 50 | + uses: step-security/harden-runner@9af89fc71515a100421586dfdb3dc9c984fbf411 # v2.19.4 |
| 51 | + with: |
| 52 | + egress-policy: audit |
| 53 | + |
| 54 | + - name: "Checkout" |
| 55 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 56 | + with: |
| 57 | + persist-credentials: false |
| 58 | + fetch-depth: 0 # the replay walks the full history of the branch |
| 59 | + |
| 60 | + - name: "Replay commits touching turbo-ext/ and push them to phpstan/turbo-ext" |
| 61 | + env: |
| 62 | + PUSH_TOKEN: ${{ secrets.PHPSTAN_BOT_TOKEN }} |
| 63 | + BRANCH: ${{ github.ref_name }} |
| 64 | + run: | |
| 65 | + PREFIX="turbo-ext" |
| 66 | +
|
| 67 | + # The split branch must already exist (see the header comment); the |
| 68 | + # fetch fails loudly when it does not. |
| 69 | + git fetch --no-tags https://github.com/phpstan/turbo-ext.git "refs/heads/$BRANCH" |
| 70 | + target_head="$(git rev-parse FETCH_HEAD)" |
| 71 | +
|
| 72 | + # The most recent Split-From trailer marks where the previous |
| 73 | + # replay stopped. No trailer anywhere (the split repository only |
| 74 | + # has its initial commit) means the full history gets replayed. |
| 75 | + last_synced="$(git log FETCH_HEAD --format='%(trailers:key=Split-From,valueonly)' | grep -m1 . || true)" |
| 76 | + if [ -n "$last_synced" ] && ! git merge-base --is-ancestor "$last_synced" HEAD; then |
| 77 | + echo "::error::$BRANCH does not contain $last_synced, the most recent Split-From trailer in the split repository. Was the branch history rewritten?" |
| 78 | + exit 1 |
| 79 | + fi |
| 80 | +
|
| 81 | + # Every commit touching turbo-ext/ so far sits on the first-parent |
| 82 | + # chain (verified July 2026), and first-parent order is append-only, |
| 83 | + # so the replay never reorders. A maintained-branch forward merge |
| 84 | + # that touches turbo-ext/ replays as the merge commit itself. |
| 85 | + mapfile -t commits < <(git rev-list --reverse --first-parent ${last_synced:+"$last_synced.."}HEAD -- "$PREFIX") |
| 86 | + if [ "${#commits[@]}" -eq 0 ]; then |
| 87 | + echo "The split repository is up to date at $target_head." |
| 88 | + exit 0 |
| 89 | + fi |
| 90 | +
|
| 91 | + parent="$target_head" |
| 92 | + for commit in "${commits[@]}"; do |
| 93 | + # The version at this commit, mirroring the Makefile's git |
| 94 | + # computation: the last commit touching turbo-ext/src or a |
| 95 | + # non-vendored PHP class from the manifest as it existed at this |
| 96 | + # commit ("dev" before that layout existed). |
| 97 | + watched=("$PREFIX/src") |
| 98 | + while IFS= read -r mapped; do |
| 99 | + watched+=("$mapped") |
| 100 | + done < <(git show "$commit:$PREFIX/shadowed-classes.json" 2>/dev/null | jq -r '.[] | select(.vendored != true) | .php' 2>/dev/null || true) |
| 101 | + version="$(git log -1 --format=%H "$commit" -- "${watched[@]}" | cut -c1-7)" |
| 102 | + blob="$(printf '%s\n' "${version:-dev}" | git hash-object -w --stdin)" |
| 103 | + # a commit removing the directory altogether replays as an empty tree |
| 104 | + if subtree="$(git rev-parse -q --verify "$commit:$PREFIX")"; then |
| 105 | + tree="$({ git ls-tree "$subtree" | awk -F'\t' '$2 != "VERSION.txt"'; printf '100644 blob %s\tVERSION.txt\n' "$blob"; } | git mktree)" |
| 106 | + else |
| 107 | + tree="$(git hash-object -t tree /dev/null)" |
| 108 | + fi |
| 109 | + parent="$( |
| 110 | + git log -1 --format=%B "$commit" \ |
| 111 | + | git interpret-trailers --trailer "Split-From: $commit" \ |
| 112 | + | GIT_AUTHOR_NAME="$(git log -1 --format=%an "$commit")" \ |
| 113 | + GIT_AUTHOR_EMAIL="$(git log -1 --format=%ae "$commit")" \ |
| 114 | + GIT_AUTHOR_DATE="$(git log -1 --format=%aD "$commit")" \ |
| 115 | + GIT_COMMITTER_NAME="$(git log -1 --format=%cn "$commit")" \ |
| 116 | + GIT_COMMITTER_EMAIL="$(git log -1 --format=%ce "$commit")" \ |
| 117 | + GIT_COMMITTER_DATE="$(git log -1 --format=%cD "$commit")" \ |
| 118 | + git commit-tree "$tree" -p "$parent" |
| 119 | + )" |
| 120 | + echo "$commit -> $parent" |
| 121 | + done |
| 122 | +
|
| 123 | + echo "Replayed ${#commits[@]} commit(s), pushing to $BRANCH" |
| 124 | + git push "https://x-access-token:${PUSH_TOKEN}@github.com/phpstan/turbo-ext.git" "$parent:refs/heads/$BRANCH" |
0 commit comments