Preserve diff tree order and collapsed folders (#11931)

This commit is contained in:
Julius Marminge authored and GitHub committed 2026-09-15 11:08:06 -07:00
1 parent 7235701de0
commit 3efdcc5296
4 files changed
+107 -4

No files matched your search

@@ -1,5 +1,5 @@
import type { FileDiffMetadata } from "@pierre/diffs";
import type { FileTreeBatchOperation, GitStatus } from "@pierre/trees";
import type { FileTreeBatchOperation, FileTreeSortComparator, GitStatus } from "@pierre/trees";
import { resolveFileDiffPath } from "~/lib/diffRendering";
@@ -47,6 +47,34 @@ export function collectDirectoryPaths(paths: ReadonlyArray<string>): ReadonlyArr
return [...directories];
}
/** A folder takes the position of its first file in the diff. */
export function diffFileTreePositions(paths: ReadonlyArray<string>): ReadonlyMap<string, number> {
const positions = new Map<string, number>();
paths.forEach((path, index) => {
positions.set(path, index);
let directory = "";
for (const segment of path.split("/").slice(0, -1)) {
directory += `${segment}/`;
if (!positions.has(directory)) positions.set(directory, index);
}
});
return positions;
}
export function compareDiffFileTreeEntries(
getPositions: () => ReadonlyMap<string, number>,
): FileTreeSortComparator {
return (left, right) => {
const positions = getPositions();
return (
(positions.get(left.path) ?? Number.MAX_SAFE_INTEGER) -
(positions.get(right.path) ?? Number.MAX_SAFE_INTEGER) ||
left.depth - right.depth ||
left.path.localeCompare(right.path)
);
};
}
function pathDepth(path: string): number {
return path.split("/").filter(Boolean).length;
}