Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions .fork/sync-upstream.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#!/usr/bin/env bash
# Rebuilds the fork's patch stack on the newest upstream commit.
#
# .fork/sync-upstream.sh [FORK_REF] [UPSTREAM_REF] [BRANCH]
# .fork/sync-upstream.sh --continue
#
# The stack is every non-merge commit on FORK_REF that is not in upstream and
# not in the previous stack's history. On BRANCH, it regenerates the module
# rename with rename-module.sh, cherry-picks the rest in order, and drops
# patches that upstream already contains. It ends with a merge commit whose
# parents are FORK_REF and the new stack and whose tree is the new stack's, so
# merging BRANCH into main never rewrites history. The merge commit message
# links the upstream changes for review and lists go.mod changes.
#
# Prints "up to date" and creates nothing when FORK_REF already contains
# UPSTREAM_REF. On a cherry-pick conflict it stops; resolve the conflict, stage
# the files, and run it again with --continue.
set -euo pipefail

RENAME_SUBJECT="Change module path to github.com/roachadam/utls"

cd "$(git rev-parse --show-toplevel)"
state="$(git rev-parse --absolute-git-dir)/sync-upstream"

if [ "${1:-}" = "--continue" ]; then
if [ ! -d "$state" ]; then
echo "sync-upstream: nothing to continue" >&2
exit 1
fi
if git rev-parse --verify --quiet CHERRY_PICK_HEAD >/dev/null; then
git -c core.editor=true cherry-pick --continue
fi
git log -1 --format=%s "$(cat "$state/current")" >>"$state/applied"
else
FORK=${1:-origin/main}
UPSTREAM=${2:-upstream/master}
BRANCH=${3:-sync/upstream}

# Untracked or modified files would otherwise be committed into the stack.
if [ -n "$(git status --porcelain)" ]; then
echo "sync-upstream: working tree is not clean" >&2
exit 1
fi

fork=$(git rev-parse "$FORK^{commit}")
upstream=$(git rev-parse "$UPSTREAM^{commit}")
base=$(git merge-base "$fork" "$upstream")
if [ "$base" = "$upstream" ]; then
echo "up to date with upstream ${upstream:0:12}"
exit 0
fi

# The previous stack's history is reachable from the first parent of the
# last sync merge. Upstream history is excluded from the search, so an
# upstream commit carrying the trailer cannot pose as one.
exclude=("^$base")
last_sync=$(git log --merges --grep='^Upstream-Sync: ' --format=%H -1 "$fork" "^$upstream")
if [ -n "$last_sync" ]; then
exclude+=("^$last_sync^1")
fi

rm -rf "$state"
mkdir "$state"
echo "$fork" >"$state/fork"
echo "$upstream" >"$state/upstream"
echo "$base" >"$state/base"
: >"$state/todo"
: >"$state/applied"
: >"$state/dropped"
rename=""
while read -r c; do
if [ "$(git log -1 --format=%s "$c")" = "$RENAME_SUBJECT" ]; then
rename=$c
else
echo "$c" >>"$state/todo"
fi
done < <(git rev-list --reverse --no-merges "$fork" "${exclude[@]}")
if [ -z "$rename" ]; then
rm -rf "$state"
echo "sync-upstream: no \"$RENAME_SUBJECT\" commit in the stack" >&2
exit 1
fi

git checkout -q -B "$BRANCH" "$upstream"
git checkout -q "$rename" -- .fork/rename-module.sh
.fork/rename-module.sh
git add -A
git commit -q -C "$rename"
fi

while [ -s "$state/todo" ]; do
c=$(head -n 1 "$state/todo")
tail -n +2 "$state/todo" >"$state/todo.next"
mv "$state/todo.next" "$state/todo"
echo "$c" >"$state/current"
subject=$(git log -1 --format=%s "$c")
before=$(git rev-parse HEAD)
if ! git cherry-pick --empty=drop "$c" >/dev/null; then
echo "sync-upstream: conflict applying ${c:0:12} \"$subject\"." >&2
echo "Resolve it, stage the files, and run: .fork/sync-upstream.sh --continue" >&2
exit 1
fi
if [ "$(git rev-parse HEAD)" = "$before" ]; then
echo "$subject" >>"$state/dropped"
else
echo "$subject" >>"$state/applied"
fi
done

