fix (git): Truncate oversized git output for commit generation#1275
fix (git): Truncate oversized git output for commit generation#1275Snowy7 wants to merge 1 commit intopingdotgg:mainfrom
Conversation
- Allow git execution to cap and truncate streamed output - Use truncated staged patch when building commit messages - Add tests for oversized patch handling
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment Tip CodeRabbit can use Trivy to scan for security misconfigurations and secrets in Infrastructure as Code files.Add a .trivyignore file to your project to customize which findings Trivy reports. |
| return yield* new GitCommandError({ | ||
| operation: input.operation, | ||
| command: quoteGitCommand(input.args), | ||
| cwd: input.cwd, | ||
| detail: `${quoteGitCommand(input.args)} output exceeded ${maxOutputBytes} bytes and was truncated.`, | ||
| }); |
There was a problem hiding this comment.
🟢 Low Layers/GitService.ts:75
When truncateOutput is false, the code throws an error with the message "output exceeded ${maxOutputBytes} bytes and was truncated." — but the output was not truncated; an error was thrown instead. This is misleading because it contradicts the actual behavior. Consider removing the "and was truncated" clause from the error message when truncation is disabled.
detail: `${quoteGitCommand(input.args)} output exceeded ${maxOutputBytes} bytes and was truncated.`,🚀 Reply "fix it for me" or copy this AI Prompt for your agent:
In file apps/server/src/git/Layers/GitService.ts around lines 75-80:
When `truncateOutput` is `false`, the code throws an error with the message `"output exceeded ${maxOutputBytes} bytes and was truncated."` — but the output was not truncated; an error was thrown instead. This is misleading because it contradicts the actual behavior. Consider removing the `"and was truncated"` clause from the error message when truncation is disabled.
Evidence trail:
apps/server/src/git/Layers/GitService.ts lines 66-80 at REVIEWED_COMMIT - Line 66-73 shows truncation logic when `truncateOutput` is true. Line 74-80 shows error throwing when `truncateOutput` is false, with the error message at line 78-79 saying `output exceeded ${maxOutputBytes} bytes and was truncated.` despite no truncation occurring.
There was a problem hiding this comment.
Pull request overview
Adds opt-in output truncation to the server-side git execution layer and uses it to make commit preparation resilient when the staged diff is very large (preventing commit message generation from failing purely due to output caps).
Changes:
- Extend
GitService.executeto supporttruncateOutputand to reportstdoutTruncated/stderrTruncated. - Update git output collection to optionally truncate instead of failing when
maxOutputBytesis exceeded. - Use truncation when capturing the staged patch during commit preparation; add/extend tests for truncation and large staged diffs.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| apps/server/src/git/Services/GitService.ts | Extends the GitService API types with truncation options/flags. |
| apps/server/src/git/Layers/GitService.ts | Implements truncating output collection and surfaces truncation flags in results. |
| apps/server/src/git/Layers/GitService.test.ts | Adds an integration test verifying truncation behavior. |
| apps/server/src/git/Layers/GitCore.ts | Enables truncation when capturing the staged patch for commit context. |
| apps/server/src/git/Layers/GitManager.test.ts | Adds an integration test ensuring commits still succeed with oversized staged patches. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| "GitCore.prepareCommitContext.stagedPatch", | ||
| cwd, | ||
| ["diff", "--cached", "--patch", "--minimal"], | ||
| { |
There was a problem hiding this comment.
For prepareCommitContext the staged patch is later truncated to 50,000 chars in GitManager.limitContext(...), but this git diff --cached --patch call still captures up to the GitService default maxOutputBytes (1,000,000). Consider passing an explicit smaller maxOutputBytes here (plus a small buffer) to avoid unnecessary read/decoding work on large diffs and to make the intended cap explicit at this call site.
| { | |
| { | |
| // Limit output to slightly above the 50,000-character context cap used later. | |
| maxOutputBytes: 60_000, |
| }); | ||
|
|
||
| assert.equal(result.code, 0); | ||
| assert.ok(result.stdout.length <= 16); |
There was a problem hiding this comment.
This test asserts result.stdout.length <= 16, but maxOutputBytes is a byte limit while .length is a UTF-16 code unit count. To make the test accurately reflect the contract (and avoid edge cases if output ever contains non-ASCII), assert against Buffer.byteLength(result.stdout, 'utf8') (or similar) instead of string length.
| assert.ok(result.stdout.length <= 16); | |
| assert.ok(Buffer.byteLength(result.stdout, "utf8") <= 16); |
What Changed
Why
The commit flow currently reads the full staged patch before generating a commit message. If
git diff --cached --patch --minimalexceeds the output cap, commit preparation fails even though Git could still commit successfully.This change keeps the behavior strict for normal git commands, but lets commit preparation degrade gracefully for very large staged diffs. That makes the commit feature more reliable in real user repos, especially when large generated changes or line-ending churn produce oversized patches.
Checklist
I included before/after screenshots for any UI changesNo UI Changes MadeI included a video for animation/interaction changesNo UI Changes MadeNote
Truncate oversized git output during commit generation instead of failing
collectOutputin GitService.ts now accepts atruncateOutputflag; when set, it truncates atmaxOutputBytesand sets atruncatedflag instead of throwing aGitCommandError.ExecuteGitResultgainsstdoutTruncated/stderrTruncatedfields so callers can detect truncation.prepareCommitContextin GitCore.ts now passestruncateOutput: truewhen capturing the staged patch, so large diffs no longer abort commit generation.📊 Macroscope summarized 15813db. 5 files reviewed, 1 issue evaluated, 0 issues filtered, 1 comment posted
🗂️ Filtered Issues