Add pre-merge branch freshness check to prevent merge conflicts
Summary
The pr-merge command does not verify that the feature branch is up to date with main before attempting to merge. When main has advanced since the branch was created (e.g., from other merges), pr-merge fails with merge conflicts that require manual intervention (rebase, conflict resolution, force-push). This friction should be caught and resolved earlier in the workflow.
Current Behavior
pr-mergechecks GitHub'smergeablestatus but only reports conflicts as a blocker, leaving the user to manually rebase and resolve.- The
pr-pre-mergechecklist does not verify branch freshness either. - There is no automated step anywhere in the workflow to rebase or update a feature branch before merge.
Proposed Behavior
- Add a branch freshness check early in the merge flow (either in
pr-pre-mergeorpr-merge) that detects when the feature branch is behindmain. - When the branch is behind, automatically attempt a rebase onto
main(or at minimum, clearly suggest it with the exact commands). - Consider whether
pr-mergeshould offer to handle a simple rebase+force-push automatically when conflicts are trivial (e.g., only inplugin.jsonversion bumps).
Acceptance Criteria
- The workflow detects when a feature branch is behind
mainbefore the merge step - The user is informed and offered a resolution path (manual or automated)
- Trivial conflicts (e.g., version bumps) are handled gracefully
- The solution does not introduce force-push risks to shared branches
Context
Encountered during the merge of PR #47, where a plugin.json version conflict required manual rebase and force-push. The conflict was a simple version number difference that could have been auto-resolved or prevented.
Technical Notes
- Relevant files:
commands/pr-merge.md,commands/pr-pre-merge.md - The
gh pr viewJSON already includesmergeStateStatusandmergeablefields that could be used for early detection - Consider adding a rebase step to
pr-pre-mergeso conflicts surface before the merge attempt
Implementation Plan: Pre-Merge Branch Freshness Check
Overview
Add a branch freshness check to the pre-merge workflow that detects when a feature branch is behind main, offers to merge main into the branch (with auto-resolution of trivial version-field conflicts), and adds a safety net in the merge command.
Architecture Decisions
- Merge-based update (not rebase): Uses
git merge origin/mainto avoid force-push requirements - User consent: Uses
AskUserQuestionbefore merging — user can skip or cancel - Auto-resolve trivial conflicts: Version fields in
plugin.json,package.json,pyproject.toml,setup.cfgare auto-resolved using--theirs(take main's version, since the checklist will re-bump) - Non-trivial conflicts abort: Any other conflicted file triggers
git merge --abortand stops the session - Safety net in pr-merge: Adds BEHIND detection to the readiness gate, redirecting to
pr-pre-merge
Stage 1: Add branch freshness check to pr-pre-merge
What it accomplishes: Inserts a new Step 4 ("Branch Freshness Check") into pr-pre-merge.md between checkout and the checklist. Renumbers existing steps. Adds freshness status to the report.
Files modified:
commands/pr-pre-merge.md
Details:
The new Step 4 flow:
git fetch origin+git rev-list --count HEAD..origin/main- If count is 0 → branch is current, continue
- If count > 0 →
AskUserQuestionwith three options:- Merge: "Merge origin/main into this branch now"
- Skip: "Leave the branch as-is and continue the checklist"
- Cancel: "Stop without running the checklist"
- On merge: run
git merge origin/main- Success → continue to checklist
- Conflicts → check if all are trivial (version files only)
- All trivial:
git checkout --theirs <file>+git add <file>for each, thengit merge --continue --no-edit - Any non-trivial:
git merge --abort, report conflicted files, stop session
- All trivial:
Step renumbering: 4→5 (including 4a-4d → 5a-5d), 5→6, 6→7
Step 7 report gains a freshness status line: - [ ] Branch freshness: [current with main / merged N commits from main / auto-resolved conflicts in: <files> / behind main (user skipped merge)]
Testing: Manual — invoke /mach10:pr-pre-merge on a PR whose branch is behind main and verify the flow.
Stage 2: Add BEHIND guard to pr-merge
What it accomplishes: Adds a fourth blocking condition to pr-merge.md Step 2 readiness gate that detects mergeStateStatus == BEHIND and redirects to pr-pre-merge.
Files modified:
commands/pr-merge.md
Details:
Add a new bullet after the existing three blocking conditions:
- Branch behind main: When
mergeStateStatusisBEHIND, suggest/clearthen/mach10:pr-pre-merge <pr-number>to update the branch before merging.
No new API calls needed — the data is already fetched by the existing gh pr view --json command.
Testing: Manual — invoke /mach10:pr-merge on a PR whose branch is behind main and verify it reports the BEHIND status and redirects.
Stage 3: Version bump and documentation
What it accomplishes: Bumps plugin version and updates documentation if needed.
Files modified:
.claude-plugin/plugin.json(version bump)README.md(if the pre-merge checklist description needs updating)
Dependencies: Stages 1 and 2 must be complete.
Testing: Verify plugin.json has valid JSON and README accurately describes the workflow.
Notes
This plan will guide staged implementation via /mach10:issue-implement.
Progress: Stage 1 complete
Commit: de705df — Add branch freshness check to pr-pre-merge command
What was done
Inserted a new Step 4 (Branch Freshness Check) into commands/pr-pre-merge.md between checkout (Step 3) and the checklist (now Step 5). Renumbered all subsequent steps (4→5 including substeps, 5→6, 6→7). Added a freshness status line to the report in Step 7.
The new step:
- Dynamically detects the default branch via
gh repo view - Counts commits behind with
git rev-list --count - Offers Merge/Skip/Cancel via
AskUserQuestion - Auto-resolves trivial version-file conflicts (takes default branch's version via
--theirs) - Aborts and stops on non-trivial conflicts
- Pushes immediately after merge, with error handling for push failures
Deviations from plan
- Used dynamic default branch detection (
gh repo view) instead of hardcodingorigin/main— improves portability - Added push failure handling for both clean-merge and auto-resolve paths
- Used
git commit --no-editinstead ofgit merge --continue --no-editfor portability
Remaining
- Stage 2: Add BEHIND guard to pr-merge
- Stage 3: Version bump and documentation
Progress: Stage 2 complete
Commit: f0b48d4 — Add BEHIND guard to pr-merge readiness gate
What was done
Added a fourth blocking condition to commands/pr-merge.md Step 2 (Verify Readiness). When mergeStateStatus is BEHIND, the command now suggests running /clear then /mach10:pr-pre-merge <pr-number> to update the branch before merging.
No new API calls were needed — mergeStateStatus was already included in the existing gh pr view --json query.
Deviations from plan
- None. Implementation matches the plan exactly.
Remaining
- Stage 3: Version bump and documentation
Progress: Stage 3 complete
Commit: df74e80 — Bump version to 1.3.1 and update pr-pre-merge descriptions
What was done
Bumped plugin version from 1.3.0 to 1.3.1 in .claude-plugin/plugin.json. Updated the pr-pre-merge description in all three locations where it was duplicated:
commands/pr-pre-merge.md(YAML frontmatter)README.md(command table)README.md(Phase 7 narrative)
All descriptions now read "branch freshness, docs, version, CHANGELOG, tests" to reflect the new Step 4 added in stage 1.
Deviations from plan
- None. Implementation matches the plan exactly.
Remaining
- All three stages are now complete. Ready for PR creation.