Update the content of the file in the specified worktree, then commit the updated file.
(
config: EvoGitConfig,
worktree: str,
new_content: Union[str, bytes],
filename: Optional[str] = None,
)
| 333 | |
| 334 | |
| 335 | def update_file_in_worktree( |
| 336 | config: EvoGitConfig, |
| 337 | worktree: str, |
| 338 | new_content: Union[str, bytes], |
| 339 | filename: Optional[str] = None, |
| 340 | ) -> None: |
| 341 | """Update the content of the file in the specified worktree, then commit the updated file.""" |
| 342 | filename = filename if filename is not None else config.filename |
| 343 | if isinstance(new_content, str): |
| 344 | mode = "r+" |
| 345 | elif isinstance(new_content, bytes): |
| 346 | mode = "rb+" |
| 347 | else: |
| 348 | raise ValueError("new_content must be either a string or bytes.") |
| 349 | |
| 350 | with open(os.path.join(worktree, filename), mode) as f: |
| 351 | current_content = f.read() |
| 352 | # if the content is the same, do nothing |
| 353 | if current_content == new_content: |
| 354 | return |
| 355 | |
| 356 | f.seek(0) |
| 357 | f.write(new_content) |
| 358 | f.truncate() |
| 359 | |
| 360 | subprocess.run(["git", "add", filename], cwd=worktree, check=True) |
| 361 | |
| 362 | |
| 363 | def add_file_in_worktree( |
nothing calls this directly
no outgoing calls
no test coverage detected