@termwire/cli 0.5.0 → 0.6.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/CHANGELOG.md +84 -0
- package/README.md +66 -0
- package/dist/down.d.ts +40 -0
- package/dist/down.js +99 -0
- package/dist/process-scan-darwin.d.ts +24 -0
- package/dist/process-scan-darwin.js +102 -0
- package/dist/process-scan-linux.d.ts +13 -0
- package/dist/process-scan-linux.js +66 -0
- package/dist/process-scan.d.ts +37 -23
- package/dist/process-scan.js +30 -131
- package/dist/program.d.ts +9 -1
- package/dist/program.js +76 -10
- package/dist/runtime.d.ts +15 -5
- package/dist/runtime.js +22 -4
- package/dist/workspaces.d.ts +45 -0
- package/dist/workspaces.js +116 -0
- package/dist/worktree.d.ts +15 -0
- package/dist/worktree.js +65 -7
- package/package.json +3 -3
package/dist/worktree.js
CHANGED
|
@@ -1,9 +1,25 @@
|
|
|
1
1
|
import { realpath as resolveRealpath } from "node:fs/promises";
|
|
2
2
|
import { dirname, isAbsolute, join, resolve } from "node:path";
|
|
3
3
|
import { sanitizeComponent } from "./identity.js";
|
|
4
|
+
/** Git's own words when the directory is outside a repository, next to exit 128. */
|
|
5
|
+
const notARepository = /not a git repository/i;
|
|
6
|
+
/** `git worktree list --porcelain` separates its records with a blank line. */
|
|
7
|
+
const recordSeparator = /\n\s*\n/;
|
|
8
|
+
/** The `worktree <path>` line that opens every record. */
|
|
9
|
+
const worktreePathLine = /^worktree (.+)$/m;
|
|
10
|
+
/** The `branch refs/heads/<name>` line, absent on a detached HEAD. */
|
|
11
|
+
const branchLine = /^branch refs\/heads\/(.+)$/m;
|
|
12
|
+
/** Flags that stand alone on a line of their own. */
|
|
13
|
+
const bareLine = /^bare$/m;
|
|
14
|
+
const detachedLine = /^detached$/m;
|
|
15
|
+
/** The same two flags, except each may carry a reason after a space. */
|
|
16
|
+
const reasonLines = {
|
|
17
|
+
locked: /^locked(?: (.*))?$/m,
|
|
18
|
+
prunable: /^prunable(?: (.*))?$/m,
|
|
19
|
+
};
|
|
4
20
|
export async function findGitRoot(exec, cwd) {
|
|
5
21
|
const result = await exec(["git", "rev-parse", "--show-toplevel"], { cwd });
|
|
6
|
-
if (result.exitCode === 128 &&
|
|
22
|
+
if (result.exitCode === 128 && notARepository.test(result.stderr)) {
|
|
7
23
|
return undefined;
|
|
8
24
|
}
|
|
9
25
|
if (result.exitCode !== 0) {
|
|
@@ -16,21 +32,21 @@ export function parseWorktreeList(output) {
|
|
|
16
32
|
if (trimmed === "") {
|
|
17
33
|
return [];
|
|
18
34
|
}
|
|
19
|
-
return trimmed.split(
|
|
20
|
-
const path = record.match(
|
|
35
|
+
return trimmed.split(recordSeparator).map((record) => {
|
|
36
|
+
const path = record.match(worktreePathLine)?.[1];
|
|
21
37
|
if (!path) {
|
|
22
38
|
throw new Error("Git worktree record is missing a path");
|
|
23
39
|
}
|
|
24
40
|
const entry = { path };
|
|
25
|
-
const branch = record.match(
|
|
41
|
+
const branch = record.match(branchLine)?.[1];
|
|
26
42
|
if (branch)
|
|
27
43
|
entry.branch = branch;
|
|
28
|
-
if (
|
|
44
|
+
if (bareLine.test(record))
|
|
29
45
|
entry.bare = true;
|
|
30
|
-
if (
|
|
46
|
+
if (detachedLine.test(record))
|
|
31
47
|
entry.detached = true;
|
|
32
48
|
for (const field of ["locked", "prunable"]) {
|
|
33
|
-
const match = record.match(
|
|
49
|
+
const match = record.match(reasonLines[field]);
|
|
34
50
|
if (match)
|
|
35
51
|
entry[field] = match[1] || true;
|
|
36
52
|
}
|
|
@@ -164,3 +180,45 @@ async function validateRegisteredWorktree(options) {
|
|
|
164
180
|
function resolveGitPath(path, cwd) {
|
|
165
181
|
return isAbsolute(path) ? path : resolve(cwd, path);
|
|
166
182
|
}
|
|
183
|
+
/**
|
|
184
|
+
* A worktree's own Git directory lives inside the common one, so the two paths are
|
|
185
|
+
* equal only in the main checkout.
|
|
186
|
+
*/
|
|
187
|
+
export async function inspectWorktree(exec, path) {
|
|
188
|
+
const directories = await exec([
|
|
189
|
+
"git",
|
|
190
|
+
"-C",
|
|
191
|
+
path,
|
|
192
|
+
"rev-parse",
|
|
193
|
+
"--absolute-git-dir",
|
|
194
|
+
"--git-common-dir",
|
|
195
|
+
]);
|
|
196
|
+
// Git refusing and Git answering with less than the two paths mean the same thing.
|
|
197
|
+
const [gitDir, commonDir] = directories.stdout.trim().split(/\r?\n/);
|
|
198
|
+
if (directories.exitCode !== 0 || !gitDir || !commonDir) {
|
|
199
|
+
throw new Error(`not a Git worktree: ${path}`);
|
|
200
|
+
}
|
|
201
|
+
const status = await exec(["git", "-C", path, "status", "--porcelain"]);
|
|
202
|
+
if (status.exitCode !== 0) {
|
|
203
|
+
throw new Error(`git status failed: ${status.stderr}`);
|
|
204
|
+
}
|
|
205
|
+
return {
|
|
206
|
+
main: resolveGitPath(gitDir, path) === resolveGitPath(commonDir, path),
|
|
207
|
+
dirty: status.stdout.trim().length > 0,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
/** Removes the worktree and its registration. The branch it held outlives it. */
|
|
211
|
+
export async function removeWorktree(exec, path, options) {
|
|
212
|
+
const removed = await exec([
|
|
213
|
+
"git",
|
|
214
|
+
"-C",
|
|
215
|
+
path,
|
|
216
|
+
"worktree",
|
|
217
|
+
"remove",
|
|
218
|
+
...(options.force ? ["--force"] : []),
|
|
219
|
+
path,
|
|
220
|
+
]);
|
|
221
|
+
if (removed.exitCode !== 0) {
|
|
222
|
+
throw new Error(`git worktree remove failed: ${removed.stderr}`);
|
|
223
|
+
}
|
|
224
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@termwire/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -40,8 +40,8 @@
|
|
|
40
40
|
"dependencies": {
|
|
41
41
|
"@clack/core": "^1.5.1",
|
|
42
42
|
"@clack/prompts": "^1.8.1",
|
|
43
|
-
"@termwire/nvim": "^0.
|
|
44
|
-
"@termwire/tmux": "^0.
|
|
43
|
+
"@termwire/nvim": "^0.6.0",
|
|
44
|
+
"@termwire/tmux": "^0.6.0",
|
|
45
45
|
"commander": "^15.0.0",
|
|
46
46
|
"jsonc-parser": "^3.3.1",
|
|
47
47
|
"zod": "^4.0.0"
|