fix(desktop): detect default branch changes from remote #514
Conversation
📝 WalkthroughWalkthroughAdds a git utility to refresh the remote default branch and integrates it into project and workspace routers; endpoints now fetch the remote default branch, update stored values when changed, and return the computed default branch to callers. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Router as Projects/Workspaces Router
participant GitUtil as Git Utility
participant Remote as Remote Repo
participant DB as Database
Client->>Router: request refreshDefaultBranch(id)
Router->>DB: load project by id
Router->>GitUtil: refreshDefaultBranch(mainRepoPath)
GitUtil->>Remote: git remote set-head origin --auto
GitUtil->>Remote: read refs/remotes/origin/HEAD
alt origin/HEAD available
Remote-->>GitUtil: origin/HEAD -> origin/<branch>
GitUtil-->>Router: remoteDefaultBranch
else fallback to ls-remote
GitUtil->>Remote: git ls-remote --symref origin HEAD
Remote-->>GitUtil: symref output -> <branch>
GitUtil-->>Router: remoteDefaultBranch (or null)
end
Router->>Router: compute effectiveDefaultBranch (remote ?? stored ?? local fallback)
alt branch changed
Router->>DB: update project.defaultBranch
DB-->>Router: OK
Router-->>Client: { success: true, defaultBranch, changed: true, previousBranch }
else unchanged
Router-->>Client: { success: true, defaultBranch, changed: false }
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (2 warnings)
✅ Passed checks (1 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: defaults Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
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 |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
apps/desktop/src/lib/trpc/routers/workspaces/workspaces.ts (1)
1072-1078: Align with the more efficient pattern from create mutation.The default branch resolution logic here differs from the create mutation and is less efficient. If
remoteDefaultBranchis truthy butproject.defaultBranchis falsy, this code callsgetDefaultBranchonly to immediately override it withremoteDefaultBranch.🔎 Proposed refactor for consistency and efficiency
- let defaultBranch = project.defaultBranch; - if (!defaultBranch) { - defaultBranch = await getDefaultBranch(project.mainRepoPath); - } - if (remoteDefaultBranch && remoteDefaultBranch !== defaultBranch) { - defaultBranch = remoteDefaultBranch; - } + const defaultBranch = + remoteDefaultBranch || + project.defaultBranch || + (await getDefaultBranch(project.mainRepoPath));This aligns with the create mutation pattern (lines 76-79) and avoids the unnecessary
getDefaultBranchcall whenremoteDefaultBranchis available.
📜 Review details
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
apps/desktop/src/lib/trpc/routers/projects/projects.tsapps/desktop/src/lib/trpc/routers/workspaces/utils/git.tsapps/desktop/src/lib/trpc/routers/workspaces/workspaces.ts
🧰 Additional context used
📓 Path-based instructions (3)
**/*.{ts,tsx}
📄 CodeRabbit inference engine (AGENTS.md)
Avoid using any type in TypeScript - maintain type safety unless absolutely necessary
Files:
apps/desktop/src/lib/trpc/routers/projects/projects.tsapps/desktop/src/lib/trpc/routers/workspaces/utils/git.tsapps/desktop/src/lib/trpc/routers/workspaces/workspaces.ts
**/*.{ts,tsx,js,jsx}
📄 CodeRabbit inference engine (AGENTS.md)
Run Biome for formatting, linting, import organization, and safe fixes at the root level using bun run lint:fix
Files:
apps/desktop/src/lib/trpc/routers/projects/projects.tsapps/desktop/src/lib/trpc/routers/workspaces/utils/git.tsapps/desktop/src/lib/trpc/routers/workspaces/workspaces.ts
apps/desktop/**/*.{ts,tsx}
📄 CodeRabbit inference engine (apps/desktop/AGENTS.md)
apps/desktop/**/*.{ts,tsx}: For Electron interprocess communication, ALWAYS use tRPC as defined insrc/lib/trpc
Use alias as defined intsconfig.jsonwhen possible
Prefer zustand for state management if it makes sense. Do not use effect unless absolutely necessary.
For tRPC subscriptions with trpc-electron, ALWAYS use the observable pattern from@trpc/server/observableinstead of async generators, as the library explicitly checksisObservable(result)and throws an error otherwise
Files:
apps/desktop/src/lib/trpc/routers/projects/projects.tsapps/desktop/src/lib/trpc/routers/workspaces/utils/git.tsapps/desktop/src/lib/trpc/routers/workspaces/workspaces.ts
🧬 Code graph analysis (1)
apps/desktop/src/lib/trpc/routers/projects/projects.ts (3)
apps/desktop/src/lib/trpc/routers/workspaces/utils/git.ts (2)
refreshDefaultBranch(331-366)getDefaultBranch(251-313)apps/desktop/src/main/lib/local-db/index.ts (1)
localDb(85-85)packages/local-db/src/schema/schema.ts (1)
projects(15-40)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Build
🔇 Additional comments (3)
apps/desktop/src/lib/trpc/routers/workspaces/utils/git.ts (1)
325-366: LGTM! Well-designed implementation.The function correctly handles default branch detection with appropriate fallbacks and clear documentation. The strategy of explicitly syncing
origin/HEADviagit remote set-head origin --autois the right approach, as the comments correctly note that git doesn't auto-update this on fetch.apps/desktop/src/lib/trpc/routers/workspaces/workspaces.ts (1)
71-87: LGTM! Efficient default branch resolution.The pattern using short-circuit evaluation correctly prioritizes the remote default branch over cached values, and only falls back to
getDefaultBranchwhen necessary. The DB update logic is also correct.apps/desktop/src/lib/trpc/routers/projects/projects.ts (1)
260-276: LGTM! Consistent pattern with create mutation.The default branch resolution logic correctly prioritizes remote over cached values and uses the efficient short-circuit evaluation pattern.
Expand this to see my work.
|
|
Overall this looks solid. The approach of explicitly syncing
A few specific follow-ups that would make this tighter:
|
Description
Related Issues
Type of Change
Testing
Screenshots (if applicable)
Additional Notes
Summary by CodeRabbit
New Features
Improvements
✏️ Tip: You can customize this high-level summary in your review settings.