@laoyuehanni/dsh-git-worktree 0.4.8 → 0.4.10
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 +5 -2
- package/README.zh.md +5 -2
- package/lib/client.js +2393 -205
- package/lib/git.d.ts +58 -11
- package/lib/git.js +197 -17
- package/lib/graph-preview/asciidoc-xKuVlNk6.js +5 -0
- package/lib/graph-preview/bat-Dx8nnxXE.js +5 -0
- package/lib/graph-preview/bibtex-BzgdqG3s.js +5 -0
- package/lib/graph-preview/c-BGpbtv2-.js +8 -0
- package/lib/graph-preview/clojure-DDsQ0FaV.js +5 -0
- package/lib/graph-preview/cmake-DPsCB_V6.js +5 -0
- package/lib/graph-preview/cpp-CBYrHpV9.js +27 -0
- package/lib/graph-preview/csharp-CFKaLbHg.js +5 -0
- package/lib/graph-preview/css-C6zsnaz5.js +8 -0
- package/lib/graph-preview/csv-DqIVFkK7.js +5 -0
- package/lib/graph-preview/dart-C6DUdjib.js +5 -0
- package/lib/graph-preview/diff-Blb4MjsC.js +5 -0
- package/lib/graph-preview/dotenv-Dkdv7Sm2.js +5 -0
- package/lib/graph-preview/elixir-D0Gskj1z.js +8 -0
- package/lib/graph-preview/erlang-BJwlUj8s.js +8 -0
- package/lib/graph-preview/fish-D7qAeWQz.js +5 -0
- package/lib/graph-preview/fsharp-MCvH12ek.js +8 -0
- package/lib/graph-preview/go-aXtTCanQ.js +5 -0
- package/lib/graph-preview/graphql-DBbDUzBC.js +19 -0
- package/lib/graph-preview/groovy-Bw2Ku4bU.js +5 -0
- package/lib/graph-preview/haskell-CfGsE1Cf.js +5 -0
- package/lib/graph-preview/hcl-DHg_Osgo.js +5 -0
- package/lib/graph-preview/html-DwZ-9Aol.js +15 -0
- package/lib/graph-preview/html-derivative-CdrGiVx_.js +8 -0
- package/lib/graph-preview/http-B769neBI.js +16 -0
- package/lib/graph-preview/ini-C24lZfdM.js +5 -0
- package/lib/graph-preview/java-B6HH4GJL.js +8 -0
- package/lib/graph-preview/javascript-BXA9Hfas.js +5 -0
- package/lib/graph-preview/julia--d6L9eAJ.js +19 -0
- package/lib/graph-preview/kotlin-BHE8CoV9.js +5 -0
- package/lib/graph-preview/latex-BsyjE6iS.js +12 -0
- package/lib/graph-preview/less-BzbdhFUi.js +5 -0
- package/lib/graph-preview/log-Dad54gsi.js +5 -0
- package/lib/graph-preview/lua-DL6V31K-.js +10 -0
- package/lib/graph-preview/make-B2ukzWs3.js +5 -0
- package/lib/graph-preview/markdown-CkmxmzqY.js +5 -0
- package/lib/graph-preview/mdx-BK9ZufRc.js +5 -0
- package/lib/graph-preview/nix-BRl3YduB.js +9 -0
- package/lib/graph-preview/perl-DJHPR3L5.js +19 -0
- package/lib/graph-preview/php-B74oGaLi.js +21 -0
- package/lib/graph-preview/powershell-DvOBVZPr.js +5 -0
- package/lib/graph-preview/preview.js +79431 -0
- package/lib/graph-preview/proto-ClkOcAW0.js +5 -0
- package/lib/graph-preview/python-BjnOIxPe.js +5 -0
- package/lib/graph-preview/r-BSNufAtJ.js +8 -0
- package/lib/graph-preview/rst-CDCHc-qO.js +25 -0
- package/lib/graph-preview/ruby-Dx4dTqam.js +40 -0
- package/lib/graph-preview/rust-COBzcmTx.js +5 -0
- package/lib/graph-preview/scala-pMGZAs9j.js +5 -0
- package/lib/graph-preview/scss-D2N2uO1z.js +8 -0
- package/lib/graph-preview/sql-DBa5L-Wg.js +8 -0
- package/lib/graph-preview/svelte-DQXUqa3J.js +18 -0
- package/lib/graph-preview/swift-DWmMn3lF.js +5 -0
- package/lib/graph-preview/system-verilog-DW0KSMVO.js +5 -0
- package/lib/graph-preview/toml-B7BHZIqX.js +5 -0
- package/lib/graph-preview/vb-eGVhCUXq.js +5 -0
- package/lib/graph-preview/verilog-Ma7yBE30.js +5 -0
- package/lib/graph-preview/vue-k6e1qQTO.js +32 -0
- package/lib/graph-preview/xml-DUa-g8pJ.js +10 -0
- package/lib/graph-preview/yaml-B81N8prR.js +5 -0
- package/lib/history.d.ts +7 -0
- package/lib/history.js +79 -0
- package/lib/index.js +28 -4
- package/lib/progress.d.ts +60 -0
- package/lib/progress.js +122 -0
- package/lib/push.d.ts +27 -0
- package/lib/push.js +215 -0
- package/lib/routes.d.ts +48 -6
- package/lib/routes.js +149 -7
- package/lib/stream.d.ts +33 -0
- package/lib/stream.js +54 -0
- package/lib/wire.d.ts +131 -2
- package/lib/wire.js +18 -2
- package/package.json +12 -10
package/lib/routes.js
CHANGED
|
@@ -5,9 +5,12 @@
|
|
|
5
5
|
import { basename, dirname, join, resolve } from 'node:path';
|
|
6
6
|
import { mkdir, readdir, rm, stat } from 'node:fs/promises';
|
|
7
7
|
import { runPostCreateCopy } from './copy-files.js';
|
|
8
|
-
import { appendWorktreeExclude, fsExcludeSeams, GitError, addWorktree, addWorktreeCutout, createBranch, cutoutBranchName, deleteBranch, fetchAll, fsDirExists, inspectWorktree, isAbsoluteDir, probeRepo, probeWorkspaceGit, removeWorktree, renameBranch, resolveBranch, switchBranch, updateBranch } from './git.js';
|
|
8
|
+
import { appendWorktreeExclude, fsExcludeSeams, CancelledError, GitError, addWorktree, addWorktreeCutout, createBranch, cutoutBranchName, deleteBranch, fetchAll, fsDirExists, inspectWorktree, isAbsoluteDir, probeRepo, probeWorkspaceGit, removeWorktree, renameBranch, resolveBranch, switchBranch, updateBranch } from './git.js';
|
|
9
9
|
import { readCheckedOutBranch } from './head.js';
|
|
10
|
+
import { readHistory, readCommit } from './history.js';
|
|
11
|
+
import { openPush, previewPush, pushBranch } from './push.js';
|
|
10
12
|
import { isAbsoluteConfigPath, sanitizeBranchDir } from './normalize.js';
|
|
13
|
+
import { createProgressReader } from './progress.js';
|
|
11
14
|
import { resolveRootDir } from './settings.js';
|
|
12
15
|
/** Real fs-backed one-level directory listing (names only). Plain FILES are
|
|
13
16
|
* filtered out — the storage root can carry strays (a legacy settings.json
|
|
@@ -374,6 +377,34 @@ function readBody(body, keys, booleanKeys = [], optionalKeys = []) {
|
|
|
374
377
|
function isOutcome(value) {
|
|
375
378
|
return typeof value === 'object' && value !== null && 'status' in value && 'body' in value;
|
|
376
379
|
}
|
|
380
|
+
export async function handleHistory(deps, body) {
|
|
381
|
+
const parsed = readBody(body, ['repoPath', 'ref'], [], ['tip', 'offset']);
|
|
382
|
+
if (isOutcome(parsed))
|
|
383
|
+
return parsed;
|
|
384
|
+
if (!isAbsoluteDir(parsed.repoPath))
|
|
385
|
+
return fail(400, '"repoPath" must be an absolute directory');
|
|
386
|
+
if (parsed.offset !== undefined && !/^\d+$/.test(parsed.offset))
|
|
387
|
+
return fail(400, 'Invalid history offset');
|
|
388
|
+
try {
|
|
389
|
+
return { status: 200, body: await readHistory(deps.exec, parsed.repoPath, parsed.ref, parsed.tip, Number(parsed.offset ?? 0)) };
|
|
390
|
+
}
|
|
391
|
+
catch (error) {
|
|
392
|
+
return fail(400, error instanceof Error ? error.message : String(error));
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
export async function handleCommit(deps, body) {
|
|
396
|
+
const parsed = readBody(body, ['repoPath', 'oid']);
|
|
397
|
+
if (isOutcome(parsed))
|
|
398
|
+
return parsed;
|
|
399
|
+
if (!isAbsoluteDir(parsed.repoPath))
|
|
400
|
+
return fail(400, '"repoPath" must be an absolute directory');
|
|
401
|
+
try {
|
|
402
|
+
return { status: 200, body: await readCommit(deps.exec, parsed.repoPath, parsed.oid) };
|
|
403
|
+
}
|
|
404
|
+
catch (error) {
|
|
405
|
+
return fail(400, error instanceof Error ? error.message : String(error));
|
|
406
|
+
}
|
|
407
|
+
}
|
|
377
408
|
/**
|
|
378
409
|
* POST /worktree — create or reuse the worktree for a branch, then report the
|
|
379
410
|
* directory so the client can register it as a workspace. The worktree lands
|
|
@@ -634,49 +665,155 @@ export async function handleDeleteBranch(deps, body) {
|
|
|
634
665
|
}
|
|
635
666
|
}
|
|
636
667
|
/**
|
|
637
|
-
*
|
|
668
|
+
* Translate a route's live channel into a git {@link LiveRun}: git's raw
|
|
669
|
+
* output goes through the progress reader, so the caller sees structured,
|
|
670
|
+
* throttled events instead of a thousand `\r` redraws.
|
|
671
|
+
* @param live - the route's channel; absent for a plain, non-streamed call.
|
|
672
|
+
* @returns the run to hand to git (undefined = no live behaviour) and `end`,
|
|
673
|
+
* which flushes the reader's last held observation.
|
|
674
|
+
*/
|
|
675
|
+
function startLive(live) {
|
|
676
|
+
if (live === undefined)
|
|
677
|
+
return { run: undefined, end: () => undefined };
|
|
678
|
+
const reader = createProgressReader(progress => { live.emit({ type: 'progress', ...progress }); });
|
|
679
|
+
return {
|
|
680
|
+
run: {
|
|
681
|
+
onOutput: (chunk) => { reader.feed(chunk); },
|
|
682
|
+
onRemote: (remote) => { reader.remote(remote); },
|
|
683
|
+
signal: live.signal,
|
|
684
|
+
// The merge event must come after the last progress line, not before.
|
|
685
|
+
onMerge: () => { reader.end(); live.emit({ type: 'merge' }); },
|
|
686
|
+
},
|
|
687
|
+
end: () => { reader.end(); },
|
|
688
|
+
};
|
|
689
|
+
}
|
|
690
|
+
/**
|
|
691
|
+
* POST /fetch — sync remote-tracking refs for the repository: fetch every
|
|
638
692
|
* remote and prune tracking branches the remotes no longer carry. Pure
|
|
639
693
|
* metadata — the working tree and the current checkout are untouched; the
|
|
640
694
|
* client refetches /status afterwards for the fresh branch list.
|
|
641
695
|
* @param deps - host dependencies.
|
|
642
696
|
* @param body - parsed request body.
|
|
697
|
+
* @param live - the streaming channel (absent = the plain request).
|
|
643
698
|
*/
|
|
644
|
-
export async function handleFetch(deps, body) {
|
|
699
|
+
export async function handleFetch(deps, body, live) {
|
|
645
700
|
const parsed = readBody(body, ['repoPath']);
|
|
646
701
|
if (isOutcome(parsed))
|
|
647
702
|
return parsed;
|
|
648
703
|
const { repoPath } = parsed;
|
|
649
704
|
if (!isAbsoluteDir(repoPath))
|
|
650
705
|
return fail(400, '"repoPath" must be an absolute directory');
|
|
706
|
+
const { run, end } = startLive(live);
|
|
651
707
|
try {
|
|
652
708
|
const facts = await probeRepo(deps.exec, repoPath, deps.dirExists);
|
|
653
709
|
if (facts === undefined)
|
|
654
710
|
return fail(400, `"${repoPath}" is not inside a git repository`);
|
|
655
|
-
await fetchAll(deps.exec, facts.repoRoot);
|
|
711
|
+
await fetchAll(deps.exec, facts.repoRoot, run);
|
|
656
712
|
const result = { remote: 'all' };
|
|
657
713
|
return { status: 200, body: result };
|
|
658
714
|
}
|
|
659
715
|
catch (error) {
|
|
716
|
+
if (error instanceof CancelledError)
|
|
717
|
+
return fail(499, error.message);
|
|
660
718
|
if (error instanceof GitError)
|
|
661
719
|
return gitFailure(error);
|
|
662
720
|
return fail(500, error instanceof Error ? error.message : String(error));
|
|
663
721
|
}
|
|
722
|
+
finally {
|
|
723
|
+
end();
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
/**
|
|
727
|
+
* POST /push-plan — read-only destination resolution for one local branch,
|
|
728
|
+
* bundled with a display-only local snapshot of what it would push (see
|
|
729
|
+
* `openPush`). Publishing is a separate explicit POST.
|
|
730
|
+
* @param deps - host dependencies.
|
|
731
|
+
* @param body - parsed request body: `{ repoPath, branch }`.
|
|
732
|
+
*/
|
|
733
|
+
export async function handlePushPlan(deps, body) {
|
|
734
|
+
return handlePushAction(deps, body, false);
|
|
735
|
+
}
|
|
736
|
+
/**
|
|
737
|
+
* POST /push — push one explicit local branch to an explicit destination
|
|
738
|
+
* (or, without one, to the confirmation-free default of `planPush`).
|
|
739
|
+
* @param deps - host dependencies.
|
|
740
|
+
* @param body - parsed request body.
|
|
741
|
+
* @param live - the streaming channel (absent = the plain request, which
|
|
742
|
+
* keeps its two-minute cap).
|
|
743
|
+
*/
|
|
744
|
+
export async function handlePush(deps, body, live) {
|
|
745
|
+
return handlePushAction(deps, body, true, live);
|
|
746
|
+
}
|
|
747
|
+
/**
|
|
748
|
+
* POST /push-preview — the commits one explicit remote/target would receive,
|
|
749
|
+
* read from the real push destination (or from local tracking refs with
|
|
750
|
+
* `localOnly`, which never touches the network). Pushes nothing.
|
|
751
|
+
* @param deps - host dependencies.
|
|
752
|
+
* @param body - parsed request body.
|
|
753
|
+
*/
|
|
754
|
+
export async function handlePushPreview(deps, body) {
|
|
755
|
+
const parsed = readBody(body, ['repoPath', 'branch', 'remote', 'target'], ['localOnly']);
|
|
756
|
+
if (isOutcome(parsed))
|
|
757
|
+
return parsed;
|
|
758
|
+
if (!isAbsoluteDir(parsed.repoPath) || !parsed.branch.trim() || !parsed.remote?.trim() || !parsed.target?.trim())
|
|
759
|
+
return fail(400, 'An absolute repoPath, branch, remote and target are required');
|
|
760
|
+
try {
|
|
761
|
+
return { status: 200, body: await previewPush(deps.exec, parsed.repoPath, { ...parsed, remote: parsed.remote, target: parsed.target }) };
|
|
762
|
+
}
|
|
763
|
+
catch (error) {
|
|
764
|
+
return fail(400, error instanceof Error ? error.message : String(error));
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
async function handlePushAction(deps, body, execute, live) {
|
|
768
|
+
const parsed = readBody(body, ['repoPath', 'branch'], execute ? ['force'] : [], execute ? ['remote', 'target', 'tags', 'expectedTarget', 'sourceOid'] : []);
|
|
769
|
+
if (isOutcome(parsed))
|
|
770
|
+
return parsed;
|
|
771
|
+
if (!isAbsoluteDir(parsed.repoPath))
|
|
772
|
+
return fail(400, '"repoPath" must be an absolute directory');
|
|
773
|
+
if (parsed.branch.trim() === '')
|
|
774
|
+
return fail(400, '"branch" must be non-empty');
|
|
775
|
+
if (parsed.tags !== undefined && !['none', 'all', 'follow'].includes(parsed.tags))
|
|
776
|
+
return fail(400, '"tags" must be none, all or follow');
|
|
777
|
+
if (parsed.sourceOid !== undefined && !/^(?:[a-f0-9]{40}|[a-f0-9]{64})$/.test(parsed.sourceOid))
|
|
778
|
+
return fail(400, '"sourceOid" must be a full object ID');
|
|
779
|
+
if ((parsed.remote === undefined) !== (parsed.target === undefined))
|
|
780
|
+
return fail(400, '"remote" and "target" must be provided together');
|
|
781
|
+
if (parsed.remote !== undefined && (typeof parsed.remote !== 'string' || parsed.remote.trim() === ''
|
|
782
|
+
|| typeof parsed.target !== 'string' || parsed.target.trim() === ''))
|
|
783
|
+
return fail(400, '"remote" and "target" must be non-empty strings');
|
|
784
|
+
const { run, end } = startLive(execute ? live : undefined);
|
|
785
|
+
try {
|
|
786
|
+
return { status: 200, body: execute
|
|
787
|
+
? await pushBranch(deps.exec, parsed.repoPath, parsed, run)
|
|
788
|
+
: await openPush(deps.exec, parsed.repoPath, parsed.branch) };
|
|
789
|
+
}
|
|
790
|
+
catch (error) {
|
|
791
|
+
// Stopped by the caller: not a failure, and nobody is left to read it.
|
|
792
|
+
if (error instanceof CancelledError)
|
|
793
|
+
return fail(499, error.message);
|
|
794
|
+
return fail(400, error instanceof Error ? error.message : String(error));
|
|
795
|
+
}
|
|
796
|
+
finally {
|
|
797
|
+
end();
|
|
798
|
+
}
|
|
664
799
|
}
|
|
665
800
|
/**
|
|
666
|
-
* POST /update
|
|
801
|
+
* POST /update — update the CURRENT checkout: fetch every remote, then
|
|
667
802
|
* fast-forward the branch checked out by the queried directory to its
|
|
668
803
|
* upstream. Divergence, a missing upstream, and conflicting working-tree
|
|
669
804
|
* changes all surface as 400 envelopes carrying git's own explanation.
|
|
670
805
|
* @param deps - host dependencies.
|
|
671
806
|
* @param body - parsed request body.
|
|
807
|
+
* @param live - the streaming channel (absent = the plain request).
|
|
672
808
|
*/
|
|
673
|
-
export async function handleUpdate(deps, body) {
|
|
809
|
+
export async function handleUpdate(deps, body, live) {
|
|
674
810
|
const parsed = readBody(body, ['repoPath']);
|
|
675
811
|
if (isOutcome(parsed))
|
|
676
812
|
return parsed;
|
|
677
813
|
const { repoPath } = parsed;
|
|
678
814
|
if (!isAbsoluteDir(repoPath))
|
|
679
815
|
return fail(400, '"repoPath" must be an absolute directory');
|
|
816
|
+
const { run, end } = startLive(live);
|
|
680
817
|
try {
|
|
681
818
|
const facts = await probeRepo(deps.exec, repoPath, deps.dirExists);
|
|
682
819
|
if (facts === undefined)
|
|
@@ -685,15 +822,20 @@ export async function handleUpdate(deps, body) {
|
|
|
685
822
|
// session "update current branch" means the branch that worktree holds.
|
|
686
823
|
// `repoRoot` would fast-forward the main checkout behind the user's back
|
|
687
824
|
// (both are the same directory when the session IS the main checkout).
|
|
688
|
-
const outcome = await updateBranch(deps.exec, facts.toplevel);
|
|
825
|
+
const outcome = await updateBranch(deps.exec, facts.toplevel, run);
|
|
689
826
|
const result = { branch: outcome.branch, updated: outcome.updated };
|
|
690
827
|
return { status: 200, body: result };
|
|
691
828
|
}
|
|
692
829
|
catch (error) {
|
|
830
|
+
if (error instanceof CancelledError)
|
|
831
|
+
return fail(499, error.message);
|
|
693
832
|
if (error instanceof GitError)
|
|
694
833
|
return gitFailure(error);
|
|
695
834
|
return fail(500, error instanceof Error ? error.message : String(error));
|
|
696
835
|
}
|
|
836
|
+
finally {
|
|
837
|
+
end();
|
|
838
|
+
}
|
|
697
839
|
}
|
|
698
840
|
/**
|
|
699
841
|
* POST /inspect — pre-delete facts for one worktree directory: the
|
package/lib/stream.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Streamed route responses: the HTTP half of the live fetch/update progress.
|
|
3
|
+
*
|
|
4
|
+
* A streamed route answers `200` at once and then writes one JSON
|
|
5
|
+
* {@link SyncFrame} per line while the handler works; the last line is `done`
|
|
6
|
+
* and carries the status and body the same route would have answered in one
|
|
7
|
+
* piece. The status line is therefore no longer the verdict — the `done`
|
|
8
|
+
* frame is — which is what lets a failure that happens after minutes of
|
|
9
|
+
* transfer still be reported.
|
|
10
|
+
*
|
|
11
|
+
* The caller cancels by closing the connection. A response that is not yet
|
|
12
|
+
* finished when its socket closes aborts the handler's signal, which kills
|
|
13
|
+
* git. (`res`'s `close`, not `req`'s: a request body that was fully read
|
|
14
|
+
* closes early on modern Node whether or not the client is still there.)
|
|
15
|
+
*
|
|
16
|
+
* @module git-worktree/stream
|
|
17
|
+
*/
|
|
18
|
+
import type { RouteLive, RouteOutcome } from './routes.js';
|
|
19
|
+
/** The slice of `http.ServerResponse` a stream needs — a fake in tests. */
|
|
20
|
+
export interface StreamTarget {
|
|
21
|
+
writeHead: (status: number, headers: Record<string, string>) => unknown;
|
|
22
|
+
write: (chunk: string) => unknown;
|
|
23
|
+
end: () => unknown;
|
|
24
|
+
on: (event: 'close', listener: () => void) => unknown;
|
|
25
|
+
readonly writableEnded: boolean;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Run one handler as an NDJSON stream.
|
|
29
|
+
* @param res - the response to stream into.
|
|
30
|
+
* @param handle - the route logic; emits events through `live` and resolves
|
|
31
|
+
* with the outcome that becomes the `done` frame.
|
|
32
|
+
*/
|
|
33
|
+
export declare function streamRoute(res: StreamTarget, handle: (live: RouteLive) => Promise<RouteOutcome>): Promise<void>;
|
package/lib/stream.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Streamed route responses: the HTTP half of the live fetch/update progress.
|
|
3
|
+
*
|
|
4
|
+
* A streamed route answers `200` at once and then writes one JSON
|
|
5
|
+
* {@link SyncFrame} per line while the handler works; the last line is `done`
|
|
6
|
+
* and carries the status and body the same route would have answered in one
|
|
7
|
+
* piece. The status line is therefore no longer the verdict — the `done`
|
|
8
|
+
* frame is — which is what lets a failure that happens after minutes of
|
|
9
|
+
* transfer still be reported.
|
|
10
|
+
*
|
|
11
|
+
* The caller cancels by closing the connection. A response that is not yet
|
|
12
|
+
* finished when its socket closes aborts the handler's signal, which kills
|
|
13
|
+
* git. (`res`'s `close`, not `req`'s: a request body that was fully read
|
|
14
|
+
* closes early on modern Node whether or not the client is still there.)
|
|
15
|
+
*
|
|
16
|
+
* @module git-worktree/stream
|
|
17
|
+
*/
|
|
18
|
+
import { NDJSON_TYPE } from './wire.js';
|
|
19
|
+
/**
|
|
20
|
+
* Run one handler as an NDJSON stream.
|
|
21
|
+
* @param res - the response to stream into.
|
|
22
|
+
* @param handle - the route logic; emits events through `live` and resolves
|
|
23
|
+
* with the outcome that becomes the `done` frame.
|
|
24
|
+
*/
|
|
25
|
+
export async function streamRoute(res, handle) {
|
|
26
|
+
const controller = new AbortController();
|
|
27
|
+
res.on('close', () => {
|
|
28
|
+
if (!res.writableEnded)
|
|
29
|
+
controller.abort();
|
|
30
|
+
});
|
|
31
|
+
res.writeHead(200, {
|
|
32
|
+
'content-type': `${NDJSON_TYPE}; charset=utf-8`,
|
|
33
|
+
'cache-control': 'no-store',
|
|
34
|
+
// Tell a reverse proxy not to hold the lines back until the stream ends.
|
|
35
|
+
'x-accel-buffering': 'no',
|
|
36
|
+
});
|
|
37
|
+
const write = (frame) => {
|
|
38
|
+
// Nobody is listening after an abort; writing would only raise EPIPE.
|
|
39
|
+
if (controller.signal.aborted || res.writableEnded)
|
|
40
|
+
return;
|
|
41
|
+
res.write(`${JSON.stringify(frame)}\n`);
|
|
42
|
+
};
|
|
43
|
+
let outcome;
|
|
44
|
+
try {
|
|
45
|
+
outcome = await handle({ signal: controller.signal, emit: write });
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
// Handlers answer their own failures; this is the backstop for a bug.
|
|
49
|
+
outcome = { status: 500, body: { error: error instanceof Error ? error.message : String(error) } };
|
|
50
|
+
}
|
|
51
|
+
write({ type: 'done', status: outcome.status, body: outcome.body });
|
|
52
|
+
if (!res.writableEnded)
|
|
53
|
+
res.end();
|
|
54
|
+
}
|
package/lib/wire.d.ts
CHANGED
|
@@ -11,6 +11,42 @@ export declare const ROUTE_STATUS = "/plugin/git-worktree/status";
|
|
|
11
11
|
/** GET ROUTE_PREFIX/head?path=<absolute dir> — checked-out branch only.
|
|
12
12
|
* A file read of HEAD, not a git process. */
|
|
13
13
|
export declare const ROUTE_HEAD = "/plugin/git-worktree/head";
|
|
14
|
+
/** Read-only POSTs: bounded history and one commit's full message/summary. */
|
|
15
|
+
export declare const ROUTE_HISTORY = "/plugin/git-worktree/history";
|
|
16
|
+
export declare const ROUTE_COMMIT = "/plugin/git-worktree/commit";
|
|
17
|
+
export interface HistoryBody {
|
|
18
|
+
repoPath: string;
|
|
19
|
+
/** Explicit refs/heads/... or refs/remotes/...; HEAD for detached/unborn. */
|
|
20
|
+
ref: string;
|
|
21
|
+
/** Subsequent pages stay pinned to the first response's tip. */
|
|
22
|
+
tip?: string;
|
|
23
|
+
offset?: string;
|
|
24
|
+
}
|
|
25
|
+
export interface HistoryCommit {
|
|
26
|
+
oid: string;
|
|
27
|
+
parents: string[];
|
|
28
|
+
author: string;
|
|
29
|
+
date: string;
|
|
30
|
+
subject: string;
|
|
31
|
+
}
|
|
32
|
+
export interface CommitHistory {
|
|
33
|
+
tip: string | null;
|
|
34
|
+
commits: HistoryCommit[];
|
|
35
|
+
next: number | null;
|
|
36
|
+
}
|
|
37
|
+
export interface CommitDetail extends HistoryCommit {
|
|
38
|
+
body: string;
|
|
39
|
+
/** Summary relative to the first parent (empty tree for a root). */
|
|
40
|
+
files: number;
|
|
41
|
+
firstFile: string | null;
|
|
42
|
+
added: number;
|
|
43
|
+
removed: number;
|
|
44
|
+
binaryFiles: number;
|
|
45
|
+
}
|
|
46
|
+
export interface CommitBody {
|
|
47
|
+
repoPath: string;
|
|
48
|
+
oid: string;
|
|
49
|
+
}
|
|
14
50
|
/** POST ROUTE_PREFIX/worktree — create-or-reuse a worktree for a branch. */
|
|
15
51
|
export declare const ROUTE_WORKTREE = "/plugin/git-worktree/worktree";
|
|
16
52
|
/** POST ROUTE_PREFIX/switch — in-place branch switch of the main checkout. */
|
|
@@ -24,10 +60,21 @@ export declare const ROUTE_BRANCH_RENAME = "/plugin/git-worktree/branch-rename";
|
|
|
24
60
|
/** POST ROUTE_PREFIX/branch-delete — delete a LOCAL branch (`-d`, merged
|
|
25
61
|
* only; git refuses the rest). */
|
|
26
62
|
export declare const ROUTE_BRANCH_DELETE = "/plugin/git-worktree/branch-delete";
|
|
27
|
-
/** POST ROUTE_PREFIX/fetch — sync remote-tracking refs (fetch every remote + prune).
|
|
63
|
+
/** POST ROUTE_PREFIX/fetch — sync remote-tracking refs (fetch every remote + prune).
|
|
64
|
+
* Streams {@link SyncFrame} lines (NDJSON) so a long transfer can show progress. */
|
|
28
65
|
export declare const ROUTE_FETCH = "/plugin/git-worktree/fetch";
|
|
29
|
-
/** POST ROUTE_PREFIX/update — fast-forward the current branch to its upstream.
|
|
66
|
+
/** POST ROUTE_PREFIX/update — fast-forward the current branch to its upstream.
|
|
67
|
+
* Streams {@link SyncFrame} lines (NDJSON), like {@link ROUTE_FETCH}. */
|
|
30
68
|
export declare const ROUTE_UPDATE = "/plugin/git-worktree/update";
|
|
69
|
+
/** POST push-plan — resolve the selected branch's push destination. */
|
|
70
|
+
export declare const ROUTE_PUSH_PLAN = "/plugin/git-worktree/push-plan";
|
|
71
|
+
/** POST push-preview — inspect one explicit destination without pushing. */
|
|
72
|
+
export declare const ROUTE_PUSH_PREVIEW = "/plugin/git-worktree/push-preview";
|
|
73
|
+
/** POST push — push one explicit local branch. Streams {@link SyncFrame}
|
|
74
|
+
* lines (NDJSON), like {@link ROUTE_FETCH}; the `done` body is a
|
|
75
|
+
* {@link PushResult}. A cancel (499) kills `git push`; the remote only moves
|
|
76
|
+
* a ref once the whole pack has arrived, so a stopped transfer changes nothing. */
|
|
77
|
+
export declare const ROUTE_PUSH = "/plugin/git-worktree/push";
|
|
31
78
|
/** POST ROUTE_PREFIX/group — git belonging facts for a batch of workspace paths. */
|
|
32
79
|
export declare const ROUTE_GROUP = "/plugin/git-worktree/group";
|
|
33
80
|
/** POST ROUTE_PREFIX/inspect — pre-delete facts for one worktree directory. */
|
|
@@ -54,6 +101,59 @@ export interface BranchEntry {
|
|
|
54
101
|
ahead?: number;
|
|
55
102
|
/** Commits the local branch is BEHIND its upstream (same conditions). */
|
|
56
103
|
behind?: number;
|
|
104
|
+
/** Configured upstream; absent means unpublished, not synchronized. */
|
|
105
|
+
upstream?: string;
|
|
106
|
+
/** The configured upstream no longer exists in the local tracking refs. */
|
|
107
|
+
upstreamGone?: true;
|
|
108
|
+
}
|
|
109
|
+
export interface PushPlanBody {
|
|
110
|
+
repoPath: string;
|
|
111
|
+
branch: string;
|
|
112
|
+
}
|
|
113
|
+
export interface PushPlan {
|
|
114
|
+
branch: string;
|
|
115
|
+
remotes: string[];
|
|
116
|
+
remote: string;
|
|
117
|
+
target: string;
|
|
118
|
+
/** Publishing or changing the tracking destination requires a target dialog. */
|
|
119
|
+
confirm: boolean;
|
|
120
|
+
upstream?: string;
|
|
121
|
+
/** `/push-plan` only: display-only snapshot of the default destination from
|
|
122
|
+
* local refs, so opening the push window needs one request, not two. Absent
|
|
123
|
+
* when it could not be built; never a substitute for the verified preview. */
|
|
124
|
+
local?: PushPreview;
|
|
125
|
+
}
|
|
126
|
+
export interface PushBody extends PushPlanBody {
|
|
127
|
+
/** Both present only after the user confirms the publish destination. */
|
|
128
|
+
remote?: string;
|
|
129
|
+
target?: string;
|
|
130
|
+
tags?: 'none' | 'all' | 'follow';
|
|
131
|
+
/** Force only the destination branch, leased against the previewed OID. */
|
|
132
|
+
force?: boolean;
|
|
133
|
+
/** Empty string means the remote branch did not exist in the preview. */
|
|
134
|
+
expectedTarget?: string;
|
|
135
|
+
sourceOid?: string;
|
|
136
|
+
}
|
|
137
|
+
export interface PushPreviewBody extends PushPlanBody {
|
|
138
|
+
remote: string;
|
|
139
|
+
target: string;
|
|
140
|
+
/** Approximate display-only preview from local tracking refs; never contacts the remote. */
|
|
141
|
+
localOnly?: boolean;
|
|
142
|
+
}
|
|
143
|
+
export interface PushPreview {
|
|
144
|
+
sourceOid: string;
|
|
145
|
+
targetOid: string | null;
|
|
146
|
+
commits: {
|
|
147
|
+
oid: string;
|
|
148
|
+
subject: string;
|
|
149
|
+
}[];
|
|
150
|
+
total: number;
|
|
151
|
+
truncated: boolean;
|
|
152
|
+
}
|
|
153
|
+
export interface PushResult {
|
|
154
|
+
branch: string;
|
|
155
|
+
remote: string;
|
|
156
|
+
target: string;
|
|
57
157
|
}
|
|
58
158
|
/** One existing worktree of the repository. */
|
|
59
159
|
export interface WorktreeEntry {
|
|
@@ -228,6 +328,35 @@ export interface UpdateResult {
|
|
|
228
328
|
/** False when the upstream already contained the branch (nothing moved). */
|
|
229
329
|
updated: boolean;
|
|
230
330
|
}
|
|
331
|
+
/** Content type of a streamed route: one JSON {@link SyncFrame} per line. */
|
|
332
|
+
export declare const NDJSON_TYPE = "application/x-ndjson";
|
|
333
|
+
/**
|
|
334
|
+
* A live event of a long route while it still runs.
|
|
335
|
+
* - `progress` is git's own progress line (`Receiving objects: 3% (2147/58680),
|
|
336
|
+
* 708.01 KiB | 128.00 KiB/s`) with its percentage when it has one, and the
|
|
337
|
+
* remote being fetched (the host fetches them one by one and names each
|
|
338
|
+
* ahead of its counter lines);
|
|
339
|
+
* - `merge` marks the switch from fetching to the fast-forward, the one step
|
|
340
|
+
* that can no longer be cancelled.
|
|
341
|
+
*/
|
|
342
|
+
export type SyncEvent = {
|
|
343
|
+
type: 'progress';
|
|
344
|
+
text: string;
|
|
345
|
+
percent?: number;
|
|
346
|
+
remote?: string;
|
|
347
|
+
} | {
|
|
348
|
+
type: 'merge';
|
|
349
|
+
};
|
|
350
|
+
/**
|
|
351
|
+
* One line of a streamed route response. The last line is always `done`,
|
|
352
|
+
* carrying the status and body the same route would have answered in one piece
|
|
353
|
+
* (the result, or a {@link RouteError}); a stream that ends without it was cut.
|
|
354
|
+
*/
|
|
355
|
+
export type SyncFrame = SyncEvent | {
|
|
356
|
+
type: 'done';
|
|
357
|
+
status: number;
|
|
358
|
+
body: unknown;
|
|
359
|
+
};
|
|
231
360
|
/** Error envelope every non-2xx response carries. */
|
|
232
361
|
export interface RouteError {
|
|
233
362
|
error: string;
|
package/lib/wire.js
CHANGED
|
@@ -11,6 +11,9 @@ export const ROUTE_STATUS = `${ROUTE_PREFIX}/status`;
|
|
|
11
11
|
/** GET ROUTE_PREFIX/head?path=<absolute dir> — checked-out branch only.
|
|
12
12
|
* A file read of HEAD, not a git process. */
|
|
13
13
|
export const ROUTE_HEAD = `${ROUTE_PREFIX}/head`;
|
|
14
|
+
/** Read-only POSTs: bounded history and one commit's full message/summary. */
|
|
15
|
+
export const ROUTE_HISTORY = `${ROUTE_PREFIX}/history`;
|
|
16
|
+
export const ROUTE_COMMIT = `${ROUTE_PREFIX}/commit`;
|
|
14
17
|
/** POST ROUTE_PREFIX/worktree — create-or-reuse a worktree for a branch. */
|
|
15
18
|
export const ROUTE_WORKTREE = `${ROUTE_PREFIX}/worktree`;
|
|
16
19
|
/** POST ROUTE_PREFIX/switch — in-place branch switch of the main checkout. */
|
|
@@ -24,10 +27,21 @@ export const ROUTE_BRANCH_RENAME = `${ROUTE_PREFIX}/branch-rename`;
|
|
|
24
27
|
/** POST ROUTE_PREFIX/branch-delete — delete a LOCAL branch (`-d`, merged
|
|
25
28
|
* only; git refuses the rest). */
|
|
26
29
|
export const ROUTE_BRANCH_DELETE = `${ROUTE_PREFIX}/branch-delete`;
|
|
27
|
-
/** POST ROUTE_PREFIX/fetch — sync remote-tracking refs (fetch every remote + prune).
|
|
30
|
+
/** POST ROUTE_PREFIX/fetch — sync remote-tracking refs (fetch every remote + prune).
|
|
31
|
+
* Streams {@link SyncFrame} lines (NDJSON) so a long transfer can show progress. */
|
|
28
32
|
export const ROUTE_FETCH = `${ROUTE_PREFIX}/fetch`;
|
|
29
|
-
/** POST ROUTE_PREFIX/update — fast-forward the current branch to its upstream.
|
|
33
|
+
/** POST ROUTE_PREFIX/update — fast-forward the current branch to its upstream.
|
|
34
|
+
* Streams {@link SyncFrame} lines (NDJSON), like {@link ROUTE_FETCH}. */
|
|
30
35
|
export const ROUTE_UPDATE = `${ROUTE_PREFIX}/update`;
|
|
36
|
+
/** POST push-plan — resolve the selected branch's push destination. */
|
|
37
|
+
export const ROUTE_PUSH_PLAN = `${ROUTE_PREFIX}/push-plan`;
|
|
38
|
+
/** POST push-preview — inspect one explicit destination without pushing. */
|
|
39
|
+
export const ROUTE_PUSH_PREVIEW = `${ROUTE_PREFIX}/push-preview`;
|
|
40
|
+
/** POST push — push one explicit local branch. Streams {@link SyncFrame}
|
|
41
|
+
* lines (NDJSON), like {@link ROUTE_FETCH}; the `done` body is a
|
|
42
|
+
* {@link PushResult}. A cancel (499) kills `git push`; the remote only moves
|
|
43
|
+
* a ref once the whole pack has arrived, so a stopped transfer changes nothing. */
|
|
44
|
+
export const ROUTE_PUSH = `${ROUTE_PREFIX}/push`;
|
|
31
45
|
/** POST ROUTE_PREFIX/group — git belonging facts for a batch of workspace paths. */
|
|
32
46
|
export const ROUTE_GROUP = `${ROUTE_PREFIX}/group`;
|
|
33
47
|
/** POST ROUTE_PREFIX/inspect — pre-delete facts for one worktree directory. */
|
|
@@ -44,3 +58,5 @@ export const ROUTE_WORKTREES_ALL = `${ROUTE_PREFIX}/worktrees-all`;
|
|
|
44
58
|
/** POST ROUTE_PREFIX/purge — delete a NON-git directory sitting DIRECTLY in
|
|
45
59
|
* the storage root (an orphaned leftover whose .git is already gone). */
|
|
46
60
|
export const ROUTE_PURGE = `${ROUTE_PREFIX}/purge`;
|
|
61
|
+
/** Content type of a streamed route: one JSON {@link SyncFrame} per line. */
|
|
62
|
+
export const NDJSON_TYPE = 'application/x-ndjson';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@laoyuehanni/dsh-git-worktree",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.10",
|
|
4
4
|
"description": "DSH plugin: branch visibility and git worktree isolation for Web UI sessions",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"dsh",
|
|
@@ -65,6 +65,15 @@
|
|
|
65
65
|
]
|
|
66
66
|
}
|
|
67
67
|
},
|
|
68
|
+
"scripts": {
|
|
69
|
+
"build": "tsc -p tsconfig.json",
|
|
70
|
+
"build:client": "tsdown",
|
|
71
|
+
"build:all": "pnpm run build && pnpm run build:client",
|
|
72
|
+
"watch:client": "tsdown --watch",
|
|
73
|
+
"test": "vitest run",
|
|
74
|
+
"typecheck": "tsc -p tsconfig.client.json --noEmit",
|
|
75
|
+
"prepublishOnly": "pnpm run typecheck && pnpm test && pnpm run build:all"
|
|
76
|
+
},
|
|
68
77
|
"peerDependencies": {
|
|
69
78
|
"@deepseek-ai/cordis": "^4.0.4",
|
|
70
79
|
"@deepseek-ai/dsh-host-webserver": "^0.2.0-rc.1",
|
|
@@ -129,12 +138,5 @@
|
|
|
129
138
|
"engines": {
|
|
130
139
|
"node": ">=22.19"
|
|
131
140
|
},
|
|
132
|
-
"
|
|
133
|
-
|
|
134
|
-
"build:client": "tsdown",
|
|
135
|
-
"build:all": "pnpm run build && pnpm run build:client",
|
|
136
|
-
"watch:client": "tsdown --watch",
|
|
137
|
-
"test": "vitest run",
|
|
138
|
-
"typecheck": "tsc -p tsconfig.client.json --noEmit"
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
+
"packageManager": "pnpm@11.8.0"
|
|
142
|
+
}
|