Update the content of the file in the specified commit_id and commit the updated file. new_content can be either a string or bytes, when it is a string, it will be written in text mode, otherwise, it will be written in binary mode.
(
config: EvoGitConfig,
commit: str,
new_content: Union[str, bytes],
commit_message: str,
filename: Optional[str] = None,
)
| 419 | |
| 420 | |
| 421 | def update_file( |
| 422 | config: EvoGitConfig, |
| 423 | commit: str, |
| 424 | new_content: Union[str, bytes], |
| 425 | commit_message: str, |
| 426 | filename: Optional[str] = None, |
| 427 | ) -> None: |
| 428 | """Update the content of the file in the specified commit_id and commit the updated file. |
| 429 | new_content can be either a string or bytes, |
| 430 | when it is a string, it will be written in text mode, otherwise, it will be written in binary mode. |
| 431 | """ |
| 432 | checkout(config, commit) |
| 433 | |
| 434 | filename = filename if filename is not None else config.filename |
| 435 | |
| 436 | if isinstance(new_content, str): |
| 437 | mode = "r+" |
| 438 | elif isinstance(new_content, bytes): |
| 439 | mode = "rb+" |
| 440 | else: |
| 441 | raise ValueError("new_content must be either a string or bytes.") |
| 442 | |
| 443 | with open(os.path.join(config.git_dir, filename), mode) as f: |
| 444 | current_content = f.read() |
| 445 | # if the content is the same, do nothing |
| 446 | if current_content == new_content: |
| 447 | return |
| 448 | |
| 449 | f.seek(0) |
| 450 | f.write(new_content) |
| 451 | f.truncate() |
| 452 | |
| 453 | subprocess.run(["git", "add", filename], cwd=config.git_dir, check=True) |
| 454 | commit_message = git_commit_message_pattern.sub("", commit_message) |
| 455 | commit_message = commit_message[:256] # truncate the message to 256 characters |
| 456 | subprocess.run( |
| 457 | ["git", "commit", "-q", "-m", commit_message], cwd=config.git_dir, check=True |
| 458 | ) |
| 459 | |
| 460 | |
| 461 | def read_file(config: EvoGitConfig, commit: str, mode: str = "text") -> str | bytes: |
nothing calls this directly
no test coverage detected