@ftrdotdev/pi-tui 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +63 -0
- package/package.json +80 -0
- package/src/autocomplete-contract.test.ts +126 -0
- package/src/autocomplete-contract.ts +91 -0
- package/src/commands/git-execution.test.ts +69 -0
- package/src/commands/git-execution.ts +198 -0
- package/src/commands/git-status.test.ts +379 -0
- package/src/commands/git-status.ts +320 -0
- package/src/components/bordered-box.test.ts +136 -0
- package/src/components/bordered-box.ts +106 -0
- package/src/components/define-component.ts +33 -0
- package/src/components/index.ts +35 -0
- package/src/config.test.ts +469 -0
- package/src/config.ts +307 -0
- package/src/frame/box-frame.test.ts +44 -0
- package/src/frame/box-frame.ts +160 -0
- package/src/icons.ts +203 -0
- package/src/index.ts +359 -0
- package/src/runtime.ts +256 -0
- package/src/session-lifecycle.ts +41 -0
- package/src/state.test.ts +7 -0
- package/src/state.ts +90 -0
- package/src/telemetry.ts +301 -0
- package/src/ui/editor-tint.test.ts +70 -0
- package/src/ui/editor-tint.ts +78 -0
- package/src/ui/editor.ts +403 -0
- package/src/ui/footer.test.ts +350 -0
- package/src/ui/footer.ts +225 -0
- package/src/ui/header.test.ts +260 -0
- package/src/ui/header.ts +165 -0
- package/src/ui/preview.test.ts +639 -0
- package/src/ui/preview.ts +374 -0
- package/src/ui/render-helpers.ts +275 -0
- package/src/ui/settings-command.test.ts +218 -0
- package/src/ui/settings-command.ts +390 -0
- package/src/utils.ts +448 -0
- package/src/vim/editor-close-autocomplete.ts +26 -0
- package/src/vim/editor-host.test.ts +340 -0
- package/src/vim/editor-state.test.ts +385 -0
- package/src/vim/editor-state.ts +314 -0
- package/src/vim/mode-indicator.test.ts +27 -0
- package/src/vim/mode-indicator.ts +44 -0
- package/src/vim/selection-render.test.ts +77 -0
- package/src/vim/selection-render.ts +85 -0
- package/src/vim/text-model.test.ts +85 -0
- package/src/vim/text-model.ts +75 -0
- package/src/vim/vim-router.test.ts +50 -0
- package/src/vim/vim-router.ts +55 -0
- package/src/vim/word-motion.ts +96 -0
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
import { assert, it } from "@effect/vitest";
|
|
2
|
+
import { Duration, Effect, Layer } from "effect";
|
|
3
|
+
import type { StubEntry } from "./git-execution.ts";
|
|
4
|
+
import { GitExecutionService } from "./git-execution.ts";
|
|
5
|
+
import {
|
|
6
|
+
FileCounts,
|
|
7
|
+
GitCommitInfo,
|
|
8
|
+
GitStatus,
|
|
9
|
+
GitStatusService,
|
|
10
|
+
ParsedBranch,
|
|
11
|
+
parseBranchLine,
|
|
12
|
+
parseFileLines,
|
|
13
|
+
parseStashCount,
|
|
14
|
+
} from "./git-status.ts";
|
|
15
|
+
|
|
16
|
+
const PRIMARY_ARGS = ["status", "--porcelain=v1", "--branch", "--show-stash"];
|
|
17
|
+
|
|
18
|
+
const statusLayer = (entries: ReadonlyArray<StubEntry>, cap?: Duration.Input) =>
|
|
19
|
+
GitStatusService.layer.pipe(Layer.provideMerge(GitExecutionService.layerStub(entries, cap)));
|
|
20
|
+
|
|
21
|
+
const runStatus = (
|
|
22
|
+
entries: ReadonlyArray<StubEntry>,
|
|
23
|
+
options?: { readTag?: boolean },
|
|
24
|
+
cap?: Duration.Input,
|
|
25
|
+
) =>
|
|
26
|
+
Effect.gen(function* () {
|
|
27
|
+
const svc = yield* GitStatusService;
|
|
28
|
+
return yield* svc.read("/cwd", options);
|
|
29
|
+
}).pipe(Effect.provide(statusLayer(entries, cap)));
|
|
30
|
+
|
|
31
|
+
it("GitStatus.empty returns a fresh all-zero record", () => {
|
|
32
|
+
const a = GitStatus.empty();
|
|
33
|
+
const b = GitStatus.empty();
|
|
34
|
+
assert.deepStrictEqual(
|
|
35
|
+
a,
|
|
36
|
+
GitStatus.make({
|
|
37
|
+
branch: undefined,
|
|
38
|
+
ahead: 0,
|
|
39
|
+
behind: 0,
|
|
40
|
+
modified: 0,
|
|
41
|
+
untracked: 0,
|
|
42
|
+
staged: 0,
|
|
43
|
+
stashed: 0,
|
|
44
|
+
conflicted: 0,
|
|
45
|
+
renamed: 0,
|
|
46
|
+
deleted: 0,
|
|
47
|
+
commit: null,
|
|
48
|
+
}),
|
|
49
|
+
);
|
|
50
|
+
assert.notStrictEqual(a, b);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("parseBranchLine parses a clean branch line", () => {
|
|
54
|
+
assert.deepStrictEqual(
|
|
55
|
+
parseBranchLine("## main"),
|
|
56
|
+
ParsedBranch.make({ detached: false, name: "main", ahead: 0, behind: 0 }),
|
|
57
|
+
);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("parseBranchLine parses an ahead-only divergence", () => {
|
|
61
|
+
assert.deepStrictEqual(
|
|
62
|
+
parseBranchLine("## feature...main [ahead 2]"),
|
|
63
|
+
ParsedBranch.make({ detached: false, name: "feature", ahead: 2, behind: 0 }),
|
|
64
|
+
);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("parseBranchLine parses a behind-only divergence", () => {
|
|
68
|
+
assert.deepStrictEqual(
|
|
69
|
+
parseBranchLine("## feature...main [behind 1]"),
|
|
70
|
+
ParsedBranch.make({ detached: false, name: "feature", ahead: 0, behind: 1 }),
|
|
71
|
+
);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("parseBranchLine parses the combined diverged form", () => {
|
|
75
|
+
assert.deepStrictEqual(
|
|
76
|
+
parseBranchLine("## feature...main [ahead 1, behind 4]"),
|
|
77
|
+
ParsedBranch.make({ detached: false, name: "feature", ahead: 1, behind: 4 }),
|
|
78
|
+
);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("parseBranchLine parses an upstream reference without divergence", () => {
|
|
82
|
+
assert.deepStrictEqual(
|
|
83
|
+
parseBranchLine("## feature...origin/feature"),
|
|
84
|
+
ParsedBranch.make({ detached: false, name: "feature", ahead: 0, behind: 0 }),
|
|
85
|
+
);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("parseBranchLine detects detached HEAD", () => {
|
|
89
|
+
assert.deepStrictEqual(
|
|
90
|
+
parseBranchLine("## HEAD (no branch)"),
|
|
91
|
+
ParsedBranch.make({ detached: true, name: undefined, ahead: 0, behind: 0 }),
|
|
92
|
+
);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("parseFileLines counts worktree-modified lines", () => {
|
|
96
|
+
assert.deepStrictEqual(
|
|
97
|
+
parseFileLines([" M a.txt", "MM b.txt"]),
|
|
98
|
+
FileCounts.make({ ...FileCounts.empty(), modified: 2, staged: 1 }),
|
|
99
|
+
);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("parseFileLines counts staged additions and staged deletions", () => {
|
|
103
|
+
assert.deepStrictEqual(
|
|
104
|
+
parseFileLines(["A s.txt", "D e.txt"]),
|
|
105
|
+
FileCounts.make({ ...FileCounts.empty(), staged: 1, deleted: 1 }),
|
|
106
|
+
);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("parseFileLines counts untracked and renamed files", () => {
|
|
110
|
+
assert.deepStrictEqual(
|
|
111
|
+
parseFileLines(["?? u.txt", "R d.txt -> renamed.txt"]),
|
|
112
|
+
FileCounts.make({ ...FileCounts.empty(), untracked: 1, renamed: 1 }),
|
|
113
|
+
);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("parseFileLines counts conflicted files (UU)", () => {
|
|
117
|
+
assert.deepStrictEqual(
|
|
118
|
+
parseFileLines(["UU c.txt"]),
|
|
119
|
+
FileCounts.make({ ...FileCounts.empty(), conflicted: 1 }),
|
|
120
|
+
);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("parseFileLines counts the full captured matrix", () => {
|
|
124
|
+
assert.deepStrictEqual(
|
|
125
|
+
parseFileLines([
|
|
126
|
+
" M a.txt",
|
|
127
|
+
"MM a.txt",
|
|
128
|
+
"A s.txt",
|
|
129
|
+
"D e.txt",
|
|
130
|
+
"R d.txt -> renamed.txt",
|
|
131
|
+
"UU c.txt",
|
|
132
|
+
"?? d.txt",
|
|
133
|
+
]),
|
|
134
|
+
FileCounts.make({
|
|
135
|
+
...FileCounts.empty(),
|
|
136
|
+
modified: 2,
|
|
137
|
+
staged: 2,
|
|
138
|
+
deleted: 1,
|
|
139
|
+
renamed: 1,
|
|
140
|
+
conflicted: 1,
|
|
141
|
+
untracked: 1,
|
|
142
|
+
}),
|
|
143
|
+
);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("parseFileLines ignores the branch header and empty lines", () => {
|
|
147
|
+
assert.deepStrictEqual(
|
|
148
|
+
parseFileLines(["## main", "", " M a.txt"]),
|
|
149
|
+
FileCounts.make({ ...FileCounts.empty(), modified: 1 }),
|
|
150
|
+
);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("parseFileLines counts a worktree deletion as deleted, not modified", () => {
|
|
154
|
+
assert.deepStrictEqual(
|
|
155
|
+
parseFileLines([" D d.txt"]),
|
|
156
|
+
FileCounts.make({ ...FileCounts.empty(), deleted: 1 }),
|
|
157
|
+
);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("parseStashCount counts stash list entries", () => {
|
|
161
|
+
assert.strictEqual(
|
|
162
|
+
parseStashCount("stash@{0}: On main: s1\nstash@{1}: WIP on feature: xyz\n"),
|
|
163
|
+
2,
|
|
164
|
+
);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("parseStashCount returns 0 for an empty stash", () => {
|
|
168
|
+
assert.strictEqual(parseStashCount(""), 0);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it.effect("read returns a full status for a clean repository", () =>
|
|
172
|
+
Effect.gen(function* () {
|
|
173
|
+
const result = yield* runStatus([
|
|
174
|
+
{ command: "git", args: PRIMARY_ARGS, stdout: "## main\n", exitCode: 0 },
|
|
175
|
+
{ command: "git", args: ["stash", "list"], stdout: "", exitCode: 0 },
|
|
176
|
+
]);
|
|
177
|
+
assert.deepStrictEqual(result, GitStatus.make({ ...GitStatus.empty(), branch: "main" }));
|
|
178
|
+
}),
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
it.effect("read parses a diverged branch end-to-end", () =>
|
|
182
|
+
Effect.gen(function* () {
|
|
183
|
+
const result = yield* runStatus([
|
|
184
|
+
{
|
|
185
|
+
command: "git",
|
|
186
|
+
args: PRIMARY_ARGS,
|
|
187
|
+
stdout: "## feature...main [ahead 1, behind 4]\n",
|
|
188
|
+
exitCode: 0,
|
|
189
|
+
},
|
|
190
|
+
{ command: "git", args: ["stash", "list"], stdout: "", exitCode: 0 },
|
|
191
|
+
]);
|
|
192
|
+
assert.strictEqual(result.branch, "feature");
|
|
193
|
+
assert.strictEqual(result.ahead, 1);
|
|
194
|
+
assert.strictEqual(result.behind, 4);
|
|
195
|
+
}),
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
it.effect("read counts working-tree changes from captured real output", () =>
|
|
199
|
+
Effect.gen(function* () {
|
|
200
|
+
const result = yield* runStatus([
|
|
201
|
+
{
|
|
202
|
+
command: "git",
|
|
203
|
+
args: PRIMARY_ARGS,
|
|
204
|
+
stdout:
|
|
205
|
+
"## feature...main [ahead 2, behind 1]\n M a.txt\nMM a.txt\nA s.txt\nD e.txt\nR d.txt -> renamed.txt\nUU c.txt\n?? d.txt\n",
|
|
206
|
+
exitCode: 0,
|
|
207
|
+
},
|
|
208
|
+
{ command: "git", args: ["stash", "list"], stdout: "", exitCode: 0 },
|
|
209
|
+
]);
|
|
210
|
+
assert.strictEqual(result.modified, 2);
|
|
211
|
+
assert.strictEqual(result.staged, 2);
|
|
212
|
+
assert.strictEqual(result.deleted, 1);
|
|
213
|
+
assert.strictEqual(result.renamed, 1);
|
|
214
|
+
assert.strictEqual(result.conflicted, 1);
|
|
215
|
+
assert.strictEqual(result.untracked, 1);
|
|
216
|
+
assert.strictEqual(result.ahead, 2);
|
|
217
|
+
assert.strictEqual(result.behind, 1);
|
|
218
|
+
}),
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
it.effect("read parses the stash count from a separate invocation", () =>
|
|
222
|
+
Effect.gen(function* () {
|
|
223
|
+
const result = yield* runStatus([
|
|
224
|
+
{ command: "git", args: PRIMARY_ARGS, stdout: "## main\n", exitCode: 0 },
|
|
225
|
+
{
|
|
226
|
+
command: "git",
|
|
227
|
+
args: ["stash", "list"],
|
|
228
|
+
stdout: "stash@{0}: On main: s1\nstash@{1}: WIP on feature: xyz\n",
|
|
229
|
+
exitCode: 0,
|
|
230
|
+
},
|
|
231
|
+
]);
|
|
232
|
+
assert.strictEqual(result.stashed, 2);
|
|
233
|
+
}),
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
it.effect("read degrades the stash field to 0 when the stash invocation fails", () =>
|
|
237
|
+
Effect.gen(function* () {
|
|
238
|
+
const result = yield* runStatus([
|
|
239
|
+
{ command: "git", args: PRIMARY_ARGS, stdout: "## main\n", exitCode: 0 },
|
|
240
|
+
]);
|
|
241
|
+
assert.strictEqual(result.stashed, 0);
|
|
242
|
+
}),
|
|
243
|
+
);
|
|
244
|
+
|
|
245
|
+
it.effect("read returns the silent empty status for a non-repository directory", () =>
|
|
246
|
+
Effect.gen(function* () {
|
|
247
|
+
const result = yield* runStatus([
|
|
248
|
+
{ command: "git", args: PRIMARY_ARGS, stdout: "", exitCode: 128 },
|
|
249
|
+
{ command: "git", args: ["stash", "list"], stdout: "", exitCode: 0 },
|
|
250
|
+
]);
|
|
251
|
+
assert.deepStrictEqual(result, GitStatus.empty());
|
|
252
|
+
}),
|
|
253
|
+
);
|
|
254
|
+
|
|
255
|
+
it.effect("read fails with GitStatusError on non-zero exit with non-empty stdout", () =>
|
|
256
|
+
Effect.gen(function* () {
|
|
257
|
+
const tag = yield* runStatus([
|
|
258
|
+
{ command: "git", args: PRIMARY_ARGS, stdout: "unexpected output\n", exitCode: 128 },
|
|
259
|
+
{ command: "git", args: ["stash", "list"], stdout: "", exitCode: 0 },
|
|
260
|
+
]).pipe(
|
|
261
|
+
Effect.match({
|
|
262
|
+
onFailure: (err) => err._tag,
|
|
263
|
+
onSuccess: () => "unexpected-success",
|
|
264
|
+
}),
|
|
265
|
+
);
|
|
266
|
+
assert.strictEqual(tag, "GitStatusError");
|
|
267
|
+
}),
|
|
268
|
+
);
|
|
269
|
+
|
|
270
|
+
it.effect("read propagates primary spawn failures", () =>
|
|
271
|
+
Effect.gen(function* () {
|
|
272
|
+
const tag = yield* runStatus([
|
|
273
|
+
{ command: "git", args: ["stash", "list"], stdout: "", exitCode: 0 },
|
|
274
|
+
]).pipe(
|
|
275
|
+
Effect.match({
|
|
276
|
+
onFailure: (err) => err._tag,
|
|
277
|
+
onSuccess: () => "unexpected-success",
|
|
278
|
+
}),
|
|
279
|
+
);
|
|
280
|
+
assert.strictEqual(tag, "GitSpawnError");
|
|
281
|
+
}),
|
|
282
|
+
);
|
|
283
|
+
|
|
284
|
+
it.live("read fails with GitTimeoutError when the primary invocation hangs past the cap", () =>
|
|
285
|
+
Effect.gen(function* () {
|
|
286
|
+
const tag = yield* runStatus(
|
|
287
|
+
[{ command: "git", args: PRIMARY_ARGS, neverCompletes: true }],
|
|
288
|
+
undefined,
|
|
289
|
+
"10 millis",
|
|
290
|
+
).pipe(
|
|
291
|
+
Effect.match({
|
|
292
|
+
onFailure: (err) => err._tag,
|
|
293
|
+
onSuccess: () => "unexpected-success",
|
|
294
|
+
}),
|
|
295
|
+
);
|
|
296
|
+
assert.strictEqual(tag, "GitTimeoutError");
|
|
297
|
+
}),
|
|
298
|
+
);
|
|
299
|
+
|
|
300
|
+
it.effect("read reads the detached commit id and skips the tag when readTag is off", () =>
|
|
301
|
+
Effect.gen(function* () {
|
|
302
|
+
const result = yield* runStatus([
|
|
303
|
+
{ command: "git", args: PRIMARY_ARGS, stdout: "## HEAD (no branch)\n", exitCode: 0 },
|
|
304
|
+
{ command: "git", args: ["stash", "list"], stdout: "", exitCode: 0 },
|
|
305
|
+
{
|
|
306
|
+
command: "git",
|
|
307
|
+
args: ["rev-parse", "HEAD"],
|
|
308
|
+
stdout: "0502eea0d67bf502c9f910af4c61fbb5c89d474c\n",
|
|
309
|
+
exitCode: 0,
|
|
310
|
+
},
|
|
311
|
+
]);
|
|
312
|
+
assert.deepStrictEqual(
|
|
313
|
+
result.commit,
|
|
314
|
+
GitCommitInfo.make({
|
|
315
|
+
oid: "0502eea0d67bf502c9f910af4c61fbb5c89d474c",
|
|
316
|
+
detached: true,
|
|
317
|
+
tag: null,
|
|
318
|
+
}),
|
|
319
|
+
);
|
|
320
|
+
}),
|
|
321
|
+
);
|
|
322
|
+
|
|
323
|
+
it.effect("read reads the detached tag when readTag is on", () =>
|
|
324
|
+
Effect.gen(function* () {
|
|
325
|
+
const result = yield* runStatus(
|
|
326
|
+
[
|
|
327
|
+
{ command: "git", args: PRIMARY_ARGS, stdout: "## HEAD (no branch)\n", exitCode: 0 },
|
|
328
|
+
{ command: "git", args: ["stash", "list"], stdout: "", exitCode: 0 },
|
|
329
|
+
{
|
|
330
|
+
command: "git",
|
|
331
|
+
args: ["rev-parse", "HEAD"],
|
|
332
|
+
stdout: "0502eea0d67bf502c9f910af4c61fbb5c89d474c\n",
|
|
333
|
+
exitCode: 0,
|
|
334
|
+
},
|
|
335
|
+
{
|
|
336
|
+
command: "git",
|
|
337
|
+
args: ["describe", "--tags", "--exact-match", "HEAD"],
|
|
338
|
+
stdout: "v2\n",
|
|
339
|
+
exitCode: 0,
|
|
340
|
+
},
|
|
341
|
+
],
|
|
342
|
+
{ readTag: true },
|
|
343
|
+
);
|
|
344
|
+
assert.strictEqual(result.commit?.tag, "v2");
|
|
345
|
+
}),
|
|
346
|
+
);
|
|
347
|
+
|
|
348
|
+
it.effect("read degrades the detached id to null when rev-parse fails", () =>
|
|
349
|
+
Effect.gen(function* () {
|
|
350
|
+
const result = yield* runStatus([
|
|
351
|
+
{ command: "git", args: PRIMARY_ARGS, stdout: "## HEAD (no branch)\n", exitCode: 0 },
|
|
352
|
+
{ command: "git", args: ["stash", "list"], stdout: "", exitCode: 0 },
|
|
353
|
+
]);
|
|
354
|
+
assert.deepStrictEqual(
|
|
355
|
+
result.commit,
|
|
356
|
+
GitCommitInfo.make({ oid: null, detached: true, tag: null }),
|
|
357
|
+
);
|
|
358
|
+
}),
|
|
359
|
+
);
|
|
360
|
+
|
|
361
|
+
it.effect("read degrades the detached tag to null when describe fails", () =>
|
|
362
|
+
Effect.gen(function* () {
|
|
363
|
+
const result = yield* runStatus(
|
|
364
|
+
[
|
|
365
|
+
{ command: "git", args: PRIMARY_ARGS, stdout: "## HEAD (no branch)\n", exitCode: 0 },
|
|
366
|
+
{ command: "git", args: ["stash", "list"], stdout: "", exitCode: 0 },
|
|
367
|
+
{
|
|
368
|
+
command: "git",
|
|
369
|
+
args: ["rev-parse", "HEAD"],
|
|
370
|
+
stdout: "0502eea0d67bf502c9f910af4c61fbb5c89d474c\n",
|
|
371
|
+
exitCode: 0,
|
|
372
|
+
},
|
|
373
|
+
],
|
|
374
|
+
{ readTag: true },
|
|
375
|
+
);
|
|
376
|
+
assert.strictEqual(result.commit?.tag, null);
|
|
377
|
+
assert.strictEqual(result.commit?.oid, "0502eea0d67bf502c9f910af4c61fbb5c89d474c");
|
|
378
|
+
}),
|
|
379
|
+
);
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
import { Context, Effect, Layer, Match, Schema } from "effect";
|
|
2
|
+
import {
|
|
3
|
+
GitExecutionService,
|
|
4
|
+
type CommandResult,
|
|
5
|
+
type GitExecutionError,
|
|
6
|
+
} from "./git-execution.ts";
|
|
7
|
+
|
|
8
|
+
// ---------------------------------------------------------------------------
|
|
9
|
+
// Domain records
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Detached-HEAD commit information: the full oid, the detached flag, and the
|
|
14
|
+
* exact-matching tag when one exists (each degraded to `null` on read failure).
|
|
15
|
+
*/
|
|
16
|
+
export class GitCommitInfo extends Schema.Class<GitCommitInfo>("tui/git/GitCommitInfo")({
|
|
17
|
+
oid: Schema.NullOr(Schema.String),
|
|
18
|
+
detached: Schema.Boolean,
|
|
19
|
+
tag: Schema.NullOr(Schema.String),
|
|
20
|
+
}) {}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The git status record the footer consumes: branch, ahead/behind counts,
|
|
24
|
+
* working-tree counts, stash count, and detached-HEAD commit info.
|
|
25
|
+
*/
|
|
26
|
+
export class GitStatus extends Schema.Class<GitStatus>("tui/git/GitStatus")({
|
|
27
|
+
branch: Schema.optional(Schema.String),
|
|
28
|
+
ahead: Schema.Finite,
|
|
29
|
+
behind: Schema.Finite,
|
|
30
|
+
modified: Schema.Finite,
|
|
31
|
+
untracked: Schema.Finite,
|
|
32
|
+
staged: Schema.Finite,
|
|
33
|
+
stashed: Schema.Finite,
|
|
34
|
+
conflicted: Schema.Finite,
|
|
35
|
+
renamed: Schema.Finite,
|
|
36
|
+
deleted: Schema.Finite,
|
|
37
|
+
commit: Schema.NullOr(GitCommitInfo),
|
|
38
|
+
}) {
|
|
39
|
+
/** A fresh all-zero status: no branch, no changes, no commit info. */
|
|
40
|
+
static empty() {
|
|
41
|
+
return GitStatus.make({
|
|
42
|
+
branch: undefined,
|
|
43
|
+
ahead: 0,
|
|
44
|
+
behind: 0,
|
|
45
|
+
modified: 0,
|
|
46
|
+
untracked: 0,
|
|
47
|
+
staged: 0,
|
|
48
|
+
stashed: 0,
|
|
49
|
+
conflicted: 0,
|
|
50
|
+
renamed: 0,
|
|
51
|
+
deleted: 0,
|
|
52
|
+
commit: null,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** A parsed `## ...` branch header from porcelain v1 output. */
|
|
58
|
+
export class ParsedBranch extends Schema.Class<ParsedBranch>("tui/git/ParsedBranch")({
|
|
59
|
+
detached: Schema.Boolean,
|
|
60
|
+
name: Schema.optional(Schema.String), // undefined when detached
|
|
61
|
+
ahead: Schema.Finite,
|
|
62
|
+
behind: Schema.Finite,
|
|
63
|
+
}) {}
|
|
64
|
+
|
|
65
|
+
// Compiled once at module load; reused across calls (stateless, no `g` flag).
|
|
66
|
+
const BRANCH_NAME_PATTERN = /^(\S+?)(?:\.\.\.\S+)?(?:\s+\[.*\])?\s*$/;
|
|
67
|
+
const AHEAD_PATTERN = /\bahead (\d+)/;
|
|
68
|
+
const BEHIND_PATTERN = /\bbehind (\d+)/;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Parses a `## ...` branch header line: detached HEAD, branch name (with or
|
|
72
|
+
* without an `...upstream` reference), and ahead/behind divergence counts.
|
|
73
|
+
*/
|
|
74
|
+
export function parseBranchLine(line: string): ParsedBranch {
|
|
75
|
+
const branchPart = line.startsWith("## ") ? line.slice(3) : line;
|
|
76
|
+
if (branchPart.startsWith("HEAD (no branch)")) {
|
|
77
|
+
return ParsedBranch.make({ detached: true, name: undefined, ahead: 0, behind: 0 });
|
|
78
|
+
}
|
|
79
|
+
const nameMatch = BRANCH_NAME_PATTERN.exec(branchPart);
|
|
80
|
+
const aheadMatch = AHEAD_PATTERN.exec(branchPart);
|
|
81
|
+
const behindMatch = BEHIND_PATTERN.exec(branchPart);
|
|
82
|
+
return ParsedBranch.make({
|
|
83
|
+
detached: false,
|
|
84
|
+
name: nameMatch?.[1] ?? undefined,
|
|
85
|
+
ahead: aheadMatch ? Number(aheadMatch[1]) : 0,
|
|
86
|
+
behind: behindMatch ? Number(behindMatch[1]) : 0,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Mutable fields: `parseFileLines` counts into the instance, like the legacy
|
|
91
|
+
// counters did. All other records are built immutably via `make`/spread.
|
|
92
|
+
/**
|
|
93
|
+
* Porcelain v1 file-status counters with the footer's counting semantics:
|
|
94
|
+
* each status line increments one or two of the six buckets.
|
|
95
|
+
*/
|
|
96
|
+
export class FileCounts extends Schema.Class<FileCounts>("tui/git/FileCounts")({
|
|
97
|
+
modified: Schema.mutableKey(Schema.Finite),
|
|
98
|
+
untracked: Schema.mutableKey(Schema.Finite),
|
|
99
|
+
staged: Schema.mutableKey(Schema.Finite),
|
|
100
|
+
conflicted: Schema.mutableKey(Schema.Finite),
|
|
101
|
+
renamed: Schema.mutableKey(Schema.Finite),
|
|
102
|
+
deleted: Schema.mutableKey(Schema.Finite),
|
|
103
|
+
}) {
|
|
104
|
+
/** A fresh all-zero counts record. */
|
|
105
|
+
static empty() {
|
|
106
|
+
return FileCounts.make({
|
|
107
|
+
modified: 0,
|
|
108
|
+
untracked: 0,
|
|
109
|
+
staged: 0,
|
|
110
|
+
conflicted: 0,
|
|
111
|
+
renamed: 0,
|
|
112
|
+
deleted: 0,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Counts porcelain v1 file-status lines into per-field totals, skipping the
|
|
119
|
+
* `##` branch header and empty lines.
|
|
120
|
+
*/
|
|
121
|
+
export function parseFileLines(lines: ReadonlyArray<string>): FileCounts {
|
|
122
|
+
const counts = FileCounts.empty();
|
|
123
|
+
for (const line of lines) {
|
|
124
|
+
if (line.length < 3 || line.startsWith("#")) continue; // skip empty and header lines
|
|
125
|
+
const x = line[0]!;
|
|
126
|
+
const y = line[1]!;
|
|
127
|
+
if (x === "U" || y === "U" || (x === "C" && y === "C")) counts.conflicted++;
|
|
128
|
+
else if (x === "?" && y === "?") counts.untracked++;
|
|
129
|
+
else if (x === "R") counts.renamed++;
|
|
130
|
+
else if (x === "D" || y === "D") counts.deleted++;
|
|
131
|
+
else {
|
|
132
|
+
if (x !== " " && x !== "?") counts.staged++;
|
|
133
|
+
if (y === "M" || y === "D") counts.modified++;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return counts;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Counts `git stash list` entries by counting non-empty lines; empty output
|
|
141
|
+
* (no stashes) yields 0. The count contract only — subjects are not parsed.
|
|
142
|
+
*/
|
|
143
|
+
export function parseStashCount(output: string): number {
|
|
144
|
+
if (output.trim() === "") return 0;
|
|
145
|
+
return output.split("\n").filter((line) => line.trim() !== "").length;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// ---------------------------------------------------------------------------
|
|
149
|
+
// Status service
|
|
150
|
+
// ---------------------------------------------------------------------------
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* The primary status invocation exited non-zero with unexpected non-empty
|
|
154
|
+
* output. (A non-zero exit with empty stdout is the silent not-a-repository
|
|
155
|
+
* case and returns the empty status instead of this error.)
|
|
156
|
+
*/
|
|
157
|
+
export class GitStatusError extends Schema.TaggedError<GitStatusError>()("GitStatusError", {
|
|
158
|
+
cwd: Schema.String,
|
|
159
|
+
command: Schema.String,
|
|
160
|
+
exitCode: Schema.Int,
|
|
161
|
+
stdout: Schema.String,
|
|
162
|
+
}) {}
|
|
163
|
+
|
|
164
|
+
/** Error union surfaced by the status service to its callers. */
|
|
165
|
+
export type GitError = GitExecutionError | GitStatusError;
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Options for {@link GitStatusService.read}. `readTag` gates the detached-HEAD
|
|
169
|
+
* tag lookup (default off); the commit-id lookup always runs when detached.
|
|
170
|
+
*/
|
|
171
|
+
export interface GitStatusOptions {
|
|
172
|
+
readonly readTag?: boolean; // read the tag when the head is detached (default off)
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const PRIMARY_COMMAND = "git status --porcelain=v1 --branch --show-stash";
|
|
176
|
+
const PRIMARY_ARGS: ReadonlyArray<string> = [
|
|
177
|
+
"status",
|
|
178
|
+
"--porcelain=v1",
|
|
179
|
+
"--branch",
|
|
180
|
+
"--show-stash",
|
|
181
|
+
];
|
|
182
|
+
|
|
183
|
+
const TerminalOutcome = Schema.TaggedStruct("Terminal", { status: GitStatus });
|
|
184
|
+
const ContinueOutcome = Schema.TaggedStruct("Continue", { status: GitStatus });
|
|
185
|
+
/** Outcome of interpreting the primary invocation: stop, or continue with the secondary reads. */
|
|
186
|
+
const PrimaryOutcome = Schema.Union([TerminalOutcome, ContinueOutcome]);
|
|
187
|
+
type PrimaryOutcome = typeof PrimaryOutcome.Type;
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Owns the git domain logic on top of {@link GitExecutionService}: the four
|
|
191
|
+
* invocations, exit-code interpretation, per-field degradation, and the
|
|
192
|
+
* {@link GitStatus} record the footer consumes.
|
|
193
|
+
*/
|
|
194
|
+
export class GitStatusService extends Context.Service<
|
|
195
|
+
GitStatusService,
|
|
196
|
+
{
|
|
197
|
+
/**
|
|
198
|
+
* Reads the git status for `cwd`: primary status invocation, unconditional
|
|
199
|
+
* stash count, then (when detached) the commit id and optionally the tag.
|
|
200
|
+
* Primary failures surface as {@link GitError}; secondary-read failures
|
|
201
|
+
* degrade their own field instead of aborting.
|
|
202
|
+
*/
|
|
203
|
+
readonly read: (
|
|
204
|
+
cwd: string,
|
|
205
|
+
options?: GitStatusOptions,
|
|
206
|
+
) => Effect.Effect<GitStatus, GitError, GitExecutionService>;
|
|
207
|
+
}
|
|
208
|
+
>()("tui/git/GitStatusService") {
|
|
209
|
+
/** The status service over the execution service. */
|
|
210
|
+
static readonly layer = Layer.effect(
|
|
211
|
+
GitStatusService,
|
|
212
|
+
Effect.gen(function* () {
|
|
213
|
+
const exec = yield* GitExecutionService;
|
|
214
|
+
// One git invocation through the execution service.
|
|
215
|
+
const git = Effect.fn("GitStatusService.runGit")(function* (
|
|
216
|
+
args: ReadonlyArray<string>,
|
|
217
|
+
cwd: string,
|
|
218
|
+
) {
|
|
219
|
+
return yield* exec.run("git", args, cwd);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
// Exit-code interpretation boundary plus branch/file parsing.
|
|
223
|
+
const interpretPrimary = Effect.fn("GitStatusService.interpretPrimary")(function* (
|
|
224
|
+
primary: CommandResult,
|
|
225
|
+
cwd: string,
|
|
226
|
+
): Effect.fn.Return<PrimaryOutcome, GitStatusError> {
|
|
227
|
+
if (primary.exitCode !== 0) {
|
|
228
|
+
if (primary.stdout.trim() === "") {
|
|
229
|
+
// Not a repository / no output: silent empty status.
|
|
230
|
+
return TerminalOutcome.make({ status: GitStatus.empty() });
|
|
231
|
+
}
|
|
232
|
+
return yield* new GitStatusError({
|
|
233
|
+
cwd,
|
|
234
|
+
command: PRIMARY_COMMAND,
|
|
235
|
+
exitCode: primary.exitCode,
|
|
236
|
+
stdout: primary.stdout,
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
let status = GitStatus.empty();
|
|
241
|
+
const lines = primary.stdout.split("\n");
|
|
242
|
+
const branchLine = lines.find((line) => line.startsWith("## "));
|
|
243
|
+
if (branchLine) {
|
|
244
|
+
const parsed = parseBranchLine(branchLine);
|
|
245
|
+
if (parsed.detached) {
|
|
246
|
+
status = GitStatus.make({
|
|
247
|
+
...status,
|
|
248
|
+
commit: GitCommitInfo.make({ oid: null, detached: true, tag: null }),
|
|
249
|
+
});
|
|
250
|
+
} else {
|
|
251
|
+
status = GitStatus.make({
|
|
252
|
+
...status,
|
|
253
|
+
branch: parsed.name,
|
|
254
|
+
ahead: parsed.ahead,
|
|
255
|
+
behind: parsed.behind,
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
const counts = parseFileLines(lines);
|
|
260
|
+
return ContinueOutcome.make({
|
|
261
|
+
status: GitStatus.make({
|
|
262
|
+
...status,
|
|
263
|
+
...counts,
|
|
264
|
+
}),
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
// Secondary reads: each degrades its own field on any failure.
|
|
269
|
+
const readStash = Effect.fn("GitStatusService.readStash")(function* (cwd: string) {
|
|
270
|
+
const result = yield* git(["stash", "list"], cwd).pipe(
|
|
271
|
+
Effect.orElseSucceed(() => ({ stdout: "", exitCode: 0 })),
|
|
272
|
+
);
|
|
273
|
+
return parseStashCount(result.stdout);
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
const readCommitOid = Effect.fn("GitStatusService.readCommitOid")(function* (cwd: string) {
|
|
277
|
+
const result = yield* git(["rev-parse", "HEAD"], cwd).pipe(
|
|
278
|
+
Effect.orElseSucceed(() => ({ stdout: "", exitCode: 0 })),
|
|
279
|
+
);
|
|
280
|
+
return result.stdout.trim() || null;
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
const readCommitTag = Effect.fn("GitStatusService.readCommitTag")(function* (cwd: string) {
|
|
284
|
+
const result = yield* git(["describe", "--tags", "--exact-match", "HEAD"], cwd).pipe(
|
|
285
|
+
Effect.orElseSucceed(() => ({ stdout: "", exitCode: 0 })),
|
|
286
|
+
);
|
|
287
|
+
return result.stdout.trim() || null;
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
const read = Effect.fn("GitStatusService.read")(function* (
|
|
291
|
+
cwd: string,
|
|
292
|
+
options: GitStatusOptions = {},
|
|
293
|
+
) {
|
|
294
|
+
const primary = yield* git(PRIMARY_ARGS, cwd);
|
|
295
|
+
const outcome = yield* interpretPrimary(primary, cwd);
|
|
296
|
+
return yield* Match.value(outcome).pipe(
|
|
297
|
+
Match.tags({
|
|
298
|
+
Continue: () =>
|
|
299
|
+
Effect.gen(function* () {
|
|
300
|
+
const stashed = yield* readStash(cwd);
|
|
301
|
+
const base = GitStatus.make({ ...outcome.status, stashed });
|
|
302
|
+
if (!base.commit?.detached) return base;
|
|
303
|
+
|
|
304
|
+
const oid = yield* readCommitOid(cwd);
|
|
305
|
+
const tag = options.readTag === true ? yield* readCommitTag(cwd) : null;
|
|
306
|
+
return GitStatus.make({
|
|
307
|
+
...base,
|
|
308
|
+
commit: GitCommitInfo.make({ oid, detached: true, tag }),
|
|
309
|
+
});
|
|
310
|
+
}),
|
|
311
|
+
Terminal: () => Effect.succeed(outcome.status),
|
|
312
|
+
}),
|
|
313
|
+
Match.exhaustive,
|
|
314
|
+
);
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
return GitStatusService.of({ read });
|
|
318
|
+
}),
|
|
319
|
+
);
|
|
320
|
+
}
|