Skip to content

Refresh upstream state after branch checkout and invalidate git queries#46

Merged
juliusmarminge merged 3 commits intomainfrom
git-in
Feb 15, 2026
Merged

Refresh upstream state after branch checkout and invalidate git queries#46
juliusmarminge merged 3 commits intomainfrom
git-in

Conversation

@juliusmarminge
Copy link
Copy Markdown
Member

@juliusmarminge juliusmarminge commented Feb 15, 2026

Summary

  • refresh tracked upstream metadata after git checkout by fetching the checked-out branch's remote (quiet, no-tags)
  • ensure checkout-derived status reflects current ahead/behind counts when the remote branch has advanced
  • add an integration test covering the stale-behind-count scenario after checkout
  • invalidate git-related React Query caches on turn/completed events so UI git state refreshes promptly

Testing

  • Added integration test in apps/server/src/git.test.ts that:
  • creates a remote + source + clone setup
  • advances the remote tracked branch from another clone
  • checks out the branch locally and asserts aheadCount === 0 and behindCount === 1
  • Verified implementation path in apps/server/src/git.ts calls upstream refresh after successful checkout
  • Not run: project lint scripts
  • Not run: full test suite

Open with Devin

Summary by CodeRabbit

  • New Features

    • Checkout now attempts to refresh upstream tracking after switching branches.
    • UI invalidates git queries when background events complete to keep git state up to date.
  • Bug Fixes / Resiliency

    • Checkout proceeds even if the upstream refresh/fetch fails; a single fetch retry is attempted and failures are best-effort only.
  • Tests

    • Added tests covering upstream behind-count, scoped fetch behavior, and checkout behavior during refresh/fetch failures.

- fetch tracked remote after checkout to update ahead/behind counts
- add integration test for behind count when remote branch advances
- invalidate git React Query data on `turn/completed` events in the web app
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Feb 15, 2026

Walkthrough

Adds best-effort upstream refresh after branch checkout in the server Git service, two tests validating upstream behind-count and checkout resilience to fetch failures, and client-side invalidation of Git queries on "turn/completed" events.

Changes

Cohort / File(s) Summary
Git Tests
apps/server/src/git.test.ts
Adds multiple tests that create a bare remote, a local source repo, and a clone to verify upstream behind-count after remote branch advancement, that checkout proceeds when an upstream fetch/refresh fails, and that fetches are scoped to the checked-out branch.
Git Operations (server)
apps/server/src/git.ts
Adds a private refreshCheckedOutBranchUpstream(cwd: string) to resolve the current branch upstream and run a branch-scoped git fetch; checkoutBranch now calls this method after checkout (best-effort, failures ignored). Note: the new method appears duplicated in the class locations.
Client: event-driven invalidation
apps/web/src/App.tsx
Imports useQueryClient and invalidateGitQueries, and invokes invalidateGitQueries(queryClient) when event.method === "turn/completed" in EventRouter; adds queryClient to effect dependencies.

Sequence Diagram(s)

mermaid
sequenceDiagram
participant Client as Web Client
participant Server as GitService
participant Remote as Remote Git (bare repo)
Client->>Server: request checkoutBranch(repo, branch)
Server->>Server: git checkout
Server->>Server: resolve upstream (remote + ref)
Server->>Remote: git fetch
Remote-->>Server: fetch result
Server-->>Client: checkout result (fetch errors ignored)

mermaid
sequenceDiagram
participant EventRouter as Web EventRouter
participant QueryClient as React QueryClient
participant ServerAPI as Server Git API
EventRouter->>QueryClient: invalidateGitQueries(queryClient)
QueryClient->>ServerAPI: refetch Git-related queries
ServerAPI-->>QueryClient: latest Git data
QueryClient-->>EventRouter: updated data (subscribers)

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 3 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and concisely summarizes the main changes: refreshing upstream state after branch checkout and invalidating git queries in the UI.
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into main

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch git-in

No actionable comments were generated in the recent review. 🎉


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

@greptile-apps
Copy link
Copy Markdown

greptile-apps bot commented Feb 15, 2026

Greptile Summary

Added upstream branch refresh after checkout to ensure accurate ahead/behind counts when remote branches advance, plus UI query invalidation on agent turn completion.

Key changes:

  • refreshCheckedOutBranchUpstream() fetches the checked-out branch's upstream refspec after successful checkout (scoped fetch, not all remote refs)
  • Fetch failures are silently caught since checkout already succeeded (best-effort refresh)
  • Integration tests verify behind-count accuracy, failure tolerance, and refspec scoping
  • UI now invalidates git-related React Query caches on turn/completed events to reflect post-agent git state

