Skip to content

Commit bfc9111

Browse files
committed
fix(web): Changes row follows the base picked in the diff panel
1 parent bf8ffe8 commit bfc9111

5 files changed

Lines changed: 272 additions & 21 deletions

File tree

‎apps/web/src/components/GitActionsControl.logic.test.ts‎

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { SourceControlProviderKind } from "@t3tools/contracts";
2-
import type { VcsStatusResult } from "@t3tools/contracts";
2+
import * as DateTime from "effect/DateTime";
3+
import type {
4+
ReviewDiffPreviewResult,
5+
ReviewDiffPreviewSource,
6+
VcsStatusResult,
7+
} from "@t3tools/contracts";
38
import { assert, describe, it } from "vite-plus/test";
49
import {
510
buildGitActionProgressStages,
@@ -12,6 +17,7 @@ import {
1217
resolveGitActionResultToastTiming,
1318
resolveLiveThreadBranchUpdate,
1419
resolveQuickAction,
20+
resolveChangesTotals,
1521
resolveThreadBranchUpdate,
1622
resolveThreadBranchMetadataPatch,
1723
} from "./GitActionsControl.logic";
@@ -1245,3 +1251,113 @@ describe("resolveAutoFeatureBranchName", () => {
12451251
assert.equal(ref, "feature/update");
12461252
});
12471253
});
1254+
1255+
describe("resolveChangesTotals", () => {
1256+
const branchChanges = { baseRef: "main", insertions: 20, deletions: 10 };
1257+
const workingTree = { files: [], insertions: 3, deletions: 1 };
1258+
const gitStatus = status({ branchChanges, workingTree });
1259+
const patch = [
1260+
"diff --git a/a.txt b/a.txt",
1261+
"--- a/a.txt",
1262+
"+++ b/a.txt",
1263+
"@@ -1 +1,2 @@",
1264+
"-before",
1265+
"+after",
1266+
"+another",
1267+
].join("\n");
1268+
1269+
function source(overrides: Partial<ReviewDiffPreviewSource> = {}): ReviewDiffPreviewSource {
1270+
return {
1271+
id: "branch",
1272+
kind: "branch-range",
1273+
title: "Changes",
1274+
baseRef: "origin/release",
1275+
headRef: "HEAD",
1276+
diff: patch,
1277+
diffHash: "hash",
1278+
truncated: false,
1279+
files: [{ path: "a.txt", previousPath: null, additions: 8, deletions: 4 }],
1280+
...overrides,
1281+
};
1282+
}
1283+
1284+
function preview(sources: ReadonlyArray<ReviewDiffPreviewSource>): ReviewDiffPreviewResult {
1285+
return { cwd: "/repo", generatedAt: DateTime.nowUnsafe(), sources };
1286+
}
1287+
1288+
it("keeps git status totals for the automatic base even with a cached preview", () => {
1289+
assert.strictEqual(
1290+
resolveChangesTotals({ gitStatus, useDiffPreview: false, diffPreview: preview([source()]) }),
1291+
branchChanges,
1292+
);
1293+
});
1294+
1295+
it("keeps the working tree fallback for older status responses in the default comparison", () => {
1296+
assert.strictEqual(
1297+
resolveChangesTotals({
1298+
gitStatus: status({ workingTree }),
1299+
useDiffPreview: false,
1300+
diffPreview: null,
1301+
}),
1302+
workingTree,
1303+
);
1304+
});
1305+
1306+
it("uses the branch preview totals for an overridden comparison, ignoring uncommitted totals", () => {
1307+
assert.deepEqual(
1308+
resolveChangesTotals({
1309+
gitStatus,
1310+
useDiffPreview: true,
1311+
diffPreview: preview([source({ kind: "working-tree" }), source()]),
1312+
}),
1313+
{ insertions: 8, deletions: 4 },
1314+
);
1315+
});
1316+
1317+
it("uses all file statistics when the patch is truncated", () => {
1318+
assert.deepEqual(
1319+
resolveChangesTotals({
1320+
gitStatus,
1321+
useDiffPreview: true,
1322+
diffPreview: preview([
1323+
source({
1324+
truncated: true,
1325+
files: [
1326+
{ path: "a.txt", previousPath: null, additions: 8, deletions: 4 },
1327+
{ path: "b.txt", previousPath: null, additions: 5, deletions: 2 },
1328+
],
1329+
}),
1330+
]),
1331+
}),
1332+
{ insertions: 13, deletions: 6 },
1333+
);
1334+
});
1335+
1336+
it("keeps zero preview totals when ignoring whitespace removes all changes", () => {
1337+
assert.deepEqual(
1338+
resolveChangesTotals({
1339+
gitStatus,
1340+
useDiffPreview: true,
1341+
diffPreview: preview([source({ files: [] })]),
1342+
}),
1343+
{ insertions: 0, deletions: 0 },
1344+
);
1345+
});
1346+
1347+
it("matches the panel's patch totals for older previews without file statistics", () => {
1348+
const { files: _files, ...oldSource } = source();
1349+
assert.deepEqual(
1350+
resolveChangesTotals({ gitStatus, useDiffPreview: true, diffPreview: preview([oldSource]) }),
1351+
{ insertions: 2, deletions: 1 },
1352+
);
1353+
});
1354+
1355+
it("keeps the status totals while the requested preview is unavailable", () => {
1356+
for (const diffPreview of [null, preview([]), preview([source({ kind: "working-tree" })])]) {
1357+
assert.strictEqual(
1358+
resolveChangesTotals({ gitStatus, useDiffPreview: true, diffPreview }),
1359+
branchChanges,
1360+
);
1361+
}
1362+
});
1363+
});

