fix: fetch remote base branch before PR range context to prevent stale diffs#1519
fix: fetch remote base branch before PR range context to prevent stale diffs#1519zerone0x wants to merge 2 commits intopingdotgg:mainfrom
Conversation
…e diffs When creating a pull request, the range context (commit log and diff) was computed against the local base branch ref (e.g. `main`), which can be stale if the user hasn't explicitly fetched or pulled it. This caused previously merged PR commits to appear in the range, leading to PR titles and bodies that included old, already-merged content. Now, `runPrStep` fetches the remote tracking ref for the base branch (e.g. `origin/main`) before computing the range context, ensuring the diff only includes commits not yet present upstream. Fixes pingdotgg#1487 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
| const rangeRef = yield* Effect.gen(function* () { | ||
| // If baseBranch already contains a remote prefix, use it as-is. | ||
| if (baseBranch.includes("/")) { | ||
| return baseBranch; |
There was a problem hiding this comment.
Slash check misidentifies local branches as remote refs
Medium Severity
The baseBranch.includes("/") check is intended to detect remote-prefixed refs like origin/main, but it incorrectly matches any branch name containing a slash (e.g., release/1.0, feature/main, hotfix/critical). These are common local branch naming conventions. When resolveBaseBranch returns such a name — via git config, extractBranchFromRef, or getDefaultBranch — the code skips the remote fetch and uses the potentially stale local ref, defeating the fix for issue #1487 in those cases.
|
Closing in favor of a simpler fix using git's built-in --cherry-pick --right-only flags. |
|
Closing in favor of a more targeted fix that regenerates PR title/body when an existing PR is found, rather than returning stale metadata. |
Improve the base branch fetch logic by: - Using headContext.remoteName instead of hardcoding "origin" to support fork remotes correctly - Adding allowNonZeroExit to the fetch call for robustness - Verifying the remote ref exists with rev-parse after fetch - Wrapping the entire block in Effect.catch to fall back gracefully Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
There are 2 total unresolved issues (including 1 from previous review).
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
| // tracking ref for range computation. Prefer the head context's | ||
| // remote when available (e.g. fork remotes) and fall back to "origin". | ||
| const remoteName = headContext.remoteName ?? "origin"; | ||
| const remoteRef = `${remoteName}/${baseBranch}`; |
There was a problem hiding this comment.
Fork workflows fetch base branch from wrong remote
Medium Severity
In cross-repository (fork) workflows, headContext.remoteName is the remote for the feature branch's push target (the fork), not the upstream repo where the PR base branch lives. Fetching the base branch (e.g., main) from the fork remote can retrieve a stale or missing ref, potentially making the range context worse than the original local ref. For example, when origin = upstream and the branch pushes to myfork, the old code used local main (tracking upstream), but the new code fetches myfork/main (the fork's possibly-never-synced main).


Summary
Fixes #1487
When creating a pull request via the git button, the range context (commit log and diff used to generate the PR title/body) was computed using
baseBranch..HEADwherebaseBranchreferred to the local branch ref (e.g.main). If the local ref hadn't been fetched since previous PRs were merged upstream, the range would include all old commits from previously merged PRs, causing:Root cause:
runPrStepinGitManager.tscalledreadRangeContext(cwd, baseBranch)with a potentially stale local branch ref. Since thepushstep only pushes the current feature branch (not fetching the base), the localmaincould be many commits behindorigin/main.Fix: Before computing the range context, fetch the remote tracking ref for the base branch (e.g.
origin/main) and use it for the range computation. This ensures the diff only contains commits not yet present upstream, so each PR gets a fresh title and body based solely on its own changes.The fix is conservative:
baseBranchalready contains a remote prefix (e.g.origin/main), it's used as-isbaseBranchvariable is still used for PR targeting — only the range computation uses the refreshed remote refTest plan
Note
Medium Risk
Adds a network
git fetch(30s timeout) during PR creation and changes which commits/diffs are used to generate PR title/body; failures fall back to prior local-ref behavior.Overview
Ensures PR title/body generation uses an up-to-date base by fetching the base branch’s remote tracking ref before computing the range context in
runPrStep.If the base branch already includes a remote prefix it’s used directly; otherwise it fetches
headContext.remoteName(ororigin) and prefersremote/<baseBranch>forreadRangeContext, falling back to the localbaseBranchon fetch/verify failure.Written by Cursor Bugbot for commit a00a0dc. This will update automatically on new commits. Configure here.
Note
Fetch remote base branch before computing PR diff range to prevent stale diffs
In
configurePullRequestHeadUpstream, the base branch ref used forreadRangeContextis now resolved to its remote-tracking counterpart before computing PR content. The code fetches the base branch from the appropriate remote (usingheadContext.remoteNameor falling back toorigin), verifies the ref exists viagit rev-parse --verify, and uses it if successful. On any error, it falls back to the local base branch name.Macroscope summarized a00a0dc.