Skip to content

Commit 435c47b

Browse files
Copilotpelikhan
andauthored
fix: compute patch size from additions only, ignore deletions
Agent-Logs-Url: https://github.com/github/gh-aw/sessions/0ee9a48f-b693-487f-9b21-caecbf2e0c01 Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
1 parent 595ec7f commit 435c47b

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

actions/setup/js/push_repo_memory.cjs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -384,18 +384,23 @@ async function main() {
384384
}
385385

386386
// Validate total patch size before committing
387-
// Note: the patch size is the size of `git diff --cached`, which includes both the old (deleted)
388-
// and new (added) content for each modified file. For files that are rewritten entirely, the
389-
// patch can be significantly larger than the file itself (old_size + new_size + headers).
387+
// Only additions (new content) are counted toward the patch size limit.
388+
// Deletions are ignored since removing content is acceptable and does not
389+
// contribute to the size of the content being pushed.
390390
try {
391391
const patchContent = execGitSync(["diff", "--cached"], { stdio: "pipe" });
392-
const patchSizeBytes = Buffer.byteLength(patchContent, "utf8");
392+
// Count only added lines (starting with '+', excluding '+++' file-header lines)
393+
const addedSizeBytes = patchContent
394+
.split("\n")
395+
.filter(line => line.startsWith("+") && !line.startsWith("+++"))
396+
.reduce((sum, line) => sum + Buffer.byteLength(line + "\n", "utf8"), 0);
397+
const patchSizeBytes = addedSizeBytes;
393398
const patchSizeKb = Math.ceil(patchSizeBytes / 1024);
394399
const maxPatchSizeKb = Math.floor(maxPatchSize / 1024);
395400
// Allow 20% overhead to account for git diff format (headers, context lines, etc.)
396401
const effectiveMaxPatchSize = Math.floor(maxPatchSize * 1.2);
397402
const effectiveMaxPatchSizeKb = Math.floor(effectiveMaxPatchSize / 1024);
398-
const patchSizeMessage = `Patch size: ${patchSizeKb} KB (${patchSizeBytes} bytes) (configured limit: ${maxPatchSizeKb} KB (${maxPatchSize} bytes), effective with 20% overhead: ${effectiveMaxPatchSizeKb} KB (${effectiveMaxPatchSize} bytes))`;
403+
const patchSizeMessage = `Patch additions size: ${patchSizeKb} KB (${patchSizeBytes} bytes) (configured limit: ${maxPatchSizeKb} KB (${maxPatchSize} bytes), effective with 20% overhead: ${effectiveMaxPatchSizeKb} KB (${effectiveMaxPatchSize} bytes))`;
399404
if (patchSizeBytes > effectiveMaxPatchSize) {
400405
// Warn at warning level so the size is visible even without verbose mode
401406
core.warning(patchSizeMessage);
@@ -409,7 +414,7 @@ async function main() {
409414
}
410415
core.setOutput("patch_size_exceeded", "true");
411416
core.setFailed(
412-
`Patch size (${patchSizeKb} KB, ${patchSizeBytes} bytes) exceeds maximum allowed size (${effectiveMaxPatchSizeKb} KB, ${effectiveMaxPatchSize} bytes, configured limit: ${maxPatchSizeKb} KB with 20% overhead allowance). Reduce the number or size of changes, or increase max-patch-size.`
417+
`Patch additions size (${patchSizeKb} KB, ${patchSizeBytes} bytes) exceeds maximum allowed size (${effectiveMaxPatchSizeKb} KB, ${effectiveMaxPatchSize} bytes, configured limit: ${maxPatchSizeKb} KB with 20% overhead allowance). Reduce the number or size of changes, or increase max-patch-size.`
413418
);
414419
return;
415420
} else if (patchSizeBytes > maxPatchSize) {
@@ -419,7 +424,7 @@ async function main() {
419424
core.info(patchSizeMessage);
420425
}
421426
} catch (error) {
422-
core.setFailed(`Failed to compute patch size: ${getErrorMessage(error)}`);
427+
core.setFailed(`Failed to compute patch additions size: ${getErrorMessage(error)}`);
423428
return;
424429
}
425430

0 commit comments

Comments
 (0)