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

@@ -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);