From 3160f8f6832abb0dc62cc9688f9d50411bdbae42 Mon Sep 17 00:00:00 2001 From: Maxim Geraskin Date: Wed, 25 Feb 2026 13:20:10 +0100 Subject: [PATCH 1/2] Fix cmd_apply branch switch-back handling --- .../2602251153-pr-apply-switch-back/change.md | 11 +++++++++++ .../2602251153-pr-apply-switch-back/how.md | 18 ++++++++++++++++++ .../2602251153-pr-apply-switch-back/impl.md | 12 ++++++++++++ uspecs/u/scripts/_lib/pr.sh | 9 --------- uspecs/u/scripts/conf.sh | 10 ++++++---- 5 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 uspecs/changes/2602251153-pr-apply-switch-back/change.md create mode 100644 uspecs/changes/2602251153-pr-apply-switch-back/how.md create mode 100644 uspecs/changes/2602251153-pr-apply-switch-back/impl.md diff --git a/uspecs/changes/2602251153-pr-apply-switch-back/change.md b/uspecs/changes/2602251153-pr-apply-switch-back/change.md new file mode 100644 index 0000000..1835037 --- /dev/null +++ b/uspecs/changes/2602251153-pr-apply-switch-back/change.md @@ -0,0 +1,11 @@ +--- +registered_at: 2026-02-25T11:53:53Z +change_id: 2602251153-pr-apply-switch-back +baseline: 3ae1eedce4ac7c85de33e2f621ef820b07b4ea79 +--- + +# Change request: conf.sh cmd_apply should switch back to original branch after sending PR + +## Why + +When `cmd_apply` is called on a PR branch, it needs to switch to the default branch, fast-forward it, and then switch back to the original branch. Currently `cmd_ffdefault` in `pr.sh` switches back to the original branch immediately after fast-forwarding, instead of letting the caller control the return flow. diff --git a/uspecs/changes/2602251153-pr-apply-switch-back/how.md b/uspecs/changes/2602251153-pr-apply-switch-back/how.md new file mode 100644 index 0000000..64eb557 --- /dev/null +++ b/uspecs/changes/2602251153-pr-apply-switch-back/how.md @@ -0,0 +1,18 @@ +# How: pr.sh cmd_apply should switch back to original branch after sending PR + +## Approach + +- Fix `cmd_ffdefault` in `pr.sh`: remove the switch-back block and its cleanup trap so the command + stays on the default branch after fast-forwarding. This makes it a clean primitive: switch to + default if needed, fetch, fast-forward, done. + +- Fix `cmd_apply` in `conf.sh`: move the `prev_branch` capture to before the `ffdefault` call. + Currently it is captured after `ffdefault` has already switched back, which is redundant and + obscures intent. With `ffdefault` no longer switching back, the correct branch is captured before + any switching occurs, and `pr.sh pr --next-branch "$prev_branch"` (already present) handles the + final switch-back. + +References: + +- [uspecs/u/scripts/_lib/pr.sh](../../u/scripts/_lib/pr.sh) +- [uspecs/u/scripts/conf.sh](../../u/scripts/conf.sh) diff --git a/uspecs/changes/2602251153-pr-apply-switch-back/impl.md b/uspecs/changes/2602251153-pr-apply-switch-back/impl.md new file mode 100644 index 0000000..ea934f9 --- /dev/null +++ b/uspecs/changes/2602251153-pr-apply-switch-back/impl.md @@ -0,0 +1,12 @@ +# Implementation plan: conf.sh cmd_apply should switch back to original branch after sending PR + +## Construction + +- [x] update: [uspecs/u/scripts/_lib/pr.sh](../../u/scripts/_lib/pr.sh) + - fix: remove switch-back block (lines 211-215) and its cleanup trap (line 200) from `cmd_ffdefault` so it stays on the default branch after fast-forwarding + +- [x] update: [uspecs/u/scripts/conf.sh](../../u/scripts/conf.sh) + - fix: move `prev_branch` capture in `cmd_apply` to before the `ffdefault` call so the original branch is remembered before any switching occurs + +- [x] update: [uspecs/u/scripts/conf.sh](../../u/scripts/conf.sh) + - add: ERR trap in `cmd_apply` after `ffdefault` succeeds to switch back to `prev_branch` on failure; clear trap after `pr.sh pr` succeeds diff --git a/uspecs/u/scripts/_lib/pr.sh b/uspecs/u/scripts/_lib/pr.sh index 224159c..d94e741 100644 --- a/uspecs/u/scripts/_lib/pr.sh +++ b/uspecs/u/scripts/_lib/pr.sh @@ -192,12 +192,9 @@ cmd_ffdefault() { local current_branch current_branch=$(git symbolic-ref --short HEAD) - local switched=false if [[ "$current_branch" != "$default_branch" ]]; then echo "Switching from '$current_branch' to '$default_branch'..." git checkout "$default_branch" - switched=true - trap 'git merge --abort 2>/dev/null || true; git checkout "$current_branch"' EXIT fi echo "Fetching $pr_remote/$default_branch..." @@ -207,12 +204,6 @@ cmd_ffdefault() { if ! git merge --ff-only "$pr_remote/$default_branch" 2>&1; then error "Cannot fast-forward '$default_branch' to '$pr_remote/$default_branch'. The branches have diverged." fi - - if [[ "$switched" == "true" ]]; then - trap - EXIT - echo "Switching back to '$current_branch'..." - git checkout "$current_branch" - fi } cmd_pr() { diff --git a/uspecs/u/scripts/conf.sh b/uspecs/u/scripts/conf.sh index e100a23..09ffcfd 100644 --- a/uspecs/u/scripts/conf.sh +++ b/uspecs/u/scripts/conf.sh @@ -581,9 +581,12 @@ cmd_apply() { error "uspecs is already installed, use update instead" fi - # PR: fast-forward default branch (may update local uspecs.yml) + # PR: remember current branch, then fast-forward default branch (may update local uspecs.yml) + local prev_branch="" if [[ "$pr_flag" == "true" ]]; then + prev_branch=$(cd "$project_dir" && git symbolic-ref --short HEAD) (cd "$project_dir" && bash "$script_dir/_lib/pr.sh" ffdefault) + trap 'git -C "$project_dir" checkout "$prev_branch" 2>/dev/null || true' ERR fi local -A config @@ -610,11 +613,9 @@ cmd_apply() { show_operation_plan "$command_name" "$current_version" "$version" "$commit" "$commit_timestamp" "$plan_invocation_methods_str" "$pr_flag" "$project_dir" "$script_dir" confirm_action "$command_name" || return 0 - # PR: capture current branch, then create feature branch + # PR: create feature branch local branch_name="${command_name}-uspecs-${version_string_branch}" - local prev_branch="" if [[ "$pr_flag" == "true" ]]; then - prev_branch=$(cd "$project_dir" && git symbolic-ref --short HEAD) (cd "$project_dir" && bash "$script_dir/_lib/pr.sh" prbranch "$branch_name") fi @@ -664,6 +665,7 @@ cmd_apply() { # Capture PR info from stderr while showing normal output (cd "$project_dir" && bash "$script_dir/_lib/pr.sh" pr --title "$pr_title" --body "$pr_body" \ --next-branch "$prev_branch" --delete-branch) 2> "$pr_info_file" + trap - ERR # Parse PR info from temp file pr_url=$(_grep '^PR_URL=' "$pr_info_file" | cut -d= -f2-) From 45db9615f21a82fb7996f3b69e131fe1b99196c4 Mon Sep 17 00:00:00 2001 From: Maxim Geraskin Date: Wed, 25 Feb 2026 13:22:04 +0100 Subject: [PATCH 2/2] archive uspecs/changes/2602251153-pr-apply-switch-back to uspecs/changes/archive/2602/2602251222-pr-apply-switch-back --- .../2602/2602251222-pr-apply-switch-back}/change.md | 1 + .../2602/2602251222-pr-apply-switch-back}/how.md | 4 ++-- .../2602/2602251222-pr-apply-switch-back}/impl.md | 6 +++--- 3 files changed, 6 insertions(+), 5 deletions(-) rename uspecs/changes/{2602251153-pr-apply-switch-back => archive/2602/2602251222-pr-apply-switch-back}/change.md (94%) rename uspecs/changes/{2602251153-pr-apply-switch-back => archive/2602/2602251222-pr-apply-switch-back}/how.md (85%) rename uspecs/changes/{2602251153-pr-apply-switch-back => archive/2602/2602251222-pr-apply-switch-back}/impl.md (71%) diff --git a/uspecs/changes/2602251153-pr-apply-switch-back/change.md b/uspecs/changes/archive/2602/2602251222-pr-apply-switch-back/change.md similarity index 94% rename from uspecs/changes/2602251153-pr-apply-switch-back/change.md rename to uspecs/changes/archive/2602/2602251222-pr-apply-switch-back/change.md index 1835037..4bc8de0 100644 --- a/uspecs/changes/2602251153-pr-apply-switch-back/change.md +++ b/uspecs/changes/archive/2602/2602251222-pr-apply-switch-back/change.md @@ -2,6 +2,7 @@ registered_at: 2026-02-25T11:53:53Z change_id: 2602251153-pr-apply-switch-back baseline: 3ae1eedce4ac7c85de33e2f621ef820b07b4ea79 +archived_at: 2026-02-25T12:22:03Z --- # Change request: conf.sh cmd_apply should switch back to original branch after sending PR diff --git a/uspecs/changes/2602251153-pr-apply-switch-back/how.md b/uspecs/changes/archive/2602/2602251222-pr-apply-switch-back/how.md similarity index 85% rename from uspecs/changes/2602251153-pr-apply-switch-back/how.md rename to uspecs/changes/archive/2602/2602251222-pr-apply-switch-back/how.md index 64eb557..5679310 100644 --- a/uspecs/changes/2602251153-pr-apply-switch-back/how.md +++ b/uspecs/changes/archive/2602/2602251222-pr-apply-switch-back/how.md @@ -14,5 +14,5 @@ References: -- [uspecs/u/scripts/_lib/pr.sh](../../u/scripts/_lib/pr.sh) -- [uspecs/u/scripts/conf.sh](../../u/scripts/conf.sh) +- [uspecs/u/scripts/_lib/pr.sh](../../../../u/scripts/_lib/pr.sh) +- [uspecs/u/scripts/conf.sh](../../../../u/scripts/conf.sh) diff --git a/uspecs/changes/2602251153-pr-apply-switch-back/impl.md b/uspecs/changes/archive/2602/2602251222-pr-apply-switch-back/impl.md similarity index 71% rename from uspecs/changes/2602251153-pr-apply-switch-back/impl.md rename to uspecs/changes/archive/2602/2602251222-pr-apply-switch-back/impl.md index ea934f9..42801df 100644 --- a/uspecs/changes/2602251153-pr-apply-switch-back/impl.md +++ b/uspecs/changes/archive/2602/2602251222-pr-apply-switch-back/impl.md @@ -2,11 +2,11 @@ ## Construction -- [x] update: [uspecs/u/scripts/_lib/pr.sh](../../u/scripts/_lib/pr.sh) +- [x] update: [uspecs/u/scripts/_lib/pr.sh](../../../../u/scripts/_lib/pr.sh) - fix: remove switch-back block (lines 211-215) and its cleanup trap (line 200) from `cmd_ffdefault` so it stays on the default branch after fast-forwarding -- [x] update: [uspecs/u/scripts/conf.sh](../../u/scripts/conf.sh) +- [x] update: [uspecs/u/scripts/conf.sh](../../../../u/scripts/conf.sh) - fix: move `prev_branch` capture in `cmd_apply` to before the `ffdefault` call so the original branch is remembered before any switching occurs -- [x] update: [uspecs/u/scripts/conf.sh](../../u/scripts/conf.sh) +- [x] update: [uspecs/u/scripts/conf.sh](../../../../u/scripts/conf.sh) - add: ERR trap in `cmd_apply` after `ffdefault` succeeds to switch back to `prev_branch` on failure; clear trap after `pr.sh pr` succeeds