fix(worktree): allow Unicode letters/numbers in workspace names (#1624)

Users typing CJK or other non-ASCII names (e.g. 中文) into the Create
Workspace dialog hit "Invalid worktree name" because the sanitizer's
ASCII-only allowlist stripped every character, leaving an empty string.

Replace the allowlist with a Unicode-aware one (\p{L}\p{N}) so non-Latin
scripts pass through. Git ref-format and modern filesystems accept UTF-8.

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Jinjing
2026-05-09 00:27:41 -07:00
committed by GitHub
co-authored by Orca
parent b023c5713e
commit ce67f794d2
2 changed files with 19 additions and 3 deletions
+14
View File
@@ -51,6 +51,20 @@ describe('sanitizeWorktreeName', () => {
expect(sanitizeWorktreeName('a..b...c')).toBe('a.b.c')
})
it('preserves non-ASCII letters and numbers', () => {
// Why: users name workspaces in their own language (CJK, accented Latin,
// Cyrillic, etc.). Stripping these to ASCII left the name empty and threw
// "Invalid worktree name" on every non-Latin keyboard input.
expect(sanitizeWorktreeName('中文')).toBe('中文')
expect(sanitizeWorktreeName('日本語 テスト')).toBe('日本語-テスト')
expect(sanitizeWorktreeName('café-déjà')).toBe('café-déjà')
expect(sanitizeWorktreeName('Привет мир')).toBe('Привет-мир')
})
it('still strips git-unsafe punctuation around Unicode names', () => {
expect(sanitizeWorktreeName('feat: 中文 (v2)')).toBe('feat-中文-v2')
})
it('throws for empty name', () => {
expect(() => sanitizeWorktreeName('')).toThrow('Invalid worktree name')
})
+5 -3
View File
@@ -7,11 +7,13 @@ import { getWslHome, parseWslPath } from '../wsl'
* Strips unsafe characters and collapses runs of special chars to a single hyphen.
*/
export function sanitizeWorktreeName(input: string): string {
// Why: keep Unicode letters/numbers (CJK, accented Latin, etc.) so users can
// name workspaces in their own language. Git ref-format permits non-ASCII
// bytes, and modern filesystems handle UTF-8 paths. Only strip characters
// git or the filesystem actually rejects.
const sanitized = input
.trim()
.replace(/[\\/]+/g, '-')
.replace(/\s+/g, '-')
.replace(/[^A-Za-z0-9._-]+/g, '-')
.replace(/[^\p{L}\p{N}._-]+/gu, '-')
.replace(/-+/g, '-')
// Why: git check-ref-format rejects any ref containing `..`, so a prompt
// like "../../foo" that survives slugification as `..-..-foo` would