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:
@@ -114,17 +114,15 @@ function joinPaths(...parts) {
|
|||||||
function resolvePath(chroot, filepath) {
|
function resolvePath(chroot, filepath) {
|
||||||
const file = normalizePath(filepath);
|
const file = normalizePath(filepath);
|
||||||
if (!chroot) return file;
|
if (!chroot) return file;
|
||||||
|
|
||||||
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
|
||||||
const resolved = joinPaths(root, file);
|
const resolved = joinPaths(root, file);
|
||||||
return resolved.startsWith('/') ? resolved : '/' + resolved;
|
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");
|
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);
|
||||||
|
|||||||
Reference in New Issue
Block a user