-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathdiffStat.tsx
More file actions
97 lines (88 loc) · 2.98 KB
/
Copy pathdiffStat.tsx
File metadata and controls
97 lines (88 loc) · 2.98 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
import { FileDiff } from "@/features/git";
const TOTAL_SQUARES = 5;
// Count `+`/`-` lines across all hunks in a file.
export const computeChangeCounts = (file: FileDiff) => {
let additions = 0;
let deletions = 0;
for (const hunk of file.hunks) {
for (const raw of hunk.body.split('\n')) {
if (raw.startsWith('+')) {
additions++;
} else if (raw.startsWith('-')) {
deletions++;
}
}
}
return { additions, deletions };
};
// Sum line-level change counts across multiple files.
export const computeTotalChangeCounts = (files: FileDiff[]) => {
let additions = 0;
let deletions = 0;
for (const file of files) {
const counts = computeChangeCounts(file);
additions += counts.additions;
deletions += counts.deletions;
}
return { additions, deletions };
};
// Map a total change count to a number of filled squares (0–5) using a
// log-ish scale so tiny diffs still show one square and huge diffs cap out.
// Mirrors GitHub's diffstat indicator behavior.
const filledSquaresForTotal = (total: number): number => {
if (total === 0) {
return 0;
}
if (total < 5) {
return 1;
}
if (total < 10) {
return 2;
}
if (total < 30) {
return 3;
}
if (total < 100) {
return 4;
}
return 5;
};
interface DiffStatProps {
additions: number;
deletions: number;
}
export const DiffStat = ({ additions, deletions }: DiffStatProps) => {
const total = additions + deletions;
// Skip rendering when there are no line-level changes (e.g. pure renames).
if (total === 0) {
return null;
}
const filled = filledSquaresForTotal(total);
const greenCount = Math.round((filled * additions) / total);
const redCount = filled - greenCount;
const emptyCount = TOTAL_SQUARES - filled;
return (
<div
className="flex flex-row items-center gap-2 text-xs flex-shrink-0 font-mono"
title={`${additions} additions, ${deletions} deletions`}
>
{additions > 0 && (
<span className="text-green-700 dark:text-green-400">+{additions}</span>
)}
{deletions > 0 && (
<span className="text-red-700 dark:text-red-400">-{deletions}</span>
)}
<div className="flex flex-row gap-px">
{Array.from({ length: greenCount }).map((_, i) => (
<span key={`g-${i}`} className="w-2 h-2 bg-green-500 dark:bg-green-400 rounded-[1px]" />
))}
{Array.from({ length: redCount }).map((_, i) => (
<span key={`r-${i}`} className="w-2 h-2 bg-red-500 dark:bg-red-400 rounded-[1px]" />
))}
{Array.from({ length: emptyCount }).map((_, i) => (
<span key={`e-${i}`} className="w-2 h-2 bg-border rounded-[1px]" />
))}
</div>
</div>
);
};