mirror of
https://github.com/AutoMaker-Org/automaker.git
synced 2026-02-04 21:23:07 +00:00
feat(diff): add helper function to create synthetic diffs for new files
This update introduces a new function, createNewFileDiff, to streamline the generation of synthetic diffs for untracked files. The function reduces code duplication by handling the diff formatting for new files, including directories and large files, improving overall maintainability.
This commit is contained in:
@@ -24,6 +24,24 @@ function isBinaryFile(filePath: string): boolean {
|
|||||||
return BINARY_EXTENSIONS.has(ext);
|
return BINARY_EXTENSIONS.has(ext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a synthetic diff for a new file with the given content lines
|
||||||
|
* This helper reduces duplication in diff generation logic
|
||||||
|
*/
|
||||||
|
function createNewFileDiff(relativePath: string, mode: string, contentLines: string[]): string {
|
||||||
|
const lineCount = contentLines.length;
|
||||||
|
const addedLines = contentLines.map((line) => `+${line}`).join('\n');
|
||||||
|
|
||||||
|
return `diff --git a/${relativePath} b/${relativePath}
|
||||||
|
new file mode ${mode}
|
||||||
|
index 0000000..0000000
|
||||||
|
--- /dev/null
|
||||||
|
+++ b/${relativePath}
|
||||||
|
@@ -0,0 +${lineCount === 1 ? '1' : `1,${lineCount}`} @@
|
||||||
|
${addedLines}
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a synthetic unified diff for an untracked (new) file
|
* Generate a synthetic unified diff for an untracked (new) file
|
||||||
* This is needed because `git diff HEAD` doesn't include untracked files
|
* This is needed because `git diff HEAD` doesn't include untracked files
|
||||||
@@ -49,26 +67,14 @@ Binary file ${relativePath} added
|
|||||||
|
|
||||||
// Check if it's a directory (can happen with untracked directories from git status)
|
// Check if it's a directory (can happen with untracked directories from git status)
|
||||||
if (stats.isDirectory()) {
|
if (stats.isDirectory()) {
|
||||||
return `diff --git a/${relativePath} b/${relativePath}
|
return createNewFileDiff(relativePath, '040000', ['[Directory]']);
|
||||||
new file mode 040000
|
|
||||||
index 0000000..0000000
|
|
||||||
--- /dev/null
|
|
||||||
+++ b/${relativePath}
|
|
||||||
@@ -0,0 +1 @@
|
|
||||||
+[Directory]
|
|
||||||
`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stats.size > MAX_SYNTHETIC_DIFF_SIZE) {
|
if (stats.size > MAX_SYNTHETIC_DIFF_SIZE) {
|
||||||
const sizeKB = Math.round(stats.size / 1024);
|
const sizeKB = Math.round(stats.size / 1024);
|
||||||
return `diff --git a/${relativePath} b/${relativePath}
|
return createNewFileDiff(relativePath, '100644', [
|
||||||
new file mode 100644
|
`[File too large to display: ${sizeKB}KB]`,
|
||||||
index 0000000..0000000
|
]);
|
||||||
--- /dev/null
|
|
||||||
+++ b/${relativePath}
|
|
||||||
@@ -0,0 +1 @@
|
|
||||||
+[File too large to display: ${sizeKB}KB]
|
|
||||||
`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read file content
|
// Read file content
|
||||||
@@ -103,14 +109,7 @@ ${addedLines}`;
|
|||||||
// Log the error for debugging
|
// Log the error for debugging
|
||||||
logger.error(`Failed to generate synthetic diff for ${fullPath}:`, error);
|
logger.error(`Failed to generate synthetic diff for ${fullPath}:`, error);
|
||||||
// Return a placeholder diff
|
// Return a placeholder diff
|
||||||
return `diff --git a/${relativePath} b/${relativePath}
|
return createNewFileDiff(relativePath, '100644', ['[Unable to read file content]']);
|
||||||
new file mode 100644
|
|
||||||
index 0000000..0000000
|
|
||||||
--- /dev/null
|
|
||||||
+++ b/${relativePath}
|
|
||||||
@@ -0,0 +1 @@
|
|
||||||
+[Unable to read file content]
|
|
||||||
`;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user