From 4e9b4181aa7d89674098826beec42dbcb6bb719a Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Thu, 20 Feb 2025 15:27:30 -0500 Subject: [PATCH] Encode the showFiletRef output as base64 out of the gate --- src/git-command-manager.ts | 11 ++++++----- src/github-helper.ts | 2 +- src/utils.ts | 10 ---------- 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/src/git-command-manager.ts b/src/git-command-manager.ts index b5cc5721..47e3937f 100644 --- a/src/git-command-manager.ts +++ b/src/git-command-manager.ts @@ -22,7 +22,8 @@ export type Commit = { } export type ExecOpts = { - allowAllExitCodes?: boolean, + allowAllExitCodes?: boolean + encoding?: 'utf8' | 'base64' } export class GitCommandManager { @@ -282,9 +283,9 @@ export class GitCommandManager { return output.stdout.trim() } - async showFileAtRef(ref: string, path: string): Promise { + async showFileAtRefBase64(ref: string, path: string): Promise { const args = ['show', `${ref}:${path}`] - const output = await this.exec(args) + const output = await this.exec(args, {encoding: 'base64'}) return output.stdout.trim() } @@ -376,10 +377,10 @@ export class GitCommandManager { ignoreReturnCode: opts.allowAllExitCodes ?? false, listeners: { stdout: (data: Buffer) => { - stdout.push(data.toString()) + stdout.push(data.toString(opts.encoding ?? 'utf8')) }, stderr: (data: Buffer) => { - stderr.push(data.toString()) + stderr.push(data.toString(opts.encoding ?? 'utf8')) } } } diff --git a/src/github-helper.ts b/src/github-helper.ts index 0aeb4e9e..fb541b40 100644 --- a/src/github-helper.ts +++ b/src/github-helper.ts @@ -275,7 +275,7 @@ export class GitHubHelper { const {data: blob} = await blobCreationLimit(() => this.octokit.rest.git.createBlob({ ...repository, - content: git.showFileAtRef(commit.sha, path), + content: await git.showFileAtRefBase64(commit.sha, path), encoding: 'base64' }) ) diff --git a/src/utils.ts b/src/utils.ts index ced3cbdf..b501dd48 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -126,16 +126,6 @@ export function readFile(path: string): string { return fs.readFileSync(path, 'utf-8') } -export function readFileBase64(pathParts: string[]): string { - const resolvedPath = path.resolve(...pathParts) - if (fs.lstatSync(resolvedPath).isSymbolicLink()) { - return fs - .readlinkSync(resolvedPath, {encoding: 'buffer'}) - .toString('base64') - } - return fs.readFileSync(resolvedPath).toString('base64') -} - /* eslint-disable @typescript-eslint/no-explicit-any */ function hasErrorCode(error: any): error is {code: string} { return typeof (error && error.code) === 'string'