Coverage for sel_tools/diff_creation/create_diff.py: 100%

22 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-04 21:22 +0000

1"""Diff creation module.""" 

2 

3from datetime import date 

4from pathlib import Path 

5 

6import git 

7from git.objects.commit import Commit 

8from tqdm import tqdm 

9 

10from sel_tools.diff_creation.report import Diff, DiffReport 

11 

12 

13def create_diff( 

14 repo_paths: list[Path], 

15 date_last_homework: date | None, 

16 evaluation_date: date | None, 

17) -> list[DiffReport]: 

18 """Create diff for given repositories since the last homework date.""" 

19 return ( 

20 [] 

21 if date_last_homework is None 

22 else [ 

23 DiffCreator(repo_path, "build").create(date_last_homework, evaluation_date) 

24 for repo_path in tqdm(repo_paths, desc=f"Create diff since {date_last_homework}") 

25 ] 

26 ) 

27 

28 

29class DiffCreator: 

30 """Diff creator class.""" 

31 

32 def __init__(self, repo_path: Path, exclude: str | None = None) -> None: 

33 self.__path = repo_path 

34 self.__repo = git.Repo(self.__path) 

35 self.__exclude = exclude 

36 

37 def create(self, date_last_homework: date, evaluation_date: date | None) -> DiffReport: 

38 commits_since_last_homework = list( 

39 self.__repo.iter_commits(since=date_last_homework, before=evaluation_date, no_merges=True) 

40 ) 

41 diffs = ( 

42 [ 

43 self.__create_overall_diff(commits_since_last_homework[-1]), 

44 *self.__create_diff_per_commit(commits_since_last_homework), 

45 ] 

46 if commits_since_last_homework 

47 else [] 

48 ) 

49 return DiffReport(self.__path, diffs) 

50 

51 def __create_overall_diff(self, commit: Commit) -> Diff: 

52 patch = ( 

53 self.__repo.git.diff(commit.tree) 

54 if self.__exclude is None 

55 else self.__repo.git.diff(commit.tree, f":(exclude){self.__exclude}") 

56 ) 

57 return Diff( 

58 commit.hexsha, 

59 "total", 

60 str(commit.summary), 

61 patch, 

62 ) 

63 

64 def __create_diff_per_commit(self, commits: list[Commit]) -> list[Diff]: 

65 return [ 

66 Diff( 

67 commit.hexsha, 

68 str(commit.author), 

69 str(commit.summary), 

70 self.__repo.git.diff(commit.parents[0].tree, commit.tree), 

71 ) 

72 for commit in commits 

73 if commit.parents 

74 ]