Encode the showFiletRef output as base64 out of the gate

This commit is contained in:
Graham Christensen 2025-02-20 15:27:30 -05:00
parent 65c46285dc
commit 4e9b4181aa
3 changed files with 7 additions and 16 deletions

View File

@ -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<string> {
async showFileAtRefBase64(ref: string, path: string): Promise<string> {
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'))
}
}
}

View File

@ -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'
})
)

View File

@ -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'