Open History to see the last commit
1import { parsePatchFiles } from "@pierre/diffs/utils/parsePatchFiles";
2import { parseDiffFromFile } from "@pierre/diffs";
3import type { FileDiffMetadata } from "@pierre/diffs/types";
4
5const DIFF_THEME_NAMES = {
6 light: "pierre-light",
7 dark: "pierre-dark",
8} as const;
9
10export type DiffThemeName = (typeof DIFF_THEME_NAMES)[keyof typeof DIFF_THEME_NAMES];
11
12export function resolveDiffThemeName(theme: "light" | "dark"): DiffThemeName {
13 return theme === "dark" ? DIFF_THEME_NAMES.dark : DIFF_THEME_NAMES.light;
14}
15
16const FNV_OFFSET_BASIS_32 = 0x811c9dc5;
17const FNV_PRIME_32 = 0x01000193;
18const SECONDARY_HASH_SEED = 0x9e3779b9;
19const SECONDARY_HASH_MULTIPLIER = 0x85ebca6b;
20
21export function fnv1a32(
22 input: string,
23 seed = FNV_OFFSET_BASIS_32,
24 multiplier = FNV_PRIME_32,
25): number {
26 let hash = seed >>> 0;
27 for (let index = 0; index < input.length; index += 1) {
28 hash ^= input.charCodeAt(index);
29 hash = Math.imul(hash, multiplier) >>> 0;
30 }
31 return hash >>> 0;
32}
33
34function buildPatchCacheKey(patch: string, scope = "diff-panel"): string {
35 const normalizedPatch = patch.trim();
36 const primary = fnv1a32(normalizedPatch, FNV_OFFSET_BASIS_32, FNV_PRIME_32).toString(36);
37 const secondary = fnv1a32(
38 normalizedPatch,
39 SECONDARY_HASH_SEED,
40 SECONDARY_HASH_MULTIPLIER,
41 ).toString(36);
42 return `${scope}:${normalizedPatch.length}:${primary}:${secondary}`;
43}
44
45export type RenderablePatch =
46 | {
47 kind: "files";
48 files: FileDiffMetadata[];
49 sourceFiles: FileDiffMetadata[];
50 }
51 | {
52 kind: "raw";
53 text: string;
54 reason: string;
55 };
56
57export interface DiffLineStat {
58 additions: number;
59 deletions: number;
60}
61
62export function getDiffLineStat(files: ReadonlyArray<FileDiffMetadata>): DiffLineStat {
63 return files.reduce<DiffLineStat>(
64 (total, file) => {
65 for (const hunk of file.hunks) {
66 total.additions += hunk.additionLines;
67 total.deletions += hunk.deletionLines;
68 }
69
70 return total;
71 },
72 { additions: 0, deletions: 0 },
73 );
74}
75
76interface RenderablePatchOptions {
77 ignoreWhitespace?: boolean;
78 /**
79 * Pierre's partial-patch parser keeps hunk render starts in source-file
80 * coordinates. Its virtualizer iterates partial patches as compact rows, so
81 * review diffs need compact render starts while retaining collapsedBefore
82 * for the "N unmodified lines" separator.
83 */
84 compactPartialHunkOffsets?: boolean;
85}
86
87function hideWhitespaceChanges(file: FileDiffMetadata): FileDiffMetadata {
88 let splitDelta = 0;
89 let unifiedDelta = 0;
90 const hunks = file.hunks.map((hunk) => {
91 const oldContents = file.deletionLines
92 .slice(hunk.deletionLineIndex, hunk.deletionLineIndex + hunk.deletionCount)
93 .map((line) => `${line.replace(/\s/g, "")}\n`)
94 .join("");
95 const newContents = file.additionLines
96 .slice(hunk.additionLineIndex, hunk.additionLineIndex + hunk.additionCount)
97 .map((line) => `${line.replace(/\s/g, "")}\n`)
98 .join("");
99 const filtered = parseDiffFromFile(
100 { name: file.name, contents: oldContents },
101 { name: file.name, contents: newContents },
102 { context: Infinity },
103 ).hunks[0];
104 const next = {
105 ...hunk,
106 additionLines: filtered?.additionLines ?? 0,
107 deletionLines: filtered?.deletionLines ?? 0,
108 hunkContent: filtered
109 ? filtered.hunkContent.map((content) => ({
110 ...content,
111 additionLineIndex: content.additionLineIndex + hunk.additionLineIndex,
112 deletionLineIndex: content.deletionLineIndex + hunk.deletionLineIndex,
113 }))
114 : [
115 {
116 type: "context" as const,
117 lines: hunk.additionCount,
118 additionLineIndex: hunk.additionLineIndex,
119 deletionLineIndex: hunk.deletionLineIndex,
120 },
121 ],
122 splitLineStart: hunk.splitLineStart + splitDelta,
123 unifiedLineStart: hunk.unifiedLineStart + unifiedDelta,
124 splitLineCount: filtered?.splitLineCount ?? hunk.additionCount,
125 unifiedLineCount: filtered?.unifiedLineCount ?? hunk.additionCount,
126 };
127 splitDelta += next.splitLineCount - hunk.splitLineCount;
128 unifiedDelta += next.unifiedLineCount - hunk.unifiedLineCount;
129 return next;
130 });
131 return {
132 ...file,
133 hunks,
134 splitLineCount: file.splitLineCount + splitDelta,
135 unifiedLineCount: file.unifiedLineCount + unifiedDelta,
136 ...(file.cacheKey ? { cacheKey: `${file.cacheKey}:ignore-whitespace` } : {}),
137 };
138}
139
140function compactPartialHunkOffsets(file: FileDiffMetadata): FileDiffMetadata {
141 if (!file.isPartial) return file;
142
143 let splitLineStart = 0;
144 let unifiedLineStart = 0;
145 const hunks = file.hunks.map((hunk) => {
146 const compactHunk = {
147 ...hunk,
148 splitLineStart,
149 unifiedLineStart,
150 };
151 splitLineStart += hunk.splitLineCount;
152 unifiedLineStart += hunk.unifiedLineCount;
153 return compactHunk;
154 });
155
156 return {
157 ...file,
158 hunks,
159 splitLineCount: splitLineStart,
160 unifiedLineCount: unifiedLineStart,
161 ...(file.cacheKey ? { cacheKey: `${file.cacheKey}:compact-partial` } : {}),
162 };
163}
164
165export function getRenderablePatch(
166 patch: string | undefined,
167 cacheScope = "diff-panel",
168 options: RenderablePatchOptions = {},
169): RenderablePatch | null {
170 if (!patch) return null;
171 const normalizedPatch = patch.trim();
172 if (normalizedPatch.length === 0) return null;
173
174 try {
175 const parsedPatches = parsePatchFiles(
176 normalizedPatch,
177 buildPatchCacheKey(normalizedPatch, cacheScope),
178 );
179 const sourceFiles = parsedPatches.flatMap((parsedPatch) => parsedPatch.files);
180 const files = sourceFiles.map((file) => {
181 const filtered = options.ignoreWhitespace ? hideWhitespaceChanges(file) : file;
182 return options.compactPartialHunkOffsets ? compactPartialHunkOffsets(filtered) : filtered;
183 });
184 if (files.length > 0) {
185 return { kind: "files", files, sourceFiles };
186 }
187
188 return {
189 kind: "raw",
190 text: normalizedPatch,
191 reason: "Unsupported diff format. Showing raw patch.",
192 };
193 } catch {
194 return {
195 kind: "raw",
196 text: normalizedPatch,
197 reason: "Failed to parse patch. Showing raw patch.",
198 };
199 }
200}
201
202/**
203 * The file's own name. Git quotes and escapes a name holding a tab, a newline, a quote or a
204 * backslash, and the parser decodes it, so this is the name the host knows the file by.
205 */
206export function resolveFileDiffPath(fileDiff: FileDiffMetadata): string {
207 return fileDiff.name ?? fileDiff.prevName ?? "";
208}
209
210export function resolveFileDiffPreviousPath(fileDiff: FileDiffMetadata): string {
211 return fileDiff.prevName ?? fileDiff.name ?? "";
212}
213
214/**
215 * A type change (regular file to symlink) arrives as a deletion and an addition
216 * of the same path, so the change type is part of the identity.
217 */
218export function buildFileDiffIdentityKey(fileDiff: FileDiffMetadata): string {
219 return `${resolveFileDiffPreviousPath(fileDiff)}\u0000${resolveFileDiffPath(fileDiff)}\u0000${fileDiff.type}`;
220}
221
222export function buildFileDiffRenderKey(fileDiff: FileDiffMetadata): string {
223 const cacheKey = fileDiff.cacheKey;
224 if (!cacheKey) return `${fileDiff.prevName ?? "none"}:${fileDiff.name}`;
225
226 return cacheKey.endsWith(":hydrated") ? cacheKey.slice(0, -":hydrated".length) : cacheKey;
227}
228
229function hashFileDiffPart(hash: number, value: string | number | boolean | undefined): number {
230 const serialized = value === undefined ? "undefined" : String(value);
231 const withLength = fnv1a32(`${typeof value}:${serialized.length}:`, hash);
232 return fnv1a32(serialized, withLength);
233}
234
235/**
236 * Pierre's cache key includes the whole patch, so using it here would repaint
237 * every file when one changes.
238 */
239export function buildFileDiffContentVersion(fileDiff: FileDiffMetadata): number {
240 let hash = FNV_OFFSET_BASIS_32;
241 const append = (value: string | number | boolean | undefined) => {
242 hash = hashFileDiffPart(hash, value);
243 };
244
245 append(fileDiff.name);
246 append(fileDiff.prevName);
247 append(fileDiff.lang);
248 append(fileDiff.newObjectId);
249 append(fileDiff.prevObjectId);
250 append(fileDiff.mode);
251 append(fileDiff.prevMode);
252 append(fileDiff.type);
253 append(fileDiff.isPartial);
254 append(fileDiff.splitLineCount);
255 append(fileDiff.unifiedLineCount);
256
257 for (const line of fileDiff.additionLines) append(line);
258 for (const line of fileDiff.deletionLines) append(line);
259 for (const hunk of fileDiff.hunks) {
260 append(hunk.collapsedBefore);
261 append(hunk.additionStart);
262 append(hunk.additionCount);
263 append(hunk.additionLines);
264 append(hunk.additionLineIndex);
265 append(hunk.deletionStart);
266 append(hunk.deletionCount);
267 append(hunk.deletionLines);
268 append(hunk.deletionLineIndex);
269 append(hunk.hunkContext);
270 append(hunk.hunkSpecs);
271 append(hunk.splitLineStart);
272 append(hunk.splitLineCount);
273 append(hunk.unifiedLineStart);
274 append(hunk.unifiedLineCount);
275 append(hunk.noEOFCRAdditions);
276 append(hunk.noEOFCRDeletions);
277 for (const content of hunk.hunkContent) {
278 append(content.type);
279 append(content.additionLineIndex);
280 append(content.deletionLineIndex);
281 append(content.type === "change" ? content.additions : content.lines);
282 append(content.type === "change" ? content.deletions : undefined);
283 }
284 }
285
286 return hash;
287}
288
289export function getDiffCollapseIconClassName(fileDiff: FileDiffMetadata): string {
290 switch (fileDiff.type) {
291 case "new":
292 return "text-[var(--diffs-addition-base)]";
293 case "deleted":
294 return "text-[var(--diffs-deletion-base)]";
295 case "change":
296 case "rename-pure":
297 case "rename-changed":
298 return "text-[var(--diffs-modified-base)]";
299 default:
300 return "text-muted-foreground/80";
301 }
302}
303
304export const DIFF_SURFACE_THEME_UNSAFE_CSS = `
305[data-diffs-header],
306[data-diff],
307[data-file],
308[data-error-wrapper],
309[data-virtualizer-buffer] {
310 --diffs-header-font-family: var(--font-sans) !important;
311 --diffs-font-family: var(--font-mono) !important;
312 --diffs-bg: var(--code-background) !important;
313 --diffs-light-bg: var(--code-background) !important;
314 --diffs-dark-bg: var(--code-background) !important;
315 --diffs-token-light-bg: transparent;
316 --diffs-token-dark-bg: transparent;
317
318 /* Gutter, context, and row tints all derive from the code surface the diff
319 body sits on — mixing from the canvas leaves the gutter looking unthemed
320 when a palette separates the two. */
321 --diffs-bg-context-override: color-mix(in srgb, var(--code-background) 97%, var(--code-foreground));
322 --diffs-bg-hover-override: color-mix(in srgb, var(--code-background) 94%, var(--code-foreground));
323 --diffs-bg-separator-override: color-mix(
324 in srgb,
325 var(--code-background) 95%,
326 var(--code-foreground)
327 );
328 --diffs-bg-buffer-override: color-mix(in srgb, var(--code-background) 90%, var(--code-foreground));
329
330 --diffs-bg-addition-override: light-dark(
331 color-mix(in srgb, var(--code-background) 50%, var(--diff-addition)),
332 color-mix(in srgb, var(--code-background) 70%, var(--diff-addition))
333 );
334 --diffs-bg-addition-number-override: light-dark(
335 color-mix(in srgb, var(--code-background) 35%, var(--diff-addition)),
336 color-mix(in srgb, var(--code-background) 60%, var(--diff-addition))
337 );
338 --diffs-bg-addition-hover-override: color-mix(in srgb, var(--code-background) 85%, var(--diff-addition));
339 --diffs-bg-addition-emphasis-override: color-mix(
340 in srgb,
341 var(--code-background) 80%,
342 var(--diff-addition)
343 );
344
345 --diffs-bg-deletion-override: light-dark(
346 color-mix(in srgb, var(--code-background) 50%, var(--diff-deletion)),
347 color-mix(in srgb, var(--code-background) 70%, var(--diff-deletion))
348 );
349 --diffs-bg-deletion-number-override: light-dark(
350 color-mix(in srgb, var(--code-background) 35%, var(--diff-deletion)),
351 color-mix(in srgb, var(--code-background) 60%, var(--diff-deletion))
352 );
353 --diffs-bg-deletion-hover-override: color-mix(
354 in srgb,
355 var(--code-background) 85%,
356 var(--diff-deletion)
357 );
358 --diffs-bg-deletion-emphasis-override: color-mix(
359 in srgb,
360 var(--code-background) 80%,
361 var(--diff-deletion)
362 );
363
364 background-color: var(--diffs-bg) !important;
365 color: var(--code-foreground) !important;
366}
367`;