‎apps/web/src/components/GitActionsControl.logic.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import type {
22
GitRunStackedActionResult,
33
GitStackedAction,
4+
ReviewDiffPreviewResult,
45
VcsStatusResult,
56
} from "@t3tools/contracts";
67
import { isTemporaryWorktreeBranch } from "@t3tools/shared/git";
78
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
89
import type { ChangeRequestTerminology } from "@t3tools/client-runtime/source-control-clients";
910

11+
import { getDiffLineStat, getRenderablePatch } from "../lib/diffRendering";
12+
1013
export type GitActionIconName = "commit" | "push" | "pr";
1114

1215
export type GitDialogAction = "commit" | "push" | "create_pr";
@@ -486,3 +489,28 @@ export function resolveLiveThreadBranchUpdate(input: {
486489

487490
// Re-export from shared for backwards compatibility in this module's exports
488491
export { resolveAutoFeatureBranchName } from "@t3tools/shared/git";
492+
493+
export function resolveChangesTotals(input: {
494+
readonly gitStatus: VcsStatusResult | null;
495+
readonly useDiffPreview: boolean;
496+
readonly diffPreview: ReviewDiffPreviewResult | null;
497+
}) {
498+
// Older servers only report uncommitted totals.
499+
const statusTotals = input.gitStatus?.branchChanges ?? input.gitStatus?.workingTree;
500+
if (!input.useDiffPreview) return statusTotals;
501+
const source = input.diffPreview?.sources.find((source) => source.kind === "branch-range");
502+
// Keep the status totals on screen until the preview loads rather than showing zeros.
503+
if (!source) return statusTotals;
504+
if (source.files) {
505+
return source.files.reduce(
506+
(total, file) => ({
507+
insertions: total.insertions + file.additions,
508+
deletions: total.deletions + file.deletions,
509+
}),
510+
{ insertions: 0, deletions: 0 },
511+
);
512+
}
513+
const patch = getRenderablePatch(source.diff);
514+
const totals = getDiffLineStat(patch?.kind === "files" ? patch.files : []);
515+
return { insertions: totals.additions, deletions: totals.deletions };
516+
}

‎apps/web/src/components/GitActionsControl.tsx‎

Lines changed: 71 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import {
5757
resolveLiveThreadBranchUpdate,
5858
resolveThreadBranchMetadataPatch,
5959
resolveQuickAction,
60+
resolveChangesTotals,
6061
resolveThreadBranchUpdate,
6162
} from "./GitActionsControl.logic";
6263
import { WizardPopup, WizardHeader, WizardSteps, WizardPanel, WizardFooter } from "./ui/wizard";
@@ -119,6 +120,10 @@ import { getSourceControlPresentation, sourceControlIcon } from "~/sourceControl
119120
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
120121
import { formatDiffCount } from "@t3tools/client-runtime/diff-count";
121122
import { useOpenLink } from "~/browser/useOpenLink";
123+
import { selectThreadDiffPanelBaseRef, useDiffPanelStore } from "~/diffPanelStore";
124+
import { useClientSettings } from "~/hooks/useSettings";
125+
import { useFilesystemReadAccess } from "~/state/filesystem";
126+
import { reviewEnvironment } from "~/state/review";
122127

123128
interface GitActionsControlProps {
124129
presentation?: "toolbar" | "menu";
@@ -991,6 +996,66 @@ function PublishRepositoryDialog(props: PublishRepositoryDialogProps) {
991996
);
992997
}
993998

999+
function ThreadChangesControl({
1000+
gitCwd,
1001+
activeThreadRef,
1002+
gitStatus,
1003+
onOpenChanges,
1004+
}: Pick<GitActionsControlProps, "gitCwd" | "activeThreadRef"> & {
1005+
gitStatus: VcsStatusResult | null;
1006+
onOpenChanges: GitActionsControlProps["onOpenChanges"];
1007+
}) {
1008+
const baseRef = useDiffPanelStore((state) =>
1009+
selectThreadDiffPanelBaseRef(state, activeThreadRef),
1010+
);
1011+
const ignoreWhitespace = useClientSettings((settings) => settings.diffIgnoreWhitespace);
1012+
// Status totals use the automatic base; a base picked in the diff panel needs its preview.
1013+
const useDiffPreview = baseRef !== null;
1014+
const { canReadFiles } = useFilesystemReadAccess(
1015+
useDiffPreview ? (activeThreadRef?.environmentId ?? null) : null,
1016+
);
1017+
// Same atom and key as the diff panel, so an open or recently closed panel's result is reused.
1018+
const changesDiffPreview = useEnvironmentQuery(
1019+
canReadFiles && useDiffPreview && activeThreadRef && gitCwd
1020+
? reviewEnvironment.diffPreview({
1021+
environmentId: activeThreadRef.environmentId,
1022+
input: {
1023+
cwd: gitCwd,
1024+
...(baseRef ? { baseRef } : {}),
1025+
ignoreWhitespace,
1026+
},
1027+
})
1028+
: null,
1029+
);
1030+
const changesTotals = useMemo(
1031+
() =>
1032+
resolveChangesTotals({
1033+
gitStatus,
1034+
useDiffPreview,
1035+
diffPreview: canReadFiles ? changesDiffPreview.data : null,
1036+
}),
1037+
[canReadFiles, changesDiffPreview.data, gitStatus, useDiffPreview],
1038+
);
1039+
1040+
return (
1041+
<ThreadDetailsControl
1042+
type="button"
1043+
variant="ghost"
1044+
size="sm"
1045+
part="row"
1046+
disabled={!onOpenChanges}
1047+
onClick={onOpenChanges}
1048+
>
1049+
<FileDiffIcon className={THREAD_DETAILS_PANEL_ICON_CLASS} aria-hidden />
1050+
<span className={cn("flex-1 text-left", THREAD_DETAILS_PANEL_LABEL_CLASS)}>Changes</span>
1051+
<span className="flex items-center gap-1 font-mono text-2xs tabular-nums">
1052+
<span className="text-success">+{formatDiffCount(changesTotals?.insertions ?? 0)}</span>
1053+
<span className="text-destructive">-{formatDiffCount(changesTotals?.deletions ?? 0)}</span>
1054+
</span>
1055+
</ThreadDetailsControl>
1056+
);
1057+
}
1058+
9941059
export default function GitActionsControl({
9951060
presentation = "toolbar",
9961061
gitCwd,
@@ -1145,8 +1210,6 @@ export default function GitActionsControl({
11451210
const isRepo = gitStatus?.isRepo ?? true;
11461211
const hasPrimaryRemote = gitStatus?.hasPrimaryRemote ?? false;
11471212
const gitStatusForActions = gitStatus;
1148-
// Matches the diff panel's Changes view. Older servers only report uncommitted totals.
1149-
const changesTotals = gitStatusForActions?.branchChanges ?? gitStatusForActions?.workingTree;
11501213

11511214
const allFiles = gitStatusForActions?.workingTree.files ?? [];
11521215
const selectedFiles = allFiles.filter((f) => !excludedFiles.has(f.path));
@@ -1907,23 +1970,12 @@ export default function GitActionsControl({
19071970
)}
19081971

19091972
{isPanel && isRepo ? (
1910-
<ThreadDetailsControl
1911-
type="button"
1912-
variant="ghost"
1913-
size="sm"
1914-
part="row"
1915-
disabled={!onOpenChanges}
1916-
onClick={onOpenChanges}
1917-
>
1918-
<FileDiffIcon className={THREAD_DETAILS_PANEL_ICON_CLASS} aria-hidden />
1919-
<span className={cn("flex-1 text-left", THREAD_DETAILS_PANEL_LABEL_CLASS)}>Changes</span>
1920-
<span className="flex items-center gap-1 font-mono text-2xs tabular-nums">
1921-
<span className="text-success">+{formatDiffCount(changesTotals?.insertions ?? 0)}</span>
1922-
<span className="text-destructive">
1923-
-{formatDiffCount(changesTotals?.deletions ?? 0)}
1924-
</span>
1925-
</span>
1926-
</ThreadDetailsControl>
1973+
<ThreadChangesControl
1974+
gitCwd={gitCwd}
1975+
activeThreadRef={activeThreadRef}
1976+
gitStatus={gitStatusForActions}
1977+
onOpenChanges={onOpenChanges}
1978+
/>
19271979
) : null}
19281980

19291981
<Dialog

‎apps/web/src/diffPanelStore.test.ts‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { scopeThreadRef } from "@t3tools/client-runtime/environment";
22
import { EnvironmentId, ThreadId, RunId } from "@t3tools/contracts";
33
import { beforeEach, describe, expect, it } from "vite-plus/test";
44

5-
import { selectThreadDiffPanelSelection, useDiffPanelStore } from "./diffPanelStore";
5+
import {
6+
selectThreadDiffPanelBaseRef,
7+
selectThreadDiffPanelSelection,
8+
useDiffPanelStore,
9+
} from "./diffPanelStore";
610

711
const THREAD_REF = scopeThreadRef(EnvironmentId.make("environment-1"), ThreadId.make("thread-1"));
812

@@ -123,3 +127,43 @@ describe("diffPanelStore", () => {
123127
});
124128
});
125129
});
130+
131+
describe("selectThreadDiffPanelBaseRef", () => {
132+
beforeEach(() => useDiffPanelStore.setState({ byThreadKey: {}, branchBaseRefByThreadKey: {} }));
133+
134+
it("uses the remembered Changes base while viewing uncommitted edits or a turn", () => {
135+
const store = useDiffPanelStore.getState();
136+
store.selectBranchBaseRef(THREAD_REF, "origin/release");
137+
expect(selectThreadDiffPanelBaseRef(useDiffPanelStore.getState(), THREAD_REF)).toBe(
138+
"origin/release",
139+
);
140+
store.selectGitScope(THREAD_REF, "unstaged");
141+
expect(selectThreadDiffPanelBaseRef(useDiffPanelStore.getState(), THREAD_REF)).toBe(
142+
"origin/release",
143+
);
144+
store.selectTurn(THREAD_REF, RunId.make("turn-1"));
145+
expect(selectThreadDiffPanelBaseRef(useDiffPanelStore.getState(), THREAD_REF)).toBe(
146+
"origin/release",
147+
);
148+
});
149+
150+
it("returns to automatic after clearing a custom base", () => {
151+
const store = useDiffPanelStore.getState();
152+
store.selectBranchBaseRef(THREAD_REF, "origin/release");
153+
store.selectBranchBaseRef(THREAD_REF, null);
154+
store.selectTurn(THREAD_REF, RunId.make("turn-1"));
155+
expect(selectThreadDiffPanelBaseRef(useDiffPanelStore.getState(), THREAD_REF)).toBeNull();
156+
});
157+
158+
it("keeps bases scoped to their environment and thread", () => {
159+
const otherEnvironmentRef = scopeThreadRef(
160+
EnvironmentId.make("environment-2"),
161+
THREAD_REF.threadId,
162+
);
163+
useDiffPanelStore.getState().selectBranchBaseRef(THREAD_REF, "origin/release");
164+
expect(
165+
selectThreadDiffPanelBaseRef(useDiffPanelStore.getState(), otherEnvironmentRef),
166+
).toBeNull();
167+
expect(selectThreadDiffPanelBaseRef(useDiffPanelStore.getState(), null)).toBeNull();
168+
});
169+
});

‎apps/web/src/diffPanelStore.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,14 @@ export function selectThreadDiffPanelSelection(
138138
if (!ref) return DEFAULT_SELECTION;
139139
return byThreadKey[scopedThreadKey(ref)] ?? DEFAULT_SELECTION;
140140
}
141+
142+
export function selectThreadDiffPanelBaseRef(
143+
state: Pick<DiffPanelStoreState, "byThreadKey" | "branchBaseRefByThreadKey">,
144+
ref: ScopedThreadRef | null | undefined,
145+
): string | null {
146+
if (!ref) return null;
147+
const selection = selectThreadDiffPanelSelection(state.byThreadKey, ref);
148+
return selection.kind === "branch"
149+
? selection.baseRef
150+
: (state.branchBaseRefByThreadKey[scopedThreadKey(ref)] ?? null);
151+
}

0 commit comments

Comments
 (0)