fork=$(cat "$state/fork")
upstream=$(cat "$state/upstream")
base=$(cat "$state/base")
stack=$(git rev-parse HEAD)
gomod=$(git diff --unified=0 "$fork" "$stack" -- go.mod | grep '^[+-][^+-]' | tr -d '`' || true)
{
echo "Sync with upstream ${upstream:0:12}"
echo
echo "Rebuilds the patch stack on refraction-networking/utls ${upstream:0:12}."
echo
echo "Upstream changes ($(git rev-list --count "$base..$upstream") commits):"
echo "https://github.com/refraction-networking/utls/compare/$base...$upstream"
echo
if [ -n "$gomod" ]; then
echo "go.mod changes, review new or bumped dependencies:"
echo
echo '```diff'
echo "$gomod"
echo '```'
else
echo "go.mod: unchanged."
fi
echo
echo "Applied:"
sed 's/^/- /' "$state/applied"
if [ -s "$state/dropped" ]; then
echo
echo "Dropped, already upstream (update PATCHES.md):"
sed 's/^/- /' "$state/dropped"
fi
echo
echo "Upstream-Sync: $upstream"
} >"$state/message"

merge=$(git commit-tree "$stack^{tree}" -p "$fork" -p "$stack" -F "$state/message")
git reset -q --hard "$merge"
rm -rf "$state"
git log -1 --format=%B HEAD
110 changes: 110 additions & 0 deletions .github/workflows/sync-upstream.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Rebuilds the fork's patch stack on the newest upstream master and, once the
# tests pass, proposes it as a pull request. Nothing is merged or approved
# here; see PATCHES.md.
#
# Upstream code runs only in the build job, which has a read-only token and no
# stored credentials. The publish job never runs upstream code: it pushes the
# branch the build job bundled before testing and opens the pull request.
name: "Sync upstream"

on:
schedule:
- cron: "17 6 * * 1"
workflow_dispatch:

permissions: {}

concurrency:
group: sync-upstream

env:
BRANCH: sync/upstream

defaults:
run:
shell: bash # -eo pipefail

jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
outputs:
changed: ${{ steps.stack.outputs.changed }}
sha256: ${{ steps.stack.outputs.sha256 }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
persist-credentials: false
- uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
with:
go-version: "1.27.x"
cache: false
- name: Rebuild patch stack
id: stack
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git remote add upstream https://github.com/refraction-networking/utls.git
git fetch --no-tags upstream master
.fork/sync-upstream.sh origin/main upstream/master "$BRANCH"
if git rev-parse --verify --quiet "refs/heads/$BRANCH" >/dev/null; then
git bundle create "$RUNNER_TEMP/sync.bundle" origin/main.."$BRANCH"
echo "sha256=$(sha256sum "$RUNNER_TEMP/sync.bundle" | cut -d' ' -f1)" >>"$GITHUB_OUTPUT"
echo "changed=true" >>"$GITHUB_OUTPUT"
fi
# Hashed and uploaded before any upstream code runs. The publish job
# rejects a bundle whose hash differs from the one recorded here.
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
if: steps.stack.outputs.changed == 'true'
with:
name: sync-bundle
path: ${{ runner.temp }}/sync.bundle
retention-days: 7
- name: Vet
if: steps.stack.outputs.changed == 'true'
run: go vet ./...
- name: Test
if: steps.stack.outputs.changed == 'true'
run: go test -race ./...

publish:
needs: build
if: needs.build.outputs.changed == 'true'
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
env:
GH_TOKEN: ${{ github.token }}
BUNDLE_SHA256: ${{ needs.build.outputs.sha256 }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: main
fetch-depth: 0
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
name: sync-bundle
path: ${{ runner.temp }}
- name: Push branch and open pull request
run: |
# Never change a pull request that is under review.
if [ "$(gh pr list --head "$BRANCH" --state open --json number --jq length)" != "0" ]; then
echo "::notice::A sync pull request is already open. Merge or close it, then rerun to get a newer sync."
exit 0
fi
echo "$BUNDLE_SHA256 $RUNNER_TEMP/sync.bundle" | sha256sum --check --strict -
git bundle verify "$RUNNER_TEMP/sync.bundle"
git fetch "$RUNNER_TEMP/sync.bundle" "refs/heads/$BRANCH:refs/heads/$BRANCH"
# The tip must be a sync merge on top of main, nothing else.
test "$(git rev-parse "$BRANCH^1")" = "$(git rev-parse origin/main)"
grep -q '^Upstream-Sync: ' <<<"$(git log -1 --format=%B "$BRANCH")"
git push --force origin "$BRANCH"
body="$(git log -1 --format=%b "$BRANCH")

Review the upstream changes before merging. Merge with **Create a merge commit**; squash or rebase loses the stack the next sync starts from."
if ! gh pr create --base main --head "$BRANCH" --title "$(git log -1 --format=%s "$BRANCH")" --body "$body"; then
echo "::warning::Pushed $BRANCH but could not open the pull request. Open it from https://github.com/${GITHUB_REPOSITORY}/compare/main...$BRANCH"
fi