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.

This commit is contained in:
sebseb7
2025-08-11 21:38:17 +02:00
parent 01bde0d7c3
commit fc0c0669f8

View File

@@ -117,12 +117,10 @@ function resolvePath(chroot, filepath) {
const root = normalizePath(chroot); 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('/')) { if (file.startsWith('/')) {
// Remove leading slash and join with chroot return file;
const relativePath = file.substring(1);
const resolved = joinPaths(root, relativePath);
return resolved.startsWith('/') ? resolved : '/' + resolved;
} }
// If file is relative, join with chroot // If file is relative, join with chroot
@@ -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"); throw new DiffError("Patch text must start with *** Begin Patch");
} }
// Load ONLY update/delete targets - do NOT load add targets // Load update/delete targets and also ADD targets if they already exist,
// because add targets are expected to not exist yet // so the parser can detect "Add File Error - file already exists".
const updateDeletePaths = identify_files_needed(text, chroot); 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 [patch, _fuzz] = text_to_patch(text, orig, chroot);
const commit = patch_to_commit(patch, orig, chroot); const commit = patch_to_commit(patch, orig, chroot);