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