1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
|
import { useState, useMemo, useCallback } from "react";
interface ConflictHunk {
id: string;
filePath: string;
startLine: number;
endLine: number;
ours: string[]; // Changes from current branch
theirs: string[]; // Changes from incoming branch
base?: string[]; // Original content (if 3-way merge)
resolved?: "ours" | "theirs" | "both" | "custom";
customResolution?: string[];
}
interface ConflictFile {
path: string;
hunks: ConflictHunk[];
resolved: boolean;
}
interface MergeConflictResolverProps {
conflicts: ConflictFile[];
sourceBranch: string;
targetBranch: string;
loading?: boolean;
onResolve: (resolutions: Map<string, ConflictHunk[]>) => Promise<void>;
onAbort: () => void;
onAskLLM?: (hunk: ConflictHunk) => Promise<string[]>;
}
type ResolutionChoice = "ours" | "theirs" | "both" | "custom";
function ConflictHunkView({
hunk,
sourceBranch,
targetBranch,
onResolve,
onAskLLM,
}: {
hunk: ConflictHunk;
sourceBranch: string;
targetBranch: string;
onResolve: (resolution: ResolutionChoice, customLines?: string[]) => void;
onAskLLM?: () => Promise<void>;
}) {
const [showCustomEditor, setShowCustomEditor] = useState(false);
const [customText, setCustomText] = useState(
hunk.customResolution?.join("\n") || [...hunk.ours, ...hunk.theirs].join("\n")
);
const [askingLLM, setAskingLLM] = useState(false);
const handleAskLLM = async () => {
if (!onAskLLM || askingLLM) return;
setAskingLLM(true);
try {
await onAskLLM();
} finally {
setAskingLLM(false);
}
};
const handleCustomSave = () => {
const lines = customText.split("\n");
onResolve("custom", lines);
setShowCustomEditor(false);
};
const isResolved = hunk.resolved !== undefined;
return (
<div
className={`border ${
isResolved
? "border-green-400/30 bg-green-400/5"
: "border-yellow-400/30 bg-yellow-400/5"
} mb-3`}
>
{/* Hunk header */}
<div className="flex items-center justify-between px-3 py-2 border-b border-[rgba(117,170,252,0.2)]">
<div className="font-mono text-xs text-[#75aafc]">
Lines {hunk.startLine}-{hunk.endLine}
{isResolved && (
<span className="ml-2 text-green-400">
(Resolved: {hunk.resolved})
</span>
)}
</div>
<div className="flex items-center gap-2">
{onAskLLM && (
<button
onClick={handleAskLLM}
disabled={askingLLM}
className="px-2 py-1 font-mono text-[10px] text-purple-400 border border-purple-400/30 hover:border-purple-400/50 disabled:opacity-50 transition-colors"
>
{askingLLM ? "..." : "Ask LLM"}
</button>
)}
<button
onClick={() => setShowCustomEditor(!showCustomEditor)}
className="px-2 py-1 font-mono text-[10px] text-[#75aafc] border border-[rgba(117,170,252,0.25)] hover:border-[#3f6fb3] transition-colors"
>
Edit
</button>
</div>
</div>
{/* Conflict content */}
{!showCustomEditor ? (
<div className="grid grid-cols-2 divide-x divide-[rgba(117,170,252,0.2)]">
{/* Ours (current branch) */}
<div className="p-2">
<div className="flex items-center justify-between mb-2">
<span className="font-mono text-[10px] text-[#9bc3ff] uppercase">
{targetBranch} (ours)
</span>
<button
onClick={() => onResolve("ours")}
className={`px-2 py-0.5 font-mono text-[9px] border transition-colors ${
hunk.resolved === "ours"
? "text-green-400 border-green-400/50 bg-green-400/10"
: "text-[#75aafc] border-[rgba(117,170,252,0.25)] hover:border-[#3f6fb3]"
}`}
>
Use This
</button>
</div>
<pre className="font-mono text-xs text-red-400 bg-red-400/5 p-2 overflow-x-auto">
{hunk.ours.map((line, i) => (
<div key={i}>
<span className="text-[#555] select-none mr-2">-</span>
{line}
</div>
))}
</pre>
</div>
{/* Theirs (incoming branch) */}
<div className="p-2">
<div className="flex items-center justify-between mb-2">
<span className="font-mono text-[10px] text-[#9bc3ff] uppercase">
{sourceBranch} (theirs)
</span>
<button
onClick={() => onResolve("theirs")}
className={`px-2 py-0.5 font-mono text-[9px] border transition-colors ${
hunk.resolved === "theirs"
? "text-green-400 border-green-400/50 bg-green-400/10"
: "text-[#75aafc] border-[rgba(117,170,252,0.25)] hover:border-[#3f6fb3]"
}`}
>
Use This
</button>
</div>
<pre className="font-mono text-xs text-green-400 bg-green-400/5 p-2 overflow-x-auto">
{hunk.theirs.map((line, i) => (
<div key={i}>
<span className="text-[#555] select-none mr-2">+</span>
{line}
</div>
))}
</pre>
</div>
</div>
) : (
/* Custom editor */
<div className="p-2">
<div className="flex items-center justify-between mb-2">
<span className="font-mono text-[10px] text-[#9bc3ff] uppercase">
Custom Resolution
</span>
<div className="flex items-center gap-2">
<button
onClick={() => setShowCustomEditor(false)}
className="px-2 py-0.5 font-mono text-[9px] text-[#555] hover:text-[#9bc3ff]"
>
Cancel
</button>
<button
onClick={handleCustomSave}
className="px-2 py-0.5 font-mono text-[9px] text-green-400 border border-green-400/30 hover:border-green-400/50"
>
Apply
</button>
</div>
</div>
<textarea
value={customText}
onChange={(e) => setCustomText(e.target.value)}
className="w-full bg-[rgba(0,0,0,0.3)] border border-[rgba(117,170,252,0.25)] text-[#dbe7ff] font-mono text-xs p-2 outline-none focus:border-[#3f6fb3] min-h-[100px] resize-y"
/>
</div>
)}
{/* Both option */}
<div className="px-3 py-2 border-t border-[rgba(117,170,252,0.1)] flex justify-center">
<button
onClick={() => onResolve("both")}
className={`px-3 py-1 font-mono text-[10px] border transition-colors ${
hunk.resolved === "both"
? "text-green-400 border-green-400/50 bg-green-400/10"
: "text-[#75aafc] border-[rgba(117,170,252,0.25)] hover:border-[#3f6fb3]"
}`}
>
Keep Both
</button>
</div>
</div>
);
}
function ConflictFileView({
file,
sourceBranch,
targetBranch,
onResolveHunk,
onAskLLM,
}: {
file: ConflictFile;
sourceBranch: string;
targetBranch: string;
onResolveHunk: (hunkId: string, resolution: ResolutionChoice, customLines?: string[]) => void;
onAskLLM?: (hunk: ConflictHunk) => Promise<string[]>;
}) {
const [expanded, setExpanded] = useState(true);
const resolvedCount = file.hunks.filter((h) => h.resolved !== undefined).length;
return (
<div className="border border-[rgba(117,170,252,0.2)] mb-3">
{/* File header */}
<button
onClick={() => setExpanded(!expanded)}
className="w-full flex items-center gap-2 px-3 py-2 bg-[rgba(0,0,0,0.2)] hover:bg-[rgba(0,0,0,0.3)] text-left"
>
<span className="font-mono text-[10px] text-[#555]">
{expanded ? "▼" : "▶"}
</span>
<span
className={`px-1.5 py-0.5 font-mono text-[9px] ${
file.resolved
? "text-green-400 bg-green-400/10"
: "text-yellow-400 bg-yellow-400/10"
}`}
>
{file.resolved ? "RESOLVED" : "CONFLICT"}
</span>
<span className="font-mono text-sm text-[#dbe7ff] flex-1 truncate">
{file.path}
</span>
<span className="font-mono text-[10px] text-[#555]">
{resolvedCount}/{file.hunks.length} hunks
</span>
</button>
{/* Hunks */}
{expanded && (
<div className="p-3">
{file.hunks.map((hunk) => (
<ConflictHunkView
key={hunk.id}
hunk={hunk}
sourceBranch={sourceBranch}
targetBranch={targetBranch}
onResolve={(resolution, customLines) =>
onResolveHunk(hunk.id, resolution, customLines)
}
onAskLLM={
onAskLLM
? async () => {
const resolution = await onAskLLM(hunk);
onResolveHunk(hunk.id, "custom", resolution);
}
: undefined
}
/>
))}
</div>
)}
</div>
);
}
export function MergeConflictResolver({
conflicts: initialConflicts,
sourceBranch,
targetBranch,
loading = false,
onResolve,
onAbort,
onAskLLM,
}: MergeConflictResolverProps) {
const [conflicts, setConflicts] = useState<ConflictFile[]>(initialConflicts);
const [resolving, setResolving] = useState(false);
const [error, setError] = useState<string | null>(null);
// Calculate resolution stats
const stats = useMemo(() => {
const totalHunks = conflicts.reduce((sum, f) => sum + f.hunks.length, 0);
const resolvedHunks = conflicts.reduce(
(sum, f) => sum + f.hunks.filter((h) => h.resolved !== undefined).length,
0
);
const resolvedFiles = conflicts.filter((f) => f.resolved).length;
return {
totalFiles: conflicts.length,
resolvedFiles,
totalHunks,
resolvedHunks,
allResolved: resolvedHunks === totalHunks,
};
}, [conflicts]);
// Handle resolving a single hunk
const handleResolveHunk = useCallback(
(filePath: string, hunkId: string, resolution: ResolutionChoice, customLines?: string[]) => {
setConflicts((prev) =>
prev.map((file) => {
if (file.path !== filePath) return file;
const updatedHunks = file.hunks.map((hunk) => {
if (hunk.id !== hunkId) return hunk;
return {
...hunk,
resolved: resolution,
customResolution: customLines,
};
});
const allHunksResolved = updatedHunks.every((h) => h.resolved !== undefined);
return {
...file,
hunks: updatedHunks,
resolved: allHunksResolved,
};
})
);
},
[]
);
// Resolve all hunks in a file with same choice
const handleResolveFileAll = useCallback(
(filePath: string, resolution: ResolutionChoice) => {
setConflicts((prev) =>
prev.map((file) => {
if (file.path !== filePath) return file;
const updatedHunks = file.hunks.map((hunk) => ({
...hunk,
resolved: resolution,
customResolution:
resolution === "both"
? [...hunk.ours, ...hunk.theirs]
: resolution === "ours"
? hunk.ours
: resolution === "theirs"
? hunk.theirs
: undefined,
}));
return {
...file,
hunks: updatedHunks,
resolved: true,
};
})
);
},
[]
);
// Apply all resolutions
const handleApplyResolutions = async () => {
if (!stats.allResolved || resolving) return;
setResolving(true);
setError(null);
try {
const resolutionMap = new Map<string, ConflictHunk[]>();
conflicts.forEach((file) => {
resolutionMap.set(file.path, file.hunks);
});
await onResolve(resolutionMap);
} catch (err) {
setError(err instanceof Error ? err.message : "Failed to apply resolutions");
} finally {
setResolving(false);
}
};
if (loading) {
return (
<div className="panel p-4">
<div className="flex items-center justify-center h-32">
<div className="font-mono text-sm text-[#75aafc]">
Analyzing conflicts...
</div>
</div>
</div>
);
}
return (
<div className="panel flex flex-col max-h-[80vh] overflow-hidden">
{/* Header */}
<div className="flex items-center justify-between p-4 border-b border-[rgba(117,170,252,0.2)] shrink-0">
<div className="flex items-center gap-3">
<div className="font-mono text-xs text-[#9bc3ff] tracking-wide uppercase">
Merge Conflicts
</div>
<span className="px-2 py-0.5 font-mono text-[10px] text-yellow-400 bg-yellow-400/10 border border-yellow-400/20">
{sourceBranch} → {targetBranch}
</span>
</div>
<button
onClick={onAbort}
className="font-mono text-xs text-red-400 hover:text-red-300"
>
Abort Merge
</button>
</div>
{/* Progress */}
<div className="px-4 py-3 border-b border-[rgba(117,170,252,0.1)] shrink-0">
<div className="flex items-center justify-between mb-2">
<span className="font-mono text-[10px] text-[#75aafc]">
{stats.resolvedFiles}/{stats.totalFiles} files resolved
</span>
<span className="font-mono text-[10px] text-[#75aafc]">
{stats.resolvedHunks}/{stats.totalHunks} conflicts resolved
</span>
</div>
<div className="h-1.5 bg-[rgba(117,170,252,0.1)] rounded-full overflow-hidden">
<div
className="h-full bg-green-400 transition-all"
style={{
width: `${(stats.resolvedHunks / stats.totalHunks) * 100}%`,
}}
/>
</div>
</div>
{/* Error */}
{error && (
<div className="mx-4 mt-3 bg-red-400/10 border border-red-400/30 p-3 font-mono text-xs text-red-400 shrink-0">
{error}
</div>
)}
{/* Conflict files */}
<div className="flex-1 overflow-y-auto p-4">
{conflicts.map((file) => (
<ConflictFileView
key={file.path}
file={file}
sourceBranch={sourceBranch}
targetBranch={targetBranch}
onResolveHunk={(hunkId, resolution, customLines) =>
handleResolveHunk(file.path, hunkId, resolution, customLines)
}
onAskLLM={onAskLLM}
/>
))}
</div>
{/* Footer actions */}
<div className="flex items-center justify-between p-4 border-t border-[rgba(117,170,252,0.2)] shrink-0">
<div className="flex items-center gap-2">
<button
onClick={() =>
conflicts.forEach((f) => handleResolveFileAll(f.path, "ours"))
}
className="px-3 py-1.5 font-mono text-[10px] text-[#75aafc] border border-[rgba(117,170,252,0.25)] hover:border-[#3f6fb3] transition-colors"
>
Accept All Ours
</button>
<button
onClick={() =>
conflicts.forEach((f) => handleResolveFileAll(f.path, "theirs"))
}
className="px-3 py-1.5 font-mono text-[10px] text-[#75aafc] border border-[rgba(117,170,252,0.25)] hover:border-[#3f6fb3] transition-colors"
>
Accept All Theirs
</button>
</div>
<button
onClick={handleApplyResolutions}
disabled={!stats.allResolved || resolving}
className="px-4 py-2 font-mono text-xs text-green-400 border border-green-400/30 hover:border-green-400/50 hover:bg-green-400/10 disabled:opacity-50 disabled:cursor-not-allowed transition-colors uppercase"
>
{resolving
? "Applying..."
: stats.allResolved
? "Complete Merge"
: `Resolve ${stats.totalHunks - stats.resolvedHunks} Conflicts`}
</button>
</div>
</div>
);
}
// Export types for use in other components
export type { ConflictHunk, ConflictFile, ResolutionChoice };
|