MCPcopy
hub / github.com/Aider-AI/aider / _parse_patch_text

Method _parse_patch_text

aider/coders/patch_coder.py:290–410  ·  view source on GitHub ↗

Parses patch content lines into a Patch object. Adapted from the Parser class in apply_patch.py.

(
        self, lines: List[str], start_index: int, current_files: Dict[str, str]
    )

Source from the content-addressed store, hash-verified

288 raise ValueError(f"Unexpected error parsing patch: {e}")
289
290 def _parse_patch_text(
291 self, lines: List[str], start_index: int, current_files: Dict[str, str]
292 ) -> Patch:
293 """
294 Parses patch content lines into a Patch object.
295 Adapted from the Parser class in apply_patch.py.
296 """
297 patch = Patch()
298 index = start_index
299 fuzz_accumulator = 0
300
301 while index < len(lines):
302 line = lines[index]
303 norm_line = _norm(line)
304
305 if norm_line == "*** End Patch":
306 index += 1
307 break # Successfully reached end
308
309 # ---------- UPDATE ---------- #
310 if norm_line.startswith("*** Update File: "):
311 path = norm_line[len("*** Update File: ") :].strip()
312 index += 1
313 if not path:
314 raise DiffError("Update File action missing path.")
315
316 # Optional move target
317 move_to = None
318 if index < len(lines) and _norm(lines[index]).startswith("*** Move to: "):
319 move_to = _norm(lines[index])[len("*** Move to: ") :].strip()
320 index += 1
321 if not move_to:
322 raise DiffError("Move to action missing path.")
323
324 if path not in current_files:
325 raise DiffError(f"Update File Error - missing file content for: {path}")
326
327 file_content = current_files[path]
328
329 existing_action = patch.actions.get(path)
330 if existing_action is not None:
331 # Merge additional UPDATE block into the existing one
332 if existing_action.type != ActionType.UPDATE:
333 raise DiffError(f"Conflicting actions for file: {path}")
334
335 new_action, index, fuzz = self._parse_update_file_sections(
336 lines, index, file_content
337 )
338 existing_action.chunks.extend(new_action.chunks)
339
340 if move_to:
341 if existing_action.move_path and existing_action.move_path != move_to:
342 raise DiffError(f"Conflicting move targets for file: {path}")
343 existing_action.move_path = move_to
344 fuzz_accumulator += fuzz
345 else:
346 # First UPDATE block for this file
347 action, index, fuzz = self._parse_update_file_sections(

Callers 1

get_editsMethod · 0.95

Calls 7

PatchClass · 0.85
_normFunction · 0.85
DiffErrorClass · 0.85
PatchActionClass · 0.85
tool_warningMethod · 0.45

Tested by

no test coverage detected