Coverage for src/pullapprove/git.py: 0%

26 statements  

« prev     ^ index     » next       coverage.py v7.8.2, created at 2026-03-11 17:04 -0500

1import subprocess 

2from collections.abc import Generator 

3from pathlib import Path 

4 

5 

6def git_root() -> Path: 

7 """Return the root directory of the git repository.""" 

8 output = subprocess.check_output(["git", "rev-parse", "--show-toplevel"]).strip() 

9 return Path(output.decode("utf-8")) 

10 

11 

12def git_ls_files(path: Path) -> Generator[str]: 

13 """Yield files in the git repository one at a time.""" 

14 process = subprocess.Popen( 

15 [ 

16 "git", 

17 "ls-files", 

18 "--cached", 

19 "--deleted", 

20 "--others", 

21 "--exclude-standard", 

22 ], 

23 cwd=path, 

24 stdout=subprocess.PIPE, 

25 text=True, 

26 ) 

27 

28 assert process.stdout is not None 

29 for line in process.stdout: 

30 yield line.strip() 

31 

32 process.stdout.close() 

33 process.wait() 

34 

35 

36def git_ls_changes(path: Path) -> Generator[str]: 

37 process = subprocess.Popen( 

38 [ 

39 "git", 

40 "status", 

41 "--porcelain=v1", 

42 "--untracked-files=all", 

43 ], 

44 cwd=path, 

45 stdout=subprocess.PIPE, 

46 text=True, 

47 ) 

48 

49 assert process.stdout is not None 

50 for line in process.stdout: 

51 yield line.strip().split(" ", 1)[1] 

52 

53 process.stdout.close() 

54 process.wait() 

55 

56 

57def git_diff_stream(path: Path, *diff_args: str) -> Generator[str]: 

58 process = subprocess.Popen( 

59 ["git", "diff", "--no-ext-diff"] + list(diff_args), 

60 cwd=path, 

61 stdout=subprocess.PIPE, 

62 text=True, 

63 ) 

64 

65 assert process.stdout is not None 

66 yield from process.stdout 

67 

68 process.stdout.close() 

69 process.wait()