Skip to content

Commit 0ea73a7

Browse files
authored
fix(code): fix binary file count for toolbar diff count (#2999)
## Problem <!-- Who is this for and what problem does it solve? --> <!-- Closes #ISSUE_ID --> ## Changes <!-- What did you change and why? --> <!-- If there are frontend changes, include screenshots. --> ## How did you test this? <!-- Describe what you tested -- manual steps, automated tests, or both. --> <!-- If you're an agent, only list tests you actually ran. --> ## Automatic notifications - [ ] Publish to changelog? - [ ] Alert Sales and Marketing teams?
1 parent 5d2a429 commit 0ea73a7

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

packages/git/src/queries.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import path from "node:path";
44
import { afterEach, beforeEach, describe, expect, it } from "vitest";
55
import { createGitClient } from "./client";
66
import {
7+
type ChangedFileInfo,
8+
computeDiffStatsFromFiles,
79
detectDefaultBranch,
810
getAllBranches,
911
getBranchDiffPatchesByPath,
@@ -314,6 +316,59 @@ describe("getChangedFilesDetailed > untracked line counts", () => {
314316
linesAdded: LINE_COUNT_LARGER_THAN_READ_STREAM_CHUNK,
315317
});
316318
});
319+
320+
// Regression for #2983 follow-up: an untracked binary file (png, mp4, …)
321+
// must not have its newline bytes counted as added lines. Pre-fix this
322+
// surfaced as an inflated diff badge (e.g. +8147) after dropping in a
323+
// screenshot or screen recording.
324+
it.each([["shot.png"], ["clip.mp4"]])(
325+
"reports no line count for untracked binary file %s",
326+
async (name) => {
327+
repoDir = await setupRepo();
328+
// Content packed with newline bytes — what the line counter would have
329+
// tallied if it didn't skip binary files.
330+
await writeFile(path.join(repoDir, name), "\n".repeat(8147));
331+
332+
const files = await getChangedFilesDetailed(repoDir);
333+
const binary = files.find((f) => f.path === name);
334+
335+
expect(binary).toMatchObject({ status: "untracked" });
336+
expect(binary?.linesAdded).toBeUndefined();
337+
expect(binary?.linesRemoved).toBeUndefined();
338+
},
339+
);
340+
});
341+
342+
describe("computeDiffStatsFromFiles", () => {
343+
it("excludes binary files from line totals but still counts them as changed", () => {
344+
const files: ChangedFileInfo[] = [
345+
{
346+
path: "src/app.ts",
347+
status: "modified",
348+
linesAdded: 10,
349+
linesRemoved: 4,
350+
},
351+
// Binary line counts are meaningless newline-byte tallies — exclude them.
352+
{
353+
path: "assets/shot.png",
354+
status: "untracked",
355+
linesAdded: 8147,
356+
linesRemoved: 0,
357+
},
358+
{
359+
path: "assets/clip.mp4",
360+
status: "untracked",
361+
linesAdded: 5000,
362+
linesRemoved: 0,
363+
},
364+
];
365+
366+
expect(computeDiffStatsFromFiles(files)).toEqual({
367+
filesChanged: 3,
368+
linesAdded: 10,
369+
linesRemoved: 4,
370+
});
371+
});
317372
});
318373

319374
describe("getAllBranches", () => {

packages/git/src/queries.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createReadStream } from "node:fs";
22
import * as fs from "node:fs/promises";
33
import * as path from "node:path";
4+
import { isBinaryFile } from "@posthog/shared";
45
import type { CreateGitClientOptions } from "./client";
56
import { mapWithConcurrency } from "./concurrency";
67
import { getGitOperationManager } from "./operation-manager";
@@ -655,6 +656,10 @@ export async function getChangedFilesDetailed(
655656
if (excludePatterns && matchesExcludePattern(file, excludePatterns)) {
656657
continue;
657658
}
659+
if (isBinaryFile(file)) {
660+
files.push({ path: file, status: "untracked" });
661+
continue;
662+
}
658663
untrackedToCount.push(file);
659664
}
660665

@@ -826,9 +831,10 @@ export function computeDiffStatsFromFiles(files: ChangedFileInfo[]): DiffStats {
826831
const uniquePaths = new Set<string>();
827832

828833
for (const file of files) {
834+
uniquePaths.add(file.path);
835+
if (isBinaryFile(file.path)) continue;
829836
linesAdded += file.linesAdded ?? 0;
830837
linesRemoved += file.linesRemoved ?? 0;
831-
uniquePaths.add(file.path);
832838
}
833839

834840
return {

0 commit comments

Comments
 (0)