Skip to content

feat(git): track request source branch as upstream on checkout#730

Open
skevetter wants to merge 1 commit into
mainfrom
feat/track-request-source-branch
Open

feat(git): track request source branch as upstream on checkout#730
skevetter wants to merge 1 commit into
mainfrom
feat/track-request-source-branch

Conversation

@skevetter

@skevetter skevetter commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Summary

CheckoutPR fetches a pull/merge request into a synthetic MR<N>/PR<N> local branch with no upstream, so the local branch does not reference the remote branch the request was opened from (git status shows no tracking relationship).

After checkout, this resolves the origin branch whose tip matches the request head and sets it as the local branch's upstream. Because the head ref (refs/{pull,merge-requests}/N/head) carries the tip commit but not the source branch name, the match is done by commit via git for-each-ref --points-at over the already-fetched refs/remotes/origin/* — no provider API, token, or network call required.

It degrades cleanly to the prior behavior (untracked branch) when the match can't be trusted:

  • Fork-based requests — source branch isn't on origin
  • Ambiguous matches — more than one origin branch points at the head (e.g. the default branch too)
  • Shallow clones — other branches aren't fetched

Works identically for GitHub and GitLab (including self-hosted).

Adds TestRepoCheckoutPRTracksSourceBranch and TestRepoCheckoutPRAmbiguousSourceBranchLeftUntracked.

Summary by CodeRabbit

  • Enhancements
    • Pull request checkouts now automatically track the originating remote branch when a unique match can be identified.
    • Branches remain untracked when the source branch cannot be determined unambiguously.
  • Bug Fixes
    • Improved pull request checkout behavior across supported Git providers while preserving existing fetch and switch workflows.

CheckoutPR fetched a pull/merge request into a synthetic MR<N>/PR<N>
branch with no upstream, so the local branch did not reference the
remote branch it came from. After checkout, resolve the origin branch
whose tip matches the request head (the head ref carries the commit but
not the branch name) and set it as upstream. Forks, shallow clones, and
ambiguous matches leave the branch untracked, preserving prior behavior.
@netlify

netlify Bot commented Jul 24, 2026

Copy link
Copy Markdown

Deploy Preview for images-devsy-sh canceled.

Name Link
🔨 Latest commit 6514203
🔍 Latest deploy log https://app.netlify.com/projects/images-devsy-sh/deploys/6a62e08d6c32770008187e90

@netlify

netlify Bot commented Jul 24, 2026

Copy link
Copy Markdown

Deploy Preview for devsydev canceled.

Name Link
🔨 Latest commit 6514203
🔍 Latest deploy log https://app.netlify.com/projects/devsydev/deploys/6a62e08dd01d8c0008291933

@coderabbitai

coderabbitai Bot commented Jul 24, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: fb93dd44-058f-4fd8-9692-33aababa735c

📥 Commits

Reviewing files that changed from the base of the PR and between c07b339 and 6514203.

📒 Files selected for processing (2)
  • pkg/git/repo.go
  • pkg/git/repo_test.go

📝 Walkthrough

Walkthrough

CheckoutPR now best-effort tracks a uniquely resolved origin/* source branch after switching to a PR branch. Tests cover both unique source resolution and ambiguous matches, while other repository methods retain their behavior.

Changes

CheckoutPR source tracking

Layer / File(s) Summary
Repository surface and command wrappers
pkg/git/repo.go
Repository declarations, accessors, command wrappers, cloning helpers, and comments were reorganized without behavior changes.
Checkout source branch tracking
pkg/git/repo.go, pkg/git/repo_test.go
CheckoutPR resolves a unique origin branch by revision and sets it as the local branch upstream; ambiguous matches leave the branch untracked, with both cases tested.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

  • devsy-org/devsy#704: Both changes extend CheckoutPR handling for GitLab merge request references.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: tracking the request source branch as upstream during checkout.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@skevetter
skevetter marked this pull request as ready for review July 24, 2026 04:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant