@myrialabs/clopen 0.4.36 → 0.4.38

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.
Files changed (115) hide show
  1. package/README.md +2 -2
  2. package/backend/auth/permissions.ts +14 -0
  3. package/backend/bootstrap.ts +16 -0
  4. package/backend/database/crypto/envelope.test.ts +93 -0
  5. package/backend/database/crypto/envelope.ts +127 -0
  6. package/backend/database/crypto/index.ts +22 -0
  7. package/backend/database/crypto/master-key.ts +129 -0
  8. package/backend/database/crypto/secret-columns.ts +147 -0
  9. package/backend/database/migrations/071_create_notes_tables.ts +90 -0
  10. package/backend/database/migrations/072_encrypt_stored_secrets.ts +107 -0
  11. package/backend/database/migrations/073_create_integration_tables.ts +119 -0
  12. package/backend/database/migrations/074_create_work_tables.ts +94 -0
  13. package/backend/database/migrations/index.ts +28 -0
  14. package/backend/database/queries/db-client-connection-queries.ts +23 -12
  15. package/backend/database/queries/engine-queries.ts +23 -10
  16. package/backend/database/queries/index.ts +6 -1
  17. package/backend/database/queries/integration-queries.ts +297 -0
  18. package/backend/database/queries/mcp-server-queries.ts +38 -11
  19. package/backend/database/queries/note-queries.test.ts +190 -0
  20. package/backend/database/queries/note-queries.ts +269 -0
  21. package/backend/database/queries/profile-queries.ts +1 -1
  22. package/backend/database/queries/ssh-connection-queries.ts +19 -8
  23. package/backend/database/queries/work-queries.ts +210 -0
  24. package/backend/engine/docs/artifacts.md +22 -9
  25. package/backend/files/os-clipboard.test.ts +209 -0
  26. package/backend/files/os-clipboard.ts +567 -0
  27. package/backend/git/conflict-state.test.ts +396 -0
  28. package/backend/git/git-executor.ts +6 -0
  29. package/backend/git/git-parser.ts +144 -48
  30. package/backend/git/git-service.ts +727 -57
  31. package/backend/git/push-target.test.ts +264 -0
  32. package/backend/http/integration-hooks.ts +42 -0
  33. package/backend/http/notes-images.test.ts +228 -0
  34. package/backend/http/notes-images.ts +196 -0
  35. package/backend/index.ts +9 -0
  36. package/backend/integrations/accounts.ts +229 -0
  37. package/backend/integrations/health.ts +146 -0
  38. package/backend/integrations/index.ts +36 -0
  39. package/backend/integrations/projections/index.ts +120 -0
  40. package/backend/integrations/projections/mcp-projector.test.ts +204 -0
  41. package/backend/integrations/projections/mcp-projector.ts +159 -0
  42. package/backend/integrations/projections/types.ts +51 -0
  43. package/backend/integrations/registry/define.ts +38 -0
  44. package/backend/integrations/registry/index.ts +56 -0
  45. package/backend/integrations/registry/providers/context7.ts +44 -0
  46. package/backend/integrations/registry/providers/firecrawl.ts +41 -0
  47. package/backend/integrations/registry/providers/github.ts +78 -0
  48. package/backend/integrations/registry/types.ts +96 -0
  49. package/backend/integrations/webhooks/gateway.ts +160 -0
  50. package/backend/integrations/webhooks/verify.ts +65 -0
  51. package/backend/mcp/README.md +25 -3
  52. package/backend/mcp/external/config.ts +35 -1
  53. package/backend/mcp/index.ts +1 -1
  54. package/backend/mcp/internal/config.ts +1 -1
  55. package/backend/notes/access.test.ts +82 -0
  56. package/backend/notes/access.ts +27 -0
  57. package/backend/notes/storage.test.ts +77 -0
  58. package/backend/notes/storage.ts +114 -0
  59. package/backend/permissions/service.ts +2 -2
  60. package/backend/profiles/service.ts +1 -1
  61. package/backend/project/status-manager.ts +20 -0
  62. package/backend/projects/project-cleanup.ts +6 -0
  63. package/backend/work/bindings.ts +124 -0
  64. package/backend/work/index.ts +42 -0
  65. package/backend/work/prompts.ts +150 -0
  66. package/backend/work/providers/github/actions-logs.ts +180 -0
  67. package/backend/work/providers/github/adapter.test.ts +88 -0
  68. package/backend/work/providers/github/adapter.ts +1255 -0
  69. package/backend/work/providers/github/client.ts +280 -0
  70. package/backend/work/providers/github/list-range.test.ts +158 -0
  71. package/backend/work/providers/github/token-kind.test.ts +42 -0
  72. package/backend/work/pull-requests.ts +242 -0
  73. package/backend/work/registry.ts +27 -0
  74. package/backend/work/service.ts +436 -0
  75. package/backend/work/start-work.test.ts +71 -0
  76. package/backend/work/start-work.ts +232 -0
  77. package/backend/work/types.ts +180 -0
  78. package/backend/ws/files/clipboard.ts +78 -0
  79. package/backend/ws/files/index.ts +5 -0
  80. package/backend/ws/git/branch-name.ts +6 -5
  81. package/backend/ws/git/branch.ts +43 -1
  82. package/backend/ws/git/commit-message.ts +13 -10
  83. package/backend/ws/git/conflict.ts +106 -3
  84. package/backend/ws/git/diff-budget.test.ts +125 -0
  85. package/backend/ws/git/diff-budget.ts +279 -0
  86. package/backend/ws/git/log.ts +25 -0
  87. package/backend/ws/git/remote.ts +75 -1
  88. package/backend/ws/index.ts +11 -1
  89. package/backend/ws/integrations/crud.ts +219 -0
  90. package/backend/ws/integrations/index.ts +22 -0
  91. package/backend/ws/mcp/crud.ts +25 -1
  92. package/backend/ws/notes/index.ts +278 -0
  93. package/backend/ws/profiles/crud.ts +1 -1
  94. package/backend/ws/work/actions.ts +206 -0
  95. package/backend/ws/work/context.ts +51 -0
  96. package/backend/ws/work/crud.ts +347 -0
  97. package/backend/ws/work/index.ts +17 -0
  98. package/backend/ws/work/pull-requests.ts +177 -0
  99. package/backend/ws/work/schemas.ts +226 -0
  100. package/dist/assets/{index-Bg-SUCFg.js → index-CyH7fSbR.js} +1 -1
  101. package/dist/assets/{index-Cz4YpHVi.js → index-DSHIANSy.js} +1 -1
  102. package/dist/assets/main-D2IOjftb.css +1 -0
  103. package/dist/assets/main-DM-tHKbW.js +1152 -0
  104. package/dist/index.html +2 -2
  105. package/package.json +1 -1
  106. package/shared/constants/engine-tools.ts +1 -1
  107. package/shared/constants/tool-icons.ts +33 -0
  108. package/shared/types/database/schema.ts +41 -0
  109. package/shared/types/git.ts +123 -0
  110. package/shared/types/integrations.ts +140 -0
  111. package/shared/types/stores/settings.ts +8 -0
  112. package/shared/types/work.ts +426 -0
  113. package/shared/utils/logger.ts +3 -0
  114. package/dist/assets/main-9G7IpQpK.css +0 -1
  115. package/dist/assets/main-BofFsH8E.js +0 -1085
