From fc0c0669f8bf8f08aa842f30d6339c41e553335f Mon Sep 17 00:00:00 2001 From: sebseb7 Date: Mon, 11 Aug 2025 21:38:17 +0200 Subject: [PATCH] Refactor path resolution in patch_files.js to simplify handling of absolute file paths and enhance file loading logic to include existing add targets. This improves error detection for file operations. --- tools/patch_files.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tools/patch_files.js b/tools/patch_files.js index 7d19243..8497938 100644 --- a/tools/patch_files.js +++ b/tools/patch_files.js @@ -114,17 +114,15 @@ function joinPaths(...parts) { function resolvePath(chroot, filepath) { const file = normalizePath(filepath); if (!chroot) return file; - + const root = normalizePath(chroot); - - // If file is absolute, treat it as relative to chroot + + // If file is absolute, use it as-is (after normalization). + // We assume the caller ensures it is inside the chroot. if (file.startsWith('/')) { - // Remove leading slash and join with chroot - const relativePath = file.substring(1); - const resolved = joinPaths(root, relativePath); - return resolved.startsWith('/') ? resolved : '/' + resolved; + return file; } - + // If file is relative, join with chroot const resolved = joinPaths(root, file); return resolved.startsWith('/') ? resolved : '/' + resolved; @@ -747,10 +745,12 @@ function process_patch(text, open_fn, write_fn, remove_fn, chroot = null) { throw new DiffError("Patch text must start with *** Begin Patch"); } - // Load ONLY update/delete targets - do NOT load add targets - // because add targets are expected to not exist yet + // Load update/delete targets and also ADD targets if they already exist, + // so the parser can detect "Add File Error - file already exists". const updateDeletePaths = identify_files_needed(text, chroot); - const orig = load_files(updateDeletePaths, open_fn); + const addPaths = identify_files_added(text, chroot); + const allPaths = [...updateDeletePaths, ...addPaths]; + const orig = load_files(allPaths, open_fn); const [patch, _fuzz] = text_to_patch(text, orig, chroot); const commit = patch_to_commit(patch, orig, chroot);