Files
orca/src/main/git/source-control/diff-result.ts
T

49 lines
1.5 KiB
TypeScript

import * as path from 'node:path'
import type { GitDiffResult } from '../../../shared/git-diff-compare-types'
import { getLargeDiffRenderLimit } from '../../../shared/large-diff-render-limit'
import { PREVIEWABLE_BINARY_MIME_TYPES } from './previewable-binary-mime-types'
export function buildDiffResult(
originalContent: string,
modifiedContent: string,
originalIsBinary: boolean,
modifiedIsBinary: boolean,
filePath?: string
): GitDiffResult {
if (originalIsBinary || modifiedIsBinary) {
const mimeType = filePath
? PREVIEWABLE_BINARY_MIME_TYPES[path.extname(filePath).toLowerCase()]
: undefined
return {
kind: 'binary',
originalContent,
modifiedContent,
originalIsBinary,
modifiedIsBinary,
// Why: renderer still checks legacy `isImage` before previewing, so set it for PDFs too until the contract is renamed.
...(mimeType ? { isImage: true, mimeType } : {})
} as GitDiffResult
}
// Why: over the render limit, return metadata instead of huge text so the renderer can show fallback UI.
const largeDiffRenderLimit = getLargeDiffRenderLimit({ originalContent, modifiedContent })
if (largeDiffRenderLimit.limited) {
return {
kind: 'text',
originalContent: '',
modifiedContent: '',
originalIsBinary: false,
modifiedIsBinary: false,
largeDiffRenderLimit
}
}
return {
kind: 'text',
originalContent,
modifiedContent,
originalIsBinary: false,
modifiedIsBinary: false
}
}