Coverage for foran/foran.py: 96.88%

50 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-04 18:05:08 +00:00

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

2 

3import os 

4import pathlib 

5import warnings 

6from typing import List, Union 

7 

8from git.exc import GitCommandError 

9from git.repo import Repo 

10 

11from foran.report import Format, Report, report_as 

12from foran.status import Status 

13 

14DEBUG_VAR = 'FORAN_DEBUG' 

15DEBUG = os.getenv(DEBUG_VAR) 

16 

17ENCODING = 'utf-8' 

18ENCODING_ERRORS_POLICY = 'ignore' 

19 

20 

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

22 """Yes""" 

23 if not status.foran: 

24 branch = repo.active_branch.name 

25 try: 

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

27 except GitCommandError as ex: 

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

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

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

31 status.local_commits = [warning] 

32 warnings.warn(warning) 

33 else: 

34 raise 

35 

36 

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

38 """Truly""" 

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

40 

41 

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

43 """Sure""" 

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

45 

46 

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

48 """Drive the analysis.""" 

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

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

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

52 return 2 

53 

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

55 warnings.warn('received unknown command') 

56 return 2 

57 

58 command, repo_root, target, template = argv 

59 

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

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

62 return 1 

63 

64 if template and target != 'STD_OUT': 

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

66 

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

68 

69 repo = Repo(repo_root, search_parent_directories=True) 

70 status = Status(repo) 

71 if command == 'diff': 

72 local_commits(repo, status) 

73 local_staged(repo, status) 

74 local_files(repo, status) 

75 report_as(status, report) 

76 return 0