diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1c35a31..c88bcd1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -82,7 +82,8 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - run: shellcheck -x upgrade.sh bump.sh sigstore.sh interop.sh suite/run.sh + - run: shellcheck -x upgrade.sh bump.sh release-branch.sh sigstore.sh interop.sh suite/run.sh tests/release-branch.sh + - run: tests/release-branch.sh - name: actionlint run: | bash <(curl -sSfL https://raw.githubusercontent.com/rhysd/actionlint/914e7df21a07ef503a81201c76d2b11c789d3fca/scripts/download-actionlint.bash) 1.7.12 diff --git a/.github/workflows/sqlite3mc-release.yml b/.github/workflows/sqlite3mc-release.yml index 9bb503c..0df2b52 100644 --- a/.github/workflows/sqlite3mc-release.yml +++ b/.github/workflows/sqlite3mc-release.yml @@ -31,10 +31,12 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 30 steps: - # A manual dispatch may select another ref, and only main's tree belongs here. + # A manual dispatch may select another ref, and only main's tree belongs here. The full history lets + # release-branch.sh tell human commits on a release branch from the bot's. - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: ref: main + fetch-depth: 0 persist-credentials: false - name: Pick the release @@ -96,38 +98,8 @@ jobs: VERSION: ${{ steps.release.outputs.target }} run: | set -euo pipefail - sqlite=$(sed -nE 's/^pub const SQLITE_VERSION: &str = "(.*)";$/\1/p' src/lib.rs) - branch="sqlite3mc/v$VERSION" - title="Vendor SQLite3MC $VERSION on SQLite $sqlite" bot_id=$(gh api "/users/${APP_SLUG}%5Bbot%5D" --jq .id) git config user.name "${APP_SLUG}[bot]" git config user.email "${bot_id}+${APP_SLUG}[bot]@users.noreply.github.com" git remote set-url origin "https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" - git checkout -b "$branch" - git add Cargo.toml Cargo.lock smoke/Cargo.lock fuzz/Cargo.lock upgrade.sh src/lib.rs sqlite3mc - git commit -m "$title" - # An unchanged push would only rerun every check, so the branch moves only when its - # content changed or main moved under it. - if git ls-remote --exit-code --heads origin "$branch" >/dev/null; then - git fetch --depth=2 origin "$branch":refs/remotes/origin/"$branch" - if [ "$(git rev-parse "origin/$branch^{tree}")" = "$(git rev-parse "HEAD^{tree}")" ] \ - && [ "$(git rev-parse "origin/$branch^")" = "$(git rev-parse HEAD^)" ]; then - echo "$branch already holds this content on the current main" - else - git push --force-with-lease origin HEAD:refs/heads/"$branch" - fi - else - git push origin HEAD:refs/heads/"$branch" - fi - open=$(gh pr list --head "$branch" --state open --json number --jq length) - [ "$open" -gt 0 ] || gh pr create \ - --base main --head "$branch" \ - --title "$title" \ - --body "SQLite3MC $VERSION, from https://github.com/utelle/SQLite3MultipleCiphers/releases/tag/v$VERSION. The archive's checksum was taken from the release's SHA256SUMS after its Sigstore signature verified against SQLite3MC's own release workflow, and the vendored files were extracted from the archive unchanged." - # Every other release branch is superseded, whether its pull request is open or closed. - gh pr list --state open --json number,headRefName \ - --jq ".[] | select(.headRefName | startswith(\"sqlite3mc/v\")) | select(.headRefName != \"$branch\") | .number" \ - | while read -r n; do gh pr close "$n" --comment "Superseded by SQLite3MC $VERSION."; done - git ls-remote --heads origin 'refs/heads/sqlite3mc/v*' | cut -f2 \ - | { grep -vx "refs/heads/$branch" || true; } \ - | while read -r ref; do git push origin --delete "$ref"; done + ./release-branch.sh "$VERSION" diff --git a/release-branch.sh b/release-branch.sh new file mode 100755 index 0000000..0333aaf --- /dev/null +++ b/release-branch.sh @@ -0,0 +1,133 @@ +#!/bin/sh -e +# Commits the vendored SQLite3MC VERSION onto its release branch, opens its pull request and retires every other +# release branch. A commit whose author or committer is not this bot is human work, which no push ever drops. +VERSION=${1:?usage: release-branch.sh VERSION} + +cd "$(dirname "$0")" +sqlite=$(sed -nE 's/^pub const SQLITE_VERSION: &str = "(.*)";$/\1/p' src/lib.rs) +branch="sqlite3mc/v$VERSION" +title="Vendor SQLite3MC $VERSION on SQLite $sqlite" +bot=$(git config user.email) +retired=$(mktemp) +trap 'rm -f "$retired"' EXIT + +git fetch --quiet --prune origin main '+refs/heads/sqlite3mc/v*:refs/remotes/origin/sqlite3mc/v*' +main=$(git rev-parse HEAD) +git checkout --quiet -b "$branch" +git add Cargo.toml Cargo.lock smoke/Cargo.lock fuzz/Cargo.lock upgrade.sh src/lib.rs sqlite3mc +git commit --quiet -m "$title" +fresh=$(git rev-parse HEAD) + +# Prints the commits in main..$1 whose author or committer is not this bot, oldest first. +human_commits() { + git log --reverse --format='%H %ae %ce' "$main..$1" | awk -v bot="$bot" '$2 != bot || $3 != bot { print $1 }' +} + +# Prints the last commit in main..$1 this bot both wrote and committed. +bot_commit() { + git log --format='%H %ae %ce' "$main..$1" | awk -v bot="$bot" '$2 == bot && $3 == bot { print $1; exit }' +} + +# Names the files whose change in the fresh commit differs from their change in commit $1, or every file the +# fresh commit changes when $1 is empty. +changed_files() { + if [ -z "$1" ]; then + git diff --name-only "$fresh^" "$fresh" | sed 's/^/ /' | tr -d '\n' + return + fi + for file in $({ git diff --name-only "$fresh^" "$fresh" && git diff --name-only "$1^" "$1"; } | sort -u); do + [ "$(git diff "$fresh^" "$fresh" -- "$file" | git patch-id --stable)" = \ + "$(git diff "$1^" "$1" -- "$file" | git patch-id --stable)" ] || printf ' %s' "$file" + done +} + +# Cherry-picks the human commits of $1 onto HEAD, dropping those main already holds. On a merge commit or a +# conflict it names that commit in $stuck, puts HEAD back and fails. +replay() { + start=$(git rev-parse HEAD) + stuck=$(git rev-list --merges -n 1 "$main..$1") + [ -z "$stuck" ] || return 1 + for commit in $(human_commits "$1"); do + if ! git cherry-pick --no-commit "$commit" > /dev/null 2>&1; then + stuck=$commit + git reset --quiet --hard "$start" + return 1 + fi + git diff --cached --quiet || git commit --quiet --reuse-message="$commit" + done +} + +open_pr() { + gh pr list --head "$1" --state open --json number --jq '.[0].number // empty' +} + +# Comments $3 on the open pull request of $1 unless an earlier comment there carries the marker $2. +comment_once() { + number=$(open_pr "$1") + [ -n "$number" ] || return 0 + gh pr view "$number" --json comments --jq '.comments[].body' | grep -qF "$2" || + gh pr comment "$number" --body "$3 $2" +} + +old=$(git rev-parse -q --verify "refs/remotes/origin/$branch" || true) +if [ -z "$old" ]; then + echo "$branch is new" +elif [ -z "$(human_commits "$old")" ]; then + if [ "$(git rev-parse "$old^{tree}")" = "$(git rev-parse "$fresh^{tree}")" ] && [ "$(git rev-parse "$old^")" = "$main" ]; then + echo "$branch already holds this release on the current main" + git reset --quiet --hard "$old" + else + echo "$branch moves to this release on the current main" + fi +elif [ "$(git merge-base "$old" "$main")" = "$main" ]; then + changed=$(changed_files "$(bot_commit "$old")") + git reset --quiet --hard "$old" + if [ -z "$changed" ]; then + echo "$branch already holds this release and commits of its own on the current main" + else + echo "$branch keeps its own commits over a release that differs in$changed" + comment_once "$branch" "" \ + "The release vendored today differs from this branch's in$changed, and this branch has commits of its own, so it was left as it is." + fi +elif replay "$old"; then + echo "$branch moves to this release on the current main, with its own commits" +else + git reset --quiet --hard "$old" + echo "$branch stays, because $stuck does not apply on the current main" + comment_once "$branch" "" \ + "Main moved to $main, and $stuck does not apply onto this release vendored there, so this branch was left as it is." +fi + +# Older releases first, so their commits land in the order they were written. +for ref in $(git for-each-ref --format='%(refname:strip=3)' 'refs/remotes/origin/sqlite3mc/v*' | sort -V); do + [ "$ref" != "$branch" ] || continue + number=$(open_pr "$ref") + if [ -z "$(human_commits "origin/$ref")" ]; then + echo "$ref ${number:--} retire" >> "$retired" + elif [ -z "$number" ]; then + echo "$ref has commits of its own and no open pull request, so it stays" + elif replay "origin/$ref"; then + echo "$ref $number carried" >> "$retired" + else + echo "$ref $number stuck $stuck" >> "$retired" + fi +done + +# The release branch is pushed before any branch whose commits it carries is deleted. +[ "$(git rev-parse HEAD)" = "$old" ] || + git push --quiet --force-with-lease="refs/heads/$branch:$old" origin "HEAD:refs/heads/$branch" +[ -n "$(open_pr "$branch")" ] || gh pr create --base main --head "$branch" --title "$title" \ + --body "SQLite3MC $VERSION, from https://github.com/utelle/SQLite3MultipleCiphers/releases/tag/v$VERSION. The archive's checksum was taken from the release's SHA256SUMS after its Sigstore signature verified against SQLite3MC's own release workflow, and the vendored files were extracted from the archive unchanged." + +while read -r ref number outcome stuck; do + case $outcome in + retire) + [ "$number" = - ] || gh pr close "$number" --comment "Superseded by SQLite3MC $VERSION." ;; + carried) + gh pr close "$number" --comment "Superseded by SQLite3MC $VERSION, whose branch $branch carries this branch's commits." ;; + stuck) + gh pr close "$number" --comment "Superseded by SQLite3MC $VERSION. $stuck does not apply onto $branch, so this branch is kept." + continue ;; + esac + git push --quiet --force-with-lease="refs/heads/$ref:$(git rev-parse "origin/$ref")" origin ":refs/heads/$ref" +done < "$retired" diff --git a/tests/release-branch.sh b/tests/release-branch.sh new file mode 100755 index 0000000..2cb0f41 --- /dev/null +++ b/tests/release-branch.sh @@ -0,0 +1,229 @@ +#!/bin/sh -e +# Drives release-branch.sh against a local bare remote and a fake gh, one case per situation it handles. + +ROOT=$(cd "$(dirname "$0")/.." && pwd) +WORK=$(mktemp -d) +trap 'rm -rf "$WORK"' EXIT +# The user's git configuration, such as commit signing, stays out of the cases. +GIT_CONFIG_GLOBAL=/dev/null +GIT_CONFIG_NOSYSTEM=1 +export GIT_CONFIG_GLOBAL GIT_CONFIG_NOSYSTEM + +BOT_EMAIL="1+release[bot]@users.noreply.github.com" +OLD=sqlite3mc/v2.7.0 +NEW=sqlite3mc/v2.7.1 +failed="" + +mkdir "$WORK/bin" +cat > "$WORK/bin/gh" <<'EOF' +#!/bin/sh -e +# Answers release-branch.sh's gh calls from the files in $GH_STATE, logging every write. +prs=$GH_STATE/prs +comments=$GH_STATE/comments +sub="$1 $2" +shift 2 +case $sub in +"pr list") + while [ $# -gt 0 ]; do [ "$1" != --head ] || head=$2; shift; done + awk -v head="$head" '$2 == head && $3 == "open" { print $1; exit }' "$prs" ;; +"pr view") + awk -F '\t' -v n="$1" '$1 == n { print $2 }' "$comments" ;; +"pr comment") + printf '%s\t%s\n' "$1" "$3" >> "$comments" + echo "comment $1 $3" >> "$GH_STATE/log" ;; +"pr close") + printf '%s\t%s\n' "$1" "$3" >> "$comments" + awk -v n="$1" '$1 == n { $3 = "closed" } { print }' "$prs" > "$prs.new" + mv "$prs.new" "$prs" + echo "close $1 $3" >> "$GH_STATE/log" ;; +"pr create") + while [ $# -gt 0 ]; do [ "$1" != --head ] || head=$2; shift; done + echo "$(($(wc -l < "$prs") + 1)) $head open" >> "$prs" + echo "create $head" >> "$GH_STATE/log" ;; +*) + echo "fake gh: unexpected gh $sub $*" >&2 + exit 1 ;; +esac +EOF +chmod +x "$WORK/bin/gh" + +# Commits as a human in the current directory. +human_commit() { + git add -A + GIT_AUTHOR_NAME=Luca GIT_AUTHOR_EMAIL=luca@example.com GIT_COMMITTER_NAME=Luca \ + GIT_COMMITTER_EMAIL=luca@example.com git commit --quiet "$@" +} + +# Starts case $1 with a remote whose main holds a vendored 2.6.1 and no release branch. +start() { + CASE=$WORK/$1 + mkdir -p "$CASE/gh" "$CASE/seed" + : > "$CASE/gh/prs" + : > "$CASE/gh/comments" + : > "$CASE/gh/log" + git init --quiet --bare -b main "$CASE/remote.git" + cd "$CASE/seed" + git init --quiet -b main + mkdir src sqlite3mc smoke fuzz + echo 'pub const SQLITE_VERSION: &str = "3.54.1";' > src/lib.rs + echo "release 2.6.1" > sqlite3mc/amalgamation.c + for file in Cargo.toml Cargo.lock smoke/Cargo.lock fuzz/Cargo.lock upgrade.sh README.md; do echo "$file" > "$file"; done + human_commit -m "Vendor SQLite3MC 2.6.1" + git push --quiet "$CASE/remote.git" main +} + +# Runs the bot on a fresh checkout of main, vendoring release $1 with amalgamation text $2. +bot() { + cd "$CASE" + rm -rf "$CASE/run" + git clone --quiet "$CASE/remote.git" "$CASE/run" + cd "$CASE/run" + git config user.name "release[bot]" + git config user.email "$BOT_EMAIL" + echo "${2:-release $1}" > sqlite3mc/amalgamation.c + cp "$ROOT/release-branch.sh" . + PATH="$WORK/bin:$PATH" GH_STATE="$CASE/gh" ./release-branch.sh "$1" > "$CASE/out" 2>&1 || + { echo "release-branch.sh $1 failed:"; cat "$CASE/out"; } +} + +# Commits file $2 with text $3 as a human on remote branch $1, merging main in first when $4 is merge. +push_as_human() { + cd "$CASE" + rm -rf "$CASE/human" + git clone --quiet --branch "$1" "$CASE/remote.git" "$CASE/human" + cd "$CASE/human" + if [ "${4:-}" = merge ]; then + GIT_AUTHOR_NAME=Luca GIT_AUTHOR_EMAIL=luca@example.com GIT_COMMITTER_NAME=Luca \ + GIT_COMMITTER_EMAIL=luca@example.com git merge --quiet --no-edit origin/main + fi + echo "$3" > "$2" + human_commit -m "Change $2 on $1" + git push --quiet origin "$1" +} + +remote() { git --git-dir="$CASE/remote.git" "$@"; } +tip() { remote rev-parse -q --verify "refs/heads/$1" || true; } +subjects() { remote log --format=%s "main..$1" | tr '\n' '|'; } +logged() { grep -c "$1" "$CASE/gh/log" || true; } + +# Records a failure of case $1, described by $2, unless the observed $3 equals the wanted $4. +expect() { + if [ "$3" != "$4" ]; then + printf 'FAIL %s %s\n got: %s\n want: %s\n' "$1" "$2" "$3" "$4" + sed 's/^/ /' "$CASE/out" + failed="$failed $1" + fi +} + +start new-branch +bot 2.7.1 +expect new-branch "the release is one commit on main" "$(subjects $NEW)" "Vendor SQLite3MC 2.7.1 on SQLite 3.54.1|" +expect new-branch "the branch holds the release" "$(remote show $NEW:sqlite3mc/amalgamation.c)" "release 2.7.1" +expect new-branch "its pull request opens" "$(logged "^create $NEW$")" 1 + +start unchanged +bot 2.7.1 +before=$(tip $NEW) +bot 2.7.1 +expect unchanged "the branch stays" "$(tip $NEW)" "$before" +expect unchanged "no second pull request opens" "$(logged "^create")" 1 + +start release-changed +bot 2.7.1 "release 2.7.1 first upload" +bot 2.7.1 "release 2.7.1 second upload" +expect release-changed "the branch holds the new upload" "$(remote show $NEW:sqlite3mc/amalgamation.c)" "release 2.7.1 second upload" +expect release-changed "the release is still one commit" "$(remote rev-list --count main..$NEW)" 1 + +start main-moved +bot 2.7.1 +push_as_human main README.md "main moved" +bot 2.7.1 +expect main-moved "the branch sits on the current main" "$(remote rev-parse $NEW^)" "$(tip main)" +expect main-moved "the release is still one commit" "$(remote rev-list --count main..$NEW)" 1 + +start fix-kept +bot 2.7.1 +push_as_human $NEW fix.txt fixed +before=$(tip $NEW) +bot 2.7.1 +expect fix-kept "the branch with its fix stays" "$(tip $NEW)" "$before" + +start fix-kept-release-changed +bot 2.7.1 +push_as_human $NEW fix.txt fixed +before=$(tip $NEW) +bot 2.7.1 "release 2.7.1 second upload" +bot 2.7.1 "release 2.7.1 second upload" +expect fix-kept-release-changed "the branch with its fix stays" "$(tip $NEW)" "$before" +expect fix-kept-release-changed "one comment names the changed file" "$(logged "^comment 1 .*sqlite3mc/amalgamation.c")" 1 + +start release-commit-amended +bot 2.7.1 +cd "$CASE" && git clone --quiet --branch $NEW "$CASE/remote.git" amend && cd amend +GIT_COMMITTER_NAME=Luca GIT_COMMITTER_EMAIL=luca@example.com git commit --quiet --amend -m "Vendor SQLite3MC 2.7.1, amended" +git push --quiet --force origin $NEW +before=$(tip $NEW) +bot 2.7.1 +bot 2.7.1 +expect release-commit-amended "the amended branch stays" "$(tip $NEW)" "$before" +expect release-commit-amended "one comment names the vendored files" "$(logged "^comment 1 .*sqlite3mc/amalgamation.c")" 1 + +start fix-replayed +bot 2.7.1 +push_as_human $NEW fix.txt fixed +push_as_human main README.md "main moved" +bot 2.7.1 +expect fix-replayed "the fix sits on the release on the current main" "$(remote rev-parse $NEW~2)" "$(tip main)" +expect fix-replayed "the fix keeps its text" "$(remote show $NEW:fix.txt)" fixed +expect fix-replayed "the fix keeps its author" "$(remote log -1 --format=%ae $NEW)" luca@example.com +expect fix-replayed "the release is updated" "$(subjects $NEW)" "Change fix.txt on $NEW|Vendor SQLite3MC 2.7.1 on SQLite 3.54.1|" + +start fix-conflicts-with-main +bot 2.7.1 +push_as_human $NEW README.md "branch readme" +push_as_human main README.md "main readme" +before=$(tip $NEW) +bot 2.7.1 +bot 2.7.1 +expect fix-conflicts-with-main "the branch stays" "$(tip $NEW)" "$before" +expect fix-conflicts-with-main "one comment names the commit" "$(logged "^comment 1 .*$before")" 1 + +start merge-commit +bot 2.7.1 +push_as_human main README.md "main moved" +push_as_human $NEW fix.txt fixed merge +push_as_human main upgrade.sh "main moved again" +before=$(tip $NEW) +bot 2.7.1 +bot 2.7.1 +expect merge-commit "the branch stays" "$(tip $NEW)" "$before" +expect merge-commit "one comment names the merge" "$(logged "^comment 1 .*$(remote rev-parse $NEW^)")" 1 + +start superseded +bot 2.7.0 +bot 2.7.1 +expect superseded "the old branch is deleted" "$(tip $OLD)" "" +expect superseded "the old pull request closes" "$(logged "^close 1 Superseded by SQLite3MC 2.7.1.$")" 1 +expect superseded "the new branch holds only its release" "$(remote rev-list --count main..$NEW)" 1 + +start superseded-fix-carried +bot 2.7.0 +push_as_human $OLD fix.txt fixed +bot 2.7.1 +expect superseded-fix-carried "the fix moves to the new branch" "$(subjects $NEW)" "Change fix.txt on $OLD|Vendor SQLite3MC 2.7.1 on SQLite 3.54.1|" +expect superseded-fix-carried "the old branch is deleted" "$(tip $OLD)" "" +expect superseded-fix-carried "the old pull request closes naming the new branch" "$(logged "^close 1 .*$NEW")" 1 + +start superseded-fix-conflicts +bot 2.7.0 +push_as_human $OLD sqlite3mc/amalgamation.c "release 2.7.0 patched" +before=$(tip $OLD) +bot 2.7.1 +bot 2.7.1 +expect superseded-fix-conflicts "the new branch holds only its release" "$(remote rev-list --count main..$NEW)" 1 +expect superseded-fix-conflicts "the old branch stays" "$(tip $OLD)" "$before" +expect superseded-fix-conflicts "the old pull request closes once, naming the commit" "$(logged "^close 1 .*$before")" 1 +expect superseded-fix-conflicts "the closed pull request gets no further comment" "$(logged "^comment 1")" 0 + +[ -z "$failed" ] || { echo "Failed:$failed" >&2; exit 1; } +echo "pass every release branch case"