Skip to content

Commit 4e9b418

Browse files
committed
Encode the showFiletRef output as base64 out of the gate
1 parent 65c4628 commit 4e9b418

File tree

3 files changed

+7
-16
lines changed

3 files changed

+7
-16
lines changed

src/git-command-manager.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ export type Commit = {
2222
}
2323

2424
export type ExecOpts = {
25-
allowAllExitCodes?: boolean,
25+
allowAllExitCodes?: boolean
26+
encoding?: 'utf8' | 'base64'
2627
}
2728

2829
export class GitCommandManager {
@@ -282,9 +283,9 @@ export class GitCommandManager {
282283
return output.stdout.trim()
283284
}
284285

285-
async showFileAtRef(ref: string, path: string): Promise<string> {
286+
async showFileAtRefBase64(ref: string, path: string): Promise<string> {
286287
const args = ['show', `${ref}:${path}`]
287-
const output = await this.exec(args)
288+
const output = await this.exec(args, {encoding: 'base64'})
288289
return output.stdout.trim()
289290
}
290291

@@ -376,10 +377,10 @@ export class GitCommandManager {
376377
ignoreReturnCode: opts.allowAllExitCodes ?? false,
377378
listeners: {
378379
stdout: (data: Buffer) => {
379-
stdout.push(data.toString())
380+
stdout.push(data.toString(opts.encoding ?? 'utf8'))
380381
},
381382
stderr: (data: Buffer) => {
382-
stderr.push(data.toString())
383+
stderr.push(data.toString(opts.encoding ?? 'utf8'))
383384
}
384385
}
385386
}

src/github-helper.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ export class GitHubHelper {
275275
const {data: blob} = await blobCreationLimit(() =>
276276
this.octokit.rest.git.createBlob({
277277
...repository,
278-
content: git.showFileAtRef(commit.sha, path),
278+
content: await git.showFileAtRefBase64(commit.sha, path),
279279
encoding: 'base64'
280280
})
281281
)

src/utils.ts

-10
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,6 @@ export function readFile(path: string): string {
126126
return fs.readFileSync(path, 'utf-8')
127127
}
128128

129-
export function readFileBase64(pathParts: string[]): string {
130-
const resolvedPath = path.resolve(...pathParts)
131-
if (fs.lstatSync(resolvedPath).isSymbolicLink()) {
132-
return fs
133-
.readlinkSync(resolvedPath, {encoding: 'buffer'})
134-
.toString('base64')
135-
}
136-
return fs.readFileSync(resolvedPath).toString('base64')
137-
}
138-
139129
/* eslint-disable @typescript-eslint/no-explicit-any */
140130
function hasErrorCode(error: any): error is {code: string} {
141131
return typeof (error && error.code) === 'string'

0 commit comments

Comments
 (0)