From ec40d8a81876bca18db4a92620ac89b950b8e0b0 Mon Sep 17 00:00:00 2001 From: Marko Petzold Date: Mon, 26 Oct 2026 08:39:57 +0100 Subject: [PATCH] pr.sh: leave a diverged local main alone after the merge After a release merge, pr.sh switched to the local main and fast-forwarded it. When that main held unpushed commits (or was checked out in another worktree), the pull failed and the script stopped before waiting for the release run. It now takes over the local main only when main can follow origin/main; otherwise it continues detached on origin/main and leaves main as it is. --- scripts/pr.sh | 11 +++++++---- scripts/test_pr.sh | 10 ++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/scripts/pr.sh b/scripts/pr.sh index d764d44..d0705a6 100755 --- a/scripts/pr.sh +++ b/scripts/pr.sh @@ -99,14 +99,17 @@ await_merge() { # await_merge : waits up to 40 minutes die "the pull request of $1 is not merged after 40 minutes: gh pr checks $1" } -# Back on main after the merge; in a worktree main is checked out elsewhere, -# so this continues detached on origin/main. +# Back on main after the merge, when the local main can simply follow +# origin/main. Otherwise (main is checked out in another worktree, or holds +# commits origin/main lacks) this continues detached on origin/main and leaves +# the local main as it is. leave_branch() { git fetch --quiet origin main - if git switch --quiet main 2>/dev/null; then - git pull --quiet --ff-only + if git merge-base --is-ancestor main origin/main 2>/dev/null && git switch --quiet main 2>/dev/null; then + git merge --quiet --ff-only origin/main else git switch --quiet --detach origin/main + echo "==> continuing detached on origin/main; the local main is left as it is" fi git branch -D "$1" >/dev/null 2>&1 || true } diff --git a/scripts/test_pr.sh b/scripts/test_pr.sh index 0020750..4a1d2d0 100755 --- a/scripts/test_pr.sh +++ b/scripts/test_pr.sh @@ -201,6 +201,16 @@ git -C "$repo" switch -q main git -C "$repo" commit -q --allow-empty -m "local only" check "release: refused on a main ahead of origin" 1 release patch x VERSION release.yml VERSION +setup VERSION 1.2.3 +ci_tag v1.2.4 +git -C "$repo" switch -q main +git -C "$repo" commit -q --allow-empty -m "unpushed work on the local main" +local_main=$(git -C "$repo" rev-parse main) +git -C "$repo" switch -q feat +check "release: a local main with unpushed commits does not stop it" 0 release patch x VERSION release.yml VERSION +expect "release: the local main is left as it was" test "$(git -C "$repo" rev-parse main)" = "$local_main" +expect "release: continues detached on origin/main" test "$(git -C "$repo" rev-parse --abbrev-ref HEAD)" = HEAD + setup VERSION 1.2.3 ci_tag v2.0.0 check "release: major" 0 release major x VERSION release.yml VERSION