@@ -0,0 +1,396 @@
1
+ import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
2
+ import { mkdtemp, rm, writeFile, readFile, unlink } from 'node:fs/promises';
3
+ import { tmpdir } from 'node:os';
4
+ import path from 'node:path';
5
+ import { execGit } from './git-executor';
6
+ import { gitService } from './git-service';
7
+ import { parseConflictMarkers, parseReflog, parseUnmergedStages } from './git-parser';
8
+
9
+ /**
10
+ * Covers the conflict states the panel used to mishandle: paths with no working
11
+ * -tree file (which were silently dropped), binary conflicts (decoded as UTF-8),
12
+ * and in-progress operations that had no "continue" at all.
13
+ */
14
+
15
+ let repo: string;
16
+
17
+ async function git(...args: string[]) {
18
+ return execGit(args, repo);
19
+ }
20
+
21
+ async function gitOk(...args: string[]) {
22
+ const result = await git(...args);
23
+ if (result.exitCode !== 0) throw new Error(`git ${args.join(' ')}: ${result.stderr}`);
24
+ return result.stdout;
25
+ }
26
+
27
+ async function commitAll(message: string) {
28
+ await gitOk('add', '-A');
29
+ await gitOk('commit', '-m', message);
30
+ }
31
+
32
+ beforeEach(async () => {
33
+ repo = await mkdtemp(path.join(tmpdir(), 'clopen-conflict-'));
34
+ await gitOk('init', '-b', 'main', '.');
35
+ await gitOk('config', 'user.email', 'test@example.com');
36
+ await gitOk('config', 'user.name', 'Test');
37
+ await gitOk('config', 'commit.gpgsign', 'false');
38
+ await writeFile(path.join(repo, 'base.txt'), 'base\n');
39
+ await commitAll('init');
40
+ });
41
+
42
+ afterEach(async () => {
43
+ await rm(repo, { recursive: true, force: true });
44
+ });
45
+
46
+ /** Create a both-modified conflict on `file.txt` by merging `feature` into main. */
47
+ async function makeBothModifiedConflict() {
48
+ await writeFile(path.join(repo, 'file.txt'), 'one\ntwo\nthree\n');
49
+ await commitAll('add file');
50
+
51
+ await gitOk('checkout', '-b', 'feature');
52
+ await writeFile(path.join(repo, 'file.txt'), 'one\nfeature\nthree\n');
53
+ await commitAll('feature edit');
54
+
55
+ await gitOk('checkout', 'main');
56
+ await writeFile(path.join(repo, 'file.txt'), 'one\nmain\nthree\n');
57
+ await commitAll('main edit');
58
+
59
+ // Expected to fail with a conflict.
60
+ await git('merge', 'feature');
61
+ }
62
+
63
+ describe('parseConflictMarkers', () => {
64
+ test('parses a plain conflict', () => {
65
+ const markers = parseConflictMarkers(
66
+ ['a', '<<<<<<< HEAD', 'ours', '=======', 'theirs', '>>>>>>> feature', 'b'].join('\n')
67
+ );
68
+ expect(markers).toHaveLength(1);
69
+ expect(markers[0].ourContent).toBe('ours');
70
+ expect(markers[0].theirContent).toBe('theirs');
71
+ expect(markers[0].ourStart).toBe(1);
72
+ });
73
+
74
+ test('captures the merge base from diff3-style markers', () => {
75
+ const markers = parseConflictMarkers(
76
+ [
77
+ '<<<<<<< HEAD',
78
+ 'ours',
79
+ '||||||| base',
80
+ 'original',
81
+ '=======',
82
+ 'theirs',
83
+ '>>>>>>> feature'
84
+ ].join('\n')
85
+ );
86
+ expect(markers[0].baseContent).toBe('original');
87
+ expect(markers[0].ourContent).toBe('ours');
88
+ });
89
+
90
+ test('ignores dividers that merely start with the same character', () => {
91
+ // Eight arrows is an ASCII divider, not a marker. The old `startsWith`
92
+ // check treated it as one and reported a phantom conflict.
93
+ const markers = parseConflictMarkers(
94
+ ['<<<<<<<<<<<<<<<<', 'text', '================', 'more', '>>>>>>>>>>>>>>>>'].join('\n')
95
+ );
96
+ expect(markers).toHaveLength(0);
97
+ });
98
+
99
+ test('ignores an opener with no separator', () => {
100
+ expect(parseConflictMarkers(['<<<<<<< HEAD', 'orphan', 'text'].join('\n'))).toHaveLength(0);
101
+ });
102
+
103
+ test('recovers when a second opener appears before the first closes', () => {
104
+ const markers = parseConflictMarkers(
105
+ [
106
+ '<<<<<<< stray',
107
+ 'noise',
108
+ '<<<<<<< HEAD',
109
+ 'ours',
110
+ '=======',
111
+ 'theirs',
112
+ '>>>>>>> feature'
113
+ ].join('\n')
114
+ );
115
+ expect(markers).toHaveLength(1);
116
+ expect(markers[0].ourContent).toBe('ours');
117
+ });
118
+ });
119
+
120
+ describe('parseUnmergedStages', () => {
121
+ test('groups stages per path', () => {
122
+ // `\x00`, not `\0`: in a JS string `\0` followed by a digit is a legacy
123
+ // octal escape, so `\0100644` would be one character, not NUL + "100644".
124
+ const output = [
125
+ '100644 aaa 1\tsrc/a.ts',
126
+ '100644 bbb 2\tsrc/a.ts',
127
+ '100644 ccc 3\tsrc/a.ts',
128
+ '100644 ddd 3\tsrc/b.ts',
129
+ ''
130
+ ].join('\x00');
131
+ const stages = parseUnmergedStages(output);
132
+ expect([...(stages.get('src/a.ts') ?? [])].sort()).toEqual([1, 2, 3]);
133
+ expect([...(stages.get('src/b.ts') ?? [])]).toEqual([3]);
134
+ });
135
+ });
136
+
137
+ describe('parseReflog', () => {
138
+ test('splits the action from the subject', () => {
139
+ const entries = parseReflog(
140
+ 'abc123|||abc123|||HEAD@{0}|||rebase (finish): returning to refs/heads/main|||2026-01-01T00:00:00Z'
141
+ );
142
+ expect(entries[0].action).toBe('rebase (finish)');
143
+ expect(entries[0].subject).toBe('returning to refs/heads/main');
144
+ expect(entries[0].selector).toBe('HEAD@{0}');
145
+ });
146
+
147
+ test('tolerates a subject with no action prefix', () => {
148
+ const entries = parseReflog('abc|||abc|||HEAD@{1}|||something|||2026-01-01T00:00:00Z');
149
+ expect(entries[0].action).toBe('something');
150
+ expect(entries[0].subject).toBe('');
151
+ });
152
+ });
153
+
154
+ describe('getConflictFiles', () => {
155
+ test('reports a both-modified conflict with markers', async () => {
156
+ await makeBothModifiedConflict();
157
+
158
+ const conflicts = await gitService.getConflictFiles(repo);
159
+ expect(conflicts).toHaveLength(1);
160
+ expect(conflicts[0].path).toBe('file.txt');
161
+ expect(conflicts[0].kind).toBe('both-modified');
162
+ expect(conflicts[0].contentOmitted).toBe(false);
163
+ expect(conflicts[0].markers).toHaveLength(1);
164
+ });
165
+
166
+ test('keeps a delete/modify conflict instead of dropping it', async () => {
167
+ await writeFile(path.join(repo, 'gone.txt'), 'content\n');
168
+ await commitAll('add gone');
169
+
170
+ await gitOk('checkout', '-b', 'feature');
171
+ await writeFile(path.join(repo, 'gone.txt'), 'changed\n');
172
+ await commitAll('modify');
173
+
174
+ await gitOk('checkout', 'main');
175
+ await unlink(path.join(repo, 'gone.txt'));
176
+ await commitAll('delete');
177
+
178
+ await git('merge', 'feature');
179
+
180
+ const conflicts = await gitService.getConflictFiles(repo);
181
+ expect(conflicts.map(c => c.path)).toContain('gone.txt');
182
+ // Deleted on our side, modified on theirs — no markers to walk.
183
+ expect(conflicts[0].kind).toBe('deleted-by-us');
184
+ expect(conflicts[0].markers).toHaveLength(0);
185
+ });
186
+
187
+ test('flags a binary conflict rather than decoding it as text', async () => {
188
+ const binary = (a: number) => Buffer.from([0x00, 0x01, a, 0xff, 0x00]);
189
+ await writeFile(path.join(repo, 'blob.bin'), binary(0x10));
190
+ await commitAll('add blob');
191
+
192
+ await gitOk('checkout', '-b', 'feature');
193
+ await writeFile(path.join(repo, 'blob.bin'), binary(0x20));
194
+ await commitAll('feature blob');
195
+
196
+ await gitOk('checkout', 'main');
197
+ await writeFile(path.join(repo, 'blob.bin'), binary(0x30));
198
+ await commitAll('main blob');
199
+
200
+ await git('merge', 'feature');
201
+
202
+ const conflicts = await gitService.getConflictFiles(repo);
203
+ const blob = conflicts.find(c => c.path === 'blob.bin');
204
+ expect(blob).toBeDefined();
205
+ expect(blob?.contentOmitted).toBe(true);
206
+ expect(blob?.omitReason).toBe('binary');
207
+ expect(blob?.content).toBe('');
208
+ });
209
+ });
210
+
211
+ describe('resolveConflict', () => {
212
+ test('refuses to stage content that still has markers', async () => {
213
+ await makeBothModifiedConflict();
214
+ const withMarkers = await readFile(path.join(repo, 'file.txt'), 'utf-8');
215
+
216
+ await expect(
217
+ gitService.resolveConflict(repo, 'file.txt', 'custom', withMarkers)
218
+ ).rejects.toThrow(/conflict markers/i);
219
+
220
+ // Still unmerged — the guard must not half-apply.
221
+ expect(await gitService.getUnmergedPaths(repo)).toEqual(['file.txt']);
222
+ });
223
+
224
+ test('"ours" takes our side and stages it', async () => {
225
+ await makeBothModifiedConflict();
226
+ await gitService.resolveConflict(repo, 'file.txt', 'ours');
227
+
228
+ expect(await readFile(path.join(repo, 'file.txt'), 'utf-8')).toBe('one\nmain\nthree\n');
229
+ expect(await gitService.getUnmergedPaths(repo)).toEqual([]);
230
+ });
231
+
232
+ test('"ours" resolves as a deletion when our side deleted the path', async () => {
233
+ await writeFile(path.join(repo, 'gone.txt'), 'content\n');
234
+ await commitAll('add gone');
235
+ await gitOk('checkout', '-b', 'feature');
236
+ await writeFile(path.join(repo, 'gone.txt'), 'changed\n');
237
+ await commitAll('modify');
238
+ await gitOk('checkout', 'main');
239
+ await unlink(path.join(repo, 'gone.txt'));
240
+ await commitAll('delete');
241
+ await git('merge', 'feature');
242
+
243
+ // `git checkout --ours` cannot work here; the answer git wants is `rm`.
244
+ await gitService.resolveConflict(repo, 'gone.txt', 'ours');
245
+ expect(await gitService.getUnmergedPaths(repo)).toEqual([]);
246
+ });
247
+
248
+ test('"reset" puts the markers back', async () => {
249
+ await makeBothModifiedConflict();
250
+ await writeFile(path.join(repo, 'file.txt'), 'one\nresolved\nthree\n');
251
+ await gitService.resolveConflict(repo, 'file.txt', 'reset');
252
+
253
+ const restored = await readFile(path.join(repo, 'file.txt'), 'utf-8');
254
+ expect(restored).toContain('<<<<<<<');
255
+ expect(await gitService.getUnmergedPaths(repo)).toEqual(['file.txt']);
256
+ });
257
+ });
258
+
259
+ describe('stageAll', () => {
260
+ test('refuses while a conflicted file still has markers', async () => {
261
+ await makeBothModifiedConflict();
262
+ await expect(gitService.stageAll(repo)).rejects.toThrow(/conflict markers/i);
263
+ });
264
+
265
+ test('proceeds once the markers are gone', async () => {
266
+ await makeBothModifiedConflict();
267
+ await writeFile(path.join(repo, 'file.txt'), 'one\nresolved\nthree\n');
268
+ await gitService.stageAll(repo);
269
+ expect(await gitService.getUnmergedPaths(repo)).toEqual([]);
270
+ });
271
+ });
272
+
273
+ describe('getOperationState', () => {
274
+ test('is idle in a clean repo', async () => {
275
+ const state = await gitService.getOperationState(repo);
276
+ expect(state.operation).toBeNull();
277
+ expect(state.canContinue).toBe(false);
278
+ expect(state.unmergedCount).toBe(0);
279
+ });
280
+
281
+ test('describes a merge and names both sides', async () => {
282
+ await makeBothModifiedConflict();
283
+
284
+ const state = await gitService.getOperationState(repo);
285
+ expect(state.operation).toBe('merge');
286
+ expect(state.oursLabel).toBe('main');
287
+ expect(state.theirsLabel).toBe('feature');
288
+ expect(state.unmergedCount).toBe(1);
289
+ // Blocked until the conflict is staged.
290
+ expect(state.canContinue).toBe(false);
291
+ });
292
+
293
+ test('allows continue once every path is resolved', async () => {
294
+ await makeBothModifiedConflict();
295
+ await gitService.resolveConflict(repo, 'file.txt', 'ours');
296
+
297
+ const state = await gitService.getOperationState(repo);
298
+ expect(state.canContinue).toBe(true);
299
+ });
300
+
301
+ test('reports rebase progress with the sides inverted', async () => {
302
+ await writeFile(path.join(repo, 'file.txt'), 'one\ntwo\n');
303
+ await commitAll('add file');
304
+
305
+ await gitOk('checkout', '-b', 'feature');
306
+ await writeFile(path.join(repo, 'file.txt'), 'one\nfeature\n');
307
+ await commitAll('feature edit');
308
+
309
+ await gitOk('checkout', 'main');
310
+ await writeFile(path.join(repo, 'file.txt'), 'one\nmain\n');
311
+ await commitAll('main edit');
312
+
313
+ await gitOk('checkout', 'feature');
314
+ await git('rebase', 'main');
315
+
316
+ const state = await gitService.getOperationState(repo);
317
+ expect(state.operation).toBe('rebase');
318
+ // During a rebase "ours" is the branch being rebased ONTO.
319
+ expect(state.oursLabel).toBe('main');
320
+ expect(state.theirsLabel).toContain('feature');
321
+ expect(state.canSkip).toBe(true);
322
+ expect(state.total).toBe(1);
323
+ });
324
+
325
+ test('flags unmerged paths with no sentinel as a stash conflict', async () => {
326
+ await writeFile(path.join(repo, 'file.txt'), 'one\n');
327
+ await commitAll('add file');
328
+
329
+ await writeFile(path.join(repo, 'file.txt'), 'stashed\n');
330
+ await gitOk('stash', 'push', '-m', 'wip');
331
+ await writeFile(path.join(repo, 'file.txt'), 'other\n');
332
+ await commitAll('conflicting edit');
333
+ await git('stash', 'pop');
334
+
335
+ const state = await gitService.getOperationState(repo);
336
+ expect(state.operation).toBeNull();
337
+ expect(state.stashConflict).toBe(true);
338
+ expect(state.unmergedCount).toBeGreaterThan(0);
339
+ });
340
+ });
341
+
342
+ describe('continueOperation', () => {
343
+ test('refuses while paths are still unmerged', async () => {
344
+ await makeBothModifiedConflict();
345
+ await expect(gitService.continueOperation(repo)).rejects.toThrow(/still unmerged/i);
346
+ });
347
+
348
+ test('finishes a merge once the conflict is staged', async () => {
349
+ await makeBothModifiedConflict();
350
+ await gitService.resolveConflict(repo, 'file.txt', 'ours');
351
+
352
+ const result = await gitService.continueOperation(repo);
353
+ expect(result.success).toBe(true);
354
+
355
+ const state = await gitService.getOperationState(repo);
356
+ expect(state.operation).toBeNull();
357
+ });
358
+
359
+ test('finishes a rebase, leaving the branch on top of the upstream', async () => {
360
+ await writeFile(path.join(repo, 'file.txt'), 'one\ntwo\n');
361
+ await commitAll('add file');
362
+ await gitOk('checkout', '-b', 'feature');
363
+ await writeFile(path.join(repo, 'file.txt'), 'one\nfeature\n');
364
+ await commitAll('feature edit');
365
+ await gitOk('checkout', 'main');
366
+ await writeFile(path.join(repo, 'file.txt'), 'one\nmain\n');
367
+ await commitAll('main edit');
368
+ await gitOk('checkout', 'feature');
369
+ await git('rebase', 'main');
370
+
371
+ await gitService.resolveConflict(repo, 'file.txt', 'theirs');
372
+ const result = await gitService.continueOperation(repo);
373
+ expect(result.success).toBe(true);
374
+
375
+ const state = await gitService.getOperationState(repo);
376
+ expect(state.operation).toBeNull();
377
+ const branch = (await gitOk('branch', '--show-current')).trim();
378
+ expect(branch).toBe('feature');
379
+ });
380
+
381
+ test('reports there is nothing to continue', async () => {
382
+ await expect(gitService.continueOperation(repo)).rejects.toThrow(/no operation/i);
383
+ });
384
+ });
385
+
386
+ describe('getReflog', () => {
387
+ test('lists recent HEAD movements', async () => {
388
+ await writeFile(path.join(repo, 'a.txt'), 'a\n');
389
+ await commitAll('second');
390
+
391
+ const entries = await gitService.getReflog(repo, 10);
392
+ expect(entries.length).toBeGreaterThan(0);
393
+ expect(entries[0].selector).toBe('HEAD@{0}');
394
+ expect(entries[0].action).toContain('commit');
395
+ });
396
+ });
@@ -62,6 +62,12 @@ export async function execGit(
62
62
  // actually changing. Commands that genuinely need the index lock (commit,
63
63
  // add, checkout) still take it; only the optional refresh is suppressed.
64
64
  GIT_OPTIONAL_LOCKS: '0',
65
+ // `rebase --continue`, `merge --continue` and friends open $EDITOR for the
66
+ // commit message. With no TTY that call blocks until our timeout kills it,
67
+ // so point both editors at `true`: it exits 0 immediately and git keeps
68
+ // the message git already prepared.
69
+ GIT_EDITOR: 'true',
70
+ GIT_SEQUENCE_EDITOR: 'true',
65
71
  // Use English output for consistent parsing
66
72
  LANG: 'en_US.UTF-8',
67
73
  LC_ALL: 'en_US.UTF-8'
@@ -14,7 +14,8 @@ import type {
14
14
  GitCommit,
15
15
  GitRemote,
16
16
  GitStashEntry,
17
- GitConflictMarker
17
+ GitConflictMarker,
18
+ GitReflogEntry
18
19
  } from '$shared/types/git';
19
20
 
20
21
  /**
@@ -120,10 +121,15 @@ export function parseBranches(localOutput: string, remoteOutput: string): GitBra
120
121
  // Parse local branches
121
122
  const localLines = localOutput.split('\n').filter(Boolean);
122
123
  for (const line of localLines) {
123
- const dateSep = line.lastIndexOf('|');
124
+ // The upstream is appended after a 0x1F unit separator rather than another
125
+ // `|`, because the subject in the middle of the line can contain `|` and
126
+ // the legacy fields are still split positionally.
127
+ const [legacy, upstreamField = ''] = line.split('\x1f');
128
+
129
+ const dateSep = legacy.lastIndexOf('|');
124
130
  if (dateSep === -1) continue;
125
- const lastCommitDate = line.substring(dateSep + 1);
126
- const head = line.substring(0, dateSep);
131
+ const lastCommitDate = legacy.substring(dateSep + 1);
132
+ const head = legacy.substring(0, dateSep);
127
133
 
128
134
  // head = "<*>|<name>|<hash>|<subject>"
129
135
  const parts = head.split('|', 4);
@@ -135,7 +141,7 @@ export function parseBranches(localOutput: string, remoteOutput: string): GitBra
135
141
 
136
142
  if (isCurrent) current = name;
137
143
 
138
- local.push({
144
+ const branch: GitBranch = {
139
145
  name,
140
146
  isCurrent,
141
147
  isRemote: false,
@@ -144,7 +150,10 @@ export function parseBranches(localOutput: string, remoteOutput: string): GitBra
144
150
  lastCommit,
145
151
  lastCommitMessage,
146
152
  lastCommitDate
147
- } as GitBranch);
153
+ };
154
+ if (upstreamField.trim()) branch.upstream = upstreamField.trim();
155
+
156
+ local.push(branch);
148
157
  }
149
158
 
150
159
  // Parse remote branches
@@ -394,58 +403,145 @@ export function parseStashList(output: string): GitStashEntry[] {
394
403
  /**
395
404
  * Parse conflict markers from file content
396
405
  */
406
+ /**
407
+ * Conflict markers are exactly seven repeated characters followed by a space or
408
+ * the end of the line. Matching a bare `startsWith('<<<<<<<')` also fires on
409
+ * ordinary content — an eight-arrow ASCII divider, a diff quoted inside a
410
+ * markdown file — and turns a clean file into a phantom conflict.
411
+ */
412
+ const MARKER_OURS = /^<{7}(?:[ \t\r]|$)/;
413
+ const MARKER_BASE = /^\|{7}(?:[ \t\r]|$)/;
414
+ const MARKER_SEP = /^={7}(?:[ \t\r]|$)/;
415
+ const MARKER_THEIRS = /^>{7}(?:[ \t\r]|$)/;
416
+
417
+ /**
418
+ * True when the text still contains an unresolved conflict block. Used as a
419
+ * guard before staging: `git add` happily records a file that still has markers
420
+ * in it, which silently commits `<<<<<<<` into the tree.
421
+ */
422
+ export function hasConflictMarkers(content: string): boolean {
423
+ return parseConflictMarkers(content).length > 0;
424
+ }
425
+
397
426
  export function parseConflictMarkers(content: string): GitConflictMarker[] {
398
427
  const markers: GitConflictMarker[] = [];
399
428
  const lines = content.split('\n');
400
429
 
401
430
  let i = 0;
402
431
  while (i < lines.length) {
403
- if (lines[i].startsWith('<<<<<<<')) {
404
- const ourStart = i;
405
- let baseStart: number | undefined;
406
- let separatorIndex = -1;
407
- let theirEnd = -1;
408
-
409
- // Find the rest of the conflict
410
- let j = i + 1;
411
- while (j < lines.length) {
412
- if (lines[j].startsWith('|||||||')) {
413
- baseStart = j;
414
- } else if (lines[j].startsWith('=======')) {
415
- separatorIndex = j;
416
- } else if (lines[j].startsWith('>>>>>>>')) {
417
- theirEnd = j;
418
- break;
419
- }
420
- j++;
421
- }
432
+ if (!MARKER_OURS.test(lines[i])) {
433
+ i++;
434
+ continue;
435
+ }
422
436
 
423
- if (separatorIndex >= 0 && theirEnd >= 0) {
424
- const ourContentStart = ourStart + 1;
425
- const ourContentEnd = baseStart !== undefined ? baseStart : separatorIndex;
426
- const theirContentStart = separatorIndex + 1;
427
-
428
- const marker: GitConflictMarker = {
429
- ourStart,
430
- ourEnd: separatorIndex,
431
- theirStart: separatorIndex,
432
- theirEnd,
433
- ourContent: lines.slice(ourContentStart, ourContentEnd).join('\n'),
434
- theirContent: lines.slice(theirContentStart, theirEnd).join('\n')
435
- };
436
-
437
- if (baseStart !== undefined) {
438
- marker.baseStart = baseStart;
439
- marker.baseContent = lines.slice(baseStart + 1, separatorIndex).join('\n');
440
- }
441
-
442
- markers.push(marker);
443
- i = theirEnd + 1;
444
- continue;
437
+ const ourStart = i;
438
+ let baseStart: number | undefined;
439
+ let separatorIndex = -1;
440
+ let theirEnd = -1;
441
+ let restartAt = -1;
442
+
443
+ let j = i + 1;
444
+ while (j < lines.length) {
445
+ const line = lines[j];
446
+ if (MARKER_OURS.test(line)) {
447
+ // A second opener before this one closed means the first was never a
448
+ // real conflict. Drop it and re-scan from the opener we just found.
449
+ restartAt = j;
450
+ break;
445
451
  }
452
+ if (MARKER_BASE.test(line)) baseStart = j;
453
+ else if (MARKER_SEP.test(line)) separatorIndex = j;
454
+ else if (MARKER_THEIRS.test(line)) {
455
+ theirEnd = j;
456
+ break;
457
+ }
458
+ j++;
446
459
  }
447
- i++;
460
+
461
+ if (restartAt >= 0) {
462
+ i = restartAt;
463
+ continue;
464
+ }
465
+
466
+ if (separatorIndex < 0 || theirEnd < 0) {
467
+ i = ourStart + 1;
468
+ continue;
469
+ }
470
+
471
+ const ourContentEnd = baseStart !== undefined ? baseStart : separatorIndex;
472
+ const marker: GitConflictMarker = {
473
+ ourStart,
474
+ ourEnd: separatorIndex,
475
+ theirStart: separatorIndex,
476
+ theirEnd,
477
+ ourContent: lines.slice(ourStart + 1, ourContentEnd).join('\n'),
478
+ theirContent: lines.slice(separatorIndex + 1, theirEnd).join('\n')
479
+ };
480
+
481
+ if (baseStart !== undefined) {
482
+ marker.baseStart = baseStart;
483
+ marker.baseContent = lines.slice(baseStart + 1, separatorIndex).join('\n');
484
+ }
485
+
486
+ markers.push(marker);
487
+ i = theirEnd + 1;
448
488
  }
449
489
 
450
490
  return markers;
451
491
  }
492
+
493
+ /**
494
+ * Parse `git reflog --format=%H|||%h|||%gd|||%gs|||%cI`.
495
+ *
496
+ * `%gs` (the reflog subject) already carries both halves — "commit: fix thing",
497
+ * "rebase (finish): returning to refs/heads/main" — so we split on the first
498
+ * ": " to separate the action from the description.
499
+ */
500
+ export function parseReflog(output: string): GitReflogEntry[] {
501
+ const SEPARATOR = '|||';
502
+ const entries: GitReflogEntry[] = [];
503
+
504
+ for (const line of output.split('\n')) {
505
+ if (!line.trim()) continue;
506
+ const parts = line.split(SEPARATOR);
507
+ if (parts.length < 5) continue;
508
+ const [hash, hashShort, selector, gs, date] = parts;
509
+ const colon = gs.indexOf(': ');
510
+ entries.push({
511
+ hash,
512
+ hashShort,
513
+ selector,
514
+ action: colon >= 0 ? gs.slice(0, colon) : gs,
515
+ subject: colon >= 0 ? gs.slice(colon + 2) : '',
516
+ date
517
+ });
518
+ }
519
+
520
+ return entries;
521
+ }
522
+
523
+ /**
524
+ * Parse `git ls-files -u -z`: one row per unmerged *stage* (1 = base, 2 = ours,
525
+ * 3 = theirs), so a path shows up two or three times. Which stages are present
526
+ * is what distinguishes a both-modified conflict from a delete/modify one, and
527
+ * unlike `git status` this never omits a path whose working-tree file is gone.
528
+ */
529
+ export function parseUnmergedStages(output: string): Map<string, Set<number>> {
530
+ const stages = new Map<string, Set<number>>();
531
+
532
+ for (const row of output.split('\0')) {
533
+ if (!row.trim()) continue;
534
+ // "<mode> <sha> <stage>\t<path>"
535
+ const tab = row.indexOf('\t');
536
+ if (tab < 0) continue;
537
+ const meta = row.slice(0, tab).trim().split(/\s+/);
538
+ const path = row.slice(tab + 1);
539
+ const stage = Number(meta[2]);
540
+ if (!path || !Number.isInteger(stage)) continue;
541
+ const set = stages.get(path) ?? new Set<number>();
542
+ set.add(stage);
543
+ stages.set(path, set);
544
+ }
545
+
546
+ return stages;
547
+ }