fix: check directory before binary extension to handle edge cases

Move directory check before binary file check to handle edge cases
where a directory has a binary file extension (e.g., "images.png/").
Previously, such directories would be incorrectly treated as binary
files instead of being expanded.
This commit is contained in:
Tony Nekola
2025-12-24 23:42:05 +02:00
parent b0ce01d008
commit 407cf633e0

View File

@@ -57,20 +57,11 @@ export async function generateSyntheticDiffForNewFile(
const fullPath = path.join(basePath, cleanPath);
try {
// Check if it's a binary file
if (isBinaryFile(cleanPath)) {
return `diff --git a/${cleanPath} b/${cleanPath}
new file mode 100644
index 0000000..0000000
Binary file ${cleanPath} added
`;
}
// Get file stats to check size and type
const stats = await secureFs.stat(fullPath);
// Check if it's a directory (can happen with untracked directories from git status)
// If so, recursively list all files and generate diffs for each
// Check if it's a directory first (before binary check)
// This handles edge cases like directories named "images.png/"
if (stats.isDirectory()) {
const filesInDir = await listAllFilesInDirectory(basePath, cleanPath);
if (filesInDir.length === 0) {
@@ -86,6 +77,15 @@ Binary file ${cleanPath} added
return diffs.join('');
}
// Check if it's a binary file (after directory check to handle dirs with binary extensions)
if (isBinaryFile(cleanPath)) {
return `diff --git a/${cleanPath} b/${cleanPath}
new file mode 100644
index 0000000..0000000
Binary file ${cleanPath} added
`;
}
if (stats.size > MAX_SYNTHETIC_DIFF_SIZE) {
const sizeKB = Math.round(stats.size / 1024);
return createNewFileDiff(cleanPath, '100644', [`[File too large to display: ${sizeKB}KB]`]);