worktree-add 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +24 -6
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -15,7 +15,7 @@ import { copyUntrackedFiles } from "./worktree/untracked-file-copy.js";
|
|
|
15
15
|
import { fetchRemoteBranch, createWorktree } from "./git/worktree-creation.js";
|
|
16
16
|
import { handleExistingDirectory } from "./worktree/destination-directory.js";
|
|
17
17
|
import { setupProject } from "./project/setup.js";
|
|
18
|
-
async function main(branchRaw) {
|
|
18
|
+
async function main(branchRaw, options) {
|
|
19
19
|
const branch = normalizeBranchName(branchRaw);
|
|
20
20
|
// Prevent attempting to add a worktree for a branch that is already checked out
|
|
21
21
|
const existingWorktree = findWorktreeByBranchName(branch);
|
|
@@ -39,18 +39,36 @@ async function main(branchRaw) {
|
|
|
39
39
|
await copyUntrackedFiles(repoRoot, destinationDirectory);
|
|
40
40
|
// Step 4: Install dependencies and run project-specific setup
|
|
41
41
|
await setupProject(destinationDirectory);
|
|
42
|
-
// Step
|
|
43
|
-
|
|
44
|
-
|
|
42
|
+
// Step 5: Open the new worktree in the editor
|
|
43
|
+
const editor = options.editor ?? process.env.WORKTREE_ADD_EDITOR ?? "cursor";
|
|
44
|
+
// Validate editor command to prevent injection attacks
|
|
45
|
+
if (editor.includes(";") ||
|
|
46
|
+
editor.includes("&") ||
|
|
47
|
+
editor.includes("|") ||
|
|
48
|
+
editor.includes("`") ||
|
|
49
|
+
editor.includes("$") ||
|
|
50
|
+
editor.includes("\n")) {
|
|
51
|
+
exitWithMessage("Invalid editor command: shell metacharacters not allowed.\n" +
|
|
52
|
+
"Please use a simple editor name (e.g., 'code', 'cursor', 'vim').");
|
|
53
|
+
}
|
|
54
|
+
console.log(`➤ Opening ${editor} …`);
|
|
55
|
+
const result = spawnSync(editor, [destinationDirectory], {
|
|
56
|
+
stdio: "inherit",
|
|
57
|
+
});
|
|
58
|
+
if (result.error) {
|
|
59
|
+
console.error(`Failed to open ${editor}: ${result.error.message}`);
|
|
60
|
+
console.error("The worktree was created successfully, but the editor could not be opened.");
|
|
61
|
+
}
|
|
45
62
|
}
|
|
46
63
|
const program = new Command()
|
|
47
64
|
.name(packageJson.name)
|
|
48
65
|
.description("Create or reuse a Git worktree for a branch as a sibling of the current worktree")
|
|
49
66
|
.version(packageJson.version)
|
|
50
67
|
.argument("<branch>", "branch name for the worktree")
|
|
51
|
-
.
|
|
68
|
+
.option("-e, --editor <command>", "Editor to open the worktree with (default: WORKTREE_ADD_EDITOR env var or 'cursor')")
|
|
69
|
+
.action(async (branch, options) => {
|
|
52
70
|
try {
|
|
53
|
-
await main(branch);
|
|
71
|
+
await main(branch, options);
|
|
54
72
|
}
|
|
55
73
|
catch (error) {
|
|
56
74
|
console.error(error);
|