Show diffs of file between the latest commit and the previous one if any. Args: repo (git.Repo): The Git repository object filepath (str): Path to the file to be diffed Returns: str: The diff output as a string
(repo: git.Repo, filepath: str)
| 226 | |
| 227 | |
| 228 | def git_diff_file(repo: git.Repo, filepath: str) -> str: |
| 229 | """ |
| 230 | Show diffs of file between the latest commit and the previous one if any. |
| 231 | |
| 232 | Args: |
| 233 | repo (git.Repo): The Git repository object |
| 234 | filepath (str): Path to the file to be diffed |
| 235 | |
| 236 | Returns: |
| 237 | str: The diff output as a string |
| 238 | """ |
| 239 | try: |
| 240 | # Get the two most recent commits |
| 241 | commits = list(repo.iter_commits(paths=filepath, max_count=2)) |
| 242 | |
| 243 | if len(commits) < 2: |
| 244 | return "No previous commit found for comparison." |
| 245 | |
| 246 | # Get the diff between the two commits for the specific file |
| 247 | diff = repo.git.diff(commits[1].hexsha, commits[0].hexsha, filepath) |
| 248 | |
| 249 | return str(diff) |
| 250 | except git.GitCommandError as e: |
| 251 | logger.error(f"An error occurred while getting diff: {e}") |
| 252 | return f"Error: {str(e)}" |
no outgoing calls
searching dependent graphs…