-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathindex.ts
More file actions
265 lines (251 loc) · 8.63 KB
/
Copy pathindex.ts
File metadata and controls
265 lines (251 loc) · 8.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env node
import { Command } from "commander";
import chalk from "chalk";
import { newWorktreeHandler } from "./commands/new.js";
import { setupWorktreeHandler } from "./commands/setup.js";
import { listWorktreesHandler } from "./commands/list.js";
import { removeWorktreeHandler } from "./commands/remove.js";
import { mergeWorktreeHandler } from "./commands/merge.js";
import { purgeWorktreesHandler } from "./commands/purge.js";
import { configHandler } from "./commands/config.js";
import { prWorktreeHandler } from "./commands/pr.js";
import { openWorktreeHandler } from "./commands/open.js";
import { extractWorktreeHandler } from "./commands/extract.js";
const program = new Command();
program
.name("wt")
.description("Manage git worktrees and open them in the Cursor editor.")
.version("1.0.0");
program
.command("new")
.argument("[branchName]", "Name of the branch to base this worktree on")
.option("-p, --path <path>", "Relative path/folder name for new worktree")
.option(
"-c, --checkout",
"Create new branch if it doesn't exist and checkout automatically",
false
)
.option(
"-i, --install <packageManager>",
"Package manager to use for installing dependencies (npm, pnpm, bun, etc.)"
)
.option(
"-e, --editor <editor>",
"Editor to use for opening the worktree (e.g., code, webstorm, windsurf, etc.)"
)
.description(
"Create a new worktree for the specified branch, install dependencies if specified, and open in editor."
)
.action(newWorktreeHandler);
program
.command("setup")
.argument("[branchName]", "Name of the branch to base this worktree on")
.option("-p, --path <path>", "Relative path/folder name for new worktree")
.option(
"-c, --checkout",
"Create new branch if it doesn't exist and checkout automatically",
false
)
.option(
"-i, --install <packageManager>",
"Package manager to use for installing dependencies (npm, pnpm, bun, etc.)"
)
.option(
"-e, --editor <editor>",
"Editor to use for opening the worktree (e.g., code, webstorm, windsurf, etc.)"
)
.option(
"-t, --trust",
"Trust and run setup commands without confirmation (for CI environments)",
false
)
.description(
"Create a new worktree and run setup scripts from worktrees.json or .cursor/worktrees.json"
)
.action(setupWorktreeHandler);
program
.command("list")
.alias("ls")
.description("List all existing worktrees for this repository.")
.action(listWorktreesHandler);
program
.command("remove")
.alias("rm")
.argument("[pathOrBranch]", "Path of the worktree or branch to remove.")
.option(
"-f, --force",
"Force removal of worktree and deletion of the folder",
false
)
.description(
"Remove a specified worktree. Cleans up the .git/worktrees references."
)
.action(removeWorktreeHandler);
program
.command("merge")
.argument("<branchName>", "Name of the branch to merge from")
.option("-f, --force", "Force removal of worktree (when used with --remove)", false)
.option("--auto-commit", "Automatically commit uncommitted changes in the target worktree", false)
.option("-m, --message <message>", "Commit message to use when auto-committing (requires --auto-commit)")
.option("--remove", "Remove the worktree after merging (opt-in destructive cleanup)", false)
.description(
"Merge a branch into the current branch. By default, preserves the source worktree after merge."
)
.action(mergeWorktreeHandler);
program
.command("purge")
.description(
"Safely remove all worktrees except for the main branch, with confirmation."
)
.action(purgeWorktreesHandler);
program
.command("pr")
.argument(
"[prNumber]",
"GitHub PR or GitLab MR number to create a worktree from"
)
.option(
"-p, --path <path>",
"Specify a custom path for the worktree (defaults to repoName-branchName)"
)
.option(
"-i, --install <packageManager>",
"Package manager to use for installing dependencies (npm, pnpm, bun, etc.)"
)
.option(
"-e, --editor <editor>",
"Editor to use for opening the worktree (overrides default editor)"
)
.option(
"-s, --setup",
"Run setup scripts from worktrees.json or .cursor/worktrees.json"
)
.option(
"-t, --trust",
"Trust and execute setup scripts without confirmation (use with caution)"
)
.description(
"Fetch the branch for a given GitHub PR or GitLab MR number and create a worktree."
)
.action(prWorktreeHandler);
program
.command("open")
.argument("[pathOrBranch]", "Path to worktree or branch name to open")
.option(
"-e, --editor <editor>",
"Editor to use for opening the worktree (overrides default editor)"
)
.description("Open an existing worktree in the editor.")
.action(openWorktreeHandler);
program
.command("extract")
.argument("[branchName]", "Name of the branch to extract (defaults to current branch)")
.option("-p, --path <path>", "Relative path/folder name for the worktree")
.option(
"-i, --install <packageManager>",
"Package manager to use for installing dependencies (npm, pnpm, bun, etc.)"
)
.option(
"-e, --editor <editor>",
"Editor to use for opening the worktree (overrides default editor)"
)
.description(
"Extract an existing branch as a new worktree. If no branch is specified, extracts the current branch."
)
.action(extractWorktreeHandler);
program
.command("config")
.description("Manage CLI configuration settings.")
.addCommand(
new Command("set")
.description("Set a configuration value.")
.addCommand(
new Command("editor")
.argument(
"<editorName>",
"Name of the editor command (e.g., code, cursor, webstorm)"
)
.description("Set the default editor to open worktrees in.")
.action((editorName) => configHandler("set", "editor", editorName))
)
.addCommand(
new Command("provider")
.argument(
"<providerName>",
"Name of the git provider CLI (gh for GitHub, glab for GitLab)"
)
.description("Set the default git provider for PR/MR commands.")
.action((providerName) => configHandler("set", "provider", providerName))
)
.addCommand(
new Command("worktreepath")
.argument(
"<path>",
"Path where all worktrees will be created (e.g., ~/worktrees)"
)
.description("Set the default directory for new worktrees.")
.action((worktreePath) => configHandler("set", "worktreepath", worktreePath))
)
.addCommand(
new Command("trust")
.argument(
"<value>",
"Enable or disable trust mode (true/false)"
)
.description("Set trust mode to skip setup command confirmations.")
.action((value) => configHandler("set", "trust", value))
)
.addCommand(
new Command("subfolder")
.argument(
"<value>",
"Enable or disable subfolder mode (true/false)"
)
.description("Set subfolder mode for worktree paths (my-app-worktrees/feature).")
.action((value) => configHandler("set", "subfolder", value))
)
)
.addCommand(
new Command("get")
.description("Get a configuration value.")
.addCommand(
new Command("editor")
.description("Get the currently configured default editor.")
.action(() => configHandler("get", "editor"))
)
.addCommand(
new Command("provider")
.description("Get the currently configured git provider.")
.action(() => configHandler("get", "provider"))
)
.addCommand(
new Command("worktreepath")
.description("Get the currently configured default worktree directory.")
.action(() => configHandler("get", "worktreepath"))
)
.addCommand(
new Command("trust")
.description("Get the current trust mode setting.")
.action(() => configHandler("get", "trust"))
)
.addCommand(
new Command("subfolder")
.description("Get the current subfolder mode setting.")
.action(() => configHandler("get", "subfolder"))
)
)
.addCommand(
new Command("clear")
.description("Clear a configuration value.")
.addCommand(
new Command("worktreepath")
.description("Clear the default worktree directory (revert to sibling behavior).")
.action(() => configHandler("clear", "worktreepath"))
)
)
.addCommand(
new Command("path")
.description("Show the path to the configuration file.")
.action(() => configHandler("path"))
);
program.parse(process.argv);