3
3
from pathlib import Path
4
4
5
5
from commitizen import defaults , git
6
- from commitizen .exceptions import ConfigFileNotFound
6
+ from commitizen .exceptions import ConfigFileNotFound , ConfigFileIsEmpty
7
7
8
8
from .base_config import BaseConfig
9
9
from .json_config import JsonConfig
14
14
def read_cfg (filepath : str | None = None ) -> BaseConfig :
15
15
conf = BaseConfig ()
16
16
17
- git_project_root = git .find_git_project_root ()
18
-
19
17
if filepath is not None :
20
- given_cfg_path = Path (filepath )
21
-
22
- if not given_cfg_path .exists ():
18
+ if not Path (filepath ).exists ():
23
19
raise ConfigFileNotFound ()
24
20
25
- with open (given_cfg_path , "rb" ) as f :
26
- given_cfg_data : bytes = f .read ()
27
-
28
- given_cfg : TomlConfig | JsonConfig | YAMLConfig
29
-
30
- if "toml" in given_cfg_path .suffix :
31
- given_cfg = TomlConfig (data = given_cfg_data , path = given_cfg_path )
32
- elif "json" in given_cfg_path .suffix :
33
- given_cfg = JsonConfig (data = given_cfg_data , path = given_cfg_path )
34
- elif "yaml" in given_cfg_path .suffix :
35
- given_cfg = YAMLConfig (data = given_cfg_data , path = given_cfg_path )
36
-
37
- return given_cfg
21
+ cfg_paths = (path for path in (Path (filepath ),))
22
+ else :
23
+ git_project_root = git .find_git_project_root ()
24
+ cfg_search_paths = [Path ("." )]
25
+ if git_project_root :
26
+ cfg_search_paths .append (git_project_root )
38
27
39
- cfg_search_paths = [Path ("." )]
40
- if git_project_root :
41
- cfg_search_paths .append (git_project_root )
28
+ cfg_paths = (
29
+ path / Path (filename )
30
+ for path in cfg_search_paths
31
+ for filename in defaults .config_files
32
+ )
42
33
43
- cfg_paths = (
44
- path / Path (filename )
45
- for path in cfg_search_paths
46
- for filename in defaults .config_files
47
- )
48
34
for filename in cfg_paths :
49
35
if not filename .exists ():
50
36
continue
@@ -61,7 +47,9 @@ def read_cfg(filepath: str | None = None) -> BaseConfig:
61
47
elif "yaml" in filename .suffix :
62
48
_conf = YAMLConfig (data = data , path = filename )
63
49
64
- if _conf .is_empty_config :
50
+ if filepath is not None and _conf .is_empty_config :
51
+ raise ConfigFileIsEmpty ()
52
+ elif _conf .is_empty_config :
65
53
continue
66
54
else :
67
55
conf = _conf
0 commit comments