-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBranchComparison.tsx
More file actions
executable file
·513 lines (482 loc) · 18.8 KB
/
BranchComparison.tsx
File metadata and controls
executable file
·513 lines (482 loc) · 18.8 KB
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
505
506
507
508
509
510
511
512
513
import { createSignal, createEffect, For, Show, createMemo } from "solid-js";
import { Icon } from "../ui/Icon";
import { gitCompare, GitCompareResult } from "../../utils/tauri-api";
import { getProjectPath } from "../../utils/workspace";
interface CompareCommit {
hash: string;
shortHash: string;
message: string;
author: string;
date: string;
}
interface CompareFile {
path: string;
status: "added" | "modified" | "deleted" | "renamed";
additions: number;
deletions: number;
oldPath?: string;
}
interface BranchCompareStats {
ahead: number;
behind: number;
commits: CompareCommit[];
files: CompareFile[];
totalAdditions: number;
totalDeletions: number;
}
interface BranchComparisonProps {
baseBranch?: string;
compareBranch?: string;
branches: { name: string; current: boolean }[];
onClose?: () => void;
onFileSelect?: (path: string) => void;
onMerge?: (from: string, to: string) => void;
onCreatePR?: (from: string, to: string) => void;
}
export function BranchComparison(props: BranchComparisonProps) {
const [baseBranch, setBaseBranch] = createSignal(props.baseBranch || "");
const [compareBranch, setCompareBranch] = createSignal(props.compareBranch || "");
const [loading, setLoading] = createSignal(false);
const [stats, setStats] = createSignal<BranchCompareStats | null>(null);
const [showBaseDropdown, setShowBaseDropdown] = createSignal(false);
const [showCompareDropdown, setShowCompareDropdown] = createSignal(false);
const [_expandedSections, _setExpandedSections] = createSignal({
commits: true,
files: true
});
const [viewMode, setViewMode] = createSignal<"commits" | "files">("commits");
createEffect(() => {
if (props.baseBranch) setBaseBranch(props.baseBranch);
if (props.compareBranch) setCompareBranch(props.compareBranch);
});
createEffect(() => {
const base = baseBranch();
const compare = compareBranch();
if (base && compare && base !== compare) {
fetchComparison(base, compare);
}
});
const fetchComparison = async (base: string, compare: string) => {
setLoading(true);
try {
const projectPath = getProjectPath();
const data: GitCompareResult = await gitCompare(projectPath, base, compare);
// Map to component's BranchCompareStats interface
setStats({
ahead: data.ahead,
behind: data.behind,
commits: data.commits.map(c => ({
hash: c.hash,
shortHash: c.shortHash,
message: c.message,
author: c.author,
date: c.date
})),
files: data.files.map(f => ({
path: f.path,
status: f.status,
additions: f.additions,
deletions: f.deletions,
oldPath: f.oldPath
})),
totalAdditions: data.totalAdditions,
totalDeletions: data.totalDeletions
});
} catch (err) {
console.error("Failed to fetch comparison:", err);
} finally {
setLoading(false);
}
};
const swapBranches = () => {
const base = baseBranch();
const compare = compareBranch();
setBaseBranch(compare);
setCompareBranch(base);
};
// Prepared for expandable sections (future feature)
const _toggleSection = (section: "commits" | "files") => {
_setExpandedSections(prev => ({
...prev,
[section]: !prev[section]
}));
};
const getFileIcon = (status: string) => {
switch (status) {
case "added":
return <Icon name="plus" class="w-4 h-4 text-green-400" />;
case "deleted":
return <Icon name="minus" class="w-4 h-4 text-red-400" />;
case "modified":
return <Icon name="pen" class="w-4 h-4 text-yellow-400" />;
case "renamed":
return <Icon name="arrow-right" class="w-4 h-4 text-blue-400" />;
default:
return <Icon name="file" class="w-4 h-4" style={{ color: "var(--text-weak)" }} />;
}
};
const formatDate = (dateStr: string): string => {
const date = new Date(dateStr);
const now = new Date();
const diffMs = now.getTime() - date.getTime();
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
if (diffDays === 0) return "today";
if (diffDays === 1) return "yesterday";
if (diffDays < 7) return `${diffDays} days ago`;
if (diffDays < 30) return `${Math.floor(diffDays / 7)} weeks ago`;
return date.toLocaleDateString();
};
// Prepared for branch filtering (future feature)
const _filteredBranches = createMemo(() => {
return props.branches.filter(b =>
b.name !== baseBranch() && b.name !== compareBranch()
);
});
return (
<div
class="h-full flex flex-col overflow-hidden"
style={{ background: "var(--background-base)" }}
>
{/* Header */}
<div
class="flex items-center justify-between px-4 py-3 border-b shrink-0"
style={{ "border-color": "var(--border-weak)" }}
>
<div class="flex items-center gap-2">
<Icon name="code-branch" class="w-5 h-5" style={{ color: "var(--text-weak)" }} />
<h2 class="text-sm font-medium" style={{ color: "var(--text-base)" }}>
Compare Branches
</h2>
</div>
<Show when={props.onClose}>
<button
class="p-1 rounded hover:bg-white/10"
onClick={props.onClose}
>
<Icon name="xmark" class="w-5 h-5" style={{ color: "var(--text-weak)" }} />
</button>
</Show>
</div>
{/* Branch selectors */}
<div
class="flex items-center gap-3 px-4 py-3 border-b"
style={{ "border-color": "var(--border-weak)" }}
>
{/* Base branch */}
<div class="relative flex-1">
<button
class="w-full flex items-center justify-between px-3 py-2 rounded text-sm"
style={{
background: "var(--background-stronger)",
color: "var(--text-base)",
border: "1px solid var(--border-weak)"
}}
onClick={() => setShowBaseDropdown(!showBaseDropdown())}
>
<div class="flex items-center gap-2">
<Icon name="code-branch" class="w-4 h-4" style={{ color: "var(--text-weak)" }} />
<span>{baseBranch() || "Select base branch"}</span>
</div>
<Icon name="chevron-down" class="w-4 h-4" style={{ color: "var(--text-weak)" }} />
</button>
<Show when={showBaseDropdown()}>
<div
class="absolute top-full left-0 right-0 mt-1 z-10 rounded shadow-lg max-h-48 overflow-y-auto"
style={{ background: "var(--surface-raised)" }}
>
<For each={props.branches.filter(b => b.name !== compareBranch())}>
{(branch) => (
<button
class="w-full flex items-center gap-2 px-3 py-2 text-left hover:bg-white/10"
onClick={() => {
setBaseBranch(branch.name);
setShowBaseDropdown(false);
}}
>
<Icon name="code-branch" class="w-4 h-4" style={{ color: "var(--text-weak)" }} />
<span class="text-sm" style={{ color: "var(--text-base)" }}>
{branch.name}
</span>
<Show when={branch.current}>
<span class="text-xs px-1 rounded" style={{ background: "var(--accent-primary)", color: "white" }}>
current
</span>
</Show>
</button>
)}
</For>
</div>
</Show>
</div>
{/* Swap button */}
<button
class="p-2 rounded hover:bg-white/10 transition-colors shrink-0"
onClick={swapBranches}
title="Swap branches"
>
<Icon name="rotate" class="w-4 h-4" style={{ color: "var(--text-weak)" }} />
</button>
{/* Compare branch */}
<div class="relative flex-1">
<button
class="w-full flex items-center justify-between px-3 py-2 rounded text-sm"
style={{
background: "var(--background-stronger)",
color: "var(--text-base)",
border: "1px solid var(--border-weak)"
}}
onClick={() => setShowCompareDropdown(!showCompareDropdown())}
>
<div class="flex items-center gap-2">
<Icon name="code-branch" class="w-4 h-4" style={{ color: "var(--text-weak)" }} />
<span>{compareBranch() || "Select compare branch"}</span>
</div>
<Icon name="chevron-down" class="w-4 h-4" style={{ color: "var(--text-weak)" }} />
</button>
<Show when={showCompareDropdown()}>
<div
class="absolute top-full left-0 right-0 mt-1 z-10 rounded shadow-lg max-h-48 overflow-y-auto"
style={{ background: "var(--surface-raised)" }}
>
<For each={props.branches.filter(b => b.name !== baseBranch())}>
{(branch) => (
<button
class="w-full flex items-center gap-2 px-3 py-2 text-left hover:bg-white/10"
onClick={() => {
setCompareBranch(branch.name);
setShowCompareDropdown(false);
}}
>
<Icon name="code-branch" class="w-4 h-4" style={{ color: "var(--text-weak)" }} />
<span class="text-sm" style={{ color: "var(--text-base)" }}>
{branch.name}
</span>
<Show when={branch.current}>
<span class="text-xs px-1 rounded" style={{ background: "var(--accent-primary)", color: "white" }}>
current
</span>
</Show>
</button>
)}
</For>
</div>
</Show>
</div>
</div>
{/* Stats summary */}
<Show when={stats()}>
<div
class="flex items-center gap-4 px-4 py-2 border-b"
style={{ "border-color": "var(--border-weak)" }}
>
<div class="flex items-center gap-2">
<span class="text-sm" style={{ color: "var(--text-weak)" }}>
{compareBranch()} is
</span>
<Show when={stats()!.ahead > 0}>
<span class="text-sm text-green-400">
{stats()!.ahead} commit{stats()!.ahead !== 1 ? "s" : ""} ahead
</span>
</Show>
<Show when={stats()!.ahead > 0 && stats()!.behind > 0}>
<span class="text-sm" style={{ color: "var(--text-weak)" }}>and</span>
</Show>
<Show when={stats()!.behind > 0}>
<span class="text-sm text-orange-400">
{stats()!.behind} commit{stats()!.behind !== 1 ? "s" : ""} behind
</span>
</Show>
<span class="text-sm" style={{ color: "var(--text-weak)" }}>
{baseBranch()}
</span>
</div>
<div class="flex-1" />
<div class="flex items-center gap-3 text-xs">
<span class="text-green-400">+{stats()!.totalAdditions}</span>
<span class="text-red-400">-{stats()!.totalDeletions}</span>
<span style={{ color: "var(--text-weak)" }}>
{stats()!.files.length} files
</span>
</div>
</div>
{/* View mode tabs */}
<div
class="flex items-center gap-2 px-4 py-2 border-b"
style={{ "border-color": "var(--border-weak)" }}
>
<button
class={`px-3 py-1 rounded text-sm transition-colors ${
viewMode() === "commits" ? "" : "hover:bg-white/5"
}`}
style={{
background: viewMode() === "commits" ? "var(--accent-primary)" : "transparent",
color: viewMode() === "commits" ? "white" : "var(--text-weak)"
}}
onClick={() => setViewMode("commits")}
>
<Icon name="code-commit" class="w-4 h-4 inline-block mr-1" />
Commits ({stats()!.commits.length})
</button>
<button
class={`px-3 py-1 rounded text-sm transition-colors ${
viewMode() === "files" ? "" : "hover:bg-white/5"
}`}
style={{
background: viewMode() === "files" ? "var(--accent-primary)" : "transparent",
color: viewMode() === "files" ? "white" : "var(--text-weak)"
}}
onClick={() => setViewMode("files")}
>
<Icon name="file" class="w-4 h-4 inline-block mr-1" />
Files ({stats()!.files.length})
</button>
</div>
</Show>
{/* Content */}
<div class="flex-1 overflow-auto">
<Show when={loading()}>
<div class="flex items-center justify-center h-32">
<span style={{ color: "var(--text-weak)" }}>Loading comparison...</span>
</div>
</Show>
<Show when={!loading() && !baseBranch() || !compareBranch()}>
<div class="flex flex-col items-center justify-center h-32 gap-2">
<Icon name="code-branch" class="w-8 h-8" style={{ color: "var(--text-weaker)" }} />
<span style={{ color: "var(--text-weak)" }}>
Select two branches to compare
</span>
</div>
</Show>
<Show when={!loading() && stats() && viewMode() === "commits"}>
<div class="py-2">
<For each={stats()!.commits}>
{(commit) => (
<div
class="flex items-start gap-3 px-4 py-2 hover:bg-white/5 cursor-pointer"
onClick={() => {/* Show commit details */}}
>
<div
class="w-8 h-8 rounded-full flex items-center justify-center shrink-0"
style={{ background: "var(--accent-primary)" }}
>
<Icon name="code-commit" class="w-4 h-4 text-white" />
</div>
<div class="flex-1 min-w-0">
<div class="flex items-center gap-2">
<span
class="text-sm font-mono"
style={{ color: "var(--text-weak)" }}
>
{commit.shortHash}
</span>
<span
class="text-sm truncate"
style={{ color: "var(--text-base)" }}
>
{commit.message.split("\n")[0]}
</span>
</div>
<div class="flex items-center gap-2 text-xs mt-1" style={{ color: "var(--text-weaker)" }}>
<span>{commit.author}</span>
<span>•</span>
<span>{formatDate(commit.date)}</span>
</div>
</div>
</div>
)}
</For>
<Show when={stats()!.commits.length === 0}>
<div class="flex items-center justify-center h-24">
<span style={{ color: "var(--text-weak)" }}>No commits to compare</span>
</div>
</Show>
</div>
</Show>
<Show when={!loading() && stats() && viewMode() === "files"}>
<div class="py-2">
<For each={stats()!.files}>
{(file) => (
<div
class="flex items-center gap-3 px-4 py-2 hover:bg-white/5 cursor-pointer group"
onClick={() => props.onFileSelect?.(file.path)}
>
{getFileIcon(file.status)}
<div class="flex-1 min-w-0">
<Show when={file.status === "renamed" && file.oldPath}>
<span
class="text-sm truncate block"
style={{ color: "var(--text-weak)" }}
>
{file.oldPath} →
</span>
</Show>
<span
class="text-sm truncate block"
style={{ color: "var(--text-base)" }}
>
{file.path}
</span>
</div>
<div class="flex items-center gap-2 text-xs shrink-0">
<Show when={file.additions > 0}>
<span class="text-green-400">+{file.additions}</span>
</Show>
<Show when={file.deletions > 0}>
<span class="text-red-400">-{file.deletions}</span>
</Show>
</div>
<button
class="opacity-0 group-hover:opacity-100 p-1 rounded hover:bg-white/10"
onClick={(e) => {
e.stopPropagation();
props.onFileSelect?.(file.path);
}}
>
<Icon name="arrow-up-right-from-square" class="w-4 h-4" style={{ color: "var(--text-weak)" }} />
</button>
</div>
)}
</For>
<Show when={stats()!.files.length === 0}>
<div class="flex items-center justify-center h-24">
<span style={{ color: "var(--text-weak)" }}>No file changes</span>
</div>
</Show>
</div>
</Show>
</div>
{/* Actions footer */}
<Show when={stats() && (stats()!.ahead > 0 || stats()!.behind > 0)}>
<div
class="flex items-center justify-end gap-2 px-4 py-3 border-t"
style={{ "border-color": "var(--border-weak)" }}
>
<Show when={props.onCreatePR}>
<button
class="flex items-center gap-2 px-4 py-2 rounded text-sm transition-colors"
style={{
background: "var(--surface-active)",
color: "var(--text-base)"
}}
onClick={() => props.onCreatePR?.(compareBranch(), baseBranch())}
>
<Icon name="arrow-up-right-from-square" class="w-4 h-4" />
Create Pull Request
</button>
</Show>
<Show when={props.onMerge}>
<button
class="flex items-center gap-2 px-4 py-2 rounded text-sm font-medium transition-colors"
style={{ background: "var(--accent-primary)", color: "white" }}
onClick={() => props.onMerge?.(compareBranch(), baseBranch())}
>
<Icon name="code-branch" class="w-4 h-4" />
Merge {compareBranch()} into {baseBranch()}
</button>
</Show>
</div>
</Show>
</div>
);
}
export default BranchComparison;