Skip to content

Commit 61fc306

Browse files
authored
feat(config): add worktree subdirectory template configuration (#30)
* feat(config): add worktree subdirectory template configuration - Introduced 'worktreeSubdirectoryTemplate' option in package.json for customizable worktree subdirectory names. - Updated relevant localization files to include descriptions for the new configuration. - Enhanced folder utility functions to support the new template and validate against invalid path characters. * fix(config): update pattern error message for worktree subdirectory template
1 parent 65c3635 commit 61fc306

File tree

8 files changed

+49
-8
lines changed

8 files changed

+49
-8
lines changed

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,14 @@
943943
"type": "string",
944944
"default": "$BASE_PATH.worktree",
945945
"description": "%config.worktreePathTemplate.description%"
946+
},
947+
"git-worktree-manager.worktreeSubdirectoryTemplate": {
948+
"order": 12,
949+
"type": "string",
950+
"default": "worktree$INDEX",
951+
"pattern": "^[^/\\\\:*?\"<>|]+$",
952+
"patternErrorMessage": "%config.worktreeSubdirectoryTemplate.patternErrorMessage%",
953+
"description": "%config.worktreeSubdirectoryTemplate.description%"
946954
}
947955
}
948956
},
@@ -1153,6 +1161,7 @@
11531161
"@typescript-eslint/eslint-plugin": "^5.59.1",
11541162
"@typescript-eslint/parser": "^5.59.1",
11551163
"@vscode/test-electron": "^2.3.0",
1164+
"@vscode/vsce": "^3.6.0",
11561165
"eslint": "^8.39.0",
11571166
"mocha": "^10.2.0",
11581167
"typescript": "~5.0.4"

package.nls.ja.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,7 @@
100100
"config.checkoutIgnoreOtherWorktree.description": "有効にすると、ブランチの切り替え時に他のワークツリーの状態を無視し、複数のワークツリーで同じブランチを使用できるようになります",
101101
"config.branchPick.showRenameBranch.description": "「ブランチを選択」の結果に「ブランチのリネーム」ボタンを表示",
102102
"config.treeView.toSCM.description": "ビューをソース管理コンテナに表示",
103-
"config.worktreePathTemplate.description": "ワークツリーパスのテンプレートです。使用可能な変数:$BASE_PATH(リポジトリのルート)"
103+
"config.worktreePathTemplate.description": "ワークツリーパスのテンプレートです。使用可能な変数:$BASE_PATH(リポジトリのルート)",
104+
"config.worktreeSubdirectoryTemplate.description": "ワークツリーサブディレクトリ名のテンプレートです。使用可能な変数:$BASE_NAME(リポジトリフォルダ名)、$INDEX(増分番号)。使用できない文字: / \\ : * ? \" < > |",
105+
"config.worktreeSubdirectoryTemplate.patternErrorMessage": "テンプレートに次の文字は使用できません: / \\ : * ? \" < > |"
104106
}

package.nls.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,7 @@
100100
"config.checkoutIgnoreOtherWorktree.description": "When enabled, the state of other worktrees is ignored when switching branches, allowing multiple worktrees to use the same branch.",
101101
"config.branchPick.showRenameBranch.description": "Show the \"Rename Branch\" button on the results of \"Pick Branch\"",
102102
"config.treeView.toSCM.description": "Display views in the Source Control container",
103-
"config.worktreePathTemplate.description": "Template for the worktree path. Available variables: $BASE_PATH (repo root)."
103+
"config.worktreePathTemplate.description": "Template for the worktree path. Available variables: $BASE_PATH (repo root).",
104+
"config.worktreeSubdirectoryTemplate.description": "Template for worktree subdirectory names. Available variables: $BASE_NAME (repo folder name), $INDEX (incrementing number). Cannot contain characters: / \\ : * ? \" < > |",
105+
"config.worktreeSubdirectoryTemplate.patternErrorMessage": "Template cannot contain characters: / \\ : * ? \" < > |"
104106
}

package.nls.zh-cn.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,7 @@
100100
"config.checkoutIgnoreOtherWorktree.description": "启用后,在切换分支时会忽略其他工作树的状态,允许多个工作树使用相同分支",
101101
"config.branchPick.showRenameBranch.description": "\"挑选分支\" 列表中展示 \"重命名分支\" 按钮",
102102
"config.treeView.toSCM.description": "视图展示在源代码管理容器中",
103-
"config.worktreePathTemplate.description": "工作区路径的模板。可用的变量:$BASE_PATH(仓库根目录)"
103+
"config.worktreePathTemplate.description": "工作区路径的模板。可用的变量:$BASE_PATH(仓库根目录)",
104+
"config.worktreeSubdirectoryTemplate.description": "工作区子目录名称的模板。可用的变量:$BASE_NAME(仓库文件夹名称)、$INDEX(递增数字)。不能包含字符: / \\ : * ? \" < > |",
105+
"config.worktreeSubdirectoryTemplate.patternErrorMessage": "模板不能包含以下字符: / \\ : * ? \" < > |"
104106
}

