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
« 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."""
3import os
4import pathlib
5import warnings
6from typing import List, Union
8from git.exc import GitCommandError
9from git.repo import Repo
11from foran.report import Format, Report, report_as
12from foran.status import Status
14DEBUG_VAR = 'FORAN_DEBUG'
15DEBUG = os.getenv(DEBUG_VAR)
17ENCODING = 'utf-8'
18ENCODING_ERRORS_POLICY = 'ignore'
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
37def local_staged(repo: Repo, status: Status) -> None:
38 """Truly"""
39 status.local_staged = [item.a_path for item in repo.index.diff('HEAD')]
42def local_files(repo: Repo, status: Status) -> None:
43 """Sure"""
44 status.local_files = [item.a_path for item in repo.index.diff(None)]
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
54 if argv[0] not in ('diff', 'label'):
55 warnings.warn('received unknown command')
56 return 2
58 command, repo_root, target, template = argv
60 if not pathlib.Path(str(repo_root)).is_dir():
61 warnings.warn('repository root is no directory')
62 return 1
64 if template and target != 'STD_OUT':
65 warnings.warn('templates not yet implemented')
67 report = Report(stem=target, file_format=Format.NONE)
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