Файли
openidea/src/features/vcs/imageDiffInput.ts
T
George Mukha e1ce850a29 feat(vcs): compare image revisions and highlight changed pixels (#6232)
Image files in Git diffs (Local Changes, commit details, file history) now
open in an image diff instead of a binary notice: both revisions side by side
with their sizes, plus an optional third pane that highlights changed pixels
and counts them. Revisions are read through a new vcs_image_at command backed
by Repo::file_bytes_at_bounded, which falls back to a bounded git cat-file like
the existing blob reader.

Fixes #6229

Co-authored-by: themukha <[email protected]>
2026-11-27 02:34:33 +08:00

45 рядки
2.0 KiB
TypeScript

import { attachDocument } from "@/editor/api";
import { call, commands } from "@/platform/ipc";
import { isSvgPath } from "@/features/viewers/fileKinds";
import { maybeEditorService } from "@/workbench/contributions";
import { stagedDocumentAtPath } from "./stagedDocument";
import { isIndex, type DiffData, type DiffSide } from "./diffInput";
export function imageDiffKind(data: DiffData): "svg" | "raster" | null {
if (data.stagingRange || data.line !== undefined || data.context || data.annotations?.length || data.annotateLeft)
return null;
const paths: string[] = [];
for (const side of [data.left, data.middle, data.right]) {
if (!side || (side.type === "text" && side.absent && !side.gitlinkSource)) continue;
if (side.type !== "rev" && side.type !== "worktree" && side.type !== "file") return null;
const path = (side.type !== "worktree" && side.path) || data.path;
if (!/\.(png|jpe?g|webp|svg)$/i.test(path)) return null;
paths.push(path);
}
if (!paths.length) return null;
return paths.every(isSvgPath) ? "svg" : "raster";
}
const svgDataUrl = (text: string) => "data:image/svg+xml;charset=utf-8," + encodeURIComponent(text);
export async function readImageSide(path: string, side: DiffSide): Promise<string | null> {
if (side.type === "rev") {
const file = side.path ?? path;
const draft = isIndex(side) && isSvgPath(file) && stagedDocumentAtPath(file);
if (draft && draft.dirty) return svgDataUrl(draft.status.getState().text);
return call(commands.vcsImageAt(file, side.rev));
}
if (side.type !== "worktree" && side.type !== "file") return null;
const file = side.type === "file" ? side.path : path;
if (isSvgPath(file) && maybeEditorService()?.dirtyDocumentIds?.().length !== 0) {
// The shared document keeps unsaved text even when its editor tab is unmounted.
const doc = await attachDocument(file).catch(() => null);
try {
if (doc?.dirty()) return svgDataUrl(doc.text());
} finally {
doc?.release();
}
}
return call(commands.fsReadImage(file));
}