@myrialabs/clopen 0.4.36 → 0.4.37
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/backend/git/conflict-state.test.ts +396 -0
- package/backend/git/git-executor.ts +6 -0
- package/backend/git/git-parser.ts +144 -48
- package/backend/git/git-service.ts +727 -57
- package/backend/git/push-target.test.ts +264 -0
- package/backend/project/status-manager.ts +20 -0
- package/backend/ws/git/branch-name.ts +6 -5
- package/backend/ws/git/branch.ts +43 -1
- package/backend/ws/git/commit-message.ts +13 -10
- package/backend/ws/git/conflict.ts +106 -3
- package/backend/ws/git/diff-budget.test.ts +125 -0
- package/backend/ws/git/diff-budget.ts +220 -0
- package/backend/ws/git/log.ts +25 -0
- package/backend/ws/git/remote.ts +75 -1
- package/dist/assets/{index-Cz4YpHVi.js → index-Bik9TCBi.js} +1 -1
- package/dist/assets/{index-Bg-SUCFg.js → index-sJmS_CCm.js} +1 -1
- package/dist/assets/main-DLqolBI2.css +1 -0
- package/dist/assets/{main-BofFsH8E.js → main-Df1I3hnw.js} +481 -470
- package/dist/index.html +2 -2
- package/package.json +1 -1
- package/shared/types/git.ts +123 -0
- package/dist/assets/main-9G7IpQpK.css +0 -1
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
|
|
2
|
+
import { mkdtemp, rm, writeFile } 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
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Reviewing a pull request from a fork is the case that broke.
|
|
10
|
+
*
|
|
11
|
+
* `gh pr checkout` records the contributor's repository in
|
|
12
|
+
* `branch.<name>.remote` — often as a bare URL — and their branch name in
|
|
13
|
+
* `branch.<name>.merge`. The panel ignored both and ran
|
|
14
|
+
* `git push <selectedRemote> <localName> -u`, which pushed the review commits
|
|
15
|
+
* into the maintainer's own repository as a new branch and then rewrote the
|
|
16
|
+
* branch's remote so every later push went there too.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
let workspace: string;
|
|
20
|
+
/** Bare repo standing in for the maintainer's repository. */
|
|
21
|
+
let upstream: string;
|
|
22
|
+
/** Bare repo standing in for the contributor's fork. */
|
|
23
|
+
let fork: string;
|
|
24
|
+
/** Working clone, with `origin` pointing at `upstream`. */
|
|
25
|
+
let work: string;
|
|
26
|
+
|
|
27
|
+
async function git(cwd: string, ...args: string[]) {
|
|
28
|
+
const result = await execGit(args, cwd);
|
|
29
|
+
if (result.exitCode !== 0) throw new Error(`git ${args.join(' ')}: ${result.stderr}`);
|
|
30
|
+
return result.stdout;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function branchesOf(bareRepo: string): Promise<string[]> {
|
|
34
|
+
const out = await git(bareRepo, 'for-each-ref', '--format=%(refname:short)', 'refs/heads/');
|
|
35
|
+
return out.split('\n').map(l => l.trim()).filter(Boolean).sort();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function identify(repo: string) {
|
|
39
|
+
await git(repo, 'config', 'user.email', 'test@example.com');
|
|
40
|
+
await git(repo, 'config', 'user.name', 'Test');
|
|
41
|
+
await git(repo, 'config', 'commit.gpgsign', 'false');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
beforeEach(async () => {
|
|
45
|
+
workspace = await mkdtemp(path.join(tmpdir(), 'clopen-push-'));
|
|
46
|
+
upstream = path.join(workspace, 'upstream.git');
|
|
47
|
+
fork = path.join(workspace, 'fork.git');
|
|
48
|
+
work = path.join(workspace, 'work');
|
|
49
|
+
|
|
50
|
+
await git(workspace, 'init', '--bare', '-b', 'main', upstream);
|
|
51
|
+
|
|
52
|
+
// Seed `main` in the upstream repo.
|
|
53
|
+
const seed = path.join(workspace, 'seed');
|
|
54
|
+
await git(workspace, 'clone', '-q', upstream, seed);
|
|
55
|
+
await identify(seed);
|
|
56
|
+
await writeFile(path.join(seed, 'a.txt'), 'a\n');
|
|
57
|
+
await git(seed, 'add', '-A');
|
|
58
|
+
await git(seed, 'commit', '-m', 'init');
|
|
59
|
+
await git(seed, 'push', 'origin', 'main');
|
|
60
|
+
|
|
61
|
+
// The fork starts as a copy of upstream and gains the contributor's branch.
|
|
62
|
+
await git(workspace, 'clone', '--bare', '-q', upstream, fork);
|
|
63
|
+
const forkWork = path.join(workspace, 'forkwork');
|
|
64
|
+
await git(workspace, 'clone', '-q', fork, forkWork);
|
|
65
|
+
await identify(forkWork);
|
|
66
|
+
await git(forkWork, 'checkout', '-b', 'their-branch');
|
|
67
|
+
await writeFile(path.join(forkWork, 'contrib.txt'), 'contribution\n');
|
|
68
|
+
await git(forkWork, 'add', '-A');
|
|
69
|
+
await git(forkWork, 'commit', '-m', 'contribution');
|
|
70
|
+
await git(forkWork, 'push', 'origin', 'their-branch');
|
|
71
|
+
|
|
72
|
+
// The maintainer's clone.
|
|
73
|
+
await git(workspace, 'clone', '-q', upstream, work);
|
|
74
|
+
await identify(work);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
afterEach(async () => {
|
|
78
|
+
await rm(workspace, { recursive: true, force: true });
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
/** Reproduce what `gh pr checkout` leaves behind for a fork PR. */
|
|
82
|
+
async function checkoutForkPr(localName = 'their-branch') {
|
|
83
|
+
await git(work, 'fetch', fork, 'their-branch');
|
|
84
|
+
await git(work, 'checkout', '-b', localName, 'FETCH_HEAD');
|
|
85
|
+
await git(work, 'config', `branch.${localName}.remote`, fork);
|
|
86
|
+
await git(work, 'config', `branch.${localName}.merge`, 'refs/heads/their-branch');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
describe('getPushTarget', () => {
|
|
90
|
+
test('reports no upstream for a fresh local branch', async () => {
|
|
91
|
+
await git(work, 'checkout', '-b', 'brand-new');
|
|
92
|
+
const target = await gitService.getPushTarget(work);
|
|
93
|
+
expect(target.branch).toBe('brand-new');
|
|
94
|
+
expect(target.hasUpstream).toBe(false);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test('reports the tracking branch of a normal clone', async () => {
|
|
98
|
+
const target = await gitService.getPushTarget(work);
|
|
99
|
+
expect(target.hasUpstream).toBe(true);
|
|
100
|
+
expect(target.remote).toBe('origin');
|
|
101
|
+
expect(target.remoteBranch).toBe('main');
|
|
102
|
+
expect(target.isUrl).toBe(false);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test('resolves a fork PR to the fork, not to origin', async () => {
|
|
106
|
+
await checkoutForkPr();
|
|
107
|
+
|
|
108
|
+
const target = await gitService.getPushTarget(work);
|
|
109
|
+
expect(target.hasUpstream).toBe(true);
|
|
110
|
+
expect(target.remote).toBe(fork);
|
|
111
|
+
expect(target.remoteBranch).toBe('their-branch');
|
|
112
|
+
expect(target.isUrl).toBe(true);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test('keeps the remote branch name when the local branch was renamed', async () => {
|
|
116
|
+
// gh renames the local branch when the name would collide.
|
|
117
|
+
await checkoutForkPr('contributor-their-branch');
|
|
118
|
+
|
|
119
|
+
const target = await gitService.getPushTarget(work);
|
|
120
|
+
expect(target.branch).toBe('contributor-their-branch');
|
|
121
|
+
expect(target.remoteBranch).toBe('their-branch');
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test('honours branch.<name>.pushRemote over branch.<name>.remote', async () => {
|
|
125
|
+
await git(work, 'remote', 'add', 'fork', fork);
|
|
126
|
+
await git(work, 'config', 'branch.main.pushRemote', 'fork');
|
|
127
|
+
|
|
128
|
+
const target = await gitService.getPushTarget(work);
|
|
129
|
+
expect(target.remote).toBe('fork');
|
|
130
|
+
expect(target.isUrl).toBe(false);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
describe('push — fork pull request under review', () => {
|
|
135
|
+
test('sends the review commit to the fork and leaves origin untouched', async () => {
|
|
136
|
+
await checkoutForkPr();
|
|
137
|
+
await writeFile(path.join(work, 'review.txt'), 'review fix\n');
|
|
138
|
+
await git(work, 'add', '-A');
|
|
139
|
+
await git(work, 'commit', '-m', 'review fix');
|
|
140
|
+
|
|
141
|
+
// `remote` here is what the panel's dropdown would have supplied.
|
|
142
|
+
const result = await gitService.push(work, 'origin', 'their-branch');
|
|
143
|
+
expect(result.success).toBe(true);
|
|
144
|
+
|
|
145
|
+
// The fix landed on the contributor's branch...
|
|
146
|
+
const forkHead = await git(fork, 'log', '-1', '--format=%s', 'their-branch');
|
|
147
|
+
expect(forkHead.trim()).toBe('review fix');
|
|
148
|
+
|
|
149
|
+
// ...and the maintainer's repository gained nothing.
|
|
150
|
+
expect(await branchesOf(upstream)).toEqual(['main']);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test('does not rewrite the branch\'s configured remote', async () => {
|
|
154
|
+
await checkoutForkPr();
|
|
155
|
+
await writeFile(path.join(work, 'review.txt'), 'review fix\n');
|
|
156
|
+
await git(work, 'add', '-A');
|
|
157
|
+
await git(work, 'commit', '-m', 'review fix');
|
|
158
|
+
|
|
159
|
+
await gitService.push(work, 'origin', 'their-branch');
|
|
160
|
+
|
|
161
|
+
// The old `-u` replaced this with "origin", so every later push — including
|
|
162
|
+
// a manual `git push` — silently went to the wrong repository.
|
|
163
|
+
const configured = (await git(work, 'config', '--get', 'branch.their-branch.remote')).trim();
|
|
164
|
+
expect(configured).toBe(fork);
|
|
165
|
+
const merge = (await git(work, 'config', '--get', 'branch.their-branch.merge')).trim();
|
|
166
|
+
expect(merge).toBe('refs/heads/their-branch');
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
test('force push targets the fork rather than origin', async () => {
|
|
170
|
+
await checkoutForkPr();
|
|
171
|
+
await writeFile(path.join(work, 'review.txt'), 'review fix\n');
|
|
172
|
+
await git(work, 'add', '-A');
|
|
173
|
+
await git(work, 'commit', '-m', 'review fix');
|
|
174
|
+
|
|
175
|
+
const result = await gitService.pushAdvanced(work, 'force', 'origin', 'their-branch');
|
|
176
|
+
expect(result.success).toBe(true);
|
|
177
|
+
|
|
178
|
+
const forkHead = await git(fork, 'log', '-1', '--format=%s', 'their-branch');
|
|
179
|
+
expect(forkHead.trim()).toBe('review fix');
|
|
180
|
+
expect(await branchesOf(upstream)).toEqual(['main']);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
test('explains why --force-with-lease cannot work against a URL upstream', async () => {
|
|
184
|
+
await checkoutForkPr();
|
|
185
|
+
await writeFile(path.join(work, 'review.txt'), 'review fix\n');
|
|
186
|
+
await git(work, 'add', '-A');
|
|
187
|
+
await git(work, 'commit', '-m', 'review fix');
|
|
188
|
+
|
|
189
|
+
// There is no remote-tracking ref for the lease to compare against, so git
|
|
190
|
+
// refuses with a bare "stale info". Say what that means instead.
|
|
191
|
+
const result = await gitService.pushAdvanced(work, 'force-lease', 'origin', 'their-branch');
|
|
192
|
+
expect(result.success).toBe(false);
|
|
193
|
+
expect(result.message).toMatch(/remote-tracking ref/i);
|
|
194
|
+
expect(await branchesOf(upstream)).toEqual(['main']);
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
describe('push — branch with no upstream', () => {
|
|
199
|
+
test('still creates the branch on the chosen remote and records it', async () => {
|
|
200
|
+
await git(work, 'checkout', '-b', 'feature/new');
|
|
201
|
+
await writeFile(path.join(work, 'f.txt'), 'f\n');
|
|
202
|
+
await git(work, 'add', '-A');
|
|
203
|
+
await git(work, 'commit', '-m', 'new work');
|
|
204
|
+
|
|
205
|
+
const result = await gitService.push(work, 'origin', 'feature/new');
|
|
206
|
+
expect(result.success).toBe(true);
|
|
207
|
+
expect(await branchesOf(upstream)).toEqual(['feature/new', 'main']);
|
|
208
|
+
|
|
209
|
+
// `-u` is correct here: there was no upstream to overwrite.
|
|
210
|
+
const target = await gitService.getPushTarget(work);
|
|
211
|
+
expect(target.hasUpstream).toBe(true);
|
|
212
|
+
expect(target.remote).toBe('origin');
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
describe('getBranches', () => {
|
|
217
|
+
test('exposes the upstream of a normal tracking branch', async () => {
|
|
218
|
+
const info = await gitService.getBranches(work, 'origin');
|
|
219
|
+
expect(info.local.find(b => b.name === 'main')?.upstream).toBe('origin/main');
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
test('exposes a URL upstream that git itself cannot name', async () => {
|
|
223
|
+
await checkoutForkPr();
|
|
224
|
+
const info = await gitService.getBranches(work, 'origin');
|
|
225
|
+
const branch = info.local.find(b => b.name === 'their-branch');
|
|
226
|
+
expect(branch?.upstream).toBe(`${fork}/their-branch`);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
test('counts ahead against the real upstream, not <selected remote>/<name>', async () => {
|
|
230
|
+
await git(work, 'checkout', '-b', 'renamed-locally', 'main');
|
|
231
|
+
await git(work, 'branch', '--set-upstream-to=origin/main', 'renamed-locally');
|
|
232
|
+
await writeFile(path.join(work, 'x.txt'), 'x\n');
|
|
233
|
+
await git(work, 'add', '-A');
|
|
234
|
+
await git(work, 'commit', '-m', 'local work');
|
|
235
|
+
|
|
236
|
+
// `origin/renamed-locally` does not exist; the old code compared against it
|
|
237
|
+
// and reported nothing to push.
|
|
238
|
+
const info = await gitService.getBranches(work, 'origin');
|
|
239
|
+
expect(info.ahead).toBe(1);
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
describe('setUpstream / unsetUpstream', () => {
|
|
244
|
+
test('repoints a branch at a different remote branch', async () => {
|
|
245
|
+
await git(work, 'checkout', '-b', 'wip', 'main');
|
|
246
|
+
await gitService.setUpstream(work, 'wip', 'origin', 'main');
|
|
247
|
+
|
|
248
|
+
const target = await gitService.getPushTarget(work, 'wip');
|
|
249
|
+
expect(target.remote).toBe('origin');
|
|
250
|
+
expect(target.remoteBranch).toBe('main');
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
test('clears tracking so the next push has to choose', async () => {
|
|
254
|
+
await gitService.unsetUpstream(work, 'main');
|
|
255
|
+
const target = await gitService.getPushTarget(work, 'main');
|
|
256
|
+
expect(target.hasUpstream).toBe(false);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
test('reports a remote-tracking ref that does not exist', async () => {
|
|
260
|
+
await expect(gitService.setUpstream(work, 'main', 'origin', 'nope')).rejects.toThrow(
|
|
261
|
+
/could not set upstream/i
|
|
262
|
+
);
|
|
263
|
+
});
|
|
264
|
+
});
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
import { streamManager, type StreamState } from '../chat/stream-manager.js';
|
|
7
7
|
import { ws } from '../utils/ws.js';
|
|
8
|
+
import { sessionQueries } from '../database/queries/session-queries.js';
|
|
8
9
|
import type { UnifiedMessage } from '$shared/types/unified';
|
|
9
10
|
import { isWaitingForInteractiveInput } from '$shared/utils/interactive-input';
|
|
10
11
|
|
|
@@ -25,6 +26,21 @@ function detectStreamWaitingInput(stream: StreamState): boolean {
|
|
|
25
26
|
return isWaitingForInteractiveInput(messages);
|
|
26
27
|
}
|
|
27
28
|
|
|
29
|
+
/**
|
|
30
|
+
* Which tree a stream runs in (null = the main project tree).
|
|
31
|
+
* Resolved from the session rather than the stream, because a stream never
|
|
32
|
+
* carries the worktree it was started in — and the callers need it to tell
|
|
33
|
+
* apart "this project is busy" from "this worktree is busy".
|
|
34
|
+
*/
|
|
35
|
+
function resolveStreamWorktreeId(chatSessionId: string, memo: Map<string, string | null>): string | null {
|
|
36
|
+
const cached = memo.get(chatSessionId);
|
|
37
|
+
if (cached !== undefined) return cached;
|
|
38
|
+
|
|
39
|
+
const worktreeId = sessionQueries.getById(chatSessionId)?.worktree_id ?? null;
|
|
40
|
+
memo.set(chatSessionId, worktreeId);
|
|
41
|
+
return worktreeId;
|
|
42
|
+
}
|
|
43
|
+
|
|
28
44
|
// Store active users per project (shared with main endpoint)
|
|
29
45
|
const projectUsers = new Map<string, Set<{ userId: string; userName: string; lastSeen: number }>>();
|
|
30
46
|
|
|
@@ -57,6 +73,7 @@ export async function getProjectStatusData(projectId?: string) {
|
|
|
57
73
|
|
|
58
74
|
// Get per-chat-session user presence from WS rooms
|
|
59
75
|
const chatSessionUsers = ws.getProjectChatSessions(projectId);
|
|
76
|
+
const worktreeMemo = new Map<string, string | null>();
|
|
60
77
|
|
|
61
78
|
return {
|
|
62
79
|
projectId,
|
|
@@ -69,6 +86,7 @@ export async function getProjectStatusData(projectId?: string) {
|
|
|
69
86
|
streams: allProjectStreams.map(s => ({
|
|
70
87
|
streamId: s.streamId,
|
|
71
88
|
chatSessionId: s.chatSessionId,
|
|
89
|
+
worktreeId: resolveStreamWorktreeId(s.chatSessionId, worktreeMemo),
|
|
72
90
|
status: s.status,
|
|
73
91
|
startedAt: s.startedAt,
|
|
74
92
|
messagesCount: s.messages.length,
|
|
@@ -91,6 +109,7 @@ export async function getProjectStatusData(projectId?: string) {
|
|
|
91
109
|
|
|
92
110
|
// Get all active streams grouped by project
|
|
93
111
|
const allStreams = streamManager.getAllStreams();
|
|
112
|
+
const worktreeMemo = new Map<string, string | null>();
|
|
94
113
|
allStreams.forEach(stream => {
|
|
95
114
|
if (stream.projectId) {
|
|
96
115
|
if (!allProjects.has(stream.projectId)) {
|
|
@@ -111,6 +130,7 @@ export async function getProjectStatusData(projectId?: string) {
|
|
|
111
130
|
projectData.streams.push({
|
|
112
131
|
streamId: stream.streamId,
|
|
113
132
|
chatSessionId: stream.chatSessionId,
|
|
133
|
+
worktreeId: resolveStreamWorktreeId(stream.chatSessionId, worktreeMemo),
|
|
114
134
|
status: stream.status,
|
|
115
135
|
startedAt: stream.startedAt,
|
|
116
136
|
messagesCount: stream.messages.length,
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
import { t } from 'elysia';
|
|
8
8
|
import { createRouter } from '$shared/utils/ws-server';
|
|
9
9
|
import { execGit } from '../../git/git-executor';
|
|
10
|
+
import { buildBudgetedStagedDiff, BRANCH_NAME_BUDGET } from './diff-budget';
|
|
10
11
|
import { initializeEngine } from '../../engine';
|
|
11
12
|
import { resolveGenerationTarget } from '../../engine/resolve-model';
|
|
12
13
|
import type { EngineType } from '$shared/types/unified';
|
|
@@ -147,10 +148,10 @@ export const branchNameHandler = createRouter()
|
|
|
147
148
|
const { root } = requireProjectWorkspace(conn, data.projectId);
|
|
148
149
|
const cwd = resolveRepoCwd(root, data.repoPath);
|
|
149
150
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
if (
|
|
151
|
+
// A branch name is a topic, not a description — the file list carries nearly
|
|
152
|
+
// all the signal, so this budget is much tighter than the commit-message one.
|
|
153
|
+
const diff = await buildBudgetedStagedDiff(cwd, BRANCH_NAME_BUDGET);
|
|
154
|
+
if (diff.isEmpty) {
|
|
154
155
|
throw new Error('No staged changes to generate a branch name for');
|
|
155
156
|
}
|
|
156
157
|
|
|
@@ -167,7 +168,7 @@ export const branchNameHandler = createRouter()
|
|
|
167
168
|
const separator = normalizeBranchSeparator(data.branchSeparator);
|
|
168
169
|
const instructions = createBranchNameInstructions(currentBranch, prefix, separator);
|
|
169
170
|
const extra = data.customPrompt?.trim();
|
|
170
|
-
const prompt = `${instructions}${extra ? `\n\nAdditional constraints:\n${extra}` : ''}\n\
|
|
171
|
+
const prompt = `${instructions}${extra ? `\n\nAdditional constraints:\n${extra}` : ''}\n\n${diff.text}`;
|
|
171
172
|
|
|
172
173
|
// The caller's providerSlug/account can be stale (see resolve-model.ts).
|
|
173
174
|
const target = await resolveGenerationTarget(engine, data.modelId, data.providerSlug);
|
package/backend/ws/git/branch.ts
CHANGED
|
@@ -158,6 +158,10 @@ export const branchHandler = createRouter()
|
|
|
158
158
|
projectId: t.String(),
|
|
159
159
|
branchName: t.String(),
|
|
160
160
|
noFastForward: t.Optional(t.Boolean()),
|
|
161
|
+
/** Collapse the branch into staged changes without committing. */
|
|
162
|
+
squash: t.Optional(t.Boolean()),
|
|
163
|
+
/** Refuse the merge unless it is a fast-forward. */
|
|
164
|
+
ffOnly: t.Optional(t.Boolean()),
|
|
161
165
|
repoPath: t.Optional(t.String())
|
|
162
166
|
}),
|
|
163
167
|
response: t.Object({
|
|
@@ -167,5 +171,43 @@ export const branchHandler = createRouter()
|
|
|
167
171
|
}, async ({ data, conn }) => {
|
|
168
172
|
const { root } = requireProjectWorkspace(conn, data.projectId);
|
|
169
173
|
const cwd = resolveRepoCwd(root, data.repoPath);
|
|
170
|
-
return await gitService.mergeBranch(cwd, data.branchName,
|
|
174
|
+
return await gitService.mergeBranch(cwd, data.branchName, {
|
|
175
|
+
noFastForward: data.noFastForward ?? false,
|
|
176
|
+
squash: data.squash ?? false,
|
|
177
|
+
ffOnly: data.ffOnly ?? false
|
|
178
|
+
});
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
.http('git:rebase', {
|
|
182
|
+
data: t.Object({
|
|
183
|
+
projectId: t.String(),
|
|
184
|
+
upstream: t.String({ minLength: 1 }),
|
|
185
|
+
/** Stash and restore local changes around the rebase. Defaults to true. */
|
|
186
|
+
autostash: t.Optional(t.Boolean()),
|
|
187
|
+
repoPath: t.Optional(t.String())
|
|
188
|
+
}),
|
|
189
|
+
response: t.Object({
|
|
190
|
+
success: t.Boolean(),
|
|
191
|
+
hasConflicts: t.Boolean(),
|
|
192
|
+
message: t.String()
|
|
193
|
+
})
|
|
194
|
+
}, async ({ data, conn }) => {
|
|
195
|
+
const { root } = requireProjectWorkspace(conn, data.projectId);
|
|
196
|
+
const cwd = resolveRepoCwd(root, data.repoPath);
|
|
197
|
+
return await gitService.rebaseOnto(cwd, data.upstream, data.autostash ?? true);
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
.http('git:return-to-branch', {
|
|
201
|
+
data: t.Object({
|
|
202
|
+
projectId: t.String(),
|
|
203
|
+
/** Explicit target; omit to use git's own "previous checkout" shorthand. */
|
|
204
|
+
branch: t.Optional(t.String()),
|
|
205
|
+
repoPath: t.Optional(t.String())
|
|
206
|
+
}),
|
|
207
|
+
response: t.Object({ ok: t.Boolean() })
|
|
208
|
+
}, async ({ data, conn }) => {
|
|
209
|
+
const { root } = requireProjectWorkspace(conn, data.projectId);
|
|
210
|
+
const cwd = resolveRepoCwd(root, data.repoPath);
|
|
211
|
+
await gitService.returnToBranch(cwd, data.branch);
|
|
212
|
+
return { ok: true };
|
|
171
213
|
});
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
import { t } from 'elysia';
|
|
8
8
|
import { createRouter } from '$shared/utils/ws-server';
|
|
9
|
-
import {
|
|
9
|
+
import { buildBudgetedStagedDiff, COMMIT_MESSAGE_BUDGET } from './diff-budget';
|
|
10
10
|
import { initializeEngine } from '../../engine';
|
|
11
11
|
import { resolveGenerationTarget } from '../../engine/resolve-model';
|
|
12
12
|
import type { EngineType } from '$shared/types/unified';
|
|
@@ -77,11 +77,9 @@ export const commitMessageHandler = createRouter()
|
|
|
77
77
|
const { root } = requireProjectWorkspace(conn, data.projectId);
|
|
78
78
|
const cwd = resolveRepoCwd(root, data.repoPath);
|
|
79
79
|
|
|
80
|
-
//
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if (!rawDiff.trim()) {
|
|
80
|
+
// A budgeted view of the staged change, not the raw diff — see diff-budget.ts.
|
|
81
|
+
const diff = await buildBudgetedStagedDiff(cwd, COMMIT_MESSAGE_BUDGET);
|
|
82
|
+
if (diff.isEmpty) {
|
|
85
83
|
throw new Error('No staged changes to generate a commit message for');
|
|
86
84
|
}
|
|
87
85
|
|
|
@@ -96,16 +94,21 @@ export const commitMessageHandler = createRouter()
|
|
|
96
94
|
? 'Generate a multi-line conventional commit message with type, scope, subject, AND body fields. The body should explain WHY the change was made.'
|
|
97
95
|
: 'Generate a single-line conventional commit message with type, optional scope, and subject. Leave body empty.';
|
|
98
96
|
|
|
99
|
-
|
|
97
|
+
// When the diff was sampled, say so: without this the model describes only
|
|
98
|
+
// the files it happened to see and writes a subject that misses the change.
|
|
99
|
+
const coverageNote = diff.truncated
|
|
100
|
+
? '\n- The diff below is a sample. Describe the change as a whole using the file list, not just the visible hunks.'
|
|
101
|
+
: '';
|
|
102
|
+
|
|
103
|
+
const defaultPrompt = `Analyze the following staged git change and generate a conventional commit message.
|
|
100
104
|
|
|
101
105
|
Rules:
|
|
102
106
|
- type: one of feat, fix, refactor, docs, test, chore, style, perf, ci, build
|
|
103
107
|
- scope: optional, the area of the codebase affected (e.g., git, settings, engine)
|
|
104
108
|
- subject: imperative mood, lowercase, no period at end, max 72 characters
|
|
105
|
-
- ${formatInstruction}
|
|
109
|
+
- ${formatInstruction}${coverageNote}
|
|
106
110
|
|
|
107
|
-
|
|
108
|
-
${rawDiff}`;
|
|
111
|
+
${diff.text}`;
|
|
109
112
|
|
|
110
113
|
const extra = data.customPrompt?.trim();
|
|
111
114
|
const prompt = extra
|
|
@@ -22,6 +22,38 @@ function resolveRepoCwd(projectPath: string, repoPath: string | undefined): stri
|
|
|
22
22
|
return resolved;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
const ConflictKindSchema = t.Union([
|
|
26
|
+
t.Literal('both-modified'),
|
|
27
|
+
t.Literal('both-added'),
|
|
28
|
+
t.Literal('added-by-us'),
|
|
29
|
+
t.Literal('added-by-them'),
|
|
30
|
+
t.Literal('deleted-by-us'),
|
|
31
|
+
t.Literal('deleted-by-them'),
|
|
32
|
+
t.Literal('both-deleted')
|
|
33
|
+
]);
|
|
34
|
+
|
|
35
|
+
const OperationSchema = t.Union([
|
|
36
|
+
t.Literal('rebase'),
|
|
37
|
+
t.Literal('merge'),
|
|
38
|
+
t.Literal('cherry-pick'),
|
|
39
|
+
t.Literal('revert'),
|
|
40
|
+
t.Literal('bisect'),
|
|
41
|
+
t.Null()
|
|
42
|
+
]);
|
|
43
|
+
|
|
44
|
+
const OperationStateSchema = t.Object({
|
|
45
|
+
operation: OperationSchema,
|
|
46
|
+
step: t.Optional(t.Number()),
|
|
47
|
+
total: t.Optional(t.Number()),
|
|
48
|
+
oursLabel: t.String(),
|
|
49
|
+
theirsLabel: t.String(),
|
|
50
|
+
currentCommit: t.Optional(t.String()),
|
|
51
|
+
unmergedCount: t.Number(),
|
|
52
|
+
canContinue: t.Boolean(),
|
|
53
|
+
canSkip: t.Boolean(),
|
|
54
|
+
stashConflict: t.Boolean()
|
|
55
|
+
});
|
|
56
|
+
|
|
25
57
|
const ConflictMarkerSchema = t.Object({
|
|
26
58
|
ourStart: t.Number(),
|
|
27
59
|
ourEnd: t.Number(),
|
|
@@ -42,7 +74,15 @@ export const conflictHandler = createRouter()
|
|
|
42
74
|
response: t.Array(t.Object({
|
|
43
75
|
path: t.String(),
|
|
44
76
|
content: t.String(),
|
|
45
|
-
markers: t.Array(ConflictMarkerSchema)
|
|
77
|
+
markers: t.Array(ConflictMarkerSchema),
|
|
78
|
+
kind: ConflictKindSchema,
|
|
79
|
+
contentOmitted: t.Boolean(),
|
|
80
|
+
omitReason: t.Optional(t.Union([
|
|
81
|
+
t.Literal('binary'),
|
|
82
|
+
t.Literal('missing'),
|
|
83
|
+
t.Literal('too-large')
|
|
84
|
+
])),
|
|
85
|
+
size: t.Optional(t.Number())
|
|
46
86
|
}))
|
|
47
87
|
}, async ({ data, conn }) => {
|
|
48
88
|
const { root } = requireProjectWorkspace(conn, data.projectId);
|
|
@@ -75,7 +115,14 @@ export const conflictHandler = createRouter()
|
|
|
75
115
|
data: t.Object({
|
|
76
116
|
projectId: t.String(),
|
|
77
117
|
filePath: t.String(),
|
|
78
|
-
resolution: t.Union([
|
|
118
|
+
resolution: t.Union([
|
|
119
|
+
t.Literal('ours'),
|
|
120
|
+
t.Literal('theirs'),
|
|
121
|
+
t.Literal('custom'),
|
|
122
|
+
t.Literal('keep'),
|
|
123
|
+
t.Literal('delete'),
|
|
124
|
+
t.Literal('reset')
|
|
125
|
+
]),
|
|
79
126
|
customContent: t.Optional(t.String())
|
|
80
127
|
}),
|
|
81
128
|
response: t.Object({ ok: t.Boolean() })
|
|
@@ -93,6 +140,62 @@ export const conflictHandler = createRouter()
|
|
|
93
140
|
return { ok: true };
|
|
94
141
|
})
|
|
95
142
|
|
|
143
|
+
/**
|
|
144
|
+
* Everything needed to describe (and finish) an in-progress merge/rebase.
|
|
145
|
+
* Scoped to one repo: a project with nested repos can be mid-rebase in a
|
|
146
|
+
* sub-repo while the outer tree is clean, so the caller passes `repoPath`
|
|
147
|
+
* rather than us guessing from whichever conflict happens to be first.
|
|
148
|
+
*/
|
|
149
|
+
.http('git:operation-state', {
|
|
150
|
+
data: t.Object({
|
|
151
|
+
projectId: t.String(),
|
|
152
|
+
repoPath: t.Optional(t.String())
|
|
153
|
+
}),
|
|
154
|
+
response: OperationStateSchema
|
|
155
|
+
}, async ({ data, conn }) => {
|
|
156
|
+
const { root } = requireProjectWorkspace(conn, data.projectId);
|
|
157
|
+
const cwd = resolveRepoCwd(root, data.repoPath) ?? root;
|
|
158
|
+
return await gitService.getOperationState(cwd);
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
.http('git:continue-operation', {
|
|
162
|
+
data: t.Object({
|
|
163
|
+
projectId: t.String(),
|
|
164
|
+
repoPath: t.Optional(t.String())
|
|
165
|
+
}),
|
|
166
|
+
response: t.Object({ success: t.Boolean(), message: t.String() })
|
|
167
|
+
}, async ({ data, conn }) => {
|
|
168
|
+
const { root } = requireProjectWorkspace(conn, data.projectId);
|
|
169
|
+
const cwd = resolveRepoCwd(root, data.repoPath) ?? root;
|
|
170
|
+
return await gitService.continueOperation(cwd);
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
.http('git:skip-operation', {
|
|
174
|
+
data: t.Object({
|
|
175
|
+
projectId: t.String(),
|
|
176
|
+
repoPath: t.Optional(t.String())
|
|
177
|
+
}),
|
|
178
|
+
response: t.Object({ success: t.Boolean(), message: t.String() })
|
|
179
|
+
}, async ({ data, conn }) => {
|
|
180
|
+
const { root } = requireProjectWorkspace(conn, data.projectId);
|
|
181
|
+
const cwd = resolveRepoCwd(root, data.repoPath) ?? root;
|
|
182
|
+
return await gitService.skipOperation(cwd);
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
.http('git:abort-operation', {
|
|
186
|
+
data: t.Object({
|
|
187
|
+
projectId: t.String(),
|
|
188
|
+
repoPath: t.Optional(t.String())
|
|
189
|
+
}),
|
|
190
|
+
response: t.Object({ ok: t.Boolean() })
|
|
191
|
+
}, async ({ data, conn }) => {
|
|
192
|
+
const { root } = requireProjectWorkspace(conn, data.projectId);
|
|
193
|
+
const cwd = resolveRepoCwd(root, data.repoPath) ?? root;
|
|
194
|
+
await gitService.abortOperation(cwd);
|
|
195
|
+
return { ok: true };
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
/** Retained under the old name so existing clients keep working. */
|
|
96
199
|
.http('git:abort-merge', {
|
|
97
200
|
data: t.Object({
|
|
98
201
|
projectId: t.String(),
|
|
@@ -102,6 +205,6 @@ export const conflictHandler = createRouter()
|
|
|
102
205
|
}, async ({ data, conn }) => {
|
|
103
206
|
const { root } = requireProjectWorkspace(conn, data.projectId);
|
|
104
207
|
const cwd = resolveRepoCwd(root, data.repoPath) ?? root;
|
|
105
|
-
await gitService.
|
|
208
|
+
await gitService.abortOperation(cwd);
|
|
106
209
|
return { ok: true };
|
|
107
210
|
});
|