Skip to content

Commit a898a06

Browse files
committed
fix(gitcafe): key cached line totals on the compared commits
1 parent 736b01f commit a898a06

2 files changed

Lines changed: 48 additions & 49 deletions

File tree

‎packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.test.ts‎

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -547,36 +547,45 @@ describe("GitCafePullRequestProvider", () => {
547547
}).pipe(Effect.provide(server.layer));
548548
});
549549

550-
it.effect("summarizes line counts once per head commit", () => {
551-
const snapshot = { version: 4, headOid, comparisonBaseOid: baseOid };
550+
it.effect("recounts summary lines only when the compared commits move", () => {
551+
const landedBase = "fedcba9876543210fedcba9876543210fedcba98";
552+
let comparisonBaseOid = baseOid;
552553
let pullReads = 0;
553554
const server = fakeGitCafe({
554-
// A comment moves the version without moving the head.
555+
// A comment moves the version without moving the compared commits.
555556
"GET /repos/owner/repo/pulls/7": () => ({ ...pull, version: 4 + pullReads++ }),
556-
"GET /repos/owner/repo/pulls/7/changes": {
557-
...snapshot,
557+
"GET /repos/owner/repo/pulls/7/changes": () => ({
558+
version: 4,
559+
headOid,
560+
comparisonBaseOid,
558561
items: [{ path: "a.ts" }, { path: "b.ts" }],
559562
next: null,
560-
},
563+
}),
561564
"POST /repos/owner/repo/pulls/7/diff-files": (request: SentRequest) => ({
562-
...snapshot,
565+
version: 4,
566+
headOid,
567+
comparisonBaseOid,
563568
items: (request.body as { paths: ReadonlyArray<string> }).paths.map((path) => ({
564569
path,
565-
additions: 3,
570+
additions: comparisonBaseOid === baseOid ? 3 : 1,
566571
deletions: 1,
567572
})),
568573
}),
569574
});
575+
const hunkReads = () =>
576+
server.sent.filter((request) => request.path.endsWith("/diff-files")).length;
570577
return Effect.gen(function* () {
571578
const provider = yield* GitCafePullRequestProvider.make;
572579
const first = yield* provider.getChangeRequestSummary!(target);
573580
const second = yield* provider.getChangeRequestSummary!(target);
574581
assert.deepInclude(first, { additions: 6, deletions: 2, changedFiles: 2 });
575582
assert.deepInclude(second, { additions: 6, deletions: 2, changedFiles: 2 });
576-
assert.lengthOf(
577-
server.sent.filter((request) => request.path.endsWith("/changes")),
578-
1,
579-
);
583+
assert.strictEqual(hunkReads(), 1);
584+
// A landed parent layer moves the comparison base under the same head.
585+
comparisonBaseOid = landedBase;
586+
const third = yield* provider.getChangeRequestSummary!(target);
587+
assert.deepInclude(third, { additions: 2, deletions: 2, changedFiles: 2 });
588+
assert.strictEqual(hunkReads(), 2);
580589
}).pipe(Effect.provide(server.layer));
581590
});
582591

‎packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.ts‎

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,11 @@ export const make = Effect.gen(function* () {
355355
return yield* failure(operation, `GitCafe's stack operation ${outcome.state}.`);
356356
});
357357

358+
// GitCafe's pulls carry no line totals, and linked pull requests are summarized every minute,
359+
// so hunk totals are held per compared pair of commits. The file list that names the pair is
360+
// cheap; a retarget or a landed parent layer moves the base without moving the head.
361+
const lineTotals = new Map<string, { readonly additions: number; readonly deletions: number }>();
362+
358363
const readLineStats = Effect.fn("GitCafePullRequestProvider.readLineStats")(function* (
359364
input: PullRef,
360365
version: number,
@@ -382,45 +387,34 @@ export const make = Effect.gen(function* () {
382387
after = batch.next;
383388
if (after === null || after === previous || batch.items.length === 0) break;
384389
}
385-
const counted =
386-
files.some((file) => file.additions !== undefined) ||
387-
files.length > LINE_STATS_MAX_FILES ||
388-
revision === undefined
389-
? files
390-
: (yield* Effect.forEach(
390+
if (revision === undefined) return { changedFiles: 0, additions: 0, deletions: 0 };
391+
const key = `${input.host} ${input.repository.toLowerCase()} ${input.number} ${revision.baseOid} ${revision.headOid}`;
392+
const held = lineTotals.get(key);
393+
if (held !== undefined) return { changedFiles: files.length, ...held };
394+
const details =
395+
files.some((file) => file.additions !== undefined) || files.length > LINE_STATS_MAX_FILES
396+
? Option.some(files)
397+
: yield* Effect.forEach(
391398
chunk(detailablePaths(files), DIFF_FILES_BATCH),
392399
(paths) => readDiffFiles(input, revision, paths),
393400
{ concurrency: 4 },
394-
)).flat();
395-
return {
396-
changedFiles: files.length,
401+
).pipe(
402+
Effect.map((batches) => batches.flat()),
403+
Effect.option,
404+
);
405+
// Unread hunks leave the lines unknown rather than zero; the file count still stands.
406+
if (Option.isNone(details)) return { changedFiles: files.length };
407+
const counted = details.value;
408+
const totals = {
397409
additions: counted.reduce((total, file) => total + (file.additions ?? 0), 0),
398410
deletions: counted.reduce((total, file) => total + (file.deletions ?? 0), 0),
399411
};
412+
if (lineTotals.size >= LINE_STATS_CACHE_SIZE)
413+
lineTotals.delete(lineTotals.keys().next().value!);
414+
lineTotals.set(key, totals);
415+
return { changedFiles: files.length, ...totals };
400416
});
401417

402-
// GitCafe's pulls carry no line totals, and linked pull requests are summarized every minute,
403-
// so totals are held per head commit rather than recounted from hunks on every read. The
404-
// pull's version also moves on comments and reviews, which change no lines.
405-
const lineStatsByHead = new Map<string, Effect.Success<ReturnType<typeof readLineStats>>>();
406-
const lineStats = (
407-
input: PullRef,
408-
pull: { readonly version: number; readonly headOid: string },
409-
) => {
410-
const key = `${input.host} ${input.repository.toLowerCase()} ${input.number} ${pull.headOid}`;
411-
const held = lineStatsByHead.get(key);
412-
if (held !== undefined) return Effect.succeed(held);
413-
return readLineStats(input, pull.version).pipe(
414-
Effect.tap((stats) =>
415-
Effect.sync(() => {
416-
if (lineStatsByHead.size >= LINE_STATS_CACHE_SIZE)
417-
lineStatsByHead.delete(lineStatsByHead.keys().next().value!);
418-
lineStatsByHead.set(key, stats);
419-
}),
420-
),
421-
);
422-
};
423-
424418
const merge = Effect.fn("GitCafePullRequestProvider.merge")(function* (
425419
input: Parameters<PullRequestProviderApi["runAction"]>[0],
426420
) {
@@ -554,9 +548,7 @@ export const make = Effect.gen(function* () {
554548
const changes =
555549
pull.headOid === null
556550
? null
557-
: yield* lineStats(input, { version: pull.version, headOid: pull.headOid }).pipe(
558-
Effect.orElseSucceed(() => null),
559-
);
551+
: yield* readLineStats(input, pull.version).pipe(Effect.orElseSucceed(() => null));
560552
// A count that could not be read is absent rather than zero.
561553
const {
562554
additions: _additions,
@@ -588,9 +580,7 @@ export const make = Effect.gen(function* () {
588580
),
589581
pull.headOid === null
590582
? Effect.succeed(null)
591-
: lineStats(input, { version: pull.version, headOid: pull.headOid }).pipe(
592-
Effect.orElseSucceed(() => null),
593-
),
583+
: readLineStats(input, pull.version).pipe(Effect.orElseSucceed(() => null)),
594584
],
595585
{ concurrency: 3 },
596586
);

0 commit comments

Comments
 (0)