MCPcopy Create free account
hub / github.com/Ishabdullah/Codey-v2 / patch

Method patch

core/filesystem.py:218–279  ·  view source on GitHub ↗

Patch file content (replace old_str with new_str). Enforces checkpoint for core file modifications. Args: path: Path to file old_str: String to find and replace new_str: Replacement string Returns: Diff of ch

(self, path: Union[str, Path], old_str: str, new_str: str)

Source from the content-addressed store, hash-verified

216 raise FilesystemAccessError(f"Failed to write {path}: {e}")
217
218 def patch(self, path: Union[str, Path], old_str: str, new_str: str) -> str:
219 """
220 Patch file content (replace old_str with new_str).
221
222 Enforces checkpoint for core file modifications.
223
224 Args:
225 path: Path to file
226 old_str: String to find and replace
227 new_str: Replacement string
228
229 Returns:
230 Diff of changes made
231
232 Raises:
233 FilesystemAccessError: If patch cannot be applied
234 """
235 try:
236 path = self._validate_path(path)
237
238 if not path.exists():
239 raise FilesystemAccessError(f"File not found: {path}")
240
241 # Check if modifying core files (requires checkpoint)
242 is_core = _is_core_file(str(path))
243 if is_core:
244 self._require_checkpoint(path)
245
246 # Read current content
247 content = path.read_text(encoding='utf-8')
248
249 # Find and replace
250 if old_str not in content:
251 raise FilesystemAccessError(
252 f"Could not find specified string in {path}"
253 )
254
255 new_content = content.replace(old_str, new_str, 1)
256
257 # Generate diff
258 diff = self._generate_diff(
259 path.name,
260 content.splitlines(keepends=True),
261 new_content.splitlines(keepends=True)
262 )
263 self._last_diff = diff
264
265 # Write new content
266 path.write_text(new_content, encoding='utf-8')
267
268 try:
269 rel = path.relative_to(self.workspace)
270 except ValueError:
271 rel = path
272 msg = f"Patched {rel}"
273 success(msg)
274 return diff
275

Callers 1

tool_patch_fileFunction · 0.80

Calls 6

_validate_pathMethod · 0.95
_require_checkpointMethod · 0.95
_generate_diffMethod · 0.95
successFunction · 0.90
existsMethod · 0.80

Tested by

no test coverage detected