Skip to content

Conversation

@snomos
Copy link
Member

@snomos snomos commented Dec 9, 2025

This PR fixes the "malformed patch at line X" error that was occurring when running gut template apply commands.

Problem

The patch generation was creating malformed patches with trailing empty lines, causing the patch command to fail with errors like:

patch: **** malformed patch at line 29:  

Root Cause

The issue was in the to_content() functions in patch_file.rs:

  1. Git diff content already contained newlines
  2. When formatting patch lines with prefixes (+, -, space), we were getting double newlines or malformed endings
  3. The final patch file was ending with empty lines instead of a single newline

Solution

This fix ensures proper patch formatting by:

  • Cleaning content: Remove existing newlines before adding prefixes to avoid double newlines
  • Consistent line endings: Each patch line gets exactly one newline after the prefix
  • Proper file termination: Final patch ends with exactly one newline (no trailing empty lines)
  • Header preservation: Hunk and Info lines preserve their existing newlines correctly

Testing

  • Code compiles without errors
  • Addresses the specific malformed patch errors reported in template apply operations

The fix maintains compatibility with the existing patch format while ensuring proper newline handling throughout the patch generation process.

The patch generation was creating malformed patches with trailing empty lines
that caused 'patch: **** malformed patch at line X' errors.

This fix ensures:
- Each patch line has exactly one newline after the prefix (+, -, or space)
- Hunk and Info lines preserve their existing newlines properly
- The final patch file ends with exactly one newline (no trailing empty lines)

Fixes the issue where 'gut template apply' was failing with malformed patch errors.
@snomos snomos requested review from bbqsrc and dylanhand December 9, 2025 06:54
Copy link
Contributor

@dylanhand dylanhand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine to me. I simplified the code slightly.

@snomos snomos merged commit db79860 into main Jan 6, 2026
@snomos snomos deleted the fix/patch-formatting branch January 6, 2026 08:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants