Skip to content

Commit 33c07f5

Browse files
committed
feat(worktree): normalize branch ref names when creating worktrees (#38)
Ensure that branch names containing forward or back slashes are properly normalized by replacing them with hyphens. This prevents issues with invalid folder paths when creating worktrees. Also improve error handling for null git folder selection.
1 parent d8ca474 commit 33c07f5

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

src/core/command/addWorktreeCmd.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as vscode from 'vscode';
22
import { pickGitFolder } from '@/core/ui/pickGitFolder';
3-
import type { IWorktreeLess } from '@/types';
3+
import type { IWorktreeLess, IBranchForWorktree } from '@/types';
44
import { Alert } from '@/core/ui/message';
55
import { pickBranch } from '@/core/quickPick/pickBranch';
66
import { createWorktreeFromInfo } from '@/core/command/createWorktreeFromInfo';
@@ -20,19 +20,22 @@ const pickBranchItem = async (dir: string, mainFolder: string) => {
2020
return branchItem;
2121
};
2222

23+
const normalizeRefName = (branchItem: IBranchForWorktree) => (branchItem.branch || branchItem.hash || '').replace(/[\\/]/g, '-');
24+
2325
export const addWorktreeCmd = async (item?: IWorktreeLess) => {
2426
let gitFolder =
2527
item?.fsPath || (await pickGitFolder(vscode.l10n.t('Select Git repository to create worktree from')));
26-
if (gitFolder === null)
28+
if (gitFolder === null) {
2729
Alert.showErrorMessage(vscode.l10n.t('Please open at least one Git repository in workspace'));
30+
}
2831
if (!gitFolder) return false;
2932
const mainFolder = await getMainFolder(gitFolder);
3033
if (!mainFolder) return false;
3134

3235
// Select ref
3336
let branchItem = await pickBranchItem(gitFolder, mainFolder);
3437
if (!branchItem) return false;
35-
let refName = branchItem.branch || branchItem.hash;
38+
let refName = normalizeRefName(branchItem);
3639

3740
// Select folder
3841
let folderPath = await inputWorktreeDir({
@@ -46,7 +49,7 @@ export const addWorktreeCmd = async (item?: IWorktreeLess) => {
4649
// If no folder is selected, return to selecting ref
4750
branchItem = await pickBranchItem(gitFolder, mainFolder);
4851
if (!branchItem) return false;
49-
refName = branchItem.branch || branchItem.hash;
52+
refName = normalizeRefName(branchItem);
5053
folderPath = await inputWorktreeDir({
5154
baseDir: mainFolder,
5255
baseWorktreeDir: folderPath,

0 commit comments

Comments
 (0)