Skip to content

Commit 4095917

Browse files
committed
fix(web): report host line counts for files with withheld hunks
GitHub omits hunks for files it will not inline, so the viewer's hunk-sum counts read -0 +0. The host's own additions/deletions now ride the diff slice, and the code tab draws every file's counts itself.
1 parent e9cbc08 commit 4095917

6 files changed

Lines changed: 91 additions & 2 deletions

File tree

‎apps/server/src/pullRequest/GitHubPullRequestCli.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
PullRequestInvolvement,
1010
PullRequestListState,
1111
PullRequestMergeMethod,
12+
PullRequestOmittedFileStat,
1213
PullRequestReviewCommentDraft,
1314
PullRequestReviewVerdict,
1415
PullRequestReviewerCandidateList,
@@ -270,6 +271,8 @@ export interface GitHubPullRequestDiffSlice {
270271
readonly truncated: boolean;
271272
/** Where the next slice starts, or null once the patch is whole. */
272273
readonly nextCursor: string | null;
274+
/** GitHub's own counts for the files whose hunks it withheld from this slice. */
275+
readonly omittedFileStats?: ReadonlyArray<PullRequestOmittedFileStat>;
273276
}
274277

275278
export class GitHubPullRequestCli extends Context.Service<
@@ -796,6 +799,9 @@ export const make = Effect.gen(function* () {
796799
patch: decoded.success.patch,
797800
truncated: decoded.success.truncated,
798801
nextCursor: morePages ? String(input.page + 1) : null,
802+
...(decoded.success.omittedFileStats.length === 0
803+
? {}
804+
: { omittedFileStats: decoded.success.omittedFileStats }),
799805
});
800806
}),
801807
);

