Skip to content

Commit

Permalink
fixes for stripper
Browse files Browse the repository at this point in the history
  • Loading branch information
Domiii committed Dec 9, 2024
1 parent b1b6ece commit a8c18bd
Showing 1 changed file with 44 additions and 28 deletions.
72 changes: 44 additions & 28 deletions openhands/resolver/resolve_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,43 +112,49 @@ async def get_git_patch(runtime: Runtime, base_commit: str) -> str:
return git_patch


def strip_replay_comment(file_name: str, line: str):
"""Remove a replay comment with the specified text from a file.
REPLAY_COMMENT_PATTERN = r'^\+.*(?:\s+//|\{/\*.*?\*/\})'


def strip_replay_comments(base_path: str, git_patch: str) -> None:
"""Strip replay comments from files referenced in a git patch.
Args:
file_name: Path to the file containing the comment
line: The line containing the comment to remove
base_path: Root path for the repository
git_patch: Git patch content containing file changes
"""
logger.info(f'Stripping Replay comment from {file_name}: "{line}"')
if not os.path.exists(file_name):
raise ValueError(f'File {file_name} does not exist')
with open(file_name, 'r') as f:
lines = f.readlines()
current_file = ''
file_changes: dict[str, list[str]] = {}

# Find and remove the line
if line + '\n' in lines:
lines.remove(line + '\n')
# First pass: collect all changes per file
for line in git_patch.splitlines():
if line.startswith('+++ b/'):
current_file = os.path.join(base_path, line[6:])
continue

# Write back the file without the comment
with open(file_name, 'w') as f:
f.writelines(lines)
if re.match(REPLAY_COMMENT_PATTERN, line.lstrip()):
if current_file not in file_changes:
file_changes[current_file] = []
file_changes[current_file].append(line[1:])

# Second pass: apply changes to each file once
for file_name, comments in file_changes.items():
if not os.path.exists(file_name):
logger.warning(f'File {file_name} does not exist')
continue

REPLAY_COMMENT_PATTERN = r'^\+.*(?:\s+//|\{/\*.*?\*/\})'
with open(file_name, 'r') as f:
lines = f.readlines()

# Remove all comments from this file
for comment in comments:
if comment + '\n' in lines:
lines.remove(comment + '\n')

def strip_replay_comments(base_path: str, git_patch: str) -> None:
"""Strip all replay comments from the git patch."""
logger.info('Stripping Replay comments...')
with open(file_name, 'w') as f:
f.writelines(lines)
logger.info(f'Stripped {len(comments)} comments from {file_name}')

lines = git_patch.splitlines()
current_file = ''
for line in lines:
logger.info(f'Processing line: {line}')
if re.match(r'^\+\+\+ b/', line):
current_file = os.path.join(base_path, line[6:])
if re.match(REPLAY_COMMENT_PATTERN, line.lstrip()):
strip_replay_comment(current_file, line[1:])
logger.info(f'[REPLAY] Stripped {len(file_changes)} files: {file_changes.keys()}')


async def complete_runtime(
Expand Down Expand Up @@ -189,11 +195,18 @@ async def complete_runtime(
if not isinstance(obs, CmdOutputObservation) or obs.exit_code != 0:
raise RuntimeError(f'Failed to set git config. Observation: {obs}')

# git add.
action = CmdRunAction(command='git add -A')
logger.info(action, extra={'msg_type': 'ACTION'})
obs = runtime.run_action(action)
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
if not isinstance(obs, CmdOutputObservation) or obs.exit_code != 0:
raise RuntimeError(f'Failed to git add. Observation: {obs}')

# Strip comments.
base_git_patch = await get_git_patch(runtime, base_commit)
assert runtime.config.workspace_base is not None
strip_replay_comments(runtime.config.workspace_base, base_git_patch)
git_patch = await get_git_patch(runtime, base_commit)

# git add.
action = CmdRunAction(command='git add -A')
Expand All @@ -203,6 +216,9 @@ async def complete_runtime(
if not isinstance(obs, CmdOutputObservation) or obs.exit_code != 0:
raise RuntimeError(f'Failed to git add. Observation: {obs}')

# Get final patch.
git_patch = await get_git_patch(runtime, base_commit)

logger.info('-' * 30)
logger.info('END Runtime Completion Fn')
logger.info('-' * 30)
Expand Down

0 comments on commit a8c18bd

Please sign in to comment.