package.nls.zh-tw.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,7 @@
100100
"config.checkoutIgnoreOtherWorktree.description": "啟用後,在切換分支時會忽略其他工作樹的狀態,允許多個工作樹使用相同分支",
101101
"config.branchPick.showRenameBranch.description": "在「挑選分支」清單中顯示「重命名分支」按鈕",
102102
"config.treeView.toSCM.description": "視圖顯示在原始碼控制容器中",
103-
"config.worktreePathTemplate.description": "工作區路徑的模板。可用的變數:$BASE_PATH(倉庫根目錄)"
103+
"config.worktreePathTemplate.description": "工作區路徑的模板。可用的變數:$BASE_PATH(倉庫根目錄)",
104+
"config.worktreeSubdirectoryTemplate.description": "工作區子目錄名稱的模板。可用的變數:$BASE_NAME(倉庫資料夾名稱)、$INDEX(遞增數字)。不能包含字元: / \\ : * ? \" < > |",
105+
"config.worktreeSubdirectoryTemplate.patternErrorMessage": "範本不能包含以下字元: / \\ : * ? \" < > |"
104106
}

src/core/config/setting.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export class Config {
3434
static get(key: 'treeView.showFetchInTreeItem', defaultValue: true): boolean;
3535
static get(key: 'treeView.toSCM', defaultValue: false): boolean;
3636
static get(key: 'worktreePathTemplate', defaultValue: '$BASE_PATH.worktree'): string;
37+
static get(key: 'worktreeSubdirectoryTemplate', defaultValue: 'worktree$INDEX'): string;
3738
static get<T>(key: string, defaultValue: T): T {
3839
return vscode.workspace.getConfiguration(APP_NAME).get(key, defaultValue);
3940
}

src/core/ui/inputWorktreeDir.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as vscode from 'vscode';
22
import path from 'path';
33
import { checkExist, isDirEmpty } from '@/core/util/file';
4-
import { comparePath, getBaseWorktreeDir } from '@/core/util/folder';
4+
import { comparePath, getBaseWorktreeDir, getSubDir } from '@/core/util/folder';
55
import { Alert } from '@/core/ui/message';
66
import { withResolvers } from '@/core/util/promise';
77

@@ -43,8 +43,9 @@ export const inputWorktreeDir = async ({
4343
const { promise, resolve, reject } = withResolvers<string | undefined>();
4444
// 最终路径
4545
const workTreeDir = getBaseWorktreeDir(baseDir);
46-
let finalWorktreeDir = path.join(workTreeDir, 'worktree1');
47-
const dirReg = /worktree(\d+)/;
46+
const baseName = path.basename(baseDir);
47+
const dirReg = new RegExp(getSubDir(baseName, '(\\d+)'));
48+
let finalWorktreeDir = path.join(workTreeDir, getSubDir(baseName, 1));
4849
const inputBox = vscode.window.createInputBox();
4950
// 传入的 baseWorktreeDir 有值,且和 workTreeDir 不同,说明是从已选择的 worktree 切换过来
5051
if (baseWorktreeDir && !comparePath(workTreeDir, baseWorktreeDir)) {
@@ -57,7 +58,7 @@ export const inputWorktreeDir = async ({
5758
if (worktreeDirList.length) {
5859
worktreeDirList.sort((a, b) => Number(b.replace(dirReg, '$1')) - Number(a.replace(dirReg, '$1')));
5960
const index = worktreeDirList[0].match(dirReg)![1];
60-
finalWorktreeDir = path.join(workTreeDir, `worktree${Number(index) + 1}`);
61+
finalWorktreeDir = path.join(workTreeDir, getSubDir(baseName, Number(index) + 1));
6162
}
6263
}
6364
const selectDirBtn: vscode.QuickInputButton = {

src/core/util/folder.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,26 @@ export const getBaseWorktreeDir = (baseDir: string) => {
4545
return worktreePathTemplate.replace('$BASE_PATH', baseDir);
4646
};
4747

48+
// Validate template for invalid path characters
49+
export const validateSubdirectoryTemplate = (template: string): boolean => {
50+
// Check for invalid path characters
51+
const invalidChars = /[/\\:*?"<>|]/;
52+
return !invalidChars.test(template);
53+
};
54+
55+
// get worktree subdirectory name with baseName and index
56+
export const getSubDir = (baseName: string, index: string | number) => {
57+
const template = Config.get('worktreeSubdirectoryTemplate', 'worktree$INDEX');
58+
59+
// Validate template
60+
if (!validateSubdirectoryTemplate(template)) {
61+
console.warn('Invalid worktree subdirectory template, using default');
62+
return `worktree${String(index)}`;
63+
}
64+
65+
return template
66+
.replace('$BASE_NAME', baseName)
67+
.replace('$INDEX', String(index));
68+
};
69+
4870
export const getBaseBundleDir = (baseDir: string) => `${baseDir}.repoBackup`;

0 commit comments

Comments
 (0)