Skip to content

Commit fde68bb

Browse files
authored
refactor: use git cat-file blob instead of git show in readBlobAsBase64 (#26349)
1 parent 4907d31 commit fde68bb

2 files changed

Lines changed: 17 additions & 12 deletions

File tree

.changeset/patch-use-git-cat-file-for-blob-read.md

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

actions/setup/js/push_signed_commits.cjs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,20 @@ function unquoteCPath(s) {
7777
}
7878

7979
/**
80-
* Read a blob from a specific commit as a base64-encoded string using
81-
* `git show <sha>:<path>`. The raw bytes emitted by git are collected via
82-
* the `exec.exec` stdout listener so that binary files are not corrupted by
83-
* any UTF-8 decoding layer (unlike `exec.getExecOutput` which always passes
84-
* stdout through a `StringDecoder('utf8')`).
80+
* Read a blob object as a base64-encoded string using `git cat-file blob <blobHash>`.
81+
* The raw bytes emitted by git are collected via the `exec.exec` stdout
82+
* listener so that binary files are not corrupted by any UTF-8 decoding
83+
* layer (unlike `exec.getExecOutput` which always passes stdout through a
84+
* `StringDecoder('utf8')`).
8585
*
86-
* @param {string} sha - Commit SHA to read the blob from
87-
* @param {string} filePath - Repo-relative path of the file
86+
* @param {string} blobHash - Object hash of the blob (from `git diff-tree --raw` dstHash field)
8887
* @param {string} cwd - Working directory of the local git checkout
8988
* @returns {Promise<string>} Base64-encoded file contents
9089
*/
91-
async function readBlobAsBase64(sha, filePath, cwd) {
90+
async function readBlobAsBase64(blobHash, cwd) {
9291
/** @type {Buffer[]} */
9392
const chunks = [];
94-
await exec.exec("git", ["show", `${sha}:${filePath}`], {
93+
await exec.exec("git", ["cat-file", "blob", blobHash], {
9594
cwd,
9695
silent: true,
9796
listeners: {
@@ -200,6 +199,7 @@ async function pushSignedCommits({ githubClient, owner, repo, branch, baseRef, c
200199
}
201200
const srcMode = modeFields[0]; // source file mode (e.g. 100644, 100755, 120000, 160000)
202201
const dstMode = modeFields[1]; // destination file mode (e.g. 100644, 100755, 120000, 160000)
202+
const dstHash = modeFields[3]; // destination blob hash (object ID of the file in this commit)
203203
const status = modeFields[4]; // A=Added, M=Modified, D=Deleted, R=Renamed, C=Copied
204204

205205
const paths = line.slice(tabIdx + 1).split("\t");
@@ -231,7 +231,7 @@ async function pushSignedCommits({ githubClient, owner, repo, branch, baseRef, c
231231
if (dstMode === "100755") {
232232
core.warning(`pushSignedCommits: executable bit on ${renamedPath} will be lost in signed commit (GitHub GraphQL does not support mode 100755)`);
233233
}
234-
additions.push({ path: renamedPath, contents: await readBlobAsBase64(sha, renamedPath, cwd) });
234+
additions.push({ path: renamedPath, contents: await readBlobAsBase64(dstHash, cwd) });
235235
} else if (status && status.startsWith("C")) {
236236
// Copy: source path is kept (no deletion), only the destination path is added
237237
const copiedPath = unquoteCPath(paths[1]);
@@ -250,7 +250,7 @@ async function pushSignedCommits({ githubClient, owner, repo, branch, baseRef, c
250250
if (dstMode === "100755") {
251251
core.warning(`pushSignedCommits: executable bit on ${copiedPath} will be lost in signed commit (GitHub GraphQL does not support mode 100755)`);
252252
}
253-
additions.push({ path: copiedPath, contents: await readBlobAsBase64(sha, copiedPath, cwd) });
253+
additions.push({ path: copiedPath, contents: await readBlobAsBase64(dstHash, cwd) });
254254
} else {
255255
// Added or Modified
256256
if (dstMode === "160000") {
@@ -264,7 +264,7 @@ async function pushSignedCommits({ githubClient, owner, repo, branch, baseRef, c
264264
if (dstMode === "100755") {
265265
core.warning(`pushSignedCommits: executable bit on ${filePath} will be lost in signed commit (GitHub GraphQL does not support mode 100755)`);
266266
}
267-
additions.push({ path: filePath, contents: await readBlobAsBase64(sha, filePath, cwd) });
267+
additions.push({ path: filePath, contents: await readBlobAsBase64(dstHash, cwd) });
268268
}
269269
}
270270

0 commit comments

Comments
 (0)