@laoyuehanni/dsh-git-worktree 0.4.14 → 0.4.16
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 +9 -3
- package/README.zh.md +9 -3
- package/docs/DETAIL.md +5 -2
- package/docs/DETAIL.zh.md +5 -2
- package/lib/client.js +4253 -620
- package/lib/deferred-worktree.d.ts +105 -0
- package/lib/deferred-worktree.js +293 -0
- package/lib/git.d.ts +95 -0
- package/lib/git.js +384 -4
- package/lib/index.d.ts +13 -0
- package/lib/index.js +204 -4
- package/lib/normalize.d.ts +13 -0
- package/lib/normalize.js +51 -0
- package/lib/pr.d.ts +177 -0
- package/lib/pr.js +672 -0
- package/lib/routes.d.ts +61 -3
- package/lib/routes.js +639 -80
- package/lib/wire.d.ts +235 -3
- package/lib/wire.js +17 -0
- package/package.json +1 -1
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deferred worktree creation coordinator for shared-workspace mode
|
|
3
|
+
* (`switchSessionDirectory`).
|
|
4
|
+
*
|
|
5
|
+
* When a user chooses to create a new worktree in shared-workspace mode, no
|
|
6
|
+
* Git branch or checkout directory is written until the Session's first user
|
|
7
|
+
* message actually arrives. On `agent/inbox/inserted`, the coordinator holds
|
|
8
|
+
* the Agent via `agent.runMaintenance`, runs the plugin's full worktree
|
|
9
|
+
* creation pipeline (`.git/info/exclude`, optional fetch, `git worktree add`,
|
|
10
|
+
* `.worktreeinclude` copy, and `workingDirectory.set`), streams phase updates
|
|
11
|
+
* to the browser dock, and lets the user retry or withdraw on failure without
|
|
12
|
+
* losing the waiting message.
|
|
13
|
+
*/
|
|
14
|
+
import type { DeferredWorktreePhase, DeferredWorktreeSnapshot, DeferredWorktreeSpec, DeferredWorktreeState } from './wire.ts';
|
|
15
|
+
/** Minimal Host Agent slice needed to hold a turn and switch directory. */
|
|
16
|
+
export interface DeferredAgentLike {
|
|
17
|
+
readonly session: {
|
|
18
|
+
readonly id: string;
|
|
19
|
+
};
|
|
20
|
+
readonly inbox?: {
|
|
21
|
+
readonly nextTurn?: readonly {
|
|
22
|
+
readonly id: string;
|
|
23
|
+
}[];
|
|
24
|
+
readonly nextStep?: readonly {
|
|
25
|
+
readonly id: string;
|
|
26
|
+
readonly source?: {
|
|
27
|
+
readonly kind?: string;
|
|
28
|
+
};
|
|
29
|
+
}[];
|
|
30
|
+
readonly remove?: (id: string) => void;
|
|
31
|
+
};
|
|
32
|
+
readonly runMaintenance?: <T>(task: (signal: AbortSignal) => Promise<T>) => Promise<T>;
|
|
33
|
+
}
|
|
34
|
+
/** Result of creating the worktree before switching the session directory. */
|
|
35
|
+
export interface DeferredCreateOutcome {
|
|
36
|
+
readonly path: string;
|
|
37
|
+
readonly branch: string;
|
|
38
|
+
readonly created: boolean;
|
|
39
|
+
readonly fetchWarning?: string;
|
|
40
|
+
readonly excludeWarning?: string;
|
|
41
|
+
readonly copyWarning?: string;
|
|
42
|
+
readonly trackPrUrl?: string;
|
|
43
|
+
}
|
|
44
|
+
/** Host seams injected into {@link DeferredWorktreeCoordinator}. */
|
|
45
|
+
export interface DeferredCoordinatorDeps {
|
|
46
|
+
/** Execute the plugin's worktree creation pipeline and report phase changes. */
|
|
47
|
+
readonly createWorktree: (spec: DeferredWorktreeSpec, onPhase: (phase: Exclude<DeferredWorktreePhase, 'switch'>, resolvedBranch?: string) => void, signal?: AbortSignal) => Promise<DeferredCreateOutcome>;
|
|
48
|
+
/** Move the Agent's working directory via DSH `ctx.workingDirectory.set`. */
|
|
49
|
+
readonly setWorkingDirectory: (agent: DeferredAgentLike, path: string, signal?: AbortSignal) => Promise<string>;
|
|
50
|
+
}
|
|
51
|
+
type SnapshotListener = (snapshot: DeferredWorktreeSnapshot) => void;
|
|
52
|
+
/**
|
|
53
|
+
* Coordinates armed, running, and failed deferred worktree creations keyed by
|
|
54
|
+
* `sessionId`. Pure in-process state machine with injectable seams.
|
|
55
|
+
*/
|
|
56
|
+
export declare class DeferredWorktreeCoordinator {
|
|
57
|
+
private readonly deps;
|
|
58
|
+
private readonly slots;
|
|
59
|
+
private readonly listeners;
|
|
60
|
+
private readonly lifetime;
|
|
61
|
+
private readonly pending;
|
|
62
|
+
constructor(deps: DeferredCoordinatorDeps);
|
|
63
|
+
/** Read the current deferred state of `sessionId`, or null when none is active. */
|
|
64
|
+
get(sessionId: string): DeferredWorktreeState | null;
|
|
65
|
+
/** Subscribe to live state/completion updates for `sessionId`. */
|
|
66
|
+
subscribe(sessionId: string, listener: SnapshotListener): () => void;
|
|
67
|
+
/**
|
|
68
|
+
* Arm (or replace) a deferred worktree creation on a blank session.
|
|
69
|
+
* Rejects while a creation is already running or holding a waiting first turn.
|
|
70
|
+
*/
|
|
71
|
+
arm(sessionId: string, spec: DeferredWorktreeSpec): DeferredWorktreeState;
|
|
72
|
+
/**
|
|
73
|
+
* Cancel an armed request before any message arrives, or withdraw a failed
|
|
74
|
+
* attempt so the waiting message continues in the current directory.
|
|
75
|
+
*/
|
|
76
|
+
cancel(sessionId: string): boolean;
|
|
77
|
+
/** Retry a failed creation while the first message is waiting in maintenance. */
|
|
78
|
+
retry(sessionId: string): boolean;
|
|
79
|
+
/** Withdraw a failed creation so the waiting message proceeds without a worktree. */
|
|
80
|
+
withdraw(sessionId: string): boolean;
|
|
81
|
+
/**
|
|
82
|
+
* Called from `agent/inbox/inserted`: if the session has an armed deferred
|
|
83
|
+
* request, hold the Agent via `runMaintenance` so the worktree is created and
|
|
84
|
+
* entered before the first turn starts.
|
|
85
|
+
*/
|
|
86
|
+
onInboxInserted(agent: DeferredAgentLike, message?: {
|
|
87
|
+
readonly id?: string;
|
|
88
|
+
}): void;
|
|
89
|
+
/**
|
|
90
|
+
* Fallback for `agent/pre-step` (step === 1): if a message somehow bypassed
|
|
91
|
+
* `agent/inbox/inserted` while an armed request is still pending, create and
|
|
92
|
+
* enter the worktree right before the first model call.
|
|
93
|
+
* @returns true when this call actually created and switched the worktree.
|
|
94
|
+
*/
|
|
95
|
+
onPreStep(agent: DeferredAgentLike, step: number, signal?: AbortSignal): Promise<boolean>;
|
|
96
|
+
/** Release any waiting maintenance holds when the plugin unloads. */
|
|
97
|
+
dispose(): Promise<void>;
|
|
98
|
+
private runLoop;
|
|
99
|
+
private executeOnce;
|
|
100
|
+
private waitForDecision;
|
|
101
|
+
private clearSlot;
|
|
102
|
+
private emit;
|
|
103
|
+
private track;
|
|
104
|
+
}
|
|
105
|
+
export {};
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deferred worktree creation coordinator for shared-workspace mode
|
|
3
|
+
* (`switchSessionDirectory`).
|
|
4
|
+
*
|
|
5
|
+
* When a user chooses to create a new worktree in shared-workspace mode, no
|
|
6
|
+
* Git branch or checkout directory is written until the Session's first user
|
|
7
|
+
* message actually arrives. On `agent/inbox/inserted`, the coordinator holds
|
|
8
|
+
* the Agent via `agent.runMaintenance`, runs the plugin's full worktree
|
|
9
|
+
* creation pipeline (`.git/info/exclude`, optional fetch, `git worktree add`,
|
|
10
|
+
* `.worktreeinclude` copy, and `workingDirectory.set`), streams phase updates
|
|
11
|
+
* to the browser dock, and lets the user retry or withdraw on failure without
|
|
12
|
+
* losing the waiting message.
|
|
13
|
+
*/
|
|
14
|
+
function createDeferred() {
|
|
15
|
+
let resolve;
|
|
16
|
+
let reject;
|
|
17
|
+
const promise = new Promise((res, rej) => {
|
|
18
|
+
resolve = res;
|
|
19
|
+
reject = rej;
|
|
20
|
+
});
|
|
21
|
+
return { promise, resolve, reject };
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Coordinates armed, running, and failed deferred worktree creations keyed by
|
|
25
|
+
* `sessionId`. Pure in-process state machine with injectable seams.
|
|
26
|
+
*/
|
|
27
|
+
export class DeferredWorktreeCoordinator {
|
|
28
|
+
deps;
|
|
29
|
+
slots = new Map();
|
|
30
|
+
listeners = new Map();
|
|
31
|
+
lifetime = new AbortController();
|
|
32
|
+
pending = new Set();
|
|
33
|
+
constructor(deps) {
|
|
34
|
+
this.deps = deps;
|
|
35
|
+
}
|
|
36
|
+
/** Read the current deferred state of `sessionId`, or null when none is active. */
|
|
37
|
+
get(sessionId) {
|
|
38
|
+
return this.slots.get(sessionId)?.state ?? null;
|
|
39
|
+
}
|
|
40
|
+
/** Subscribe to live state/completion updates for `sessionId`. */
|
|
41
|
+
subscribe(sessionId, listener) {
|
|
42
|
+
let set = this.listeners.get(sessionId);
|
|
43
|
+
if (set === undefined) {
|
|
44
|
+
set = new Set();
|
|
45
|
+
this.listeners.set(sessionId, set);
|
|
46
|
+
}
|
|
47
|
+
set.add(listener);
|
|
48
|
+
return () => {
|
|
49
|
+
set.delete(listener);
|
|
50
|
+
if (set.size === 0)
|
|
51
|
+
this.listeners.delete(sessionId);
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Arm (or replace) a deferred worktree creation on a blank session.
|
|
56
|
+
* Rejects while a creation is already running or holding a waiting first turn.
|
|
57
|
+
*/
|
|
58
|
+
arm(sessionId, spec) {
|
|
59
|
+
const existing = this.slots.get(sessionId);
|
|
60
|
+
if (existing !== undefined && (existing.state.kind === 'running' || existing.holding)) {
|
|
61
|
+
throw Object.assign(new Error('Worktree creation is already in progress for this session'), { httpStatus: 409 });
|
|
62
|
+
}
|
|
63
|
+
const state = { kind: 'armed', spec };
|
|
64
|
+
this.slots.set(sessionId, { state, holding: false, decision: undefined, created: undefined });
|
|
65
|
+
this.emit(sessionId, { state });
|
|
66
|
+
return state;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Cancel an armed request before any message arrives, or withdraw a failed
|
|
70
|
+
* attempt so the waiting message continues in the current directory.
|
|
71
|
+
*/
|
|
72
|
+
cancel(sessionId) {
|
|
73
|
+
const slot = this.slots.get(sessionId);
|
|
74
|
+
if (slot === undefined)
|
|
75
|
+
return false;
|
|
76
|
+
if (slot.state.kind === 'running')
|
|
77
|
+
return false;
|
|
78
|
+
if (slot.decision !== undefined) {
|
|
79
|
+
slot.decision.resolve('withdraw');
|
|
80
|
+
slot.decision = undefined;
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
this.slots.delete(sessionId);
|
|
84
|
+
this.emit(sessionId, { state: null });
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
/** Retry a failed creation while the first message is waiting in maintenance. */
|
|
88
|
+
retry(sessionId) {
|
|
89
|
+
const slot = this.slots.get(sessionId);
|
|
90
|
+
if (slot === undefined || slot.state.kind !== 'failed' || slot.decision === undefined)
|
|
91
|
+
return false;
|
|
92
|
+
const decision = slot.decision;
|
|
93
|
+
slot.decision = undefined;
|
|
94
|
+
decision.resolve('retry');
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
/** Withdraw a failed creation so the waiting message proceeds without a worktree. */
|
|
98
|
+
withdraw(sessionId) {
|
|
99
|
+
return this.cancel(sessionId);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Called from `agent/inbox/inserted`: if the session has an armed deferred
|
|
103
|
+
* request, hold the Agent via `runMaintenance` so the worktree is created and
|
|
104
|
+
* entered before the first turn starts.
|
|
105
|
+
*/
|
|
106
|
+
onInboxInserted(agent, message) {
|
|
107
|
+
if (this.lifetime.signal.aborted)
|
|
108
|
+
return;
|
|
109
|
+
const sessionId = agent.session.id;
|
|
110
|
+
const slot = this.slots.get(sessionId);
|
|
111
|
+
if (slot === undefined || slot.state.kind !== 'armed' || slot.holding)
|
|
112
|
+
return;
|
|
113
|
+
// Ignore next-step internal notices when nextTurn queue info is available.
|
|
114
|
+
if (message?.id !== undefined
|
|
115
|
+
&& Array.isArray(agent.inbox?.nextTurn)
|
|
116
|
+
&& !agent.inbox.nextTurn.some(queued => queued.id === message.id)) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (typeof agent.runMaintenance !== 'function')
|
|
120
|
+
return;
|
|
121
|
+
let task;
|
|
122
|
+
try {
|
|
123
|
+
task = agent.runMaintenance(async (maintenanceSignal) => {
|
|
124
|
+
// Yield a microtask so the inbox insertion finishes before touching the session.
|
|
125
|
+
await Promise.resolve();
|
|
126
|
+
const signal = AbortSignal.any([maintenanceSignal, this.lifetime.signal]);
|
|
127
|
+
await this.runLoop(sessionId, agent, slot, signal);
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
// Agent already busy with another turn/maintenance; pre-step fallback handles it if needed.
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
slot.holding = true;
|
|
135
|
+
this.track(task.finally(() => {
|
|
136
|
+
slot.holding = false;
|
|
137
|
+
}));
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Fallback for `agent/pre-step` (step === 1): if a message somehow bypassed
|
|
141
|
+
* `agent/inbox/inserted` while an armed request is still pending, create and
|
|
142
|
+
* enter the worktree right before the first model call.
|
|
143
|
+
* @returns true when this call actually created and switched the worktree.
|
|
144
|
+
*/
|
|
145
|
+
async onPreStep(agent, step, signal) {
|
|
146
|
+
if (step !== 1 || this.lifetime.signal.aborted)
|
|
147
|
+
return false;
|
|
148
|
+
const sessionId = agent.session.id;
|
|
149
|
+
const slot = this.slots.get(sessionId);
|
|
150
|
+
if (slot === undefined || slot.state.kind !== 'armed' || slot.holding)
|
|
151
|
+
return false;
|
|
152
|
+
const combined = signal === undefined ? this.lifetime.signal : AbortSignal.any([signal, this.lifetime.signal]);
|
|
153
|
+
try {
|
|
154
|
+
await this.executeOnce(sessionId, agent, slot, combined);
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
catch (error) {
|
|
158
|
+
this.clearSlot(sessionId, slot);
|
|
159
|
+
throw error;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
/** Release any waiting maintenance holds when the plugin unloads. */
|
|
163
|
+
async dispose() {
|
|
164
|
+
this.lifetime.abort(new Error('git-worktree plugin disposed'));
|
|
165
|
+
for (const [sessionId, slot] of [...this.slots]) {
|
|
166
|
+
slot.decision?.reject(this.lifetime.signal.reason);
|
|
167
|
+
slot.decision = undefined;
|
|
168
|
+
this.slots.delete(sessionId);
|
|
169
|
+
this.emit(sessionId, { state: null });
|
|
170
|
+
}
|
|
171
|
+
await Promise.allSettled(this.pending);
|
|
172
|
+
}
|
|
173
|
+
async runLoop(sessionId, agent, slot, signal) {
|
|
174
|
+
for (;;) {
|
|
175
|
+
if (this.slots.get(sessionId) !== slot)
|
|
176
|
+
return;
|
|
177
|
+
if (signal.aborted) {
|
|
178
|
+
this.clearSlot(sessionId, slot);
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
try {
|
|
182
|
+
await this.executeOnce(sessionId, agent, slot, signal);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
catch (error) {
|
|
186
|
+
if (signal.aborted || this.slots.get(sessionId) !== slot) {
|
|
187
|
+
this.clearSlot(sessionId, slot);
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
191
|
+
const currentPhase = slot.state.kind === 'running' ? slot.state.phase : 'create';
|
|
192
|
+
const resolvedBranch = slot.state.kind === 'running' ? slot.state.resolvedBranch : undefined;
|
|
193
|
+
slot.state = {
|
|
194
|
+
kind: 'failed',
|
|
195
|
+
spec: slot.state.spec,
|
|
196
|
+
phase: currentPhase,
|
|
197
|
+
message,
|
|
198
|
+
...(resolvedBranch === undefined ? {} : { resolvedBranch }),
|
|
199
|
+
};
|
|
200
|
+
this.emit(sessionId, { state: slot.state });
|
|
201
|
+
}
|
|
202
|
+
const decision = await this.waitForDecision(slot, signal);
|
|
203
|
+
if (decision === 'withdraw' || this.slots.get(sessionId) !== slot) {
|
|
204
|
+
this.clearSlot(sessionId, slot);
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
async executeOnce(sessionId, agent, slot, signal) {
|
|
210
|
+
signal.throwIfAborted();
|
|
211
|
+
const spec = slot.state.spec;
|
|
212
|
+
const publishPhase = (phase, resolvedBranch) => {
|
|
213
|
+
if (this.slots.get(sessionId) !== slot)
|
|
214
|
+
return;
|
|
215
|
+
const nextBranch = resolvedBranch ?? (slot.state.kind === 'running' ? slot.state.resolvedBranch : undefined);
|
|
216
|
+
slot.state = {
|
|
217
|
+
kind: 'running',
|
|
218
|
+
spec,
|
|
219
|
+
phase,
|
|
220
|
+
...(nextBranch === undefined ? {} : { resolvedBranch: nextBranch }),
|
|
221
|
+
};
|
|
222
|
+
this.emit(sessionId, { state: slot.state });
|
|
223
|
+
};
|
|
224
|
+
// A retry after a failed switch reuses the worktree the earlier attempt
|
|
225
|
+
// created: creating again would orphan it (generated names) or fail on
|
|
226
|
+
// the existing branch (explicit names).
|
|
227
|
+
let created = slot.created;
|
|
228
|
+
if (created === undefined) {
|
|
229
|
+
publishPhase('create', spec.cutout ? spec.name : spec.branch);
|
|
230
|
+
created = await this.deps.createWorktree(spec, (phase, branch) => {
|
|
231
|
+
publishPhase(phase, branch);
|
|
232
|
+
}, signal);
|
|
233
|
+
slot.created = created;
|
|
234
|
+
signal.throwIfAborted();
|
|
235
|
+
}
|
|
236
|
+
publishPhase('switch', created.branch);
|
|
237
|
+
const committedPath = await this.deps.setWorkingDirectory(agent, created.path, signal);
|
|
238
|
+
signal.throwIfAborted();
|
|
239
|
+
const completed = {
|
|
240
|
+
path: committedPath,
|
|
241
|
+
branch: created.branch,
|
|
242
|
+
created: created.created,
|
|
243
|
+
...(created.fetchWarning === undefined ? {} : { fetchWarning: created.fetchWarning }),
|
|
244
|
+
...(created.excludeWarning === undefined ? {} : { excludeWarning: created.excludeWarning }),
|
|
245
|
+
...(created.copyWarning === undefined ? {} : { copyWarning: created.copyWarning }),
|
|
246
|
+
...(created.trackPrUrl === undefined ? {} : { trackPrUrl: created.trackPrUrl }),
|
|
247
|
+
};
|
|
248
|
+
this.slots.delete(sessionId);
|
|
249
|
+
this.emit(sessionId, { state: null, completed });
|
|
250
|
+
}
|
|
251
|
+
async waitForDecision(slot, signal) {
|
|
252
|
+
if (signal.aborted)
|
|
253
|
+
return 'withdraw';
|
|
254
|
+
const deferred = createDeferred();
|
|
255
|
+
slot.decision = deferred;
|
|
256
|
+
const onAbort = () => { deferred.resolve('withdraw'); };
|
|
257
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
258
|
+
try {
|
|
259
|
+
return await deferred.promise;
|
|
260
|
+
}
|
|
261
|
+
catch {
|
|
262
|
+
return 'withdraw';
|
|
263
|
+
}
|
|
264
|
+
finally {
|
|
265
|
+
signal.removeEventListener('abort', onAbort);
|
|
266
|
+
if (slot.decision === deferred)
|
|
267
|
+
slot.decision = undefined;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
clearSlot(sessionId, slot) {
|
|
271
|
+
if (this.slots.get(sessionId) === slot) {
|
|
272
|
+
this.slots.delete(sessionId);
|
|
273
|
+
this.emit(sessionId, { state: null });
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
emit(sessionId, snapshot) {
|
|
277
|
+
const set = this.listeners.get(sessionId);
|
|
278
|
+
if (set === undefined)
|
|
279
|
+
return;
|
|
280
|
+
for (const listener of [...set]) {
|
|
281
|
+
try {
|
|
282
|
+
listener(snapshot);
|
|
283
|
+
}
|
|
284
|
+
catch {
|
|
285
|
+
// One broken stream listener must not break other observers.
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
track(task) {
|
|
290
|
+
this.pending.add(task);
|
|
291
|
+
void task.finally(() => { this.pending.delete(task); });
|
|
292
|
+
}
|
|
293
|
+
}
|
package/lib/git.d.ts
CHANGED
|
@@ -129,6 +129,17 @@ export declare function addWorktree(exec: Exec, repoRoot: string, branch: string
|
|
|
129
129
|
* @returns the free cutout branch name.
|
|
130
130
|
*/
|
|
131
131
|
export declare function cutoutBranchName(exec: Exec, repoRoot: string, base: string, folderTaken?: (branch: string) => boolean): Promise<string>;
|
|
132
|
+
/**
|
|
133
|
+
* Plan a random branch name (`<stem>-<4hex>`) to cut out of `base`, avoiding
|
|
134
|
+
* collisions with existing local branches and storage folders. Remote display
|
|
135
|
+
* names (`origin/feat/x`) strip their remote prefix before generating the stem.
|
|
136
|
+
* @param exec - executor seam.
|
|
137
|
+
* @param repoRoot - main worktree directory.
|
|
138
|
+
* @param base - base branch display name (local, `<remote>/<name>`, or `HEAD`).
|
|
139
|
+
* @param folderTaken - storage-folder occupancy probe (branch name → taken).
|
|
140
|
+
* @param randomHex - optional 4-hex generator seam for deterministic tests.
|
|
141
|
+
*/
|
|
142
|
+
export declare function randomCutoutBranchName(exec: Exec, repoRoot: string, base: string, folderTaken?: (branch: string) => boolean, randomHex?: () => string): Promise<string>;
|
|
132
143
|
/**
|
|
133
144
|
* Create a worktree on a brand-new branch cut out of `base`: the base is
|
|
134
145
|
* checked out by the main worktree (git forbids a second worktree on it),
|
|
@@ -329,6 +340,22 @@ export declare function removeWorktree(exec: Exec, repoRoot: string, worktreePat
|
|
|
329
340
|
* binary surfaces the same way — a non-zero exit through the seam).
|
|
330
341
|
*/
|
|
331
342
|
export declare function probeWorkspaceGit(exec: Exec, path: string): Promise<WorkspaceGitFacts | undefined>;
|
|
343
|
+
/**
|
|
344
|
+
* Best-effort persistence of the base branch a linked worktree was created
|
|
345
|
+
* from, stored inside `<worktreeGitDir>/dsh-worktree-base` so it never dirties
|
|
346
|
+
* the working tree and is removed automatically by `git worktree remove`.
|
|
347
|
+
*/
|
|
348
|
+
export declare function writeWorktreeBaseBranch(worktreePath: string, baseBranch: string): Promise<void>;
|
|
349
|
+
/** Read the persisted base branch for a linked worktree, or undefined when absent. */
|
|
350
|
+
export declare function readWorktreeBaseBranch(worktreePath: string): Promise<string | undefined>;
|
|
351
|
+
/**
|
|
352
|
+
* Best-effort persistence of the last fetched PR/MR commit SHA inside
|
|
353
|
+
* `<worktreeGitDir>/dsh-worktree-pr-sha` so later updates can distinguish a
|
|
354
|
+
* remote force-push (clean, zero local commits) from diverged local commits.
|
|
355
|
+
*/
|
|
356
|
+
export declare function writeWorktreePrSha(worktreePath: string, sha: string): Promise<void>;
|
|
357
|
+
/** Read the last synced PR/MR head SHA for a linked worktree, or undefined when absent. */
|
|
358
|
+
export declare function readWorktreePrSha(worktreePath: string): Promise<string | undefined>;
|
|
332
359
|
/** The one local-ignore rule the project-internal worktree layout needs.
|
|
333
360
|
* Exactly this pattern — NOT `/.dsh/` — so project-level `.dsh/` files a user
|
|
334
361
|
* may want to commit stay trackable. */
|
|
@@ -363,3 +390,71 @@ export declare const fsExcludeSeams: ExcludeSeams;
|
|
|
363
390
|
* failure text otherwise.
|
|
364
391
|
*/
|
|
365
392
|
export declare function appendWorktreeExclude(seams: ExcludeSeams, repoRoot: string): Promise<string | undefined>;
|
|
393
|
+
/**
|
|
394
|
+
* List repository remotes with their fetch URL, preserving git order and deduplicating names.
|
|
395
|
+
* @param exec - executor seam.
|
|
396
|
+
* @param repoRoot - main worktree directory.
|
|
397
|
+
*/
|
|
398
|
+
export declare function listRemotes(exec: Exec, repoRoot: string): Promise<Array<{
|
|
399
|
+
name: string;
|
|
400
|
+
url: string;
|
|
401
|
+
}>>;
|
|
402
|
+
/**
|
|
403
|
+
* Probe whether a specific ref (e.g. `refs/pull/42/head` or `refs/merge-requests/17/head`)
|
|
404
|
+
* exists on `remote` via `git ls-remote` without transferring any Git objects.
|
|
405
|
+
* Returns the tip commit SHA when present, or `undefined` when the remote is reachable
|
|
406
|
+
* and does not advertise the ref.
|
|
407
|
+
* @param exec - executor seam.
|
|
408
|
+
* @param repoRoot - main worktree directory.
|
|
409
|
+
* @param remote - target remote name.
|
|
410
|
+
* @param refSpec - exact remote ref name to check.
|
|
411
|
+
* @param signal - optional cancellation signal that kills the child process tree on abort.
|
|
412
|
+
*/
|
|
413
|
+
export declare function probeRemoteRef(exec: Exec, repoRoot: string, remote: string, refSpec: string, signal?: AbortSignal): Promise<string | undefined>;
|
|
414
|
+
/**
|
|
415
|
+
* Fetch an explicit remote ref (e.g. `refs/pull/42/head`) into a unique temporary ref
|
|
416
|
+
* and return its commit SHA without touching `FETCH_HEAD`, any local branch, or the working tree.
|
|
417
|
+
* @param exec - executor seam.
|
|
418
|
+
* @param repoRoot - main worktree directory.
|
|
419
|
+
* @param remote - target remote name.
|
|
420
|
+
* @param refSpec - remote ref to fetch.
|
|
421
|
+
* @param live - optional streaming progress and cancellation.
|
|
422
|
+
*/
|
|
423
|
+
export declare function fetchRefToTip(exec: Exec, repoRoot: string, remote: string, refSpec: string, live?: LiveRun): Promise<string>;
|
|
424
|
+
/**
|
|
425
|
+
* Create (or reuse) a linked worktree for `branch` pointing at `commitSha`.
|
|
426
|
+
* - If a live worktree already holds `branch`, returns it unchanged (`created: false`).
|
|
427
|
+
* - If `branch` does not exist yet, creates it at `commitSha` via `git worktree add <path> -b <branch> <sha>`.
|
|
428
|
+
* - If `branch` already exists locally without a worktree: fast-forwards it when it is an ancestor of
|
|
429
|
+
* `commitSha`, or preserves the local branch untouched (`preservedLocalBranch: true`) when it has local-only commits.
|
|
430
|
+
*/
|
|
431
|
+
export declare function addWorktreeAtCommit(exec: Exec, repoRoot: string, branch: string, commitSha: string, targetPath: string, dirExists?: DirExists): Promise<{
|
|
432
|
+
path: string;
|
|
433
|
+
created: boolean;
|
|
434
|
+
preservedLocalBranch?: boolean;
|
|
435
|
+
}>;
|
|
436
|
+
/**
|
|
437
|
+
* Return whether a worktree contains tracked or staged uncommitted changes
|
|
438
|
+
* (`--untracked-files=no`), ignoring pure untracked files such as copied
|
|
439
|
+
* `.worktreeinclude` config files (e.g. `.env.local`).
|
|
440
|
+
*/
|
|
441
|
+
export declare function hasTrackedChanges(exec: Exec, worktreePath: string): Promise<boolean>;
|
|
442
|
+
/**
|
|
443
|
+
* Sync an existing clean linked PR/MR worktree to `commitSha`:
|
|
444
|
+
* - refuses immediately if tracked/staged uncommitted changes exist (pure untracked or ignored
|
|
445
|
+
* files such as `.worktreeinclude` copies do not block syncing unless they collide with `commitSha`);
|
|
446
|
+
* - returns `up-to-date` when HEAD already equals `commitSha`;
|
|
447
|
+
* - fast-forwards when HEAD is an ancestor of `commitSha`;
|
|
448
|
+
* - when histories diverged (e.g. PR author rebased & force-pushed): safely resets via
|
|
449
|
+
* `git reset --hard <sha>` if `HEAD === lastKnownPrSha` (proving zero local-only commits),
|
|
450
|
+
* or if `forceReset: true` (after explicit user confirmation);
|
|
451
|
+
* - refuses to overwrite colliding untracked or `.gitignore`-ignored local files on all update paths;
|
|
452
|
+
* - otherwise refuses with a clear GitError so local commits are never silently discarded.
|
|
453
|
+
*/
|
|
454
|
+
export declare function updateCleanWorktreeToCommit(exec: Exec, worktreePath: string, commitSha: string, options?: {
|
|
455
|
+
readonly forceReset?: boolean;
|
|
456
|
+
readonly lastKnownPrSha?: string | undefined;
|
|
457
|
+
}): Promise<{
|
|
458
|
+
updated: boolean;
|
|
459
|
+
mode: 'up-to-date' | 'ff' | 'force-updated' | 'reset';
|
|
460
|
+
}>;
|