forked from commitizen-tools/commitizen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
46 lines (33 loc) · 1.29 KB
/
__init__.py
File metadata and controls
46 lines (33 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from __future__ import annotations
from collections.abc import Generator
from pathlib import Path
from commitizen import defaults, git
from commitizen.config.factory import create_config
from commitizen.exceptions import ConfigFileIsEmpty, ConfigFileNotFound
from .base_config import BaseConfig
def _resolve_config_paths(filepath: str | None = None) -> Generator[Path, None, None]:
if filepath is not None:
out_path = Path(filepath)
if not out_path.exists():
raise ConfigFileNotFound()
yield out_path
return
git_project_root = git.find_git_project_root()
cfg_search_paths = [Path(".")]
if git_project_root:
cfg_search_paths.append(git_project_root)
for path in cfg_search_paths:
for filename in defaults.CONFIG_FILES:
out_path = path / Path(filename)
if out_path.exists():
yield out_path
def read_cfg(filepath: str | None = None) -> BaseConfig:
for filename in _resolve_config_paths(filepath):
with open(filename, "rb") as f:
data: bytes = f.read()
conf = create_config(data=data, path=filename)
if not conf.is_empty_config:
return conf
if filepath is not None:
raise ConfigFileIsEmpty()
return BaseConfig()