From c4be6b78b6129d8f50abd75ef5909f35c5c0a582 Mon Sep 17 00:00:00 2001 From: tresabhi <43380238+tresabhi@users.noreply.github.com> Date: Fri, 16 May 2025 22:23:34 -0500 Subject: [PATCH] feat: heuristics --- .../src/core/github/commitMultipleFiles.ts | 74 ++++++++++++++----- 1 file changed, 56 insertions(+), 18 deletions(-) diff --git a/packages/scripts/src/core/github/commitMultipleFiles.ts b/packages/scripts/src/core/github/commitMultipleFiles.ts index 364ac6b0c..1efc6b54b 100644 --- a/packages/scripts/src/core/github/commitMultipleFiles.ts +++ b/packages/scripts/src/core/github/commitMultipleFiles.ts @@ -7,6 +7,8 @@ export interface FileChange { content: Buffer; } +const heuristicFormats = ['glb']; + export async function commitMultipleFiles( repoRaw: string, branch: string, @@ -21,31 +23,67 @@ export async function commitMultipleFiles( const response = await fetch( `https://raw.githubusercontent.com/${blobPath}`, ); + let isNew: boolean; + let isDifferent: boolean; + let diff: number; if (response.status === 404) { + isNew = true; + diff = change.content.length; + isDifferent = true; + } else if ( + response.headers.has('Content-Length') && + parseInt(response.headers.get('Content-Length')!) !== + change.content.length + ) { + isNew = false; + diff = + change.content.length - + parseInt(response.headers.get('Content-Length')!); + isDifferent = true; + } else if (response.status === 200) { + isNew = false; + const buffer = Buffer.from(await response.arrayBuffer()); + + if (buffer.equals(change.content)) { + diff = 0; + isDifferent = false; + } else { + diff = change.content.length - buffer.length; + isDifferent = true; + } + } else { + throw new Error( + `Unexpected status code ${response.status} for ${blobPath}`, + ); + } + + /** + * Heuristic: if it's a large blob like a .glb, a diff = 0 might end up + * being a false positive with isDifferent = true. So, it's okay to + * assume it's unchanged. + */ + if ( + diff === 0 && + heuristicFormats.some((format) => change.path.endsWith(`.${format}`)) + ) { + isDifferent = false; + isNew = false; + } + + if (isNew) { console.log( `🟢 (+${change.content.length.toLocaleString()}B) ${blobPath}`, ); changes.push(change); - } else if (response.status === 200) { - const buffer = Buffer.from(await response.arrayBuffer()); - - if (!buffer.equals(change.content)) { - const diff = change.content.length - buffer.length; - - console.log( - `🟡 (${diff > 0 ? '+' : ''}${diff.toLocaleString()}B) ${blobPath}`, - ); - - changes.push(change); - - return; - } - - console.log(`🔵 (${buffer.length.toLocaleString()}B) ${blobPath}`); + } else if (isDifferent) { + console.log( + `🟡 (${diff > 0 ? '+' : ''}${diff.toLocaleString()}B) ${blobPath}`, + ); + changes.push(change); } else { - throw new Error( - `Unexpected status code ${response.status} for ${blobPath}`, + console.log( + `🔵 (${change.content.length.toLocaleString()}B) ${blobPath}`, ); } }),