Skip to content

Conversation

@elskow
Copy link

@elskow elskow commented Dec 16, 2025

Fixes #5103

When you have a diff with consecutive deletions followed by additions (like renaming a variable across multiple lines), and you try to stage only some of them (e.g., first deletion + first addition), the resulting patch had lines in the wrong order.

The problem was that unselected deletions were being converted to context lines immediately, causing them to appear between the selected deletion and addition. Now we defer those context lines until after all additions in the block.

Before:

-  // init something
   int i = 0;
+  // init variables

After:

-  // init something
+  // init variables
   int i = 0;

Copilot AI review requested due to automatic review settings December 16, 2025 06:14
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a bug in patch transformation where staging non-contiguous lines within change blocks (multiple consecutive deletions followed by additions) would not preserve the correct line order. The fix introduces special handling for change blocks to ensure that selected deletions and additions are kept together as pairs, while unselected deletions are converted to context lines.

Key Changes:

  • Added logic to detect and handle change blocks (multiple consecutive deletions) as a unit
  • Selected deletions and additions are now processed together before appending unselected lines as context
  • New test case validates that non-contiguous selection preserves change pair relationships

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
pkg/commands/patch/transform.go Implemented change block detection and special handling for non-contiguous line selection in the transformHunkLines function
pkg/commands/patch/patch_test.go Added test case TestTransformNonContiguousSelection to verify correct behavior when selecting non-contiguous lines in change blocks

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

	modified:   pkg/commands/patch/patch_test.go
	modified:   pkg/commands/patch/transform.go
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@elskow
Copy link
Author

elskow commented Dec 16, 2025

@stefanhaller mind taking a look?

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.

Staging individual line of consecutive changes results in wrong ordered lines in patch

1 participant