Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
74d445af4d | ||
|
|
c45ea6d6f9 | ||
|
|
14acdb84e4 | ||
|
|
d898b66374 |
No files matched your search
@@ -63,11 +63,18 @@ import {
|
||||
MAX_THREAD_TERMINAL_COUNT,
|
||||
type ChatMessage,
|
||||
type Thread,
|
||||
type TurnDiffFileChange,
|
||||
type TurnDiffSummary,
|
||||
} from "../types";
|
||||
import { basenameOfPath, getVscodeIconUrlForEntry } from "../vscode-icons";
|
||||
import { useTheme } from "../hooks/useTheme";
|
||||
import { useTurnDiffSummaries } from "../hooks/useTurnDiffSummaries";
|
||||
import {
|
||||
buildTurnDiffTree,
|
||||
normalizeFilePath,
|
||||
summarizeTurnDiffStats,
|
||||
type TurnDiffTreeNode,
|
||||
} from "../lib/turnDiffTree";
|
||||
import BranchToolbar from "./BranchToolbar";
|
||||
import GitActionsControl from "./GitActionsControl";
|
||||
import {
|
||||
@@ -333,6 +340,7 @@ const VscodeEntryIcon = memo(function VscodeEntryIcon(props: {
|
||||
pathValue: string;
|
||||
kind: "file" | "directory";
|
||||
theme: "light" | "dark";
|
||||
className?: string;
|
||||
}) {
|
||||
const [failedIconUrl, setFailedIconUrl] = useState<string | null>(null);
|
||||
const iconUrl = useMemo(
|
||||
@@ -343,9 +351,9 @@ const VscodeEntryIcon = memo(function VscodeEntryIcon(props: {
|
||||
|
||||
if (failed) {
|
||||
return props.kind === "directory" ? (
|
||||
<FolderIcon className="size-4 text-muted-foreground/80" />
|
||||
<FolderIcon className={cn("size-4 text-muted-foreground/80", props.className)} />
|
||||
) : (
|
||||
<FileIcon className="size-4 text-muted-foreground/80" />
|
||||
<FileIcon className={cn("size-4 text-muted-foreground/80", props.className)} />
|
||||
);
|
||||
}
|
||||
|
||||
@@ -354,7 +362,7 @@ const VscodeEntryIcon = memo(function VscodeEntryIcon(props: {
|
||||
src={iconUrl}
|
||||
alt=""
|
||||
aria-hidden="true"
|
||||
className="size-4 shrink-0"
|
||||
className={cn("size-4 shrink-0", props.className)}
|
||||
loading="lazy"
|
||||
onError={() => setFailedIconUrl(iconUrl)}
|
||||
/>
|
||||
@@ -2547,6 +2555,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
isRevertingCheckpoint={isRevertingCheckpoint}
|
||||
onImageExpand={onExpandTimelineImage}
|
||||
markdownCwd={gitCwd ?? undefined}
|
||||
resolvedTheme={resolvedTheme}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -3126,6 +3135,153 @@ const MessageCopyButton = memo(function MessageCopyButton({ text }: { text: stri
|
||||
);
|
||||
});
|
||||
|
||||
function hasNonZeroStat(stat: { additions: number; deletions: number }): boolean {
|
||||
return stat.additions > 0 || stat.deletions > 0;
|
||||
}
|
||||
|
||||
const DiffStatLabel = memo(function DiffStatLabel(props: {
|
||||
additions: number;
|
||||
deletions: number;
|
||||
}) {
|
||||
const { additions, deletions } = props;
|
||||
return (
|
||||
<>
|
||||
<span className="text-success">+{additions}</span>
|
||||
<span className="mx-0.5 text-muted-foreground/70">/</span>
|
||||
<span className="text-destructive">-{deletions}</span>
|
||||
</>
|
||||
);
|
||||
});
|
||||
|
||||
function collectDirectoryPaths(nodes: ReadonlyArray<TurnDiffTreeNode>): string[] {
|
||||
const paths: string[] = [];
|
||||
for (const node of nodes) {
|
||||
if (node.kind !== "directory") continue;
|
||||
paths.push(node.path);
|
||||
paths.push(...collectDirectoryPaths(node.children));
|
||||
}
|
||||
return paths;
|
||||
}
|
||||
|
||||
function buildDirectoryExpansionState(
|
||||
directoryPaths: ReadonlyArray<string>,
|
||||
expanded: boolean,
|
||||
): Record<string, boolean> {
|
||||
const expandedState: Record<string, boolean> = {};
|
||||
for (const directoryPath of directoryPaths) {
|
||||
expandedState[directoryPath] = expanded;
|
||||
}
|
||||
return expandedState;
|
||||
}
|
||||
|
||||
const ChangedFilesTree = memo(function ChangedFilesTree(props: {
|
||||
turnId: TurnId;
|
||||
files: ReadonlyArray<TurnDiffFileChange>;
|
||||
allDirectoriesExpanded: boolean;
|
||||
resolvedTheme: "light" | "dark";
|
||||
onOpenTurnDiff: (turnId: TurnId, filePath?: string) => void;
|
||||
}) {
|
||||
const { files, allDirectoriesExpanded, onOpenTurnDiff, resolvedTheme, turnId } = props;
|
||||
const treeNodes = useMemo(() => buildTurnDiffTree(files), [files]);
|
||||
const directoryPathsKey = useMemo(() => collectDirectoryPaths(treeNodes).join("\u0000"), [treeNodes]);
|
||||
const allDirectoryExpansionState = useMemo(
|
||||
() =>
|
||||
buildDirectoryExpansionState(
|
||||
directoryPathsKey ? directoryPathsKey.split("\u0000") : [],
|
||||
allDirectoriesExpanded,
|
||||
),
|
||||
[allDirectoriesExpanded, directoryPathsKey],
|
||||
);
|
||||
const [expandedDirectories, setExpandedDirectories] = useState<Record<string, boolean>>(
|
||||
() => buildDirectoryExpansionState((directoryPathsKey ? directoryPathsKey.split("\u0000") : []), true),
|
||||
);
|
||||
useEffect(() => {
|
||||
setExpandedDirectories(allDirectoryExpansionState);
|
||||
}, [allDirectoryExpansionState]);
|
||||
|
||||
const toggleDirectory = useCallback((pathValue: string, fallbackExpanded: boolean) => {
|
||||
setExpandedDirectories((current) => ({
|
||||
...current,
|
||||
[pathValue]: !(current[pathValue] ?? fallbackExpanded),
|
||||
}));
|
||||
}, []);
|
||||
|
||||
const renderTreeNode = (node: TurnDiffTreeNode, depth: number) => {
|
||||
const leftPadding = 8 + depth * 14;
|
||||
if (node.kind === "directory") {
|
||||
const isExpanded = expandedDirectories[node.path] ?? depth === 0;
|
||||
return (
|
||||
<div key={`dir:${node.path}`}>
|
||||
<button
|
||||
type="button"
|
||||
className="group flex w-full items-center gap-1.5 rounded-md py-1 pr-2 text-left hover:bg-background/80"
|
||||
style={{ paddingLeft: `${leftPadding}px` }}
|
||||
onClick={() => toggleDirectory(node.path, depth === 0)}
|
||||
>
|
||||
<ChevronRightIcon
|
||||
aria-hidden="true"
|
||||
className={cn(
|
||||
"size-3.5 shrink-0 text-muted-foreground/70 transition-transform group-hover:text-foreground/80",
|
||||
isExpanded && "rotate-90",
|
||||
)}
|
||||
/>
|
||||
{isExpanded ? (
|
||||
<FolderIcon className="size-3.5 shrink-0 text-muted-foreground/75" />
|
||||
) : (
|
||||
<FolderClosedIcon className="size-3.5 shrink-0 text-muted-foreground/75" />
|
||||
)}
|
||||
<span className="truncate font-mono text-[11px] text-muted-foreground/90 group-hover:text-foreground/90">
|
||||
{node.name}
|
||||
</span>
|
||||
{hasNonZeroStat(node.stat) && (
|
||||
<span className="ml-auto shrink-0 font-mono text-[10px] tabular-nums">
|
||||
<DiffStatLabel additions={node.stat.additions} deletions={node.stat.deletions} />
|
||||
</span>
|
||||
)}
|
||||
</button>
|
||||
{isExpanded && (
|
||||
<div className="space-y-0.5">
|
||||
{node.children.map((childNode) => renderTreeNode(childNode, depth + 1))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<button
|
||||
key={`file:${node.path}`}
|
||||
type="button"
|
||||
className="group flex w-full items-center gap-1.5 rounded-md py-1 pr-2 text-left hover:bg-background/80"
|
||||
style={{ paddingLeft: `${leftPadding}px` }}
|
||||
onClick={() => onOpenTurnDiff(turnId, node.path)}
|
||||
>
|
||||
<span aria-hidden="true" className="size-3.5 shrink-0" />
|
||||
<VscodeEntryIcon
|
||||
pathValue={node.path}
|
||||
kind="file"
|
||||
theme={resolvedTheme}
|
||||
className="size-3.5 text-muted-foreground/70"
|
||||
/>
|
||||
<span className="truncate font-mono text-[11px] text-muted-foreground/80 group-hover:text-foreground/90">
|
||||
{node.name}
|
||||
</span>
|
||||
{node.stat && (
|
||||
<span className="ml-auto shrink-0 font-mono text-[10px] tabular-nums">
|
||||
<DiffStatLabel additions={node.stat.additions} deletions={node.stat.deletions} />
|
||||
</span>
|
||||
)}
|
||||
</button>
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-0.5">
|
||||
{treeNodes.map((node) => renderTreeNode(node, 0))}
|
||||
</div>
|
||||
);
|
||||
});
|
||||
|
||||
interface MessagesTimelineProps {
|
||||
hasMessages: boolean;
|
||||
isWorking: boolean;
|
||||
@@ -3145,6 +3301,7 @@ interface MessagesTimelineProps {
|
||||
isRevertingCheckpoint: boolean;
|
||||
onImageExpand: (preview: ExpandedImagePreview) => void;
|
||||
markdownCwd: string | undefined;
|
||||
resolvedTheme: "light" | "dark";
|
||||
}
|
||||
|
||||
type TimelineEntry = ReturnType<typeof deriveTimelineEntries>[number];
|
||||
@@ -3199,6 +3356,7 @@ const MessagesTimeline = memo(function MessagesTimeline({
|
||||
isRevertingCheckpoint,
|
||||
onImageExpand,
|
||||
markdownCwd,
|
||||
resolvedTheme,
|
||||
}: MessagesTimelineProps) {
|
||||
const rows = useMemo<TimelineRow[]>(() => {
|
||||
const nextRows: TimelineRow[] = [];
|
||||
@@ -3339,6 +3497,15 @@ const MessagesTimeline = memo(function MessagesTimeline({
|
||||
|
||||
const virtualRows = rowVirtualizer.getVirtualItems();
|
||||
const nonVirtualizedRows = rows.slice(virtualizedRowCount);
|
||||
const [allDirectoriesExpandedByTurnId, setAllDirectoriesExpandedByTurnId] = useState<
|
||||
Record<string, boolean>
|
||||
>({});
|
||||
const onToggleAllDirectories = useCallback((turnId: TurnId) => {
|
||||
setAllDirectoriesExpandedByTurnId((current) => ({
|
||||
...current,
|
||||
[turnId]: !(current[turnId] ?? true),
|
||||
}));
|
||||
}, []);
|
||||
|
||||
const renderRowContent = (row: TimelineRow) => (
|
||||
<div className="pb-4">
|
||||
@@ -3507,81 +3674,58 @@ const MessagesTimeline = memo(function MessagesTimeline({
|
||||
if (!turnSummary) return null;
|
||||
const checkpointFiles = turnSummary.files;
|
||||
if (checkpointFiles.length === 0) return null;
|
||||
const summaryStat = checkpointFiles.reduce(
|
||||
(acc, file) => {
|
||||
if (
|
||||
typeof file.additions !== "number" ||
|
||||
typeof file.deletions !== "number"
|
||||
) {
|
||||
return acc;
|
||||
}
|
||||
return {
|
||||
additions: acc.additions + file.additions,
|
||||
deletions: acc.deletions + file.deletions,
|
||||
};
|
||||
},
|
||||
{ additions: 0, deletions: 0 },
|
||||
);
|
||||
const summaryStat = summarizeTurnDiffStats(checkpointFiles);
|
||||
const changedFileCountLabel = String(checkpointFiles.length);
|
||||
const allDirectoriesExpanded =
|
||||
allDirectoriesExpandedByTurnId[turnSummary.turnId] ?? true;
|
||||
return (
|
||||
<div className="mt-2 rounded-lg border border-border/80 bg-card/45 p-2.5">
|
||||
<div className="mb-1.5 flex items-center justify-between gap-2">
|
||||
<p className="text-[10px] uppercase tracking-[0.12em] text-muted-foreground/65">
|
||||
<span>Changed files ({changedFileCountLabel})</span>
|
||||
{(summaryStat.additions > 0 || summaryStat.deletions > 0) && (
|
||||
{hasNonZeroStat(summaryStat) && (
|
||||
<>
|
||||
<span className="mx-1">•</span>
|
||||
<span className="text-success">+{summaryStat.additions}</span>
|
||||
<span className="mx-0.5 text-muted-foreground/70">/</span>
|
||||
<span className="text-destructive">-{summaryStat.deletions}</span>
|
||||
<DiffStatLabel
|
||||
additions={summaryStat.additions}
|
||||
deletions={summaryStat.deletions}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
</p>
|
||||
<Button
|
||||
type="button"
|
||||
size="xs"
|
||||
variant="outline"
|
||||
onClick={() =>
|
||||
onOpenTurnDiff(turnSummary.turnId, checkpointFiles[0]?.path)
|
||||
}
|
||||
>
|
||||
View diff
|
||||
</Button>
|
||||
</div>
|
||||
<div className="flex flex-wrap gap-1.5">
|
||||
{checkpointFiles.map((file) => (
|
||||
<button
|
||||
key={`${turnSummary.turnId}:${file.path}`}
|
||||
<div className="flex items-center gap-1.5">
|
||||
<Button
|
||||
type="button"
|
||||
className="rounded-md border border-border/70 bg-background/70 px-2 py-1 font-mono text-[11px] text-muted-foreground/80 transition-colors hover:border-border hover:text-foreground/90"
|
||||
onClick={() => onOpenTurnDiff(turnSummary.turnId, file.path)}
|
||||
size="xs"
|
||||
variant="outline"
|
||||
onClick={() => onToggleAllDirectories(turnSummary.turnId)}
|
||||
>
|
||||
{(() => {
|
||||
const stat =
|
||||
typeof file.additions === "number" &&
|
||||
typeof file.deletions === "number"
|
||||
? {
|
||||
additions: file.additions,
|
||||
deletions: file.deletions,
|
||||
}
|
||||
: null;
|
||||
if (!stat) {
|
||||
return file.path;
|
||||
}
|
||||
return (
|
||||
<>
|
||||
<span>{file.path}</span>
|
||||
<span className="ml-1 text-muted-foreground/70">(</span>
|
||||
<span className="text-success">+{stat.additions}</span>
|
||||
<span className="mx-0.5 text-muted-foreground/70">/</span>
|
||||
<span className="text-destructive">-{stat.deletions}</span>
|
||||
<span className="text-muted-foreground/70">)</span>
|
||||
</>
|
||||
{allDirectoriesExpanded ? "Collapse all" : "Expand all"}
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
size="xs"
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
const rawPath = checkpointFiles[0]?.path;
|
||||
onOpenTurnDiff(
|
||||
turnSummary.turnId,
|
||||
rawPath ? normalizeFilePath(rawPath) : undefined,
|
||||
);
|
||||
})()}
|
||||
</button>
|
||||
))}
|
||||
}}
|
||||
>
|
||||
View diff
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
<ChangedFilesTree
|
||||
key={`changed-files-tree:${turnSummary.turnId}`}
|
||||
turnId={turnSummary.turnId}
|
||||
files={checkpointFiles}
|
||||
allDirectoriesExpanded={allDirectoriesExpanded}
|
||||
resolvedTheme={resolvedTheme}
|
||||
onOpenTurnDiff={onOpenTurnDiff}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
})()}
|
||||
|
||||
@@ -0,0 +1,165 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { buildTurnDiffTree, summarizeTurnDiffStats } from "./turnDiffTree";
|
||||
|
||||
describe("summarizeTurnDiffStats", () => {
|
||||
it("sums only files with numeric additions/deletions", () => {
|
||||
const stat = summarizeTurnDiffStats([
|
||||
{ path: "README.md", additions: 3, deletions: 1 },
|
||||
{ path: "docs/notes.md" },
|
||||
{ path: "src/index.ts", additions: 5, deletions: 2 },
|
||||
]);
|
||||
|
||||
expect(stat).toEqual({ additions: 8, deletions: 3 });
|
||||
});
|
||||
});
|
||||
|
||||
describe("buildTurnDiffTree", () => {
|
||||
it("builds nested directory nodes with aggregated stats", () => {
|
||||
const tree = buildTurnDiffTree([
|
||||
{ path: "src/index.ts", additions: 2, deletions: 1 },
|
||||
{ path: "src/components/Button.tsx", additions: 4, deletions: 2 },
|
||||
{ path: "README.md", additions: 1, deletions: 0 },
|
||||
]);
|
||||
|
||||
expect(tree).toEqual([
|
||||
{
|
||||
kind: "directory",
|
||||
name: "src",
|
||||
path: "src",
|
||||
stat: { additions: 6, deletions: 3 },
|
||||
children: [
|
||||
{
|
||||
kind: "directory",
|
||||
name: "components",
|
||||
path: "src/components",
|
||||
stat: { additions: 4, deletions: 2 },
|
||||
children: [
|
||||
{
|
||||
kind: "file",
|
||||
name: "Button.tsx",
|
||||
path: "src/components/Button.tsx",
|
||||
stat: { additions: 4, deletions: 2 },
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
kind: "file",
|
||||
name: "index.ts",
|
||||
path: "src/index.ts",
|
||||
stat: { additions: 2, deletions: 1 },
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
kind: "file",
|
||||
name: "README.md",
|
||||
path: "README.md",
|
||||
stat: { additions: 1, deletions: 0 },
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("keeps files without stat values and excludes them from directory totals", () => {
|
||||
const tree = buildTurnDiffTree([
|
||||
{ path: "docs/notes.md" },
|
||||
{ path: "docs/todo.md", additions: 1, deletions: 1 },
|
||||
]);
|
||||
|
||||
expect(tree).toEqual([
|
||||
{
|
||||
kind: "directory",
|
||||
name: "docs",
|
||||
path: "docs",
|
||||
stat: { additions: 1, deletions: 1 },
|
||||
children: [
|
||||
{
|
||||
kind: "file",
|
||||
name: "notes.md",
|
||||
path: "docs/notes.md",
|
||||
stat: null,
|
||||
},
|
||||
{
|
||||
kind: "file",
|
||||
name: "todo.md",
|
||||
path: "docs/todo.md",
|
||||
stat: { additions: 1, deletions: 1 },
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("normalizes file paths with windows separators", () => {
|
||||
const tree = buildTurnDiffTree([{ path: "apps\\web\\src\\index.ts", additions: 2, deletions: 1 }]);
|
||||
|
||||
expect(tree).toEqual([
|
||||
{
|
||||
kind: "directory",
|
||||
name: "apps/web/src",
|
||||
path: "apps/web/src",
|
||||
stat: { additions: 2, deletions: 1 },
|
||||
children: [
|
||||
{
|
||||
kind: "file",
|
||||
name: "index.ts",
|
||||
path: "apps/web/src/index.ts",
|
||||
stat: { additions: 2, deletions: 1 },
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("compacts only single-directory chains and stops at branch points", () => {
|
||||
const tree = buildTurnDiffTree([
|
||||
{ path: "apps/server/src/index.ts", additions: 2, deletions: 1 },
|
||||
{ path: "apps/server/main.ts", additions: 4, deletions: 0 },
|
||||
]);
|
||||
|
||||
expect(tree).toEqual([
|
||||
{
|
||||
kind: "directory",
|
||||
name: "apps/server",
|
||||
path: "apps/server",
|
||||
stat: { additions: 6, deletions: 1 },
|
||||
children: [
|
||||
{
|
||||
kind: "directory",
|
||||
name: "src",
|
||||
path: "apps/server/src",
|
||||
stat: { additions: 2, deletions: 1 },
|
||||
children: [
|
||||
{
|
||||
kind: "file",
|
||||
name: "index.ts",
|
||||
path: "apps/server/src/index.ts",
|
||||
stat: { additions: 2, deletions: 1 },
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
kind: "file",
|
||||
name: "main.ts",
|
||||
path: "apps/server/main.ts",
|
||||
stat: { additions: 4, deletions: 0 },
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("preserves leading/trailing whitespace in path segments", () => {
|
||||
const tree = buildTurnDiffTree([
|
||||
{ path: "a/file.ts", additions: 1, deletions: 0 },
|
||||
{ path: " a/file.ts", additions: 2, deletions: 0 },
|
||||
]);
|
||||
|
||||
expect(tree).toHaveLength(2);
|
||||
const directoryNodes = tree.filter(
|
||||
(node): node is Extract<(typeof tree)[number], { kind: "directory" }> => node.kind === "directory",
|
||||
);
|
||||
expect(directoryNodes.map((node) => node.name).toSorted()).toEqual([" a", "a"]);
|
||||
expect(directoryNodes.map((node) => node.path).toSorted()).toEqual([" a", "a"]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,176 @@
|
||||
import type { TurnDiffFileChange } from "../types";
|
||||
|
||||
export interface TurnDiffStat {
|
||||
additions: number;
|
||||
deletions: number;
|
||||
}
|
||||
|
||||
export interface TurnDiffTreeDirectoryNode {
|
||||
kind: "directory";
|
||||
name: string;
|
||||
path: string;
|
||||
stat: TurnDiffStat;
|
||||
children: TurnDiffTreeNode[];
|
||||
}
|
||||
|
||||
export interface TurnDiffTreeFileNode {
|
||||
kind: "file";
|
||||
name: string;
|
||||
path: string;
|
||||
stat: TurnDiffStat | null;
|
||||
}
|
||||
|
||||
export type TurnDiffTreeNode = TurnDiffTreeDirectoryNode | TurnDiffTreeFileNode;
|
||||
|
||||
interface MutableDirectoryNode {
|
||||
name: string;
|
||||
path: string;
|
||||
stat: TurnDiffStat;
|
||||
directories: Map<string, MutableDirectoryNode>;
|
||||
files: TurnDiffTreeFileNode[];
|
||||
}
|
||||
|
||||
const SORT_LOCALE_OPTIONS: Intl.CollatorOptions = { numeric: true, sensitivity: "base" };
|
||||
|
||||
function normalizePathSegments(pathValue: string): string[] {
|
||||
return pathValue
|
||||
.replaceAll("\\", "/")
|
||||
.split("/")
|
||||
.filter((segment) => segment.length > 0);
|
||||
}
|
||||
|
||||
export function normalizeFilePath(pathValue: string): string {
|
||||
return normalizePathSegments(pathValue).join("/");
|
||||
}
|
||||
|
||||
function compareByName(a: { name: string }, b: { name: string }): number {
|
||||
return a.name.localeCompare(b.name, undefined, SORT_LOCALE_OPTIONS);
|
||||
}
|
||||
|
||||
function readStat(file: TurnDiffFileChange): TurnDiffStat | null {
|
||||
if (typeof file.additions !== "number" || typeof file.deletions !== "number") {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
additions: file.additions,
|
||||
deletions: file.deletions,
|
||||
};
|
||||
}
|
||||
|
||||
function compactDirectoryNode(node: TurnDiffTreeDirectoryNode): TurnDiffTreeDirectoryNode {
|
||||
const compactedChildren = node.children.map((child) =>
|
||||
child.kind === "directory" ? compactDirectoryNode(child) : child,
|
||||
);
|
||||
|
||||
let compactedNode: TurnDiffTreeDirectoryNode = {
|
||||
...node,
|
||||
children: compactedChildren,
|
||||
};
|
||||
|
||||
while (compactedNode.children.length === 1 && compactedNode.children[0]?.kind === "directory") {
|
||||
const onlyChild = compactedNode.children[0];
|
||||
compactedNode = {
|
||||
kind: "directory",
|
||||
name: `${compactedNode.name}/${onlyChild.name}`,
|
||||
path: onlyChild.path,
|
||||
stat: onlyChild.stat,
|
||||
children: onlyChild.children,
|
||||
};
|
||||
}
|
||||
|
||||
return compactedNode;
|
||||
}
|
||||
|
||||
function toTreeNodes(directory: MutableDirectoryNode): TurnDiffTreeNode[] {
|
||||
const subdirectories: TurnDiffTreeDirectoryNode[] = Array.from(directory.directories.values())
|
||||
.toSorted(compareByName)
|
||||
.map<TurnDiffTreeDirectoryNode>((subdirectory) => ({
|
||||
kind: "directory",
|
||||
name: subdirectory.name,
|
||||
path: subdirectory.path,
|
||||
stat: {
|
||||
additions: subdirectory.stat.additions,
|
||||
deletions: subdirectory.stat.deletions,
|
||||
},
|
||||
children: toTreeNodes(subdirectory),
|
||||
}))
|
||||
.map((subdirectory) => compactDirectoryNode(subdirectory));
|
||||
|
||||
const files = directory.files.toSorted(compareByName);
|
||||
return [...subdirectories, ...files];
|
||||
}
|
||||
|
||||
export function summarizeTurnDiffStats(files: ReadonlyArray<TurnDiffFileChange>): TurnDiffStat {
|
||||
return files.reduce(
|
||||
(acc, file) => {
|
||||
const stat = readStat(file);
|
||||
if (!stat) return acc;
|
||||
return {
|
||||
additions: acc.additions + stat.additions,
|
||||
deletions: acc.deletions + stat.deletions,
|
||||
};
|
||||
},
|
||||
{ additions: 0, deletions: 0 },
|
||||
);
|
||||
}
|
||||
|
||||
export function buildTurnDiffTree(files: ReadonlyArray<TurnDiffFileChange>): TurnDiffTreeNode[] {
|
||||
const root: MutableDirectoryNode = {
|
||||
name: "",
|
||||
path: "",
|
||||
stat: { additions: 0, deletions: 0 },
|
||||
directories: new Map(),
|
||||
files: [],
|
||||
};
|
||||
|
||||
for (const file of files) {
|
||||
const segments = normalizePathSegments(file.path);
|
||||
if (segments.length === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const filePath = segments.join("/");
|
||||
const fileName = segments.at(-1);
|
||||
if (!fileName) {
|
||||
continue;
|
||||
}
|
||||
const stat = readStat(file);
|
||||
const ancestors: MutableDirectoryNode[] = [root];
|
||||
let currentDirectory = root;
|
||||
|
||||
for (const segment of segments.slice(0, -1)) {
|
||||
const nextPath = currentDirectory.path ? `${currentDirectory.path}/${segment}` : segment;
|
||||
const existing = currentDirectory.directories.get(segment);
|
||||
if (existing) {
|
||||
currentDirectory = existing;
|
||||
} else {
|
||||
const created: MutableDirectoryNode = {
|
||||
name: segment,
|
||||
path: nextPath,
|
||||
stat: { additions: 0, deletions: 0 },
|
||||
directories: new Map(),
|
||||
files: [],
|
||||
};
|
||||
currentDirectory.directories.set(segment, created);
|
||||
currentDirectory = created;
|
||||
}
|
||||
ancestors.push(currentDirectory);
|
||||
}
|
||||
|
||||
currentDirectory.files.push({
|
||||
kind: "file",
|
||||
name: fileName,
|
||||
path: filePath,
|
||||
stat,
|
||||
});
|
||||
|
||||
if (stat) {
|
||||
for (const ancestor of ancestors) {
|
||||
ancestor.stat.additions += stat.additions;
|
||||
ancestor.stat.deletions += stat.deletions;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return toTreeNodes(root);
|
||||
}
|
||||
Reference in new issue
Block a user