Refactor path handling in list_files.js and patch_files.js for improved clarity and consistency. Update error messages to reflect 'root' terminology instead of 'chroot' and enhance path resolution logic for better handling of absolute and relative paths.

This commit is contained in:
sebseb7
2025-08-14 10:22:27 +00:00
parent 421b47355b
commit 9974a78394
2 changed files with 27 additions and 17 deletions

View File

@@ -126,10 +126,10 @@ function resolvePath(chroot, filepath) {
const root = normalizePath(chroot);
// If file is absolute, use it as-is (after normalization).
// We assume the caller ensures it is inside the chroot.
// If file is absolute, resolve it under the chroot rather than using host FS root
if (file.startsWith('/')) {
return file;
const resolvedAbs = joinPaths(root, file);
return resolvedAbs.startsWith('/') ? resolvedAbs : '/' + resolvedAbs;
}
// If file is relative, join with chroot
@@ -730,20 +730,23 @@ function load_files(paths, open_fn) {
function apply_commit(commit, write_fn, remove_fn, chroot = null) {
for (const [path, change] of Object.entries(commit.changes)) {
if (change.type === ActionType.DELETE) {
remove_fn(path);
const target = resolvePath(chroot, path);
remove_fn(target);
} else if (change.type === ActionType.ADD) {
if (change.new_content === null) {
throw new DiffError(`ADD change for ${path} has no content`);
}
write_fn(path, change.new_content);
const target = resolvePath(chroot, path);
write_fn(target, change.new_content);
} else if (change.type === ActionType.UPDATE) {
if (change.new_content === null) {
throw new DiffError(`UPDATE change for ${path} has no new content`);
}
const target = change.move_path ? resolvePath(chroot, change.move_path) : path;
const target = change.move_path ? resolvePath(chroot, change.move_path) : resolvePath(chroot, path);
write_fn(target, change.new_content);
if (change.move_path) {
remove_fn(path);
const source = resolvePath(chroot, path);
remove_fn(source);
}
}
}