Confidence Score: 4/5

  • Safe to merge with minor considerations around error handling patterns
  • Implementation follows CLAUDE.md priorities (reliability, predictable failure behavior) by wrapping fetch in try-catch and scoping to specific refspec. Comprehensive test coverage validates the happy path, failure tolerance, and refspec scoping. The UI integration properly invalidates queries. Previous review comments addressed fetch scoping and error masking concerns.
  • No files require special attention

Important Files Changed

Filename Overview
apps/server/src/git.ts Added refreshCheckedOutBranchUpstream method to fetch upstream state after checkout, properly scoped to the checked-out branch's refspec, with error tolerance
apps/server/src/git.test.ts Added three comprehensive integration tests covering behind-count refresh, fetch failure tolerance, and refspec scoping
apps/web/src/App.tsx Invalidates git queries on turn/completed events to ensure UI reflects current git state after agent operations

Sequence Diagram

sequenceDiagram
    participant User
    participant UI as App.tsx
    participant API as Git API
    participant Git as GitCoreService
    participant Remote as Git Remote

    User->>API: checkout branch
    API->>Git: checkoutBranch()
    Git->>Git: executeGit(["checkout", branch])
    Note over Git: Checkout succeeds
    
    Git->>Git: refreshCheckedOutBranchUpstream()
    Git->>Git: gitStdout(["rev-parse", "@{upstream}"])
    Git-->>Git: "origin/feature-branch"
    Git->>Git: Parse remote name and branch
    Git->>Remote: git fetch --quiet --no-tags origin +refs/heads/feature:refs/remotes/origin/feature
    
    alt Fetch succeeds
        Remote-->>Git: Updated refs
        Git-->>API: Checkout complete
    else Fetch fails
        Note over Git: Silently catch error
        Git-->>API: Checkout complete (best effort)
    end
    
    API-->>UI: Checkout result
    
    Note over UI: On turn/completed event
    UI->>UI: invalidateGitQueries(queryClient)
    UI->>API: Re-fetch git status
    API->>Git: statusDetails()
    Git-->>API: Current ahead/behind counts
    API-->>UI: Updated git state
    Note over UI: UI shows fresh git status
Loading

Last reviewed commit: 147f433

Copy link
Copy Markdown

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Comment thread apps/server/src/git.ts
@macroscopeapp
Copy link
Copy Markdown
Contributor

macroscopeapp bot commented Feb 15, 2026

Refresh the checked-out branch’s upstream after GitCoreService.checkoutBranch and invalidate Git React Query caches on turn/completed events

Add GitCoreService.refreshCheckedOutBranchUpstream to run a scoped fetch after checkout and call it best-effort from GitCoreService.checkoutBranch; invalidate Git-related queries in App.EventRouter on turn/completed; add tests covering behind count refresh, fetch scoping, and failure tolerance in apps/server/src/git.test.ts and logic in apps/server/src/git.ts and apps/web/src/App.tsx.

📍Where to Start

Start with GitCoreService.refreshCheckedOutBranchUpstream and its invocation inside GitCoreService.checkoutBranch in apps/server/src/git.ts.


Macroscope summarized 147f433.

- treat post-checkout upstream fetch as best effort so checkout still succeeds
- route refresh fetch through `this.git(...)` for consistent behavior
- add regression test for fetch-timeout during branch checkout
@juliusmarminge
Copy link
Copy Markdown
Member Author

@greptileai review

Copy link
Copy Markdown

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Comment thread apps/server/src/git.ts Outdated
- derive upstream branch from `branch@{upstream}` and guard empty values
- fetch only `+refs/heads/<branch>:refs/remotes/<remote>/<branch>` instead of all remote refs
- add integration test asserting scoped fetch args during `checkoutBranch`
@juliusmarminge
Copy link
Copy Markdown
Member Author

@greptileai review

@juliusmarminge juliusmarminge merged commit 6708fe9 into main Feb 15, 2026
4 checks passed
DavidIlie added a commit to DavidIlie/t3code that referenced this pull request Mar 13, 2026
Port upstream commits ac3f22c..268016d:
- Inline branch picker filtering and drop PR-only shortcut (pingdotgg#46)
- Fix PR thread upstream tracking for fork branches (pingdotgg#47)

Removes filterBranchPickerItems shared function in favor of inline filtering,
adds URL-derived fork identity resolution, ensures upstream tracking is
restored on local checkouts and reused worktrees, and simulates real
git checkout in fake GH CLI for integration tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant