Coverage for codexa/client/versioning.py: 33%

18 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-08-10 07:53 +0000

1import logging 

2import os 

3 

4from git import Repo 

5 

6from codexa.core.errors import CodexaRuntimeError 

7 

8logger = logging.getLogger(__name__) 

9 

10 

11def compare_git_diff(remote_ref: str, repo_path: str = os.getcwd()) -> str: 

12 """ 

13 Get the diff between the current working tree and a remote branch using GitPython. 

14 

15 Args: 

16 repo_path (str): Path to the Git repository. 

17 remote_ref (str): Remote branch to diff against (e.g. 'origin/main'). 

18 

19 Returns: 

20 Optional[str]: The unified diff output as a string, or None on failure. 

21 """ 

22 try: 

23 repo = Repo(repo_path) 

24 if repo.bare: 

25 logger.warning(f"No changes detected in the repository: {repo_path}") 

26 return "" 

27 

28 repo.remotes.origin.fetch() 

29 head_commit = repo.head.commit 

30 remote_commit = repo.commit(remote_ref) 

31 

32 diff_index = head_commit.diff(remote_commit, create_patch=True) 

33 return "\n".join(str(d.diff) for d in diff_index if d.diff) 

34 except Exception as e: 

35 raise CodexaRuntimeError(f"Failed to get git diff: {e}")