pi-odd-worktree 2.1.0 → 3.0.1
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/README.md +54 -319
- package/extensions/odd-worktree-tools.ts +4182 -2539
- package/lib/bash-activity.js +1166 -0
- package/lib/bash-design.js +684 -0
- package/lib/bash-destructive.js +559 -0
- package/lib/bash-frame.js +205 -0
- package/lib/card-content.js +23 -0
- package/lib/card-style.js +175 -0
- package/lib/checkout-facts.js +30 -70
- package/lib/codegraph-card-preview.js +628 -0
- package/lib/codegraph-card.js +515 -0
- package/lib/codegraph-compaction.js +76 -0
- package/lib/codegraph-daemon.js +246 -0
- package/lib/codegraph-edit-sync.js +182 -0
- package/lib/codegraph-guard-target.js +337 -0
- package/lib/codegraph-guard.js +211 -0
- package/lib/codegraph-index.js +1020 -0
- package/lib/codegraph-lock.js +607 -0
- package/lib/codegraph-mcp-config.js +134 -0
- package/lib/codegraph-mcp.js +123 -0
- package/lib/codegraph-paths.js +69 -0
- package/lib/codegraph-prompt/explore.md +11 -0
- package/lib/codegraph-prompt/fix.md +6 -0
- package/lib/codegraph-prompt/generic.md +8 -0
- package/lib/codegraph-prompt/judge.md +9 -0
- package/lib/codegraph-prompt/orchestrator.md +17 -0
- package/lib/codegraph-prompt/verify.md +10 -0
- package/lib/codegraph-prompt/worker.md +7 -0
- package/lib/codegraph-role.js +55 -0
- package/lib/codegraph-rules.js +146 -0
- package/lib/codegraph-session.js +303 -383
- package/lib/codegraph-warmup.js +139 -0
- package/lib/file-card-markdown.js +141 -0
- package/lib/file-card-resolver.js +245 -0
- package/lib/file-card.js +659 -0
- package/lib/files-index.js +387 -0
- package/lib/files-insert.js +81 -0
- package/lib/files-orders.js +326 -0
- package/lib/files-picker-view.js +1421 -0
- package/lib/files-preview.js +338 -0
- package/lib/files-scope-detail.js +171 -0
- package/lib/files-scope-view.js +507 -0
- package/lib/gh-issues.js +323 -15
- package/lib/issue-preview.js +26 -7
- package/lib/issues-bash-mutation.js +206 -0
- package/lib/issues-cache-store.js +261 -0
- package/lib/issues-section.js +710 -0
- package/lib/issues-view.js +649 -52
- package/lib/odd-prompt/odd-worktrees.md +21 -0
- package/lib/odd-worktrees-prompt.js +10 -28
- package/lib/related-issues-cache.js +210 -0
- package/lib/shell-card.js +825 -0
- package/lib/skill-odd-card.js +183 -0
- package/lib/tui-paint.js +4 -5
- package/lib/warp-create-panel.js +538 -250
- package/lib/warp-create-picker.js +56 -9
- package/lib/warp-delete-confirm.js +19 -28
- package/lib/warp-selector-view.js +160 -82
- package/lib/which-command.js +90 -0
- package/lib/worktree-card.js +267 -897
- package/lib/worktree-cards.js +383 -0
- package/lib/worktree-create.js +287 -0
- package/lib/worktree-git.js +3 -3
- package/lib/worktree-guard.js +548 -0
- package/lib/worktree-hooks.js +421 -712
- package/lib/worktree-list.js +120 -0
- package/lib/worktree-registry.js +162 -879
- package/lib/{worktree-close-herdr.js → worktree-remove.js} +9 -11
- package/lib/worktree-setup-runner.js +376 -0
- package/lib/worktree-setup.js +140 -0
- package/lib/worktree-warp-busy.js +462 -85
- package/lib/worktree-warp-sessions.js +1 -2
- package/package.json +2 -2
- package/tools/bash-activity.test.js +1420 -0
- package/tools/bash-design.test.js +1093 -0
- package/tools/bash-destructive.test.js +139 -0
- package/tools/card-content.test.js +44 -0
- package/tools/card-style.test.js +298 -0
- package/tools/codegraph-card-preview.test.js +254 -0
- package/tools/codegraph-card.test.js +1080 -0
- package/tools/codegraph-compaction.test.js +378 -0
- package/tools/codegraph-daemon.test.js +248 -0
- package/tools/codegraph-edit-sync.test.js +295 -0
- package/tools/codegraph-guard-target.test.js +174 -0
- package/tools/codegraph-guard.test.js +238 -0
- package/tools/codegraph-index.test.js +1948 -0
- package/tools/codegraph-lock.test.js +932 -0
- package/tools/codegraph-mcp-config.test.js +181 -0
- package/tools/codegraph-mcp.test.js +194 -0
- package/tools/codegraph-paths.test.js +44 -0
- package/tools/codegraph-role.test.js +30 -0
- package/tools/codegraph-rules.test.js +250 -0
- package/tools/codegraph-session.test.js +660 -391
- package/tools/codegraph-warmup.test.js +316 -0
- package/tools/commands.test.js +510 -31
- package/tools/file-card-markdown.test.js +188 -0
- package/tools/file-card.test.js +1289 -0
- package/tools/files-bridge.test.js +575 -0
- package/tools/files-command.test.js +613 -0
- package/tools/files-index.test.js +473 -0
- package/tools/files-insert.test.js +139 -0
- package/tools/files-orders.test.js +597 -0
- package/tools/files-picker-view.test.js +1889 -0
- package/tools/files-preview.test.js +452 -0
- package/tools/files-scope-detail.test.js +264 -0
- package/tools/files-scope-view.test.js +614 -0
- package/tools/fixtures/card-codegraph-outputs.js +110 -0
- package/tools/fixtures/codegraph-cli-stub.js +16 -0
- package/tools/fixtures/codegraph-init-process.js +28 -0
- package/tools/fixtures/codegraph-lock-worker.js +117 -0
- package/tools/fixtures/daemon-fake.js +98 -0
- package/tools/fixtures/daemon-mcp-server.js +39 -0
- package/tools/fixtures/daemon-warmup-worker.js +12 -0
- package/tools/fixtures/load-extension.js +136 -3
- package/tools/gh-issues-feedback.test.js +31 -7
- package/tools/gh-issues.test.js +417 -1
- package/tools/issue-preview.test.js +44 -7
- package/tools/issues-bash-mutation.test.js +101 -0
- package/tools/issues-cache-store.test.js +222 -0
- package/tools/issues-command.test.js +1517 -67
- package/tools/issues-open-guard.test.js +1096 -0
- package/tools/issues-section.test.js +4235 -0
- package/tools/issues-seed-query-regression.test.js +67 -0
- package/tools/issues-view.test.js +1489 -60
- package/tools/odd-worktrees-prompt.test.js +98 -65
- package/tools/package-surface.test.js +431 -36
- package/tools/related-issues-cache.test.js +324 -0
- package/tools/shell-card.test.js +834 -0
- package/tools/skill-odd-card.test.js +200 -0
- package/tools/t5-session-identity.test.js +44 -22
- package/tools/warp-busy-guard.test.js +2565 -309
- package/tools/warp-create-panel.test.js +499 -74
- package/tools/warp-create-picker.test.js +101 -5
- package/tools/warp-delete-confirm.test.js +49 -15
- package/tools/warp-empty-session.test.js +0 -1
- package/tools/warp-fallback-regression.test.js +533 -0
- package/tools/warp-selector-view.test.js +460 -196
- package/tools/which-command.test.js +126 -0
- package/tools/worktree-card.test.js +551 -0
- package/tools/worktree-cards.test.js +845 -0
- package/tools/worktree-changes.test.js +4 -5
- package/tools/worktree-create.test.js +489 -0
- package/tools/worktree-guard.test.js +823 -0
- package/tools/worktree-hooks.test.js +905 -0
- package/tools/worktree-list.test.js +170 -0
- package/tools/worktree-registry.test.js +89 -230
- package/tools/worktree-remove-async.test.js +318 -0
- package/tools/worktree-setup-runner.test.js +276 -0
- package/tools/worktree-setup.test.js +140 -0
- package/tools/worktree-tools.test.js +419 -199
- package/lib/codegraph-commands.js +0 -165
- package/lib/odd-prompt/odd-worktrees.with-codegraph.md +0 -19
- package/lib/odd-prompt/odd-worktrees.without-codegraph.md +0 -15
- package/lib/worktree-model-result.js +0 -291
- package/tools/codegraph-commands.test.js +0 -371
- package/tools/herdr-free-create.test.js +0 -161
- package/tools/worktree-close-herdr-async.test.js +0 -204
- package/tools/worktree-odd-migration.test.js +0 -224
- package/tools/worktree-registry-cache.test.js +0 -163
- package/tools/worktree-registry-odd.test.js +0 -231
|
@@ -0,0 +1,823 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// T1, T2, T5 and T6 (S1–S11): the two `bash` guards and the reason each prints.
|
|
3
|
+
//
|
|
4
|
+
// The guarded forms are one table: every allowed and refused spelling from the
|
|
5
|
+
// spec, through the module's one entry point and, for the extension half, through
|
|
6
|
+
// the `tool_call` handler the host actually receives. The git answers the module
|
|
7
|
+
// needs are injected in the module table (no child is spawned for a decision), and
|
|
8
|
+
// the extension half builds throwaway repositories under the system temp directory
|
|
9
|
+
// because the wiring is what resolves git — every one of them is removed in teardown.
|
|
10
|
+
//
|
|
11
|
+
// Three facts are the seams, and each call counts its reads, so "read only when the
|
|
12
|
+
// rule needs it" is observable: which checkout a cwd is (`repoIdentity`), the
|
|
13
|
+
// repository's default branch (`defaultBranch`) and the branch that checkout is on
|
|
14
|
+
// RIGHT NOW (`currentBranch`). S11 walks the command's own `cd`s, so a row may also
|
|
15
|
+
// move the effective cwd before the switch it judges.
|
|
16
|
+
//
|
|
17
|
+
// The guarded commands are INPUT TEXT only: the guard reads the string and never
|
|
18
|
+
// runs it, so the rows below are passed to the decision and to the handler, and the
|
|
19
|
+
// only children this file spawns are the `git init`/`git worktree add` of its own
|
|
20
|
+
// fixtures. Public behavior only: what the handler returns.
|
|
21
|
+
|
|
22
|
+
const { test, describe, before, after } = require('node:test');
|
|
23
|
+
const assert = require('node:assert/strict');
|
|
24
|
+
const fs = require('node:fs');
|
|
25
|
+
const os = require('node:os');
|
|
26
|
+
const path = require('node:path');
|
|
27
|
+
const { spawnSync } = require('node:child_process');
|
|
28
|
+
const { decideBashGuard } = require('../lib/worktree-guard.js');
|
|
29
|
+
const { loadExtension } = require('./fixtures/load-extension.js');
|
|
30
|
+
|
|
31
|
+
// The three refusals of S10, written out: a reworded sentence fails here.
|
|
32
|
+
const WORKTREE_ADD_REFUSAL = '`git worktree add` did not run: create the ODD\'s checkout with `worktree_create`.';
|
|
33
|
+
const WORKTREE_LIST_REFUSAL = '`git worktree list` did not run: use `worktree_list` (`all: true` also shows checkouts outside `<repo-name>-worktrees`).';
|
|
34
|
+
const BRANCH_SWITCH_REFUSAL = 'The branch switch did not run: the principal checkout stays on its default branch. Create the ODD\'s checkout with `worktree_create`.';
|
|
35
|
+
|
|
36
|
+
const REFUSALS = [WORKTREE_ADD_REFUSAL, WORKTREE_LIST_REFUSAL, BRANCH_SWITCH_REFUSAL];
|
|
37
|
+
|
|
38
|
+
// The two checkouts one repository has: the principal (its root IS the common dir's
|
|
39
|
+
// parent) and a linked worktree (it shares the principal's common dir).
|
|
40
|
+
const PRINCIPAL = { root: '/repo', commonDir: '/repo/.git' };
|
|
41
|
+
const LINKED = { root: '/elsewhere/repo-worktrees/odd-a', commonDir: '/repo/.git' };
|
|
42
|
+
|
|
43
|
+
// A SHA-looking ref, for the detached-checkout rows. Nothing runs it: the guard only
|
|
44
|
+
// reads it as text, so it is spelled through one constant.
|
|
45
|
+
const SHA_REF = '4f3a2b1c9d8e7f6a5b4c3d2e1f0a9b8c7d6e5f4a';
|
|
46
|
+
|
|
47
|
+
// The paths a `git checkout <path>` restore may name: the seam answers for these
|
|
48
|
+
// and for nothing else, so `feat/x` is a branch and `src/app.ts` a file.
|
|
49
|
+
const EXISTING_PATHS = ['README.md', 'src/app.ts', './src', 'LICENSE', '.'];
|
|
50
|
+
|
|
51
|
+
// S11: which checkout a cwd is, resolved by path equality, so a command's own `cd`
|
|
52
|
+
// is observable: `/repo` is the principal, the worktrees directory entry is linked,
|
|
53
|
+
// and anything else is not a checkout at all.
|
|
54
|
+
function identityByCwd(cwd) {
|
|
55
|
+
if (typeof cwd !== 'string') return undefined;
|
|
56
|
+
if (cwd === PRINCIPAL.root) return PRINCIPAL;
|
|
57
|
+
if (cwd === LINKED.root) return LINKED;
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// A harness over the module's one entry point: every seam is a fake, and each call
|
|
62
|
+
// counts its reads, so "read only when the rule needs it" is observable.
|
|
63
|
+
function harness(options = {}) {
|
|
64
|
+
const reads = { identity: 0, defaultBranch: 0, currentBranch: 0 };
|
|
65
|
+
const identity = 'identity' in options ? options.identity : PRINCIPAL;
|
|
66
|
+
const defaultBranch = 'defaultBranch' in options ? options.defaultBranch : 'main';
|
|
67
|
+
const currentBranch = 'currentBranch' in options ? options.currentBranch : 'main';
|
|
68
|
+
const run = (command, callOptions = {}) => decideBashGuard({
|
|
69
|
+
toolName: 'toolName' in callOptions ? callOptions.toolName : 'bash',
|
|
70
|
+
command,
|
|
71
|
+
cwd: 'cwd' in callOptions ? callOptions.cwd : '/repo',
|
|
72
|
+
repoIdentity: (cwd) => {
|
|
73
|
+
reads.identity += 1;
|
|
74
|
+
if (identity instanceof Error) throw identity;
|
|
75
|
+
return typeof identity === 'function' ? identity(cwd) : identity;
|
|
76
|
+
},
|
|
77
|
+
pathExists: (cwd, value) => ('paths' in options ? options.paths : EXISTING_PATHS).includes(value),
|
|
78
|
+
defaultBranch: () => {
|
|
79
|
+
reads.defaultBranch += 1;
|
|
80
|
+
if (defaultBranch instanceof Error) throw defaultBranch;
|
|
81
|
+
return defaultBranch;
|
|
82
|
+
},
|
|
83
|
+
currentBranch: (root) => {
|
|
84
|
+
reads.currentBranch += 1;
|
|
85
|
+
if (currentBranch instanceof Error) throw currentBranch;
|
|
86
|
+
return typeof currentBranch === 'function' ? currentBranch(root) : currentBranch;
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
return { run, reads };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// ---- S10: the refusals themselves ----
|
|
93
|
+
|
|
94
|
+
describe('the refusals are one reminder each (S10)', () => {
|
|
95
|
+
test('each one says its command did not run and names the tool that replaces it', () => {
|
|
96
|
+
for (const [sentence, tool] of [
|
|
97
|
+
[WORKTREE_ADD_REFUSAL, '`worktree_create`'],
|
|
98
|
+
[WORKTREE_LIST_REFUSAL, '`worktree_list`'],
|
|
99
|
+
[BRANCH_SWITCH_REFUSAL, '`worktree_create`'],
|
|
100
|
+
]) {
|
|
101
|
+
assert.equal(sentence.includes('!'), false, `no exclamation: ${sentence}`);
|
|
102
|
+
assert.equal(/[\u{1F300}-\u{1FAFF}\u{2600}-\u{27BF}]/u.test(sentence), false, `no emoji: ${sentence}`);
|
|
103
|
+
assert.match(sentence, /did not run:/, `says the command did not run: ${sentence}`);
|
|
104
|
+
assert.ok(sentence.endsWith('.'), `ends with a period: ${sentence}`);
|
|
105
|
+
assert.ok(sentence.includes(tool), `names ${tool}: ${sentence}`);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test('no refusal sequences two tools or restates the workflow', () => {
|
|
110
|
+
for (const sentence of REFUSALS) {
|
|
111
|
+
const named = sentence.match(/`worktree_(?:list|create)`/g) || [];
|
|
112
|
+
assert.equal(named.length, 1, `exactly one tool is named: ${sentence}`);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test('the listing refusal names all: true for the worktrees outside the directory', () => {
|
|
117
|
+
assert.match(WORKTREE_LIST_REFUSAL, /`worktree_list`/);
|
|
118
|
+
assert.match(WORKTREE_LIST_REFUSAL, /`all: true`/);
|
|
119
|
+
assert.match(WORKTREE_LIST_REFUSAL, /outside `<repo-name>-worktrees`/);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
// ---- S1 and S9: any cwd, text only ----
|
|
124
|
+
|
|
125
|
+
const WORKTREE_ADD_COMMANDS = [
|
|
126
|
+
'git worktree add ../odd-a',
|
|
127
|
+
'git -C /repo worktree add ../odd-a',
|
|
128
|
+
'git --no-pager worktree add ../odd-a',
|
|
129
|
+
'git -c core.pager=cat worktree add ../odd-a',
|
|
130
|
+
'git worktree add ../odd-a -b feat/odd-a',
|
|
131
|
+
'cd /repo && git worktree add ../odd-a',
|
|
132
|
+
'git status || git worktree add ../odd-a',
|
|
133
|
+
'git status; git worktree add ../odd-a',
|
|
134
|
+
'git status | git worktree add ../odd-a',
|
|
135
|
+
'git status & git worktree add ../odd-a',
|
|
136
|
+
'git status\ngit worktree add ../odd-a',
|
|
137
|
+
'echo $(git worktree add ../odd-a)',
|
|
138
|
+
'echo `git worktree add ../odd-a`',
|
|
139
|
+
'echo "$(git -C /repo worktree add ../odd-a)"',
|
|
140
|
+
'sudo git worktree add ../odd-a',
|
|
141
|
+
'git worktree add "$(pwd)/../odd-a"',
|
|
142
|
+
];
|
|
143
|
+
|
|
144
|
+
const WORKTREE_LIST_COMMANDS = [
|
|
145
|
+
'git worktree list',
|
|
146
|
+
'git worktree list --porcelain',
|
|
147
|
+
'git -C /repo worktree list --porcelain',
|
|
148
|
+
'git --no-pager worktree list',
|
|
149
|
+
'git status && git worktree list --porcelain',
|
|
150
|
+
'git worktree list | head -20',
|
|
151
|
+
'echo $(git worktree list --porcelain)',
|
|
152
|
+
];
|
|
153
|
+
|
|
154
|
+
describe('S1 and S9: worktree commands are refused in every cwd', () => {
|
|
155
|
+
test('the add sequence is refused with the create refusal', () => {
|
|
156
|
+
for (const command of WORKTREE_ADD_COMMANDS) {
|
|
157
|
+
for (const [label, options] of [
|
|
158
|
+
['the principal checkout', { identity: PRINCIPAL }],
|
|
159
|
+
['a linked worktree', { identity: LINKED }],
|
|
160
|
+
['no repository at all', { identity: new Error('not a git repository') }],
|
|
161
|
+
]) {
|
|
162
|
+
const { run } = harness(options);
|
|
163
|
+
assert.deepEqual(run(command), { block: true, reason: WORKTREE_ADD_REFUSAL }, `${command} in ${label}`);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
test('the list sequence is refused with the listing refusal', () => {
|
|
169
|
+
for (const command of WORKTREE_LIST_COMMANDS) {
|
|
170
|
+
for (const options of [{ identity: PRINCIPAL }, { identity: LINKED }, { identity: new Error('not a git repository') }]) {
|
|
171
|
+
const { run, reads } = harness(options);
|
|
172
|
+
assert.deepEqual(run(command), { block: true, reason: WORKTREE_LIST_REFUSAL }, command);
|
|
173
|
+
assert.deepEqual(reads, { identity: 0, defaultBranch: 0, currentBranch: 0 }, `${command} reads no repository state`);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
test('a script, alias or make target that would create one is not inspected', () => {
|
|
179
|
+
for (const command of ['bash scripts/create-worktree.sh', 'make create-worktree', 'npm run worktree:add', 'git config alias.wt "worktree add"']) {
|
|
180
|
+
const { run } = harness();
|
|
181
|
+
assert.deepEqual(run(command), { block: false }, command);
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
// ---- S3: creating a branch is allowed everywhere (L10) ----
|
|
187
|
+
|
|
188
|
+
const BRANCH_COMMANDS = [
|
|
189
|
+
'git branch odd-a',
|
|
190
|
+
'git branch odd-a main',
|
|
191
|
+
'git branch -f odd-a',
|
|
192
|
+
'git branch -f odd-a main',
|
|
193
|
+
'git branch -t odd-a origin/odd-a',
|
|
194
|
+
'git branch --track odd-a origin/odd-a',
|
|
195
|
+
'git branch -m odd-a',
|
|
196
|
+
'git branch -m odd-a odd-b',
|
|
197
|
+
'git branch -c odd-a odd-b',
|
|
198
|
+
'git branch -d odd-a',
|
|
199
|
+
'git branch -D odd-a',
|
|
200
|
+
'git branch --list odd',
|
|
201
|
+
'git branch',
|
|
202
|
+
'git branch -a',
|
|
203
|
+
'git branch -v',
|
|
204
|
+
'git status && git branch odd-a',
|
|
205
|
+
'git -C /repo branch odd-a',
|
|
206
|
+
'cd /repo && git branch odd-a',
|
|
207
|
+
];
|
|
208
|
+
|
|
209
|
+
describe('S3: creating a branch passes, in the principal checkout and everywhere else', () => {
|
|
210
|
+
test('every `git branch` form passes and reads no branch state', () => {
|
|
211
|
+
for (const command of BRANCH_COMMANDS) {
|
|
212
|
+
const { run, reads } = harness();
|
|
213
|
+
assert.deepEqual(run(command), { block: false }, command);
|
|
214
|
+
assert.deepEqual(reads, { identity: 0, defaultBranch: 0, currentBranch: 0 }, `${command} reads nothing`);
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test('the same command passes inside a linked worktree', () => {
|
|
219
|
+
for (const command of BRANCH_COMMANDS) {
|
|
220
|
+
const { run } = harness({ identity: LINKED });
|
|
221
|
+
assert.deepEqual(run(command, { cwd: LINKED.root }), { block: false }, command);
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
// ---- S4 and S5: leaving the branch the principal checkout is on ----
|
|
227
|
+
|
|
228
|
+
describe('S4: leaving the default branch of the principal checkout is refused', () => {
|
|
229
|
+
test('another branch, the previous branch and a detached checkout are refused', () => {
|
|
230
|
+
for (const command of [
|
|
231
|
+
'git checkout develop',
|
|
232
|
+
'git switch develop',
|
|
233
|
+
'git checkout -',
|
|
234
|
+
'git switch -',
|
|
235
|
+
`git checkout ${SHA_REF}`,
|
|
236
|
+
`git checkout ${SHA_REF.slice(0, 7)}`,
|
|
237
|
+
'git checkout --detach main',
|
|
238
|
+
'git switch --detach',
|
|
239
|
+
'git checkout HEAD~2',
|
|
240
|
+
'git status; git checkout develop',
|
|
241
|
+
'git -C /repo switch develop',
|
|
242
|
+
]) {
|
|
243
|
+
const { run, reads } = harness();
|
|
244
|
+
assert.deepEqual(run(command), { block: true, reason: BRANCH_SWITCH_REFUSAL }, command);
|
|
245
|
+
assert.equal(reads.defaultBranch, 1, `${command} reads the default branch lazily, once`);
|
|
246
|
+
assert.equal(reads.currentBranch, 1, `${command} reads the current branch lazily, once`);
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
test('coming back to the branch it is on, and asking for nothing, pass', () => {
|
|
251
|
+
for (const command of ['git checkout main', 'git switch main']) {
|
|
252
|
+
const { run, reads } = harness();
|
|
253
|
+
assert.deepEqual(run(command), { block: false }, command);
|
|
254
|
+
assert.equal(reads.currentBranch, 1, `${command} still asks which branch that is`);
|
|
255
|
+
}
|
|
256
|
+
for (const command of ['git checkout', 'git switch']) {
|
|
257
|
+
const { run, reads } = harness();
|
|
258
|
+
assert.deepEqual(run(command), { block: false }, command);
|
|
259
|
+
assert.deepEqual(reads, { identity: 0, defaultBranch: 0, currentBranch: 0 }, `${command} judges no switch`);
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
test('a switch when the checkout is on any other branch passes', () => {
|
|
264
|
+
for (const command of [
|
|
265
|
+
'git checkout develop',
|
|
266
|
+
'git switch develop',
|
|
267
|
+
'git checkout -',
|
|
268
|
+
'git switch --detach',
|
|
269
|
+
'git checkout -b feat/y',
|
|
270
|
+
]) {
|
|
271
|
+
const { run, reads } = harness({ currentBranch: 'feat/x' });
|
|
272
|
+
assert.deepEqual(run(command), { block: false }, command);
|
|
273
|
+
assert.equal(reads.defaultBranch, 1, `${command} resolves the default branch`);
|
|
274
|
+
assert.equal(reads.currentBranch, 1, `${command} reads the current branch once`);
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
test('a detached HEAD, an unreadable branch and a missing seam block nothing', () => {
|
|
279
|
+
for (const currentBranch of [undefined, '', new Error('detached HEAD')]) {
|
|
280
|
+
const { run } = harness({ currentBranch });
|
|
281
|
+
assert.deepEqual(run('git switch develop'), { block: false }, String(currentBranch));
|
|
282
|
+
assert.deepEqual(run('git switch --detach'), { block: false }, String(currentBranch));
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
test('creating a branch and switching to it leaves the branch too', () => {
|
|
287
|
+
for (const command of [
|
|
288
|
+
'git checkout -b feat/x',
|
|
289
|
+
'git checkout -B feat/x',
|
|
290
|
+
'git checkout -bx',
|
|
291
|
+
'git checkout --orphan odd-a',
|
|
292
|
+
'git switch -c feat/x',
|
|
293
|
+
'git switch -C feat/x',
|
|
294
|
+
'git switch --create feat/x',
|
|
295
|
+
'git switch --force-create feat/x',
|
|
296
|
+
'git switch --create=feat/x',
|
|
297
|
+
'git switch --force-create=feat/x',
|
|
298
|
+
'git checkout --orphan=odd-a',
|
|
299
|
+
'git checkout -q -b feat/x',
|
|
300
|
+
'git status && git switch -c feat/x',
|
|
301
|
+
'git -C /repo switch -c feat/x',
|
|
302
|
+
]) {
|
|
303
|
+
const { run } = harness();
|
|
304
|
+
assert.deepEqual(run(command), { block: true, reason: BRANCH_SWITCH_REFUSAL }, command);
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
test('a switch that lands on a named branch is the branch the next segment leaves', () => {
|
|
309
|
+
// The command moves the checkout from `dev` to `main` and then leaves `main`.
|
|
310
|
+
const { run, reads } = harness({ currentBranch: 'dev' });
|
|
311
|
+
assert.deepEqual(run('git switch main && git switch develop'), { block: true, reason: BRANCH_SWITCH_REFUSAL });
|
|
312
|
+
assert.equal(reads.currentBranch, 1, 'the branch the command moved to is the one the next segment leaves');
|
|
313
|
+
assert.deepEqual(run('git switch main && git switch main'), { block: false });
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
test('the same command is allowed inside a linked worktree, reading no current branch', () => {
|
|
317
|
+
for (const command of ['git checkout develop', 'git switch develop', 'git checkout -', 'git checkout --detach main', 'git switch -c feat/x']) {
|
|
318
|
+
const { run, reads } = harness({ identity: LINKED });
|
|
319
|
+
assert.deepEqual(run(command, { cwd: LINKED.root }), { block: false }, command);
|
|
320
|
+
assert.equal(reads.currentBranch, 0, `${command} reads no principal's current branch`);
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
describe('S5: the default branch is the repository\'s own', () => {
|
|
326
|
+
test('a master repository allows master and refuses another branch', () => {
|
|
327
|
+
const { run } = harness({ defaultBranch: 'master', currentBranch: 'master' });
|
|
328
|
+
assert.deepEqual(run('git checkout master'), { block: false });
|
|
329
|
+
assert.deepEqual(run('git switch master'), { block: false });
|
|
330
|
+
assert.deepEqual(run('git checkout develop'), { block: true, reason: BRANCH_SWITCH_REFUSAL });
|
|
331
|
+
assert.deepEqual(run('git checkout main'), { block: true, reason: BRANCH_SWITCH_REFUSAL });
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
test('a trunk repository allows trunk and refuses main, so nothing is allowlisted', () => {
|
|
335
|
+
const { run } = harness({ defaultBranch: 'trunk', currentBranch: 'trunk' });
|
|
336
|
+
assert.deepEqual(run('git checkout trunk'), { block: false });
|
|
337
|
+
assert.deepEqual(run('git switch trunk'), { block: false });
|
|
338
|
+
assert.deepEqual(run('git checkout main'), { block: true, reason: BRANCH_SWITCH_REFUSAL });
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
test('with no resolvable default branch a switch is not refused and no branch is read', () => {
|
|
342
|
+
for (const defaultBranch of [undefined, '', new Error('git config failed')]) {
|
|
343
|
+
const { run, reads } = harness({ defaultBranch });
|
|
344
|
+
assert.deepEqual(run('git checkout develop'), { block: false }, String(defaultBranch));
|
|
345
|
+
assert.equal(reads.currentBranch, 0, `${String(defaultBranch)}: no default branch, nothing to leave`);
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
// ---- S11: the command's own `cd` decides which checkout is judged ----
|
|
351
|
+
|
|
352
|
+
describe('S11: a cd moves the checkout the switch is judged in', () => {
|
|
353
|
+
test('a cd into a linked worktree makes the switch nothing', () => {
|
|
354
|
+
for (const command of [
|
|
355
|
+
`cd ${LINKED.root} && git switch develop`,
|
|
356
|
+
`cd ${LINKED.root}; git checkout -`,
|
|
357
|
+
`cd ${LINKED.root} && git checkout -b feat/x`,
|
|
358
|
+
`cd ${PRINCIPAL.root} && cd ${LINKED.root} && git switch --detach`,
|
|
359
|
+
]) {
|
|
360
|
+
const { run } = harness({ identity: identityByCwd });
|
|
361
|
+
assert.deepEqual(run(command), { block: false }, command);
|
|
362
|
+
}
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
test('a cd into the principal blocks, whatever the tool call\'s own cwd is', () => {
|
|
366
|
+
for (const command of [
|
|
367
|
+
`cd ${PRINCIPAL.root} && git switch develop`,
|
|
368
|
+
`cd ${LINKED.root} && cd ${PRINCIPAL.root} && git switch develop`,
|
|
369
|
+
]) {
|
|
370
|
+
const { run } = harness({ identity: identityByCwd });
|
|
371
|
+
assert.deepEqual(run(command, { cwd: LINKED.root }), { block: true, reason: BRANCH_SWITCH_REFUSAL }, command);
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
test('a relative cd resolves against the cwd in force before it', () => {
|
|
376
|
+
const { run } = harness({ identity: identityByCwd });
|
|
377
|
+
assert.deepEqual(run('cd repo && git switch develop', { cwd: '/' }), { block: true, reason: BRANCH_SWITCH_REFUSAL });
|
|
378
|
+
assert.deepEqual(run('cd odd-a && git switch develop', { cwd: '/elsewhere/repo-worktrees' }), { block: false });
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
test('git -C names the checkout that segment is judged in', () => {
|
|
382
|
+
const { run } = harness({ identity: identityByCwd });
|
|
383
|
+
// Typed in the principal, run in a linked worktree: that checkout is judged, and it
|
|
384
|
+
// leaves nothing (S8 forbids the false block the principal's own branch would give).
|
|
385
|
+
assert.deepEqual(run(`git -C ${LINKED.root} switch develop`), { block: false });
|
|
386
|
+
assert.deepEqual(run(`git -C ${LINKED.root} checkout -b feat/x`), { block: false });
|
|
387
|
+
// Typed in a linked worktree, run in the principal: the principal is judged.
|
|
388
|
+
assert.deepEqual(run(`git -C ${PRINCIPAL.root} switch develop`, { cwd: LINKED.root }), { block: true, reason: BRANCH_SWITCH_REFUSAL });
|
|
389
|
+
// `-C` may repeat, and the last one wins, as with git.
|
|
390
|
+
assert.deepEqual(run(`git -C ${LINKED.root} -C ${PRINCIPAL.root} switch develop`, { cwd: LINKED.root }), { block: true, reason: BRANCH_SWITCH_REFUSAL });
|
|
391
|
+
assert.deepEqual(run(`git -C ${PRINCIPAL.root} -C ${LINKED.root} switch develop`), { block: false });
|
|
392
|
+
// A `-C` target is a literal path, resolved against the cwd in force for the segment.
|
|
393
|
+
assert.deepEqual(run('git -C repo switch develop', { cwd: '/' }), { block: true, reason: BRANCH_SWITCH_REFUSAL });
|
|
394
|
+
assert.deepEqual(run('git -C odd-a switch develop', { cwd: '/elsewhere/repo-worktrees' }), { block: false });
|
|
395
|
+
for (const command of ['git -C $WORKTREE switch develop', 'git -C ~ switch develop']) {
|
|
396
|
+
assert.deepEqual(run(command), { block: false }, command);
|
|
397
|
+
}
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
test('a cd inside a quoted argument is text, not a directory change', () => {
|
|
401
|
+
const { run } = harness({ identity: identityByCwd });
|
|
402
|
+
// This shell never runs a quoted argument, so its `cd` moves nothing (S11).
|
|
403
|
+
assert.deepEqual(run(`echo 'cd ${LINKED.root}'; git switch develop`), { block: true, reason: BRANCH_SWITCH_REFUSAL });
|
|
404
|
+
assert.deepEqual(run(`git commit -m "cd ${LINKED.root}"; git switch develop`), { block: true, reason: BRANCH_SWITCH_REFUSAL });
|
|
405
|
+
assert.deepEqual(run(`echo "cd ${PRINCIPAL.root}"; git switch develop`, { cwd: LINKED.root }), { block: false });
|
|
406
|
+
// A shell script argument, a substitution and a backtick span ARE run, so they move it.
|
|
407
|
+
for (const command of [
|
|
408
|
+
`echo $(cd ${LINKED.root}) && git switch develop`,
|
|
409
|
+
`echo \`cd ${LINKED.root}\` && git switch develop`,
|
|
410
|
+
`bash -c 'cd ${LINKED.root}'; git switch develop`,
|
|
411
|
+
]) {
|
|
412
|
+
assert.deepEqual(run(command), { block: false }, command);
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
test('a cd to a checkout that is not this repository blocks nothing', () => {
|
|
417
|
+
const { run } = harness({ identity: identityByCwd });
|
|
418
|
+
assert.deepEqual(run('cd /tmp && git switch develop'), { block: false });
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
test('an unknown cd target blocks nothing of S4 (S8)', () => {
|
|
422
|
+
for (const command of [
|
|
423
|
+
'cd $HOME && git switch develop',
|
|
424
|
+
'cd $WORKTREE && git checkout -b feat/x',
|
|
425
|
+
'cd ~ && git switch develop',
|
|
426
|
+
'cd - && git switch develop',
|
|
427
|
+
'cd && git switch develop',
|
|
428
|
+
'cd ./* && git switch develop',
|
|
429
|
+
'cd `pwd` && git switch develop',
|
|
430
|
+
]) {
|
|
431
|
+
const { run } = harness({ identity: identityByCwd });
|
|
432
|
+
assert.deepEqual(run(command), { block: false }, command);
|
|
433
|
+
}
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
test('S1 and S9 stay any-cwd: a cd never shelters a worktree command', () => {
|
|
437
|
+
const { run } = harness({ identity: identityByCwd });
|
|
438
|
+
assert.deepEqual(run(`cd ${LINKED.root} && git worktree add ../odd-b`), { block: true, reason: WORKTREE_ADD_REFUSAL });
|
|
439
|
+
assert.deepEqual(run('cd $HOME && git worktree list'), { block: true, reason: WORKTREE_LIST_REFUSAL });
|
|
440
|
+
});
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
// ---- S6: everything else passes untouched ----
|
|
444
|
+
|
|
445
|
+
describe('S4: a slash-named ref is a branch, not a path', () => {
|
|
446
|
+
test('checkout and switch refuse a slash-named or remote ref that is not a file', () => {
|
|
447
|
+
for (const command of [
|
|
448
|
+
'git checkout feat/x',
|
|
449
|
+
'git switch feat/x',
|
|
450
|
+
'git checkout origin/main',
|
|
451
|
+
'git switch origin/main',
|
|
452
|
+
'git checkout release/1.2',
|
|
453
|
+
'git switch release/1.2',
|
|
454
|
+
'git switch README.md',
|
|
455
|
+
]) {
|
|
456
|
+
const { run } = harness();
|
|
457
|
+
assert.deepEqual(run(command), { block: true, reason: BRANCH_SWITCH_REFUSAL }, command);
|
|
458
|
+
}
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
test('a checkout of an existing file is a restore, and the default branch stays allowed', () => {
|
|
462
|
+
for (const command of ['git checkout README.md', 'git checkout src/app.ts', 'git checkout -- feat/x']) {
|
|
463
|
+
const { run } = harness();
|
|
464
|
+
assert.deepEqual(run(command), { block: false }, command);
|
|
465
|
+
}
|
|
466
|
+
const { run } = harness({ defaultBranch: 'trunk' });
|
|
467
|
+
assert.deepEqual(run('git checkout trunk'), { block: false });
|
|
468
|
+
assert.deepEqual(run('git switch trunk'), { block: false });
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
test('without the path seam every argument is a ref', () => {
|
|
472
|
+
const { run } = harness({ paths: [] });
|
|
473
|
+
assert.deepEqual(run('git checkout src/app.ts'), { block: true, reason: BRANCH_SWITCH_REFUSAL });
|
|
474
|
+
});
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
describe('S1 and S9: the sequence is found wherever the text puts it', () => {
|
|
478
|
+
test('a subshell, a quoted shell command and a line continuation still refuse', () => {
|
|
479
|
+
for (const verb of ['add ../odd-probe', 'list']) {
|
|
480
|
+
const reason = verb.startsWith('add') ? WORKTREE_ADD_REFUSAL : WORKTREE_LIST_REFUSAL;
|
|
481
|
+
for (const command of [
|
|
482
|
+
`(git worktree ${verb})`,
|
|
483
|
+
`bash -c 'git worktree ${verb}'`,
|
|
484
|
+
`sh -c "git worktree ${verb}"`,
|
|
485
|
+
`git \\\n worktree ${verb}`,
|
|
486
|
+
`/usr/bin/git worktree ${verb}`,
|
|
487
|
+
`env git worktree ${verb}`,
|
|
488
|
+
`echo x | xargs git worktree ${verb}`,
|
|
489
|
+
]) {
|
|
490
|
+
const { run } = harness();
|
|
491
|
+
assert.deepEqual(run(command, { cwd: '/anywhere' }), { block: true, reason }, command);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
});
|
|
495
|
+
});
|
|
496
|
+
|
|
497
|
+
describe('S3 and S4: attached flags, compounds and restores from a ref', () => {
|
|
498
|
+
test('an attached branch name and a bare detach are what they are', () => {
|
|
499
|
+
for (const [command, reason] of [
|
|
500
|
+
['git checkout -bx', BRANCH_SWITCH_REFUSAL],
|
|
501
|
+
['git checkout -Bx', BRANCH_SWITCH_REFUSAL],
|
|
502
|
+
['git switch -cx', BRANCH_SWITCH_REFUSAL],
|
|
503
|
+
['git switch --create x', BRANCH_SWITCH_REFUSAL],
|
|
504
|
+
['git checkout --detach', BRANCH_SWITCH_REFUSAL],
|
|
505
|
+
]) {
|
|
506
|
+
const { run } = harness();
|
|
507
|
+
assert.deepEqual(run(command), { block: true, reason }, command);
|
|
508
|
+
}
|
|
509
|
+
for (const command of ['git branch -f x', 'git branch --track x origin/x', 'git branch -D x']) {
|
|
510
|
+
const { run } = harness();
|
|
511
|
+
assert.deepEqual(run(command), { block: false }, command);
|
|
512
|
+
}
|
|
513
|
+
});
|
|
514
|
+
|
|
515
|
+
test('an allowed switch never hides a later forbidden one', () => {
|
|
516
|
+
for (const command of [
|
|
517
|
+
'git checkout main && git checkout develop',
|
|
518
|
+
'git switch main; git switch feat/x',
|
|
519
|
+
'git checkout main\ngit checkout develop',
|
|
520
|
+
]) {
|
|
521
|
+
const { run } = harness();
|
|
522
|
+
assert.deepEqual(run(command), { block: true, reason: BRANCH_SWITCH_REFUSAL }, command);
|
|
523
|
+
}
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
test('a restore from a ref, or of an extensionless file, switches nothing', () => {
|
|
527
|
+
for (const command of [
|
|
528
|
+
'git checkout HEAD -- .',
|
|
529
|
+
'git checkout develop -- src/app.ts',
|
|
530
|
+
'git checkout main -- README.md',
|
|
531
|
+
'git checkout LICENSE',
|
|
532
|
+
'git checkout .',
|
|
533
|
+
'git branch -c a b',
|
|
534
|
+
'git branch -m a b',
|
|
535
|
+
'git branch --set-upstream-to=origin/main',
|
|
536
|
+
]) {
|
|
537
|
+
const { run } = harness();
|
|
538
|
+
assert.deepEqual(run(command), { block: false }, command);
|
|
539
|
+
}
|
|
540
|
+
});
|
|
541
|
+
});
|
|
542
|
+
|
|
543
|
+
describe('S6: everything else passes', () => {
|
|
544
|
+
test('inspection, restores and the other worktree verbs', () => {
|
|
545
|
+
for (const command of [
|
|
546
|
+
'git status',
|
|
547
|
+
'git log --oneline -5',
|
|
548
|
+
'git diff',
|
|
549
|
+
'git add .',
|
|
550
|
+
'git commit -m "x"',
|
|
551
|
+
'git worktree remove ../odd-a',
|
|
552
|
+
'git worktree prune',
|
|
553
|
+
'git worktree move ../odd-a ../odd-b',
|
|
554
|
+
'git restore .',
|
|
555
|
+
'git restore --staged README.md',
|
|
556
|
+
'git checkout -- README.md',
|
|
557
|
+
'git checkout README.md',
|
|
558
|
+
'git checkout src/app.ts',
|
|
559
|
+
'git checkout ./src',
|
|
560
|
+
]) {
|
|
561
|
+
const { run } = harness();
|
|
562
|
+
assert.deepEqual(run(command), { block: false }, command);
|
|
563
|
+
}
|
|
564
|
+
});
|
|
565
|
+
|
|
566
|
+
test('every non-bash tool call passes, whatever its input', () => {
|
|
567
|
+
for (const toolName of ['read', 'write', 'edit', 'grep', 'find', 'ls', 'worktree_create', 'worktree_list']) {
|
|
568
|
+
const { run } = harness();
|
|
569
|
+
assert.deepEqual(run('git worktree add ../odd-a', { toolName }), { block: false }, toolName);
|
|
570
|
+
assert.deepEqual(run('git checkout -b feat/odd-a', { toolName }), { block: false }, toolName);
|
|
571
|
+
}
|
|
572
|
+
});
|
|
573
|
+
|
|
574
|
+
test('a command that is not text decides nothing', () => {
|
|
575
|
+
const { run } = harness();
|
|
576
|
+
for (const command of [undefined, null, '', 42, {}]) {
|
|
577
|
+
assert.deepEqual(run(command), { block: false }, String(command));
|
|
578
|
+
}
|
|
579
|
+
});
|
|
580
|
+
});
|
|
581
|
+
|
|
582
|
+
// ---- S8: honest failure, never a false block ----
|
|
583
|
+
|
|
584
|
+
describe('S8: an unreadable repository fails open, and nothing throws', () => {
|
|
585
|
+
test('without an identity only S1 and S9 apply', () => {
|
|
586
|
+
for (const identity of [new Error('not a git repository'), undefined, null, {}, { root: '/repo' }]) {
|
|
587
|
+
const { run } = harness({ identity });
|
|
588
|
+
assert.deepEqual(run('git checkout -b feat/odd-a'), { block: false }, String(identity));
|
|
589
|
+
assert.deepEqual(run('git checkout develop'), { block: false }, String(identity));
|
|
590
|
+
assert.deepEqual(run('git worktree add ../odd-a'), { block: true, reason: WORKTREE_ADD_REFUSAL }, String(identity));
|
|
591
|
+
assert.deepEqual(run('git worktree list'), { block: true, reason: WORKTREE_LIST_REFUSAL }, String(identity));
|
|
592
|
+
}
|
|
593
|
+
});
|
|
594
|
+
|
|
595
|
+
test('a cwd that is not a checkout decides nothing about branches', () => {
|
|
596
|
+
const { run } = harness();
|
|
597
|
+
for (const cwd of [undefined, null, '', 0]) {
|
|
598
|
+
assert.deepEqual(run('git checkout develop', { cwd }), { block: false }, String(cwd));
|
|
599
|
+
}
|
|
600
|
+
});
|
|
601
|
+
|
|
602
|
+
test('a seam that throws never escapes', () => {
|
|
603
|
+
const { run } = harness({ identity: new Error('boom'), defaultBranch: new Error('boom') });
|
|
604
|
+
assert.deepEqual(run('git checkout -b feat/odd-a'), { block: false });
|
|
605
|
+
assert.deepEqual(run('git status'), { block: false });
|
|
606
|
+
});
|
|
607
|
+
|
|
608
|
+
test('an allowed call reads no more than it needs', () => {
|
|
609
|
+
for (const command of ['git status', 'git worktree remove ../odd-a', 'git branch feat/x']) {
|
|
610
|
+
const { run, reads } = harness();
|
|
611
|
+
assert.deepEqual(run(command), { block: false }, command);
|
|
612
|
+
assert.deepEqual(reads, { identity: 0, defaultBranch: 0, currentBranch: 0 }, `${command} reads nothing`);
|
|
613
|
+
}
|
|
614
|
+
});
|
|
615
|
+
});
|
|
616
|
+
|
|
617
|
+
// ---- T2: the handler the host actually receives ----
|
|
618
|
+
|
|
619
|
+
const roots = [];
|
|
620
|
+
|
|
621
|
+
function track(dir) {
|
|
622
|
+
roots.push(dir);
|
|
623
|
+
return dir;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
function git(cwd, args) {
|
|
627
|
+
const out = spawnSync('git', args, { cwd, encoding: 'utf8' });
|
|
628
|
+
if (out.status !== 0) throw new Error(`git ${args.join(' ')} failed: ${out.stderr}`);
|
|
629
|
+
return out.stdout.trim();
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
function scratch(tag) {
|
|
633
|
+
return fs.realpathSync(track(fs.mkdtempSync(path.join(os.tmpdir(), `odd-wt-guard-${tag}-`))));
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
function makeRepo() {
|
|
637
|
+
const dir = scratch('repo');
|
|
638
|
+
git(dir, ['init', '-b', 'main']);
|
|
639
|
+
git(dir, ['config', 'user.email', 'test@example.test']);
|
|
640
|
+
git(dir, ['config', 'user.name', 'Test']);
|
|
641
|
+
fs.writeFileSync(path.join(dir, 'README.md'), '# fixture\n');
|
|
642
|
+
git(dir, ['add', 'README.md']);
|
|
643
|
+
git(dir, ['commit', '-m', 'init']);
|
|
644
|
+
return dir;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
function makeLinkedWorktree(repo) {
|
|
648
|
+
const target = path.join(scratch('linked'), 'odd-a');
|
|
649
|
+
git(repo, ['worktree', 'add', '-b', 'feat/odd-a', target]);
|
|
650
|
+
return fs.realpathSync(target);
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
// The host's `tool_call` round trip: every registered handler in turn, and the first
|
|
654
|
+
// block answer is the block the host sees.
|
|
655
|
+
async function callTool(hooks, event, cwd) {
|
|
656
|
+
const handlers = hooks.filter(([name]) => name === 'tool_call').map(([, handler]) => handler);
|
|
657
|
+
for (const handler of handlers) {
|
|
658
|
+
const answer = await handler(event, { cwd });
|
|
659
|
+
if (answer !== undefined) return answer;
|
|
660
|
+
}
|
|
661
|
+
return undefined;
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
const bash = (command) => ({ toolName: 'bash', input: { command } });
|
|
665
|
+
|
|
666
|
+
describe('the tool_call handler the extension registers', () => {
|
|
667
|
+
let loaded;
|
|
668
|
+
let principal;
|
|
669
|
+
let linked;
|
|
670
|
+
let outside;
|
|
671
|
+
|
|
672
|
+
before(async () => {
|
|
673
|
+
loaded = await loadExtension({});
|
|
674
|
+
principal = makeRepo();
|
|
675
|
+
linked = makeLinkedWorktree(principal);
|
|
676
|
+
outside = scratch('loose');
|
|
677
|
+
});
|
|
678
|
+
|
|
679
|
+
after(() => {
|
|
680
|
+
for (const dir of roots) {
|
|
681
|
+
try {
|
|
682
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
683
|
+
} catch {
|
|
684
|
+
// Best effort: a temp directory that is already gone is fine.
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
});
|
|
688
|
+
|
|
689
|
+
test('refuses worktree creation and listing in any cwd', async () => {
|
|
690
|
+
for (const cwd of [principal, linked, outside]) {
|
|
691
|
+
assert.deepEqual(
|
|
692
|
+
await callTool(loaded.hooks, bash('git worktree add ../odd-b'), cwd),
|
|
693
|
+
{ block: true, reason: WORKTREE_ADD_REFUSAL },
|
|
694
|
+
`add in ${cwd}`,
|
|
695
|
+
);
|
|
696
|
+
assert.deepEqual(
|
|
697
|
+
await callTool(loaded.hooks, bash('git worktree list --porcelain'), cwd),
|
|
698
|
+
{ block: true, reason: WORKTREE_LIST_REFUSAL },
|
|
699
|
+
`list in ${cwd}`,
|
|
700
|
+
);
|
|
701
|
+
assert.deepEqual(
|
|
702
|
+
await callTool(loaded.hooks, bash(`git -C ${principal} worktree add ../odd-b`), cwd),
|
|
703
|
+
{ block: true, reason: WORKTREE_ADD_REFUSAL },
|
|
704
|
+
`-C add in ${cwd}`,
|
|
705
|
+
);
|
|
706
|
+
}
|
|
707
|
+
});
|
|
708
|
+
|
|
709
|
+
test('refuses the switches that leave the principal checkout\'s default branch', async () => {
|
|
710
|
+
const expected = [
|
|
711
|
+
['git checkout -b feat/odd-b', BRANCH_SWITCH_REFUSAL],
|
|
712
|
+
['git switch -c feat/odd-b', BRANCH_SWITCH_REFUSAL],
|
|
713
|
+
['git checkout develop', BRANCH_SWITCH_REFUSAL],
|
|
714
|
+
['git switch develop', BRANCH_SWITCH_REFUSAL],
|
|
715
|
+
['git checkout feat/odd-b', BRANCH_SWITCH_REFUSAL],
|
|
716
|
+
['git switch feat/odd-b', BRANCH_SWITCH_REFUSAL],
|
|
717
|
+
['git checkout origin/main', BRANCH_SWITCH_REFUSAL],
|
|
718
|
+
['git checkout -', BRANCH_SWITCH_REFUSAL],
|
|
719
|
+
['git switch --detach', BRANCH_SWITCH_REFUSAL],
|
|
720
|
+
[`git checkout ${SHA_REF.slice(0, 7)}`, BRANCH_SWITCH_REFUSAL],
|
|
721
|
+
['git status && git checkout develop', BRANCH_SWITCH_REFUSAL],
|
|
722
|
+
['echo $(git worktree add ../odd-b)', WORKTREE_ADD_REFUSAL],
|
|
723
|
+
];
|
|
724
|
+
for (const [command, reason] of expected) {
|
|
725
|
+
assert.deepEqual(await callTool(loaded.hooks, bash(command), principal), { block: true, reason }, command);
|
|
726
|
+
}
|
|
727
|
+
});
|
|
728
|
+
|
|
729
|
+
test('creating a branch in the principal checkout passes (S3, L10)', async () => {
|
|
730
|
+
for (const command of ['git branch odd-b', 'git branch -f odd-b main', 'git branch -m odd-b odd-c', 'git branch -D odd-c', 'git branch --list']) {
|
|
731
|
+
assert.equal(await callTool(loaded.hooks, bash(command), principal), undefined, command);
|
|
732
|
+
}
|
|
733
|
+
});
|
|
734
|
+
|
|
735
|
+
test('a principal checkout on another branch is not blocked', async () => {
|
|
736
|
+
const other = makeRepo();
|
|
737
|
+
git(other, ['checkout', '-b', 'feat/elsewhere']);
|
|
738
|
+
for (const command of ['git checkout develop', 'git switch develop', 'git checkout -', 'git switch -c feat/another', 'git checkout main']) {
|
|
739
|
+
assert.equal(await callTool(loaded.hooks, bash(command), other), undefined, command);
|
|
740
|
+
}
|
|
741
|
+
});
|
|
742
|
+
|
|
743
|
+
test('the command\'s own cd decides which checkout is judged (S11)', async () => {
|
|
744
|
+
assert.equal(await callTool(loaded.hooks, bash(`cd ${linked} && git switch develop`), principal), undefined);
|
|
745
|
+
assert.deepEqual(
|
|
746
|
+
await callTool(loaded.hooks, bash(`cd ${linked} && cd ${principal} && git switch develop`), linked),
|
|
747
|
+
{ block: true, reason: BRANCH_SWITCH_REFUSAL },
|
|
748
|
+
);
|
|
749
|
+
assert.equal(await callTool(loaded.hooks, bash('cd $HOME && git switch develop'), principal), undefined);
|
|
750
|
+
});
|
|
751
|
+
|
|
752
|
+
test('allows the same branch work inside a linked worktree', async () => {
|
|
753
|
+
for (const command of [
|
|
754
|
+
'git checkout -b feat/odd-b',
|
|
755
|
+
'git switch -c feat/odd-b',
|
|
756
|
+
'git branch odd-b',
|
|
757
|
+
'git checkout develop',
|
|
758
|
+
'git switch develop',
|
|
759
|
+
'git checkout -',
|
|
760
|
+
]) {
|
|
761
|
+
assert.equal(await callTool(loaded.hooks, bash(command), linked), undefined, command);
|
|
762
|
+
}
|
|
763
|
+
});
|
|
764
|
+
|
|
765
|
+
test('a checkout of a file that exists is a restore, one that does not is a branch', async () => {
|
|
766
|
+
fs.mkdirSync(path.join(principal, 'src'), { recursive: true });
|
|
767
|
+
fs.writeFileSync(path.join(principal, 'src', 'real.txt'), 'x');
|
|
768
|
+
assert.equal(await callTool(loaded.hooks, bash('git checkout src/real.txt'), principal), undefined);
|
|
769
|
+
assert.deepEqual(
|
|
770
|
+
await callTool(loaded.hooks, bash('git checkout src/missing.txt'), principal),
|
|
771
|
+
{ block: true, reason: BRANCH_SWITCH_REFUSAL },
|
|
772
|
+
);
|
|
773
|
+
});
|
|
774
|
+
|
|
775
|
+
test('ODD_WORKTREE_GUARD=0 turns the guards off, and only that value', async () => {
|
|
776
|
+
const previous = process.env.ODD_WORKTREE_GUARD;
|
|
777
|
+
try {
|
|
778
|
+
for (const value of ['0', 'false', '', '1']) {
|
|
779
|
+
process.env.ODD_WORKTREE_GUARD = value;
|
|
780
|
+
const answer = await callTool(loaded.hooks, bash('git worktree add ../odd-b'), principal);
|
|
781
|
+
if (value === '0') assert.equal(answer, undefined, 'value "0" turns them off');
|
|
782
|
+
else assert.deepEqual(answer, { block: true, reason: WORKTREE_ADD_REFUSAL }, `value ${JSON.stringify(value)}`);
|
|
783
|
+
}
|
|
784
|
+
delete process.env.ODD_WORKTREE_GUARD;
|
|
785
|
+
assert.deepEqual(
|
|
786
|
+
await callTool(loaded.hooks, bash('git worktree add ../odd-b'), principal),
|
|
787
|
+
{ block: true, reason: WORKTREE_ADD_REFUSAL },
|
|
788
|
+
'unset keeps them on',
|
|
789
|
+
);
|
|
790
|
+
} finally {
|
|
791
|
+
if (previous === undefined) delete process.env.ODD_WORKTREE_GUARD;
|
|
792
|
+
else process.env.ODD_WORKTREE_GUARD = previous;
|
|
793
|
+
}
|
|
794
|
+
});
|
|
795
|
+
|
|
796
|
+
test('lets every other call through', async () => {
|
|
797
|
+
const calls = [
|
|
798
|
+
bash('git status'),
|
|
799
|
+
bash('git checkout main'),
|
|
800
|
+
bash('git branch feat/odd-b'),
|
|
801
|
+
bash('git worktree remove ../odd-a'),
|
|
802
|
+
bash('git worktree prune'),
|
|
803
|
+
{ toolName: 'read', input: { path: path.join(principal, 'README.md') } },
|
|
804
|
+
{ toolName: 'write', input: { path: path.join(outside, 'a.txt'), content: 'x' } },
|
|
805
|
+
{ toolName: 'grep', input: { pattern: 'main', path: principal } },
|
|
806
|
+
{ toolName: 'worktree_create', input: { odd: 'odd-b', branch: 'feat/odd-b' } },
|
|
807
|
+
{ toolName: 'worktree_list', input: {} },
|
|
808
|
+
];
|
|
809
|
+
for (const event of calls) {
|
|
810
|
+
assert.equal(await callTool(loaded.hooks, event, principal), undefined, JSON.stringify(event));
|
|
811
|
+
}
|
|
812
|
+
});
|
|
813
|
+
|
|
814
|
+
test('outside a repository, and on a cwd that does not exist, it refuses nothing about branches', async () => {
|
|
815
|
+
const missing = path.join(os.tmpdir(), `odd-wt-guard-missing-${process.pid}-${Date.now()}`);
|
|
816
|
+
for (const cwd of [outside, missing]) {
|
|
817
|
+
assert.equal(await callTool(loaded.hooks, bash('git checkout develop'), cwd), undefined, cwd);
|
|
818
|
+
assert.equal(await callTool(loaded.hooks, bash('git checkout -b feat/odd-b'), cwd), undefined, cwd);
|
|
819
|
+
// S1 stays in force: creating a worktree is refused wherever the session stands.
|
|
820
|
+
assert.deepEqual(await callTool(loaded.hooks, bash('git worktree add ../odd-b'), cwd), { block: true, reason: WORKTREE_ADD_REFUSAL }, cwd);
|
|
821
|
+
}
|
|
822
|
+
});
|
|
823
|
+
});
|