Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Normalize whitespace when comparing patch context lines #6541

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions openhands/resolver/patching/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,17 @@ def apply_diff(diff, text, reverse=False, use_patch=False):
hunk=hunk,
)
if lines[old - 1] != line:
raise HunkApplyException(
'context line {n}, "{line}" does not match "{sl}"'.format(
n=old, line=line, sl=lines[old - 1]
),
hunk=hunk,
)
# Try to normalize whitespace by replacing multiple spaces with a single space
# This helps with patches that have different indentation levels
normalized_line = ' '.join(line.split())
normalized_source = ' '.join(lines[old - 1].split())
if normalized_line != normalized_source:
raise HunkApplyException(
'context line {n}, "{line}" does not match "{sl}"'.format(
n=old, line=line, sl=lines[old - 1]
),
hunk=hunk,
)

# for calculating the old line
r = 0
Expand Down
79 changes: 79 additions & 0 deletions tests/unit/test_patch_whitespace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from openhands.resolver.patching.apply import apply_diff
from openhands.resolver.patching.patch import parse_patch


def test_patch_whitespace_mismatch():
"""Test that the patch application fails correctly when whitespace doesn't match."""
Copy link
Collaborator

Choose a reason for hiding this comment

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

This comment seems to contradict to the behavior?

# Original content has a line with spaces
original_content = """class Example:
def method(self):
pass

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you mean by

Suggested change

otherwise this test won't pass

def another(self):
pass"""

# Patch expects an empty line (no spaces)
patch_text = """diff --git a/example.py b/example.py
index 1234567..89abcdef 100644
--- a/example.py
+++ b/example.py
@@ -2,6 +2,10 @@ class Example:
def method(self):
pass

+ new_field: str = "value"
+
def another(self):
pass"""

patch = next(parse_patch(patch_text))
# The patch should still work because we normalize whitespace
new_content = apply_diff(patch, original_content)
assert new_content == [
'class Example:',
' def method(self):',
' pass',
' ',
' new_field: str = "value"',
'',
' def another(self):',
' pass',
]


def test_patch_whitespace_match():
"""Test that the patch application succeeds when whitespace matches."""
# Original content has an empty line (no spaces)
original_content = """class Example:
def method(self):
pass

def another(self):
pass"""

# Patch expects an empty line (no spaces)
patch_text = """diff --git a/example.py b/example.py
index 1234567..89abcdef 100644
--- a/example.py
+++ b/example.py
@@ -2,6 +2,10 @@ class Example:
def method(self):
pass

+ new_field: str = "value"
+
def another(self):
pass"""

patch = next(parse_patch(patch_text))
new_content = apply_diff(patch, original_content)
assert new_content == [
'class Example:',
' def method(self):',
' pass',
'',
' new_field: str = "value"',
'',
' def another(self):',
' pass',
]
Loading