‎apps/server/src/pullRequest/PullRequestProvider.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {
1313
PullRequestMergeCapabilities,
1414
PullRequestMergeMethod,
1515
PullRequestMergeability,
16+
PullRequestOmittedFileStat,
1617
PullRequestReviewCommentDraft,
1718
PullRequestReviewThread,
1819
PullRequestReviewVerdict,
@@ -165,6 +166,8 @@ export interface ProviderDiffSlice {
165166
/** Something in this slice could not be shown, as opposed to there being more slices. */
166167
readonly truncated: boolean;
167168
readonly nextCursor: string | null;
169+
/** The host's own counts for the files whose hunks it withheld from this slice. */
170+
readonly omittedFileStats?: ReadonlyArray<PullRequestOmittedFileStat>;
168171
}
169172

170173
export interface ProviderDiffFileContents {

‎apps/server/src/pullRequest/gitHubPullRequestJson.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
PullRequestCommit,
1111
PullRequestLabel,
1212
PullRequestMergeCapabilities,
13+
PullRequestOmittedFileStat,
1314
PullRequestMergeability,
1415
PullRequestReviewCommentDraft,
1516
PullRequestReviewThread,
@@ -1539,6 +1540,8 @@ export interface GitHubPullRequestFilesPatch {
15391540
readonly truncated: boolean;
15401541
/** Files GitHub returned, counted before decoding, so the caller can page. */
15411542
readonly rawCount: number;
1543+
/** GitHub's own counts for the files whose hunks it withheld. */
1544+
readonly omittedFileStats: ReadonlyArray<PullRequestOmittedFileStat>;
15421545
}
15431546

15441547
/**
@@ -1554,6 +1557,7 @@ export function decodePullRequestFilesJson(
15541557
return Result.fail(decoded.failure);
15551558
}
15561559
const sections: string[] = [];
1560+
const omittedFileStats: PullRequestOmittedFileStat[] = [];
15571561
let truncated = false;
15581562
for (const entry of decoded.success) {
15591563
const file = decodeFileEntry(entry);
@@ -1565,7 +1569,12 @@ export function decodePullRequestFilesJson(
15651569
// A file with no hunks is still a file that changed: a pure rename has none to give, and
15661570
// a binary one has none that can be shown. Both are listed, and only the second is a hole
15671571
// in the patch — leaving them out entirely would drop them from the change altogether.
1568-
if ((value.additions ?? 0) + (value.deletions ?? 0) > 0) truncated = true;
1572+
const additions = value.additions ?? 0;
1573+
const deletions = value.deletions ?? 0;
1574+
if (additions + deletions > 0) {
1575+
truncated = true;
1576+
omittedFileStats.push({ path: value.filename, additions, deletions });
1577+
}
15691578
}
15701579
// A rename counts its hunks against the old path, which is the only place it is named.
15711580
const oldPath =
@@ -1586,5 +1595,6 @@ export function decodePullRequestFilesJson(
15861595
patch: sections.join(""),
15871596
truncated,
15881597
rawCount: decoded.success.length,
1598+
omittedFileStats,
15891599
});
15901600
}

‎apps/web/src/components/diffs/StyledDiffCodeView.tsx‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,19 @@ type StyledDiffCodeViewProps<LAnnotation> = (
269269
) & {
270270
readonly options?: StyledDiffCodeViewOptions<LAnnotation>;
271271
readonly viewerRef?: Ref<CodeViewHandle<LAnnotation>>;
272+
/**
273+
* Appended to the shared stylesheet inside the viewer's shadow root, for a surface that has
274+
* to restyle chrome the viewer owns — such as replacing its per-file line counts.
275+
*/
276+
readonly unsafeCSSExtra?: string;
272277
};
273278

274279
/** The shared web CodeView surface: app styling and virtualized geometry stay paired here. */
275280
export function StyledDiffCodeView<LAnnotation = undefined>({
276281
options,
277282
viewerRef,
278283
className,
284+
unsafeCSSExtra,
279285
...props
280286
}: StyledDiffCodeViewProps<LAnnotation>) {
281287
return (
@@ -291,7 +297,9 @@ export function StyledDiffCodeView<LAnnotation = undefined>({
291297
}
292298
options={{
293299
...options,
294-
unsafeCSS: DIFF_VIEW_UNSAFE_CSS,
300+
unsafeCSS: unsafeCSSExtra
301+
? `${DIFF_VIEW_UNSAFE_CSS}\n${unsafeCSSExtra}`
302+
: DIFF_VIEW_UNSAFE_CSS,
295303
itemMetrics: {
296304
diffHeaderHeight: 32,
297305
hunkSeparatorHeight: 24,

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
EnvironmentId,
55
PullRequestDetailView,
66
PullRequestDiffSide,
7+
PullRequestOmittedFileStat,
78
PullRequestRef,
89
PullRequestReviewThread,
910
} from "@t3tools/contracts";
@@ -95,8 +96,20 @@ interface DiffSlice {
9596
readonly patch: string;
9697
readonly truncated: boolean;
9798
readonly nextCursor: string | null;
99+
readonly omittedFileStats: ReadonlyArray<PullRequestOmittedFileStat>;
98100
}
99101

102+
/**
103+
* The viewer's own per-file counts are hidden and drawn from this side of its shadow root
104+
* instead: its counts are hunk sums, and a file whose hunks the host withheld would read as
105+
* an empty change rather than as the counts the host did report.
106+
*/
107+
const REPLACE_FILE_COUNTS_CSS = `
108+
[data-diffs-header] [data-additions-count],
109+
[data-diffs-header] [data-deletions-count] {
110+
display: none !important;
111+
}`;
112+
100113
/** Nothing loaded yet, as one identity, so the memos below do not see a new array every render. */
101114
const NO_SLICES: ReadonlyArray<DiffSlice> = [];
102115

@@ -246,6 +259,7 @@ export function PullRequestCodeTab({
246259
patch: data.patch,
247260
truncated: data.truncated,
248261
nextCursor: data.nextCursor,
262+
omittedFileStats: data.omittedFileStats ?? [],
249263
};
250264
const index = slices.findIndex((slice) => slice.cursor === cursor);
251265
if (index === -1) {
@@ -471,6 +485,15 @@ export function PullRequestCodeTab({
471485
],
472486
);
473487
const lineStat = useMemo(() => getDiffLineStat(files), [files]);
488+
const omittedFileStats = useMemo(
489+
() =>
490+
new Map(
491+
loadedSlices.flatMap((slice) =>
492+
slice.omittedFileStats.map((file) => [file.path, file] as const),
493+
),
494+
),
495+
[loadedSlices],
496+
);
474497
const fileKeys = useMemo(() => items.map((item) => item.id), [items]);
475498
const collapsedFileKeys = useMemo(
476499
() => new Set(items.filter((item) => item.collapsed === true).map((item) => item.id)),
@@ -649,6 +672,30 @@ export function PullRequestCodeTab({
649672
[toggleFile],
650673
);
651674

675+
const renderHeaderMetadata = useCallback(
676+
(item: CodeViewItem<ReviewAnnotationGroup>) => {
677+
if (item.type !== "diff") return null;
678+
let additions = 0;
679+
let deletions = 0;
680+
for (const hunk of item.fileDiff.hunks) {
681+
additions += hunk.additionLines;
682+
deletions += hunk.deletionLines;
683+
}
684+
if (additions === 0 && deletions === 0) {
685+
const withheld = omittedFileStats.get(resolveFileDiffPath(item.fileDiff));
686+
if (withheld) ({ additions, deletions } = withheld);
687+
}
688+
return (
689+
<PullRequestDiffStat
690+
additions={additions}
691+
deletions={deletions}
692+
className="font-mono text-[11px]"
693+
/>
694+
);
695+
},
696+
[omittedFileStats],
697+
);
698+
652699
const diffViewOptions = useMemo(
653700
() => ({
654701
diffStyle: diffRenderMode === "split" ? ("split" as const) : ("unified" as const),
@@ -1222,7 +1269,9 @@ export function PullRequestCodeTab({
12221269
// is running out of diff.
12231270
renderCodeViewFooter={renderCodeViewFooter}
12241271
renderHeaderPrefix={renderHeaderPrefix}
1272+
renderHeaderMetadata={renderHeaderMetadata}
12251273
renderAnnotation={renderAnnotation}
1274+
unsafeCSSExtra={REPLACE_FILE_COUNTS_CSS}
12261275
/>
12271276
{reviewOverlay}
12281277
</div>

‎packages/contracts/src/pullRequest.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,14 @@ export const PullRequestDiffInput = Schema.Struct({
562562
});
563563
export type PullRequestDiffInput = typeof PullRequestDiffInput.Type;
564564

565+
/** Real line counts for a file whose hunks the host withheld from the patch. */
566+
export const PullRequestOmittedFileStat = Schema.Struct({
567+
path: TrimmedNonEmptyString,
568+
additions: Schema.Number,
569+
deletions: Schema.Number,
570+
});
571+
export type PullRequestOmittedFileStat = typeof PullRequestOmittedFileStat.Type;
572+
565573
export const PullRequestDiffResult = Schema.Struct({
566574
patch: Schema.String,
567575
/**
@@ -571,6 +579,11 @@ export const PullRequestDiffResult = Schema.Struct({
571579
truncated: Schema.Boolean,
572580
/** Where the next slice starts, or null once the diff is whole. */
573581
nextCursor: Schema.NullOr(TrimmedNonEmptyString),
582+
/**
583+
* The host's own counts for the files whose hunks it withheld, so a file the patch cannot
584+
* show still reports what changed instead of a zero the diff never had.
585+
*/
586+
omittedFileStats: Schema.optional(Schema.Array(PullRequestOmittedFileStat)),
574587
});
575588
export type PullRequestDiffResult = typeof PullRequestDiffResult.Type;
576589

0 commit comments

Comments
 (0)