Skip to content

Commit 1d34eb6

Browse files
committed
fix(web): show the change breakdown on the floating Changes button
1 parent eb751fa commit 1d34eb6

7 files changed

Lines changed: 111 additions & 56 deletions

File tree

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
11
import type { PullRequestChangeBreakdown } from "@t3tools/contracts";
2-
import { classifyChangedFile } from "@t3tools/shared/changedFileKind";
2+
import { summarizeChangedFiles } from "@t3tools/shared/changedFileKind";
33
import type { ProviderFileStats } from "@t3tools/source-control-core/server/PullRequestProvider";
44

55
export function toChangeBreakdown(stats: ProviderFileStats): PullRequestChangeBreakdown {
66
if (stats.truncated) return null;
7-
const empty = () => ({ additions: 0, deletions: 0 });
8-
const breakdown = {
9-
code: empty(),
10-
tests: empty(),
11-
mocks: empty(),
12-
generated: empty(),
13-
docs: empty(),
14-
};
15-
for (const file of stats.files) {
16-
const bucket = breakdown[classifyChangedFile(file.path)];
17-
bucket.additions += file.additions;
18-
bucket.deletions += file.deletions;
19-
}
20-
return breakdown;
7+
return summarizeChangedFiles(stats.files);
218
}

‎apps/server/src/vcs/GitVcsDriverCore.test.ts‎

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,7 +2072,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
20722072
for (const baseRef of ["upstream/main", "canonical/team/main", "release/v2"]) {
20732073
yield* git(cwd, ["config", "branch.feature/configured-base.gh-merge-base", baseRef]);
20742074
const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true });
2075-
assert.deepStrictEqual(status.branchChanges, { baseRef, insertions: 1, deletions: 0 });
2075+
assert.deepInclude(status.branchChanges, { baseRef, insertions: 1, deletions: 0 });
20762076
assert.strictEqual(status.aheadCount, 1);
20772077
const preview = yield* driver.getReviewDiffPreview({ cwd });
20782078
const changes = preview.sources.find((source) => source.kind === "branch-range")!;
@@ -2085,7 +2085,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
20852085
for (const baseRef of ["origin/main", "upstream/missing", "main"]) {
20862086
yield* git(cwd, ["config", "branch.feature/configured-base.gh-merge-base", baseRef]);
20872087
const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true });
2088-
assert.deepStrictEqual(status.branchChanges, {
2088+
assert.deepInclude(status.branchChanges, {
20892089
baseRef: "origin/main",
20902090
insertions: 2,
20912091
deletions: 0,
@@ -2102,7 +2102,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
21022102
for (const shadowRef of ["refs/heads/upstream/main", "refs/tags/upstream/main"]) {
21032103
yield* git(cwd, ["update-ref", shadowRef, "HEAD~2"]);
21042104
const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true });
2105-
assert.deepStrictEqual(status.branchChanges, {
2105+
assert.deepInclude(status.branchChanges, {
21062106
baseRef: "refs/remotes/upstream/main",
21072107
insertions: 2,
21082108
deletions: 1,
@@ -2155,7 +2155,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
21552155
}
21562156
const baseRef = configuredBase ?? "main";
21572157
const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true });
2158-
assert.deepStrictEqual(status.branchChanges, { baseRef, insertions: 1, deletions: 0 });
2158+
assert.deepInclude(status.branchChanges, { baseRef, insertions: 1, deletions: 0 });
21592159
assert.strictEqual(status.aheadCount, 1);
21602160
const preview = yield* driver.getReviewDiffPreview({ cwd });
21612161
const changes = preview.sources.find((source) => source.kind === "branch-range")!;
@@ -2243,7 +2243,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
22432243
{ path: "new.txt", previousPath: null, additions: 2, deletions: 0 },
22442244
]);
22452245
const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true });
2246-
assert.deepStrictEqual(status.branchChanges, {
2246+
assert.deepInclude(status.branchChanges, {
22472247
baseRef: null,
22482248
insertions: 2,
22492249
deletions: 0,
@@ -2329,7 +2329,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
23292329
assert.notEqual(defaultStat, minimalStat);
23302330
const [insertions, deletions] = defaultStat.split("\t");
23312331
const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true });
2332-
assert.deepStrictEqual(status.branchChanges, {
2332+
assert.deepInclude(status.branchChanges, {
23332333
baseRef: initialBranch,
23342334
insertions: Number(insertions),
23352335
deletions: Number(deletions),
@@ -2402,15 +2402,26 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
24022402
yield* writeTextFile(cwd, "README.md", "# test\ncommitted\n");
24032403
yield* git(cwd, ["commit", "-am", "commit edit"]);
24042404
yield* writeTextFile(cwd, "nested/untracked.txt", "one\ntwo\n");
2405+
yield* writeTextFile(cwd, "src/code.ts", "export const value = 1;\n");
2406+
yield* writeTextFile(cwd, "src/code.test.ts", "test\n");
2407+
yield* writeTextFile(cwd, "__mocks__/value.ts", "mock\n");
2408+
yield* writeTextFile(cwd, "pnpm-lock.yaml", "lock\n");
24052409

24062410
const status = yield* driver.statusDetailsLocal(pathService.join(cwd, "nested"), {
24072411
includeBranchChanges: true,
24082412
});
2409-
assert.deepStrictEqual(status.branchChanges, {
2413+
assert.deepInclude(status.branchChanges, {
24102414
baseRef: "main",
2411-
insertions: 3,
2415+
insertions: 7,
24122416
deletions: 0,
24132417
});
2418+
assert.deepStrictEqual(status.branchChanges?.breakdown, {
2419+
code: { additions: 1, deletions: 0 },
2420+
tests: { additions: 1, deletions: 0 },
2421+
mocks: { additions: 1, deletions: 0 },
2422+
generated: { additions: 1, deletions: 0 },
2423+
docs: { additions: 3, deletions: 0 },
2424+
});
24142425
assert.isUndefined((yield* driver.statusDetailsLocal(cwd)).branchChanges);
24152426
}),
24162427
);
@@ -2438,7 +2449,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
24382449
yield* git(cwd, ["branch", "--set-upstream-to=upstream/main", "main"]);
24392450

24402451
const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true });
2441-
assert.deepStrictEqual(status.branchChanges, {
2452+
assert.deepInclude(status.branchChanges, {
24422453
baseRef: "upstream/main",
24432454
insertions: 0,
24442455
deletions: 0,
@@ -2451,7 +2462,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
24512462
// Without an upstream the primary remote's copy stays the base.
24522463
yield* git(cwd, ["branch", "--unset-upstream", "main"]);
24532464
const untracked = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true });
2454-
assert.deepStrictEqual(untracked.branchChanges, {
2465+
assert.deepInclude(untracked.branchChanges, {
24552466
baseRef: "origin/main",
24562467
insertions: 3,
24572468
deletions: 0,
@@ -2475,7 +2486,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
24752486
);
24762487

24772488
const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true });
2478-
assert.deepStrictEqual(status.branchChanges, {
2489+
assert.deepInclude(status.branchChanges, {
24792490
baseRef: "main",
24802491
insertions: 2_002,
24812492
deletions: 0,

‎apps/server/src/vcs/GitVcsDriverCore.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
type VcsRef,
3434
} from "@t3tools/contracts";
3535
import { dedupeRemoteBranchesWithLocalMatches } from "@t3tools/shared/git";
36+
import { summarizeChangedFiles } from "@t3tools/shared/changedFileKind";
3637
import * as HostProcess from "@t3tools/shared/HostProcess";
3738
import { compactTraceAttributes } from "@t3tools/shared/observability";
3839
import { decodeJsonResult } from "@t3tools/shared/schemaJson";
@@ -3087,11 +3088,12 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
30873088
}
30883089
let insertions = 0;
30893090
let deletions = 0;
3090-
for (const file of parseReviewNumstat(result.stdout)) {
3091+
const files = parseReviewNumstat(result.stdout);
3092+
for (const file of files) {
30913093
insertions += file.additions;
30923094
deletions += file.deletions;
30933095
}
3094-
return { baseRef, insertions, deletions };
3096+
return { baseRef, insertions, deletions, breakdown: summarizeChangedFiles(files) };
30953097
}, Effect.scoped);
30963098

30973099
const getReviewDiffPreview = Effect.fn("getReviewDiffPreview")(function* (

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

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ import {
110110
import { getSourceControlPresentation, sourceControlIcon } from "~/sourceControlPresentation";
111111
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
112112
import { formatDiffCount } from "@t3tools/client-runtime/diff-count";
113+
import { summarizeChangedFiles } from "@t3tools/shared/changedFileKind";
114+
import { ChangeBreakdownGrid } from "./pullRequest/PullRequestChangeBreakdownStat";
113115
import { useOpenLink } from "~/browser/useOpenLink";
114116

115117
interface GitActionsControlProps {
@@ -1045,6 +1047,17 @@ export default function GitActionsControl({
10451047
const hasPrimaryRemote = gitStatus?.hasPrimaryRemote ?? false;
10461048
const gitStatusForActions = gitStatus;
10471049
const changesTotals = gitStatusForActions?.branchChanges ?? gitStatusForActions?.workingTree;
1050+
const changesBreakdown = gitStatusForActions?.branchChanges
1051+
? gitStatusForActions.branchChanges.breakdown
1052+
: gitStatusForActions
1053+
? summarizeChangedFiles(
1054+
gitStatusForActions.workingTree.files.map((file) => ({
1055+
path: file.path,
1056+
additions: file.insertions,
1057+
deletions: file.deletions,
1058+
})),
1059+
)
1060+
: null;
10481061

10491062
const allFiles = gitStatusForActions?.workingTree.files ?? [];
10501063
// Changed-file paths are repository-relative, so they must resolve against
@@ -1619,35 +1632,56 @@ export default function GitActionsControl({
16191632
)}
16201633

16211634
{isRepo && !quietEmptyDraft ? (
1622-
<ThreadDetailsControl
1623-
type="button"
1624-
variant="ghost"
1625-
size="sm"
1626-
part="row"
1627-
disabled={!onOpenChanges}
1628-
onClick={onOpenChanges}
1629-
>
1630-
<FileDiffIcon className={THREAD_DETAILS_PANEL_ICON_CLASS} aria-hidden />
1631-
<span className={cn("flex-1 text-left", THREAD_DETAILS_PANEL_LABEL_CLASS)}>Changes</span>
1632-
<span className="flex items-center gap-1.5 font-mono text-xs tabular-nums">
1633-
<span
1634-
className={
1635-
changesTotals?.insertions
1636-
? "text-diff-addition-foreground"
1637-
: "text-muted-foreground"
1638-
}
1639-
>
1640-
+{formatDiffCount(changesTotals?.insertions ?? 0)}
1635+
<Tooltip>
1636+
<TooltipTrigger
1637+
render={
1638+
<ThreadDetailsControl
1639+
type="button"
1640+
variant="ghost"
1641+
size="sm"
1642+
part="row"
1643+
disabled={!onOpenChanges}
1644+
onClick={onOpenChanges}
1645+
/>
1646+
}
1647+
>
1648+
<FileDiffIcon className={THREAD_DETAILS_PANEL_ICON_CLASS} aria-hidden />
1649+
<span className={cn("flex-1 text-left", THREAD_DETAILS_PANEL_LABEL_CLASS)}>
1650+
Changes
16411651
</span>
1642-
<span
1643-
className={
1644-
changesTotals?.deletions ? "text-diff-deletion-foreground" : "text-muted-foreground"
1645-
}
1646-
>
1647-
-{formatDiffCount(changesTotals?.deletions ?? 0)}
1652+
<span className="flex items-center gap-1.5 font-mono text-xs tabular-nums">
1653+
<span
1654+
className={
1655+
changesTotals?.insertions
1656+
? "text-diff-addition-foreground"
1657+
: "text-muted-foreground"
1658+
}
1659+
>
1660+
+{formatDiffCount(changesTotals?.insertions ?? 0)}
1661+
</span>
1662+
<span
1663+
className={
1664+
changesTotals?.deletions
1665+
? "text-diff-deletion-foreground"
1666+
: "text-muted-foreground"
1667+
}
1668+
>
1669+
-{formatDiffCount(changesTotals?.deletions ?? 0)}
1670+
</span>
16481671
</span>
1649-
</span>
1650-
</ThreadDetailsControl>
1672+
</TooltipTrigger>
1673+
{changesBreakdown &&
1674+
changesTotals &&
1675+
(changesTotals.insertions > 0 || changesTotals.deletions > 0) ? (
1676+
// Base UI fixes the popup width on open; remeasure when the breakdown changes.
1677+
<TooltipPopup key={JSON.stringify(changesBreakdown)} side="bottom">
1678+
<ChangeBreakdownGrid
1679+
breakdown={changesBreakdown}
1680+
total={{ additions: changesTotals.insertions, deletions: changesTotals.deletions }}
1681+
/>
1682+
</TooltipPopup>
1683+
) : null}
1684+
</Tooltip>
16511685
) : null}
16521686

16531687
<Dialog

‎apps/web/src/components/pullRequest/PullRequestChangeBreakdownStat.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export function PullRequestChangeBreakdownRows({
9494
);
9595
}
9696

97-
function ChangeBreakdownGrid({
97+
export function ChangeBreakdownGrid({
9898
breakdown,
9999
total,
100100
}: {

‎packages/contracts/src/git.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from "./baseSchemas.ts";
1111
import { SourceControlProviderError, SourceControlProviderInfo } from "./sourceControl.ts";
1212
import { VcsDriverKind } from "./vcs.ts";
13+
import { PullRequestChangeBreakdown } from "./pullRequest.ts";
1314

1415
const TrimmedNonEmptyStringSchema = TrimmedNonEmptyString;
1516
const GitFilePath = Schema.String.check(Schema.isNonEmpty());
@@ -239,6 +240,7 @@ const VcsStatusLocalShape = {
239240
baseRef: Schema.NullOr(TrimmedNonEmptyStringSchema),
240241
insertions: NonNegativeInt,
241242
deletions: NonNegativeInt,
243+
breakdown: Schema.optional(PullRequestChangeBreakdown),
242244
}),
243245
),
244246
};

‎packages/shared/src/changedFileKind.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@ import type { PullRequestChangeBreakdown } from "@t3tools/contracts";
22

33
export type ChangedFileKind = keyof NonNullable<PullRequestChangeBreakdown>;
44

5+
export function summarizeChangedFiles(
6+
files: ReadonlyArray<{ path: string; additions: number; deletions: number }>,
7+
): NonNullable<PullRequestChangeBreakdown> {
8+
const empty = () => ({ additions: 0, deletions: 0 });
9+
const breakdown = {
10+
code: empty(),
11+
tests: empty(),
12+
mocks: empty(),
13+
generated: empty(),
14+
docs: empty(),
15+
};
16+
for (const file of files) {
17+
const bucket = breakdown[classifyChangedFile(file.path)];
18+
bucket.additions += file.additions;
19+
bucket.deletions += file.deletions;
20+
}
21+
return breakdown;
22+
}
23+
524
const GENERATED_FILES = new Set([
625
"bun.lock",
726
"bun.lockb",

0 commit comments

Comments
 (0)