Coverage for foran/foran.py: 97.14%

50 statements  

« prev     ^ index     » next       coverage.py v7.0.1, created at 2023-01-02 19:51 +0100

1"""In front or behind (Foran eller bagved)? API.""" 

2import os 

3import pathlib 

4import warnings 

5from typing import List, Union 

6 

7from git.exc import GitCommandError 

8from git.repo import Repo 

9 

10from foran.report import Format, Report, report_as 

11from foran.status import Status 

12 

13DEBUG_VAR = 'FORAN_DEBUG' 

14DEBUG = os.getenv(DEBUG_VAR) 

15 

16ENCODING = 'utf-8' 

17ENCODING_ERRORS_POLICY = 'ignore' 

18 

19 

20def local_commits(repo: Repo, status: Status) -> None: 

21 """Yes""" 

22 if not status.foran: 

23 branch = repo.active_branch.name 

24 try: 

25 status.local_commits = [rev for rev in repo.iter_commits(f'origin/{branch}..{branch}')] 

26 except GitCommandError as ex: 

27 symptom = ex.stderr.replace('\n', '') 

28 if "fatal: bad revision 'origin/default..default'" in symptom: 28 ↛ 33line 28 didn't jump to line 33, because the condition on line 28 was never false

29 warning = 'No remote found, so all commit differences hypothetical' 

30 status.local_commits = [warning] 

31 warnings.warn(warning) 

32 else: 

33 raise 

34 

35 

36def local_staged(repo: Repo, status: Status) -> None: 

37 """Truly""" 

38 status.local_staged = [item.a_path for item in repo.index.diff('HEAD')] 

39 

40 

41def local_files(repo: Repo, status: Status) -> None: 

42 """Sure""" 

43 status.local_files = [item.a_path for item in repo.index.diff(None)] 

44 

45 

46def main(argv: Union[List[str], None] = None) -> int: 

47 """Drive the analysis.""" 

48 # [('diff'|'label'), repo_root, target, template] 

49 if not argv or len(argv) != 4: 

50 warnings.warn('received wrong number of arguments') 

51 return 2 

52 

53 if argv[0] not in ('diff', 'label'): 

54 warnings.warn('received unknown command') 

55 return 2 

56 

57 command, repo_root, target, template = argv 

58 

59 if not pathlib.Path(str(repo_root)).is_dir(): 

60 warnings.warn('repository root is no directory') 

61 return 1 

62 

63 if template and target != 'STD_OUT': 

64 warnings.warn('templates not yet implemented') 

65 

66 report = Report(stem=target, file_format=Format.NONE) 

67 

68 repo = Repo(repo_root, search_parent_directories=True) 

69 status = Status(repo) 

70 if command == 'diff': 

71 local_commits(repo, status) 

72 local_staged(repo, status) 

73 local_files(repo, status) 

74 report_as(status, report) 

75 return 0