fix(desktop): validate branch name for git ref path conflicts#1009
fix(desktop): validate branch name for git ref path conflicts#1009saddlepaddle wants to merge 1 commit intomainfrom
Conversation
…workspace creation Adds early validation to detect when a new branch name would conflict with existing branches due to Git's ref storage structure (e.g., can't create "release/v61" if "release" exists). This provides a clear error message with a suggested alternative before any database writes or background jobs start, ensuring MCP and UI both get immediate feedback.
📝 WalkthroughWalkthroughA utility function for detecting Git ref path conflicts between branch names has been added, along with integration into the workspace branch creation procedure to validate new branch names against existing branches before creation. Changes
Sequence DiagramsequenceDiagram
participant User
participant CreateProc as Create Procedure
participant Checker as findBranchPathConflict
participant Branches as Existing Branches
User->>CreateProc: Request branch creation
CreateProc->>Checker: Check for path conflicts
Checker->>Branches: Compare new branch vs. existing
alt Conflict detected
Branches-->>Checker: Conflicting branch found
Checker-->>CreateProc: Return conflicting name
CreateProc-->>User: Error with suggested name
else No conflict
Branches-->>Checker: No conflict
Checker-->>CreateProc: Return null
CreateProc->>CreateProc: Proceed with branch creation
CreateProc-->>User: Branch created successfully
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
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
🤖 Fix all issues with AI agents
In `@apps/desktop/src/lib/trpc/routers/workspaces/procedures/create.ts`:
- Around line 332-347: Replace the generic Error throw in the branch conflict
check with a TRPCError so tRPC handlers return a proper BAD_REQUEST; import
TRPCError from "@trpc/server" at the top of the file and change the throw inside
the block that uses findBranchPathConflict (the branch conflict logic in create
branch procedure) to: throw new TRPCError({ code: "BAD_REQUEST", message: /*
same message */ }); keeping the original message text (including suggestedName)
unchanged.
🧹 Nitpick comments (1)
apps/desktop/src/shared/utils/branch.test.ts (1)
91-140: Good test coverage for the conflict detection logic.The test suite covers the essential scenarios including child/parent conflicts, deep nesting, case-insensitivity, and the important edge case of exact matches not being flagged as path conflicts.
Consider adding edge case tests for robustness:
🧪 Optional: Additional edge case tests
test("does not match exact same branch name", () => { // Exact match is not a path conflict (it's a duplicate, handled elsewhere) expect(findBranchPathConflict("release", ["release"])).toBe(null); }); + + test("handles empty string new branch", () => { + expect(findBranchPathConflict("", ["release", "main"])).toBe(null); + }); + + test("handles branches with trailing slashes gracefully", () => { + // Edge case: malformed branch names + expect(findBranchPathConflict("release/", ["release"])).toBe("release"); + });
| // Check for Git ref path conflicts when creating a new branch | ||
| // e.g., can't create "release/v61" if "release" exists (or vice versa) | ||
| if (!existingBranchName) { | ||
| const conflictingBranch = findBranchPathConflict( | ||
| branch, | ||
| existingBranches, | ||
| ); | ||
| if (conflictingBranch) { | ||
| const suggestedName = branch.replace(/\//g, "-"); | ||
| throw new Error( | ||
| `Cannot create branch "${branch}" because branch "${conflictingBranch}" already exists. ` + | ||
| `Git cannot have branches where one is a path prefix of another. ` + | ||
| `Try using "${suggestedName}" instead.`, | ||
| ); | ||
| } | ||
| } |
There was a problem hiding this comment.
🛠️ Refactor suggestion | 🟠 Major
Use TRPCError instead of generic Error for consistent API error handling.
The conflict detection logic is correctly placed before any database writes. However, per coding guidelines for tRPC procedures, validation errors should throw TRPCError with code BAD_REQUEST instead of generic Error.
🔧 Proposed fix using TRPCError
First, ensure TRPCError is imported:
import { TRPCError } from "@trpc/server";Then update the throw statement:
if (!existingBranchName) {
const conflictingBranch = findBranchPathConflict(
branch,
existingBranches,
);
if (conflictingBranch) {
const suggestedName = branch.replace(/\//g, "-");
- throw new Error(
- `Cannot create branch "${branch}" because branch "${conflictingBranch}" already exists. ` +
- `Git cannot have branches where one is a path prefix of another. ` +
- `Try using "${suggestedName}" instead.`,
- );
+ throw new TRPCError({
+ code: "BAD_REQUEST",
+ message:
+ `Cannot create branch "${branch}" because branch "${conflictingBranch}" already exists. ` +
+ `Git cannot have branches where one is a path prefix of another. ` +
+ `Try using "${suggestedName}" instead.`,
+ });
}
}🤖 Prompt for AI Agents
In `@apps/desktop/src/lib/trpc/routers/workspaces/procedures/create.ts` around
lines 332 - 347, Replace the generic Error throw in the branch conflict check
with a TRPCError so tRPC handlers return a proper BAD_REQUEST; import TRPCError
from "@trpc/server" at the top of the file and change the throw inside the block
that uses findBranchPathConflict (the branch conflict logic in create branch
procedure) to: throw new TRPCError({ code: "BAD_REQUEST", message: /* same
message */ }); keeping the original message text (including suggestedName)
unchanged.
🧹 Preview Cleanup CompleteThe following preview resources have been cleaned up:
Thank you for your contribution! 🎉 |
Summary
Problem
When creating a workspace with branch name
release/v61, if a branchreleasealready exists, Git fails with a cryptic error:This happened during background initialization, so the MCP got no useful feedback and the UI showed a generic "Workspace setup failed" dialog.
Solution
Added
findBranchPathConflict()function that checks if a new branch name would conflict with existing branches. Now users see:Test plan
findBranchPathConflict()functionSummary by CodeRabbit
Bug Fixes
Tests
✏️ Tip: You can customize this high-level summary in your review settings.