Skip to content
Source
Sign inSign up
pingdotgg/t3code

Update cached branch list immediately after checkout #48

Closedjuliusmarmingewants to merge 1 commit intomain fromcodething/b0c3a7efopened View on GitHub

juliusmarmingecommented

Summary

  • add projectGitBranchesToCurrent to project cached branch data to the newly checked-out branch
  • add setGitBranchesCurrentBranch to update React Query cache for git.listBranches before invalidation
  • wire checkout mutation success handlers to optimistically set the current branch in cache
  • add unit tests for branch reordering, missing-branch insertion, and undefined-cache handling

Testing

  • apps/web/src/lib/gitReactQuery.test.ts: verifies checked-out branch is moved to top and marked current
  • apps/web/src/lib/gitReactQuery.test.ts: verifies missing checked-out branch is inserted as current
  • apps/web/src/lib/gitReactQuery.test.ts: verifies undefined cache input returns undefined
  • Not run: project lint/test scripts

Open with Devin

Summary by CodeRabbit

  • Tests: Added comprehensive unit tests for Git branch management, covering proper reordering of the current branch to the top of the list, preservation of branch metadata, and handling of new branches.
  • Improvements: Git operations (branch checkout and creation) now properly synchronize cached branch information in real-time, ensuring the currently active branch is immediately visible in the UI.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Walkthrough

      This PR introduces helpers to manage Git branch state, ensuring the currently checked-out branch appears first in cached branch lists. New functions projectGitBranchesToCurrent and setGitBranchesCurrentBranch are added, and mutation success handlers for git checkout and branch creation are updated to synchronize cache state with current branch information.

      Changes

      Cohort / File(s)Summary
      New Helper Functions
      apps/web/src/lib/gitReactQuery.ts
      Added projectGitBranchesToCurrent to reorder branches with current branch first, and setGitBranchesCurrentBranch to update cached branches. Updated gitCheckoutMutationOptions and gitCreateBranchAndCheckoutMutationOptions mutation success handlers to call the new cache update function. Added GitListBranchesResult to imports.
      Unit Tests
      apps/web/src/lib/gitReactQuery.test.ts
      Added comprehensive test suite for projectGitBranchesToCurrent covering branch reordering, insertion of missing branches, and undefined input handling.

      Estimated code review effort

      🎯 2 (Simple) | ⏱️ ~10 minutes

      🚥 Pre-merge checks | ✅ 3 | ❌ 1
      ❌ Failed checks (1 warning)
      Check nameStatusExplanationResolution
      Docstring Coverage⚠️ WarningDocstring 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 nameStatusExplanation
      Description Check✅ PassedCheck skipped - CodeRabbit’s high-level summary is enabled.
      Title check✅ PassedThe title 'Update cached branch list immediately after checkout' accurately and concisely describes the main change: adding logic to update the cached Git branch list right after a checkout operation.
      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 `codething/b0c3a7ef`

      No actionable comments were generated in the recent review. 🎉


      Comment `@coderabbitai help` to get the list of available commands and usage tips.
    2. Update React Query branches cache in apps/web after checkout to mark the checked-out branch current and move it to the top

      Add projectGitBranchesToCurrent to reorder and flag the current branch, add setGitBranchesCurrentBranch to update the gitQueryKeys.branches(cwd) cache, and call it from gitCheckoutMutationOptions.onSuccess and gitCreateBranchAndCheckoutMutationOptions.onSuccess; add tests in gitReactQuery.test.ts.

      📍Where to Start

      Start with gitCheckoutMutationOptions.onSuccess in gitReactQuery.ts, then review setGitBranchesCurrentBranch and projectGitBranchesToCurrent.


      Macroscope summarized ee56d5b.
    3. Greptile Summary

      This PR implements optimistic cache updates for git branch checkout operations to improve UI responsiveness. When a user checks out a branch, the React Query cache is immediately updated to reflect the new current branch before the invalidation triggers a refetch. This eliminates the brief moment where the UI would show stale branch data.

      The implementation adds two new exported functions: projectGitBranchesToCurrent performs the cache transformation (reordering branches and marking the correct one as current), and setGitBranchesCurrentBranch applies this transformation to the query cache. Both gitCheckoutMutationOptions and gitCreateBranchAndCheckoutMutationOptions now call the cache update function in their onSuccess handlers.

      Unit tests cover the core scenarios: moving an existing branch to the top and marking it current, inserting a missing branch as current, and gracefully handling undefined cache state.

      Confidence Score: 5/5

      • This PR is safe to merge with minimal risk
      • The changes are well-implemented with comprehensive unit tests covering all edge cases. The optimistic update logic is sound - it properly handles existing branches, missing branches, and undefined cache states. The implementation follows React Query best practices by updating cache before invalidation. No breaking changes or security concerns.
      • No files require special attention

      Important Files Changed

      FilenameOverview
      apps/web/src/lib/gitReactQuery.tsAdded optimistic cache update functions to immediately reflect branch checkout before invalidation
      apps/web/src/lib/gitReactQuery.test.tsNew unit tests verify branch reordering, missing branch insertion, and undefined cache handling

      Sequence Diagram

      sequenceDiagram
          participant User
          participant BranchToolbar
          participant Mutation
          participant Cache
          participant API
      
          User->>BranchToolbar: Select branch to checkout
          BranchToolbar->>Mutation: gitCheckoutMutation.mutate(branch)
          Mutation->>API: api.git.checkout({cwd, branch})
          API-->>Mutation: checkout success
          Mutation->>Cache: setGitBranchesCurrentBranch(queryClient, cwd, branch)
          Note over Cache: Optimistically update cache:<br/>1. Mark new branch as current<br/>2. Move to top of list<br/>3. Unmark old current branch
          Mutation->>Cache: invalidateGitQueries(queryClient)
          Note over Cache: Trigger refetch of all git queries
          Cache-->>BranchToolbar: Updated branch list
          BranchToolbar->>BranchToolbar: UI reflects new current branch immediately
      Last reviewed commit: ee56d5b
    4. juliusmarminge closed this

Sign in to join this conversation.